amep.functions.ExGaussian#

class amep.functions.ExGaussian#

Bases: BaseFunction

Exponentially modified Gaussian.

__init__() None#

Initialize a function object for an exponentially modified Gaussian.

Notes

The exponentially modified Gaussian function has three parameters \(\lambda, \mu\), and \(\sigma\), and it is defined as

where \({\rm erfc}\) is the complementary error function [1]. The parameters are ordered as follows:

p[0] : \(\lambda\)

p[1] : \(\mu\)

p[2] : \(\sigma\)

The mean value of the distribution is given by \(\mu+1/\lambda\) and the standard deviation by \(\sqrt{\sigma^2 + 1/\lambda^2}\).

References

Return type:

None.

Examples

>>> import amep
>>> import numpy as np
>>> g = amep.functions.ExGaussian()
>>> x = np.linspace(-10, 10, 500)
>>> y = g.generate(
...     x, p=[1.0, -5.0, 2.0]
.. ) + 0.002*np.random.normal(size=x.shape)
>>> g.fit(x, y)
>>> print(g.results)
{'lambda': (0.9531534827685344, 0.039017907566076315),
 'mu': (-5.12948790385678, 0.09219256237461067),
 'sigma': (2.0659374733316973, 0.05531580274767071)}
>>> print(g.mean)
-4.080338928294738
>>> print(g.std)
2.3170695321114207
>>> fig, axs = amep.plot.new()
>>> axs.plot(x, y, label='raw data', ls="")
>>> axs.plot(
...     x, g.generate(x), marker="", lw=2,
...     label='exponentially modified Gaussian fit'
... )
>>> axs.set_xlabel(r'$x$')
>>> axs.set_ylabel(r'$g(x)$')
>>> axs.legend(loc="upper left")
>>> axs.set_ylim(-0.005, 0.035)
>>> fig.savefig('./figures/functions/functions-ExGaussian.png')
../_images/functions-ExGaussian.png

Methods

__init__()

Initialize a function object for an exponentially modified Gaussian.

f(p, x)

Exponentially modified Gaussian function.

fit(xdata, ydata[, p0, sigma, maxit, verbose])

Fits the function self.f to the given data by using ODR (orthogonal distance regression).

generate(x[, p])

Returns the y values for given x values.

Attributes

errors

Returns the fit errors for each parameter as an array.

keys

mean

name

nparams

output

params

Returns an array of the optimal fit parameters.

results

Returns the dictionary of fit results including parameter names, parameter values, and fit errors.

std

property errors: ndarray#

Returns the fit errors for each parameter as an array.

Returns:

Fit errors.

Return type:

np.ndarray

f(p: list | ndarray, x: float | ndarray) float | ndarray#

Exponentially modified Gaussian function. Has the functional form

Parameters:
  • p (list or np.ndarray) – Parameters \(\lambda, \mu\), and \(\sigma\) of the exponentially modified Gaussian function.

  • x (float | np.ndarray) – Value(s) at which the function is evaluated.

Returns:

Function evaluated at the given x value(s).

Return type:

float or np.ndarray

fit(xdata: ndarray, ydata: ndarray, p0: list | None = None, sigma: ndarray | None = None, maxit: int | None = None, verbose: bool = False) None#

Fits the function self.f to the given data by using ODR (orthogonal distance regression).

Parameters:
  • xdata (np.ndarray) – x values.

  • ydata (np.ndarray) – y values.

  • p0 (list or None, optional) – List of initial values. The default is None.

  • sigma (np.ndarray or None, optional) – Absolute error for each data point. The default is None.

  • maxit (int, optional) – Maximum number of iterations. The default is 200.

  • verbose (bool, optional) – If True, the main results are printed. The default is False.

Return type:

None.

generate(x: ndarray, p: list | None = None) ndarray#

Returns the y values for given x values.

Parameters:
  • x (np.ndarray) – x values.

  • p (list, optional) – List of parameters. The default is None.

Returns:

y – f(x)

Return type:

np.ndarray

property params: ndarray#

Returns an array of the optimal fit parameters.

Returns:

Fit parameter values.

Return type:

np.ndarray

property results: dict#

Returns the dictionary of fit results including parameter names, parameter values, and fit errors.

Returns:

Fit results.

Return type:

dict