pygra.fitting
fitting.py — fit functions for histogram and xy data
- pygra.fitting.fit_gaussian(data)[source]
Fit a Gaussian (normal) distribution to data using MLE.
- Parameters:
data (numpy.ndarray) – 1-D array of sample values.
- Returns:
x (numpy.ndarray) – 300 evenly-spaced points spanning
[data.min(), data.max()].y (numpy.ndarray) – Gaussian PDF evaluated at each point in x.
label (str) – Human-readable label including the fitted μ and σ.
params (dict) –
{"μ": float, "σ": float}— fitted mean and standard deviation.
- pygra.fitting.fit_exponential(data)[source]
Fit an exponential distribution to data with fixed location (loc=0).
- Parameters:
data (numpy.ndarray) – 1-D array of non-negative sample values.
- Returns:
x (numpy.ndarray) – 300 evenly-spaced points spanning
[data.min(), data.max()].y (numpy.ndarray) – Exponential PDF evaluated at each point in x.
label (str) – Human-readable label including the fitted rate λ.
params (dict) –
{"λ": float}— fitted rate parameter (1 / scale).
- pygra.fitting.fit_maxwell_boltzmann(data)[source]
Fit a Maxwell-Boltzmann distribution to data with fixed location (loc=0).
- Parameters:
data (numpy.ndarray) – 1-D array of positive sample values.
- Returns:
x (numpy.ndarray) – 300 evenly-spaced points spanning
[max(data.min(), 0), data.max()].y (numpy.ndarray) – Maxwell-Boltzmann PDF evaluated at each point in x.
label (str) – Human-readable label including the fitted scale parameter a.
params (dict) –
{"a": float}— fitted scale parameter.
- pygra.fitting.fit_poisson(data)[source]
Estimate the Poisson parameter μ from data and compute the PMF.
The estimator is the sample mean, which is the MLE for the Poisson distribution.
- Parameters:
data (numpy.ndarray) – 1-D array of non-negative integer-valued samples (stored as float).
- Returns:
x (numpy.ndarray) – Integer x-values from
int(data.min())toint(data.max())inclusive, cast to float.y (numpy.ndarray) – Poisson PMF values at each integer in x.
label (str) – Human-readable label including the estimated μ.
params (dict) –
{"μ": float}— estimated mean (sample mean of data).
- pygra.fitting.fit_gaussian_curve(x, y)[source]
Fit A * exp(-(x - mu)**2 / (2 * sigma**2)) to xy data.
- Parameters:
x (numpy.ndarray) – 1-D array of x values.
y (numpy.ndarray) – 1-D array of y values.
- Returns:
x_fine (numpy.ndarray) – 300 evenly-spaced points spanning
[x.min(), x.max()].y_fine (numpy.ndarray) – Fitted curve evaluated at each point in x_fine.
label (str) – Human-readable label including the fitted parameters.
params (dict) –
{"A": float, "μ": float, "σ": float}.
- pygra.fitting.fit_exponential_curve(x, y)[source]
Fit A * exp(-x / tau) + C to xy data.
- Parameters:
x (numpy.ndarray) – 1-D array of x values.
y (numpy.ndarray) – 1-D array of y values.
- Returns:
x_fine (numpy.ndarray) – 300 evenly-spaced points spanning
[x.min(), x.max()].y_fine (numpy.ndarray) – Fitted curve evaluated at each point in x_fine.
label (str) – Human-readable label including the fitted parameters.
params (dict) –
{"A": float, "τ": float, "C": float}.
- pygra.fitting.fit_custom(data, formula, param_names)[source]
Fit a user-defined formula to the density histogram of data.
The formula is evaluated in a sandboxed namespace that exposes NumPy ufuncs (e.g.
exp,sin,sqrt) and math constants. NumPy ufuncs take priority over their scalarmathcounterparts so that array formulas work correctly.- Parameters:
data (numpy.ndarray) – 1-D array of sample values used to build the histogram.
formula (str) – Python expression in terms of
xand the names listed in param_names. Example:"a * exp(-b * x)".param_names (list of str) – Names of the free parameters appearing in formula. All initial guesses are set to 1.0.
- Returns:
x (numpy.ndarray) – 300 evenly-spaced points spanning
[data.min(), data.max()].y (numpy.ndarray) – Formula evaluated at x with the optimised parameters.
label (str) – Human-readable label listing each fitted parameter value.
params (dict) – Mapping of parameter name → fitted float value.
- Raises:
scipy.optimize.OptimizeWarning – If the covariance of the parameters could not be estimated.
RuntimeError – If the least-squares minimisation fails entirely.
Notes
All initial parameter guesses are 1.0. Formulas that require very different starting points may not converge from these defaults.