solarwindpy.fitfunctions.gaussians

Gaussian-based fit functions.

The classes here implement standard Gaussian shapes and common variations used throughout the package. Each class inherits from FitFunction and defines the target function, initial parameter estimates, and LaTeX output helpers.

Classes

Gaussian(xobs, yobs, **kwargs)

Standard Gaussian distribution for symmetric peak fitting.

GaussianLn(xobs, yobs, **kwargs)

Log-normal distribution for skewed data fitting.

GaussianNormalized(xobs, yobs, **kwargs)

Normalized Gaussian distribution where integral equals n.

class Gaussian(xobs, yobs, **kwargs)[source]

Bases: FitFunction

Standard Gaussian distribution for symmetric peak fitting.

Fits data to the form: A * exp(-0.5 * ((x - mu) / sigma)^2)

__init__(xobs, yobs, **kwargs)[source]

Initialize fit function with observed 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}")

See also

make_fit

Execute the fitting procedure

popt

Access optimized parameters

rsq

Calculate coefficient of determination

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 [mu, sigma, A] for the fit.

property TeX_function

Function written in LaTeX.

make_fit(*args, **kwargs)[source]

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:
  • *args – The description is missing.

  • **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
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:
  • pct (bool, default=False) – If True, return percentage residuals.

  • use_all (bool, default=False) – If True, calculate residuals for all input data including points excluded by constraints (xmin, xmax, etc.) passed during initialization. If False (default), calculate only for points used in fit.

Returns:

Residuals as observed - fitted.

Return type:

numpy.ndarray

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 GaussianNormalized(xobs, yobs, **kwargs)[source]

Bases: FitFunction

Normalized Gaussian distribution where integral equals n.

Fits data to the form: (n / (sqrt(2*pi) * sigma)) * exp(-0.5 * ((x - mu) / sigma)^2)

__init__(xobs, yobs, **kwargs)[source]

Initialize normalized Gaussian fit.

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 normalization parameter n represents the total area under the Gaussian curve, useful for fitting probability distributions or particle count distributions.

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}")

See also

make_fit

Execute the fitting procedure

popt

Access optimized parameters

rsq

Calculate coefficient of determination

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 [mu, sigma, n] for the fit.

property TeX_function

Function written in LaTeX.

make_fit(*args, **kwargs)[source]

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:
  • *args – The description is missing.

  • **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
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:
  • pct (bool, default=False) – If True, return percentage residuals.

  • use_all (bool, default=False) – If True, calculate residuals for all input data including points excluded by constraints (xmin, xmax, etc.) passed during initialization. If False (default), calculate only for points used in fit.

Returns:

Residuals as observed - fitted.

Return type:

numpy.ndarray

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 GaussianLn(xobs, yobs, **kwargs)[source]

Bases: FitFunction

Log-normal distribution for skewed data fitting.

Fits a Gaussian in logarithmic space where \(\ln(x)\) follows a normal distribution.

References

__init__(xobs, yobs, **kwargs)[source]

Initialize log-normal Gaussian fit.

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

xobs must be positive for log transformation. This distribution is commonly used for particle size distributions and velocity distributions in solar wind where values are positively skewed.

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}")

See also

make_fit

Execute the fitting procedure

popt

Access optimized parameters

rsq

Calculate coefficient of determination

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 [ln(mu), ln(sigma), ln(A)].

property TeX_function

Function written in LaTeX.

property normal_parameters

Calculate the normal parameters from log-normal parameters.

\[\mu = \exp[m + (s^2)/2] \sigma = \sqrt{\exp[s^2 + 2m] (\exp[s^2] - 1)}\]
property TeX_report_normal_parameters

Report normal parameters, not log-normal parameters in the TeX info.

set_TeX_report_normal_parameters(new)[source]
property TeX_popt

Create a dictionary with (k, v) pairs corresponding to parameter values.

(self.argnames, :math:`p_{\mathrm{opt}} \pm \sigma_p`) with the appropriate uncertainty.

See set_TeX_trans_argnames to translate the argnames for TeX.

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:
  • pct (bool, default=False) – If True, return percentage residuals.

  • use_all (bool, default=False) – If True, calculate residuals for all input data including points excluded by constraints (xmin, xmax, etc.) passed during initialization. If False (default), calculate only for points used in fit.

Returns:

Residuals as observed - fitted.

Return type:

numpy.ndarray

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.