You are on page 1of 1

SCIPY NOTES

from scipy import optimize


from scipy import stats
from scipy.optimize import curve_fit

0. MOTIVATION

The main features of this library are:

Integration (scipy.integrate): integrals, differential equations, etc.


Optimization (scipy.optimize): minimization, fits, etc.
Interpolation (scipy.interpolate): smoothing methods, etc.
Fourier Transforms (scipy.fftpack): spectral analysis, etc.
Signal Processing (scipy.signal): transfer functions, filtering, etc.
Linear Algebra (scipy.linalg): matrix operations, diagonalization,
determinant, etc.
Statistics (scipy.stats): random numbers, probability density function,
cumulative distribution, etc.

1. CURVE FITTING

We are gonna focus in the curve fitting part of the optimization, i.e.
fitting data points with a function.

The function 'optimize.curve_fit(function, x, y, sigma=)' fits the function


to the data in x and y. The sigma is the standard deviation for each value if it is
a 1D array and it is used to determine the incertainty of the data. The function
must be of the form 'func(x, *para)'. It returns a 1D array with the optimal
parameters and a 2D array with the estimated covariance.

If the function has no stable number of parameters, we need to give an


initial guess for the parameters so that Python knows the amount of parameters to
use. We can also set bounds and check if there are no NaNs.

Finally, we can quantify how good a fit is with the so-called goodness of the
fit. We have two options:

- def get_chi2_nDOF(y, dy, yfit):


r = (y-yfit)/dy
return np.sum(r**2), len(y)

chi squared is the first returned value

- def get_pvalue(chi2, nDOF):


return 1-stats.chi2.cdf(chi2, df=nDOF)

We can check out some info on the plotting that might not be that interesting.
Louie definied chi_2 with the following function:

def chi2( func, x, y, errs, *params):


return np.sum( (func(x, *params)-y)**2 / errs**2 )

You might also like