Data reduction & fitting

by Eugeniy E. Mikhailov and Greg Bentsen

Logistics and Agenda

Homework 3 due Monday Sept 21 at 11:59pm


Agenda


Today

AI Tools Installation

We will begin using AI tools this Wednesday

We have set up a local model in the HPC cluster for the class to use.

Please set up the AI tool ASAP. We will expect you to have it set up prior to class on Wednesday.

If you have any trouble please come to our office hours

AI Tools Instructions

Visit this link:

https://hpc.wm.edu/apikey/

Data Reduction & Fitting

Data reduction

Data reduction

Model extraction — fitting

Suppose we measured the dependence of an experimental parameter yy on another parameter xx, yielding a set of NN datapoints {xi,yi}\{x_i, y_i\}.

We have a model f(x,p→)f(x,\vec{p}) that aims to predict the yy-values based on the xx-values, i.e.

yi,pred=f(xi,p→)y_{i,\text{pred}} = f(x_i,\vec{p})

For example, this could be a simple linear model: f(x,p→)=mx+bf(x, \vec{p}) = m x + b with p→=(m,b)\vec{p} = (m,b).

Source: https://commons.wikimedia.org/w/index.php?curid=3337769
Source: https://commons.wikimedia.org/w/index.php?curid=3337769

Model extraction — fitting

Suppose we measured the dependence of an experimental parameter yy on another parameter xx, yielding a set of NN datapoints {xi,yi}\{x_i, y_i\}.

We have a model f(x,p→)f(x,\vec{p}) that aims to predict the yy-values based on the xx-values, i.e.

yi,pred=f(xi,p→)y_{i,\text{pred}} = f(x_i,\vec{p})

For example, this could be a simple linear model: f(x,p→)=mx+bf(x, \vec{p}) = m x + b with p→=(m,b)\vec{p} = (m,b).


Goal:  We want to extract the unknown model parameters p1,p2,p3,…=p→p_1, p_2, p_3, \ldots = \vec{p} (i.e. find the best parameters p→\vec{p}) by fitting the model function f(x,p→)f(x,\vec{p}) to the data.

Goodness of the fit

Define a way to estimate the goodness of fit.

Our goal is simple: find the optimal parameters p→\vec{p} that minimize χ2\chi^2 (least-squares fit).

Good fit should have the following properties

Practical realization

Example code

Example fitting code using curve_fit

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# Define Lorentzian function
def lorentzian(x, A, x0, gamma):
    return A / (1 + ((x - x0) / gamma) ** 2)

# Load experimental data
data = np.loadtxt('lorentzian_data.csv', delimiter=',', skiprows=1)
x_data = data[:, 0]
y_data = data[:, 1]

# Initial guess for the parameters [A, x0, gamma]
initial_guess = [10.0, 2.0, 1.0]

# Fit the Lorentzian function to the data
popt, pcov = curve_fit(lorentzian, x_data, y_data, p0=initial_guess)

# Extract fitted parameters
A_fit, x0_fit, gamma_fit = popt

# Plot the resulting fit
x_fit = np.linspace(-30, 30, 1000)
y_fit = lorentzian(x_fit, A_fit, x0_fit, gamma_fit)

plt.figure(figsize=(10, 6))
plt.scatter(x_data, y_data, label='Data', alpha=0.7)
plt.plot(x_fit, y_fit, 'r-', linewidth=2, label='Fit')
plt.savefig('lorentz_fit.png')
plt.show()

Fitted parameters and uncertainty

We obtain two return values from curve_fit:


The diagonal elements of pcov give you the variances Var(pi)\text{Var}(p_i) of the parameters pip_i.

To find the uncertainty, just take the square root: Δpi=Var(pi)\Delta p_i = \sqrt{\text{Var}(p_i)}

Covariance matrix pcov
Covariance matrix pcov

See https://en.wikipedia.org/wiki/Covariance_matrix for more information