solarwindpy.fitfunctions.exponentialsï
Fit functions for exponential models.
Classes in this module implement exponential decay and related forms
using the FitFunction API.
They provide reasonable starting parameters and formatted LaTeX output
for visualization.
Classes
|
Assuming that you don't want special formatting, call order is: |
|
Assuming that you don't want special formatting, call order is: |
|
Assuming that you don't want special formatting, call order is: |
- class Exponential(xobs, yobs, **kwargs)[source]
Bases:
FitFunctionAssuming that you donât want special formatting, call order is:
fit_function = FitFunction(function, TeX_string) fit_function.make_fit()
Instances are callable. If the fit fails, calling the instance will return an array of NaNs the same shape as the x-values.
- __init__(xobs, yobs, **kwargs)[source]
Fit
A * exp(-c x)to the data.- Parameters:
xobs (array-like) â Observed x values (independent variable). Shape must match yobs.
yobs (array-like) â Observed y values (dependent variable). Shape must match xobs.
**kwargs â The description is missing.
Notes
The fitting procedure uses scipy.optimize.least_squares with robust loss functions (Huber by default) to handle outliers. The initial parameter guess is provided by the p0 property, which must be implemented by subclasses.
All subclasses inherit this documentation automatically through the docstring-inheritance metaclass.
Examples
>>> import numpy as np >>> from solarwindpy.fitfunctions import Gaussian >>> x = np.linspace(-5, 5, 100) >>> y = 3 * np.exp(-0.5 * x**2) + np.random.normal(0, 0.1, 100) >>> fit = Gaussian(x, y, xmin=-3, xmax=3) >>> fit.make_fit() >>> print(f"Fitted mu: {fit.popt['mu']:.3f}")
- property function
Get the function that`curve_fit` fits.
The function is set at instantiation. It doesnât make sense to change it unless you redefine the entire FitFunction, so there is no new kwarg.
- property p0
Return initial guesses
[c, A]for the fit.
- property TeX_function
Function written in LaTeX.
- property TeX_info
- property argnames
The names of the actual function arguments pulled by getfullargspec.
- build_TeX_info()
- build_plotter()
- property chisq_dof
Chisq per degree of freedom \(\chi^2_\nu\).
If None, not calculated by make_fit_old. If np.nan, fit failed.
- property combined_popt_psigma
Return optimized parameters and uncertainties as a DataFrame.
- Returns:
DataFrame with columns âpoptâ and âpsigmaâ, indexed by parameter names. Relative uncertainty can be computed as: df[âpsigmaâ] / df[âpoptâ]
- Return type:
pd.DataFrame
- property dof
Degrees of freedom in the fit.
- property fit_bounds
Bounds used when running the fit.
- property fit_result
- property initial_guess_info
- property logger
- make_fit(return_exception=False, **kwargs)
Fit the function with the independent xobs and dependent yobs.
Uses least_squares and returns the OptimizeResult object, but treats weights as in curve_fit.
- Parameters:
return_exception (bool) â If True, return exceptions from fitting routine, instead of raising. This is useful when looping through many fits and wanting to identify failed fits after the fact.
**kwargs â The description is missing.
- property nobs
The total number of observations used in the fit.
- property observations
- property pcov
Returns a copy so that the matrix isnât accidentally edited.
- property plotter
- property popt
Optimized fit parameters.
- property psigma
- residuals(pct=False, use_all=False)
Calculate fit residuals.
- Parameters:
- Returns:
Residuals as observed - fitted.
- Return type:
Examples
>>> # Create FitFunction with constraints >>> ff = Gaussian(x, y, xmin=3, xmax=7) >>> ff.make_fit() >>> >>> # Residuals for fitted region only >>> r_fit = ff.residuals() >>> >>> # Residuals for all original data >>> r_all = ff.residuals(use_all=True) >>> >>> # Percentage residuals >>> r_pct = ff.residuals(pct=True)
Notes
Addresses TODO: âcalculate with all valuesâŠincluding those excluded by set_extremaâ (though set_extrema doesnât exist - constraints are passed in __init__).
- property rsq
Coefficient of determination.
Source: <en.wikipedia.org/wiki/Coefficient_of_determination#Definitions>
- set_fit_obs(xobs_raw, yobs_raw, weights_raw, xmin=None, xmax=None, xoutside=None, ymin=None, ymax=None, youtside=None, wmin=None, wmax=None, logx=False, logy=False)
Set the observed values weâll actually use in the fit.
By applying limits to xobs_raw and yobs_raw and checking for finite values.
All boundaries are inclusive <= or >=.
If logy, then make selection of wmin and wmax based on \(w/(y \ln(10))\).
- Parameters:
xobs_raw â The description is missing.
yobs_raw â The description is missing.
weights_raw â The description is missing.
xmin â The description is missing.
xmax â The description is missing.
xoutside â The description is missing.
ymin â The description is missing.
ymax â The description is missing.
youtside â The description is missing.
wmin â The description is missing.
wmax â The description is missing.
logx â The description is missing.
logy â The description is missing.
- property sufficient_data
Ensure that we can fit the data before doing any computations.
- class ExponentialPlusC(xobs, yobs, **kwargs)[source]
Bases:
FitFunctionAssuming that you donât want special formatting, call order is:
fit_function = FitFunction(function, TeX_string) fit_function.make_fit()
Instances are callable. If the fit fails, calling the instance will return an array of NaNs the same shape as the x-values.
- __init__(xobs, yobs, **kwargs)[source]
Fit
A * exp(-c x) + dto the data.- Parameters:
xobs (array-like) â Observed x values (independent variable). Shape must match yobs.
yobs (array-like) â Observed y values (dependent variable). Shape must match xobs.
**kwargs â The description is missing.
Notes
The fitting procedure uses scipy.optimize.least_squares with robust loss functions (Huber by default) to handle outliers. The initial parameter guess is provided by the p0 property, which must be implemented by subclasses.
All subclasses inherit this documentation automatically through the docstring-inheritance metaclass.
Examples
>>> import numpy as np >>> from solarwindpy.fitfunctions import Gaussian >>> x = np.linspace(-5, 5, 100) >>> y = 3 * np.exp(-0.5 * x**2) + np.random.normal(0, 0.1, 100) >>> fit = Gaussian(x, y, xmin=-3, xmax=3) >>> fit.make_fit() >>> print(f"Fitted mu: {fit.popt['mu']:.3f}")
- property function
Get the function that`curve_fit` fits.
The function is set at instantiation. It doesnât make sense to change it unless you redefine the entire FitFunction, so there is no new kwarg.
- property p0
Return initial guesses
[c, A, d]for the fit.
- property TeX_function
Function written in LaTeX.
- property TeX_info
- property argnames
The names of the actual function arguments pulled by getfullargspec.
- build_TeX_info()
- build_plotter()
- property chisq_dof
Chisq per degree of freedom \(\chi^2_\nu\).
If None, not calculated by make_fit_old. If np.nan, fit failed.
- property combined_popt_psigma
Return optimized parameters and uncertainties as a DataFrame.
- Returns:
DataFrame with columns âpoptâ and âpsigmaâ, indexed by parameter names. Relative uncertainty can be computed as: df[âpsigmaâ] / df[âpoptâ]
- Return type:
pd.DataFrame
- property dof
Degrees of freedom in the fit.
- property fit_bounds
Bounds used when running the fit.
- property fit_result
- property initial_guess_info
- property logger
- make_fit(return_exception=False, **kwargs)
Fit the function with the independent xobs and dependent yobs.
Uses least_squares and returns the OptimizeResult object, but treats weights as in curve_fit.
- Parameters:
return_exception (bool) â If True, return exceptions from fitting routine, instead of raising. This is useful when looping through many fits and wanting to identify failed fits after the fact.
**kwargs â The description is missing.
- property nobs
The total number of observations used in the fit.
- property observations
- property pcov
Returns a copy so that the matrix isnât accidentally edited.
- property plotter
- property popt
Optimized fit parameters.
- property psigma
- residuals(pct=False, use_all=False)
Calculate fit residuals.
- Parameters:
- Returns:
Residuals as observed - fitted.
- Return type:
Examples
>>> # Create FitFunction with constraints >>> ff = Gaussian(x, y, xmin=3, xmax=7) >>> ff.make_fit() >>> >>> # Residuals for fitted region only >>> r_fit = ff.residuals() >>> >>> # Residuals for all original data >>> r_all = ff.residuals(use_all=True) >>> >>> # Percentage residuals >>> r_pct = ff.residuals(pct=True)
Notes
Addresses TODO: âcalculate with all valuesâŠincluding those excluded by set_extremaâ (though set_extrema doesnât exist - constraints are passed in __init__).
- property rsq
Coefficient of determination.
Source: <en.wikipedia.org/wiki/Coefficient_of_determination#Definitions>
- set_fit_obs(xobs_raw, yobs_raw, weights_raw, xmin=None, xmax=None, xoutside=None, ymin=None, ymax=None, youtside=None, wmin=None, wmax=None, logx=False, logy=False)
Set the observed values weâll actually use in the fit.
By applying limits to xobs_raw and yobs_raw and checking for finite values.
All boundaries are inclusive <= or >=.
If logy, then make selection of wmin and wmax based on \(w/(y \ln(10))\).
- Parameters:
xobs_raw â The description is missing.
yobs_raw â The description is missing.
weights_raw â The description is missing.
xmin â The description is missing.
xmax â The description is missing.
xoutside â The description is missing.
ymin â The description is missing.
ymax â The description is missing.
youtside â The description is missing.
wmin â The description is missing.
wmax â The description is missing.
logx â The description is missing.
logy â The description is missing.
- property sufficient_data
Ensure that we can fit the data before doing any computations.
- class ExponentialCDF(xobs, yobs, **kwargs)[source]
Bases:
FitFunctionAssuming that you donât want special formatting, call order is:
fit_function = FitFunction(function, TeX_string) fit_function.make_fit()
Instances are callable. If the fit fails, calling the instance will return an array of NaNs the same shape as the x-values.
- __init__(xobs, yobs, **kwargs)[source]
Fit an exponential cumulative distribution function.
- Parameters:
xobs (array-like) â Observed x values (independent variable). Shape must match yobs.
yobs (array-like) â Observed y values (dependent variable). Shape must match xobs.
**kwargs â The description is missing.
Notes
The fitting procedure uses scipy.optimize.least_squares with robust loss functions (Huber by default) to handle outliers. The initial parameter guess is provided by the p0 property, which must be implemented by subclasses.
All subclasses inherit this documentation automatically through the docstring-inheritance metaclass.
Examples
>>> import numpy as np >>> from solarwindpy.fitfunctions import Gaussian >>> x = np.linspace(-5, 5, 100) >>> y = 3 * np.exp(-0.5 * x**2) + np.random.normal(0, 0.1, 100) >>> fit = Gaussian(x, y, xmin=-3, xmax=3) >>> fit.make_fit() >>> print(f"Fitted mu: {fit.popt['mu']:.3f}")
- property function
Get the function that`curve_fit` fits.
The function is set at instantiation. It doesnât make sense to change it unless you redefine the entire FitFunction, so there is no new kwarg.
- property y0
Amplitude of the CDF.
- set_y0(new)[source]
- property p0
Return initial guess
[c]for the fit.
- property TeX_function
Function written in LaTeX.
- set_TeX_info(**kwargs)[source]
Include
Avalue in the TeX annotation.- Parameters:
**kwargs â The description is missing.
- property TeX_info
- property argnames
The names of the actual function arguments pulled by getfullargspec.
- build_TeX_info()
- build_plotter()
- property chisq_dof
Chisq per degree of freedom \(\chi^2_\nu\).
If None, not calculated by make_fit_old. If np.nan, fit failed.
- property combined_popt_psigma
Return optimized parameters and uncertainties as a DataFrame.
- Returns:
DataFrame with columns âpoptâ and âpsigmaâ, indexed by parameter names. Relative uncertainty can be computed as: df[âpsigmaâ] / df[âpoptâ]
- Return type:
pd.DataFrame
- property dof
Degrees of freedom in the fit.
- property fit_bounds
Bounds used when running the fit.
- property fit_result
- property initial_guess_info
- property logger
- make_fit(return_exception=False, **kwargs)
Fit the function with the independent xobs and dependent yobs.
Uses least_squares and returns the OptimizeResult object, but treats weights as in curve_fit.
- Parameters:
return_exception (bool) â If True, return exceptions from fitting routine, instead of raising. This is useful when looping through many fits and wanting to identify failed fits after the fact.
**kwargs â The description is missing.
- property nobs
The total number of observations used in the fit.
- property observations
- property pcov
Returns a copy so that the matrix isnât accidentally edited.
- property plotter
- property popt
Optimized fit parameters.
- property psigma
- residuals(pct=False, use_all=False)
Calculate fit residuals.
- Parameters:
- Returns:
Residuals as observed - fitted.
- Return type:
Examples
>>> # Create FitFunction with constraints >>> ff = Gaussian(x, y, xmin=3, xmax=7) >>> ff.make_fit() >>> >>> # Residuals for fitted region only >>> r_fit = ff.residuals() >>> >>> # Residuals for all original data >>> r_all = ff.residuals(use_all=True) >>> >>> # Percentage residuals >>> r_pct = ff.residuals(pct=True)
Notes
Addresses TODO: âcalculate with all valuesâŠincluding those excluded by set_extremaâ (though set_extrema doesnât exist - constraints are passed in __init__).
- property rsq
Coefficient of determination.
Source: <en.wikipedia.org/wiki/Coefficient_of_determination#Definitions>
- set_fit_obs(xobs_raw, yobs_raw, weights_raw, xmin=None, xmax=None, xoutside=None, ymin=None, ymax=None, youtside=None, wmin=None, wmax=None, logx=False, logy=False)
Set the observed values weâll actually use in the fit.
By applying limits to xobs_raw and yobs_raw and checking for finite values.
All boundaries are inclusive <= or >=.
If logy, then make selection of wmin and wmax based on \(w/(y \ln(10))\).
- Parameters:
xobs_raw â The description is missing.
yobs_raw â The description is missing.
weights_raw â The description is missing.
xmin â The description is missing.
xmax â The description is missing.
xoutside â The description is missing.
ymin â The description is missing.
ymax â The description is missing.
youtside â The description is missing.
wmin â The description is missing.
wmax â The description is missing.
logx â The description is missing.
logy â The description is missing.
- property sufficient_data
Ensure that we can fit the data before doing any computations.