Displaying chi squared as the uncertainty of fit parameters in scipy.optimize

409 Views Asked by At

I am doing some curve fitting in python with the aid of scipy.optimize curve_fit. Normally I am satisfied with the scipy's default results.

However this time I would like to display the function with chi_squared as the uncertainty of my fit parameters and I don't know how to deal with this.

I have tried to use the parameter absolute_sigma=True instead of the default absolute_sigma=False. However according to my separate calculations, for absolute_sigma=False the uncertainty of the parameters, is equal to reduced_chi_squared. But it isn't chi_squared for absolute_sigma=True.

Within the documentation itself: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html chi_squared is mentioned few time, however it's not written explicitly how to display it within the plot and how to use it as the uncertainties for of the fit parameters.

My code is as follows:

# Necessary libraries
# Numpy will be used for the actual function (in this example it's not necessary)
import numpy as np 
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from uncertainties import *

#Some shortened data
Wave_lengths_500_est = [380, 447.1, 486, 492.2, 656, 700, 706.5]
Avraged_deflection_angles_500 = [11.965, 12.8, 13.93, 14.325, 19.37, 18.26, 18.335]

#The function to which the data are to be fitted
def lin_function(x,a,b):
    return a*x + b

def line_fit_lamp_1_500():
    #Niepewność kątowa na poziomie 2 minut kątowych. Wyrażona w stopniach
    angle_error = 0.03333
    #Dodanie punktów danych na wykresie
    plt.scatter(Wave_lengths_500_est, Avraged_deflection_angles_500, color='b')
    plt.errorbar(Wave_lengths_500_est, Avraged_deflection_angles_500, yerr = angle_error, fmt = 'o')

    #Fitting of the function "function_lamp1_500" to the data points
    Wave_length = np.array(Wave_lengths_500_est)
    Defletion_angle = np.array(Avraged_deflection_angles_500)
    popt, pcov = curve_fit(lin_function, Wave_length, Defletion_angle, absolute_sigma=True)
    perr = np.sqrt(np.diag(pcov))
    y = lin_function(Wave_length, *popt)

    # Graph looks 
    plt.plot(Wave_length, y, '--', color = 'g', label="fit with: $a={:.3f}\pm{:.5f}$, $b={:.3f}\pm{:.5f}$" .format(popt[0], perr[0], popt[1], perr[1]))
    plt.legend()
    plt.xlabel('Długość fali [nm]')
    plt.ylabel('Kąt załamania [Stopnie]')
    plt.show()

# Function call 
line_fit_lamp_1_500()

Toggling between absolute_sigma=True/False I get a change of uncertainty of the parameter a\pm0.00308 and b\pm1.74571 for absolute_sigma=True to a\pm0.022 and b\pm1.41679 for absolute_sigma=False. Versus an expected value of a\pm0.0001027 and b\pm0.058132 for chi_squared and a\pm0.002503 and b\pm1.4167 for reduced_chi_squared

Additionally could you please elaborate what does the expression .format(popt[0], perr[0], popt[1], perr[1]) do?

All help is appreciated, thanks in advance.

0

There are 0 best solutions below