amep.functions.SkewGaussian#

class amep.functions.SkewGaussian#

Bases: BaseFunction

Skewed Gaussian distribution.

__init__() None#

Initializes a function object of a skewed normal distribution.

Notes

The skewed normal distribution has three parameters \(\mu\) (location), \(\sigma\) (width), and \(\alpha\) (skewness). It is given by [1]

\[g_{\rm skeq}(x) = \frac{1}{\sqrt{2\pi}\sigma} \exp\left\lbrace-\frac{(x-\mu)^2}{2\sigma^2}\right\rbrace \left[1 + {\rm erf}\left(\frac{\alpha (x-\mu)}{\sqrt{2}\sigma}\right)\right].\]

The parameters are ordered as follows:

p[0] : \(\mu\)

p[1] : \(\sigma\)

p[2] : \(\alpha\)

References

Return type:

None

Examples

>>> import amep
>>> import numpy as np
>>> g = amep.functions.SkewGaussian()
>>> x = np.linspace(-10, 10, 500)
>>> y = g.generate(
...     x, p=[-1.0, 2.0, 4.0]
... ) + 0.01*np.random.normal(size=x.shape)
>>> g.fit(x, y)
>>> print(g.results)
{'mu': (-0.997718763927072, 0.009639684113005365),
 'sigma': (1.9920368667592616, 0.016343709582216908),
 'alpha': (3.8547382025213865, 0.12910248961985227)}
>>> print(g.mean)
0.5407700079557437
>>> print(g.std)
1.2654102802326845
>>> fig, axs = amep.plot.new()
>>> axs.plot(x, y, label='raw data', ls="")
>>> axs.plot(
...     x, g.generate(x), marker="", lw=2,
...     label='skewed Gaussian fit'
... )
>>> axs.set_xlabel(r'$x$')
>>> axs.set_ylabel(r'$g(x)$')
>>> axs.legend()
>>> fig.savefig('./figures/functions/functions-SkewGaussian.png')
../_images/functions-SkewGaussian.png

Methods

__init__()

Initializes a function object of a skewed normal distribution.

f(p, x)

Skewed Gaussian function of the form

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#

Skewed Gaussian function of the form

\[g_{\rm skeq}(x) = \frac{1}{\sqrt{2\pi}\sigma} \exp\left\lbrace-\frac{(x-\mu)^2}{2\sigma^2}\right\rbrace \left[1 + {\rm erf}\left(\frac{\alpha (x-\mu)}{\sqrt{2}\sigma}\right)\right].\]
Parameters:
  • p (list | np.ndarray) – Parameters \(\mu, \sigma\), and \(\alpha\) of the skewed 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