Linear
- class plasmapy.analysis.fit_functions.Linear(params: tuple[float, ...] | None = None, param_errors: tuple[float, ...] | None = None)[source]
Bases:
AbstractFitFunction
A sub-class of
AbstractFitFunction
to represent a linear function.\[ \begin{align}\begin{aligned}y &= f(x) = m \, x + b\\(\delta y)^2 &= (x \, \delta m)^2 + (m \, \delta x)^2 + (\delta b)^2\end{aligned}\end{align} \]where \(m\) and \(b\) are real constants to be fitted and \(x\) is the independent variable. \(\delta m\), \(\delta b\), and \(\delta x\) are the respective uncertainties for \(m\), \(b\), and \(x\).
- Parameters:
params (tuple[float, ...], optional) – Tuple of values for the function parameters. Equal in size to
param_names
.param_errors (tuple[float, ...], optional) – Tuple of values for the errors associated with the function parameters. Equal in size to
param_names
.
Attributes Summary
A
namedtuple
used for attributesparams
andparam_errors
.The results returned by the curve fitting routine used by
curve_fit
.LaTeX friendly representation of the fit function.
The associated errors of the fitted
params
.Names of the fitted parameters.
The fitted parameters for the fit function.
Coefficient of determination (r-squared) value of the fit.
Methods Summary
__call__
(x[, x_err, reterr])Direct call of the fit function \(f(x)\).
curve_fit
(xdata, ydata, **kwargs)Calculate a linear least-squares regression of (
xdata
,ydata
) usingscipy.stats.linregress
.func
(x, m, b)The fit function, a linear function.
func_err
(x[, x_err, rety])Calculate dependent variable uncertainties \(\delta y\) for dependent variables \(y=f(x)\).
root_solve
(*args, **kwargs)The root \(f(x_r) = 0\) for the fit function.
Attributes Documentation
- FitParamTuple
A
namedtuple
used for attributesparams
andparam_errors
. The attributeparam_names
defines the tuple field names.
- curve_fit_results
The results returned by the curve fitting routine used by
curve_fit
. This is typically fromscipy.stats.linregress
orscipy.optimize.curve_fit
.
- latex_str
LaTeX friendly representation of the fit function.
- param_names
Names of the fitted parameters.
- params
The fitted parameters for the fit function.
- rsq
Coefficient of determination (r-squared) value of the fit. Calculated by
scipy.stats.linregress
from the fit.
Methods Documentation
- __call__(x, x_err=None, reterr=False)
Direct call of the fit function \(f(x)\).
- Parameters:
x (array_like) – Dependent variables.
x_err (array_like, optional) – Errors associated with the independent variables
x
. Must be of size one or equal to the size ofx
.reterr (bool, optional) – (Default:
False
) IfTrue
, return an array of uncertainties associated with the calculated independent variables
- Returns:
y (
numpy.ndarray
) – Corresponding dependent variables \(y=f(x)\) of the independent variablesx
.y_err (
numpy.ndarray
) – Uncertainties associated with the calculated dependent variables \(\delta y\)
- curve_fit(xdata, ydata, **kwargs) None [source]
Calculate a linear least-squares regression of (
xdata
,ydata
) usingscipy.stats.linregress
. This will set the attributesparams
,param_errors
, andrsq
.The results of
scipy.stats.linregress
can be obtained viacurve_fit_results
.- Parameters:
xdata (array_like) – The independent variable where data is measured. Should be 1D of length M.
ydata (array_like) – The dependent data associated with
xdata
.**kwargs – Any keywords accepted by
scipy.stats.linregress
.
- func(x, m, b)[source]
The fit function, a linear function.
\[f(x) = m \, x + b\]where \(m\) and \(b\) are real constants representing the slope and intercept, respectively, and \(x\) is the independent variable.
- Parameters:
x (array_like) – Independent variable.
m (float) – Value for slope \(m\)
b (float) – Value for intercept \(b\)
- Returns:
y – Dependent variables corresponding to \(x\)
- Return type:
- func_err(x, x_err=None, rety=False)[source]
Calculate dependent variable uncertainties \(\delta y\) for dependent variables \(y=f(x)\).
\[(\delta y)^2 = (x \, \delta m)^2 + (m \, \delta x)^2 + (\delta b)^2\]- Parameters:
x (array_like) – Independent variables to be passed to the fit function.
x_err (array_like, optional) – Errors associated with the independent variables
x
. Must be of size one or equal to the size ofx
.rety (bool) – Set to
True
to also return the associated dependent variables \(y = f(x)\).
- Returns:
err (
numpy.ndarray
) – The calculated uncertainties \(\delta y\) of the dependent variables (\(y = f(x)\)) of the independent variablesx
.y (
numpy.ndarray
, optional) – (ifrety == True
) The associated dependent variables \(y = f(x)\).
Notes
A good reference for formulating propagation of uncertainty expressions is:
J. R. Taylor. An Introduction to Error Analysis: The Study of Uncertainties in Physical Measurements. University Science Books, second edition, August 1996 (ISBN: 093570275X)
- root_solve(*args, **kwargs)[source]
The root \(f(x_r) = 0\) for the fit function.
\[ \begin{align}\begin{aligned}x_r &= \frac{-b}{m}\\\delta x_r &= |x_r| \sqrt{ \left( \frac{\delta m}{m} \right)^2 + \left( \frac{\delta b}{b} \right)^2 }\end{aligned}\end{align} \]- Parameters:
*args – Not needed. This is to ensure signature comparability with
AbstractFitFunction
.**kwargs – Not needed. This is to ensure signature comparability with
AbstractFitFunction
.
- Returns:
root (float) – The root value for the given fit
params
.err (float) – The uncertainty in the calculated root for the given fit
params
andparam_errors
.