amep.functions.NormalizedGaussian#

class amep.functions.NormalizedGaussian#

Bases: BaseFunction

Normalized one-dimensional Gaussian.

__init__() None#

Initialize the Function object.

This function is a normalized one-dimensional Gaussian function.

Notes

This Gaussian function has two parameters mu (mean value) and sig (standard deviation). It can be written as

Return type:

None.

Examples

>>> import amep
>>> import numpy as np
>>> g = amep.functions.NormalizedGaussian()
>>> x = np.linspace(-10, 10, 500)
>>> y = g.generate(
...     x, p=[1.5, 2.0]
... ) + 0.025*np.random.normal(size=x.shape)
>>> g.fit(x, y)
>>> print(g.results)
{'mu': (1.501942256986355, 0.03582565085467049),
 'sig': (2.050890988264332, 0.029231471248178938)}
>>> fig, axs = amep.plot.new()
>>> axs.plot(x, y, label='raw data', ls="")
>>> axs.plot(
...     x, g.generate(x),  marker="", lw=2,
...     label='normalized Gaussian fit'
... )
>>> axs.set_xlabel(r'$x$')
>>> axs.set_ylabel(r'$g(x)$')
>>> axs.legend()
>>> fig.savefig('./figures/functions/functions-NormalizedGaussian.png')
../_images/functions-NormalizedGaussian.png

Methods

__init__()

Initialize the Function object.

f(p, x)

Normalized one-dimensional 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

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.

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#

Normalized one-dimensional Gaussian function of the form

Parameters:
  • p (list or np.ndarray) – Parameters $mu$ and $sigma$ of the normalized 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