linear_fit¶
Tools for linear least square fitting also called linear regression.
- class LT_Fit.linear_fit.linefit(x, y, yerr=None, quiet=False, plot_fit=True)¶
simple line fit for a set of data. Example:
>>> R = linefit(x,y, [yerr = errors]) # do the fit >>> print(R.res['parameters']) # print the parameters >>> R.plot() # plot the fitted line >>> R(x) # evaluate the fitted function at x
x and y are arrays (
numpy.array()
) R is an object containing the results of the fit with the following additional useful members:Member
Meaning
offset
offset of the line
slope
slope of the line
sigma_o
error in offset
sigma_s
error in slope
chi_red
reduce chi square
CL
probability to find a chi square larger than the fit result (ideal 50%)
cov
covariance matrix (2D
numpy.array()
)res
dictionary with all fit information
line(x)
evaluate the fitted function at x
- class LT_Fit.linear_fit.polyfit(x, y, yerr=None, order=2, np_scale=5, parameters=None, quiet=False, plot_fit=True)¶
Polynomial fit of order n, example:
>>> R = polyfit(x,y, order, [yerr = errors]) # perform the fit >>> print(R['parameters']) # R is a dictionary containing the results of the fit >>> R.plot() # plot the fitted curve
x and y are arrays (
numpy.array()
) If you want to use predefined parameters to store the results:>>> import LT_Fit.parameters as P # get the parameter module >>> C0 = P.Parameter(0., 'const'); C1 = P.Parameter(0., 'lin'); C2 = P.Parameter(0.,'quad') # create Parameter objects >>> R = polyfit(x,y, order, parameters=[C0,C1,C2]) # perform the fit >>> R.show_parameters() # will show the fit result >>> R(x) # evaluate the fitted function at x
Member
Meaning
chi_red
reduce chi square
CL
probability to find a chi square larger than the fit result (ideal 50%)
par
parameters (
numpy.array()
)parameters
parameter (list of
Parameter
objects)sig_par
parameter errors (variances)
cov
covarance matric (2D
numpy.array()
)res
dictionary with all fit information
poly(x)
evaluate the fitted function at x
- class LT_Fit.linear_fit.gen_linfit(functions, x, y, parameters=None, yerr=None, np_scale=5, quiet=False, plot_fit=True)¶
general linear fit of:
f(x) = a0*f0(x) + a1*f1(x) + ...
where:
f = [f0, f1, f2, ..]
is a list of functions provided by the user.
An example of making a list of function and using it in a fit
>>> import LT_Fit.parameters as P # get the parameter module >>> f0 = lambda x: sin(x) # define lambda functions >>> f1 = lambda x: sin(2.*x) >>> f2 = lambda x: sin(4.*x) >>> a0 = P.Parameter(0.,'ax'); a1 = P.Parameter(0.,'a2x'); a2 = P.Parameter(0.,'a4x') # define Parameter objects (optional) >>> R = gen_linfit([f0, f1, f2], x, y, parameters = [a0, a1, a2], yerr = sig_y) # do the fit >>> R.plot() # plot the fit >>> R.show_parameters() # print the parameters >>> R(x) # evaluate the fitted function at x
R is a gen_linfit object containing the fit results and the fitted function
Member
Meaning
chi_red
reduce chi square
CL
probability to find a chi square larger than the fit result (ideal 50%)
par
parameters (numpy array)
parameters
parameter (list of Parameter objects)
sig_par
parameter errors (variances)
cov
covariance matrix (2D
numpy.array()
)res
dictionary with all fit information
func(x)
evaluate the fitted function at x