Mathematical utilities — pygama.math¶
The pygama.math sub-package provides the statistical infrastructure
used throughout pygama: histogram helpers, probability distributions, and
fitting routines. It is designed to be usable independently of the rest of
the processing chain for ad-hoc data analysis.
Overview¶
pygama.math is organised around three concerns:
Histogramming — convenience wrappers around boost-histogram that return the
(hist, bins, var)triple expected by the fitting routines.Distributions — a library of probability density functions (PDFs) and cumulative distribution functions (CDFs) that model the signal and background shapes encountered in HPGe detector data. All functions are JIT-compiled with Numba for speed and are compatible with iminuit.
Fitting — thin wrappers around iminuit that expose a consistent interface for binned and unbinned maximum-likelihood fits, including goodness-of-fit statistics and uncertainty estimation.
Submodules¶
histogram¶
pygama.math.histogram provides convenience functions for working with
one-dimensional histograms. All functions operate on the standard pygama
histogram triple (hist, bins, var):
hist— array of bin counts (or weighted counts).bins— array of bin edges, lengthlen(hist) + 1.var— array of per-bin variances; defaults tohist(Poisson statistics) if not supplied.
Function |
Description |
|---|---|
Bin a 1-D array of data values, returning |
|
Compute the full-width at half-maximum (FWHM) of a histogram peak. |
|
Compute the full-width at a given fraction of the maximum (FWFM). |
|
Return the indices of local maxima above a threshold, used for automatic peak finding. |
|
Evaluate a normalised PDF on a histogram grid for use in plotting. |
|
Plot a histogram triple on a Matplotlib axes object. |
distributions¶
pygama.math.distributions re-exports all distribution objects defined
in the pygama.math.functions sub-package. Each distribution is a
PygamaContinuous instance
(a subclass of scipy.stats.rv_continuous) with Numba-compiled PDF
and CDF methods.
Composite distributions — a signal peak plus a background model — are the most commonly used in HPGe peak fitting:
Distribution |
Description |
|---|---|
|
Full HPGe peak model: a Gaussian signal component convolved with a low-energy tail (exGaussian), sitting on a step-function background with a linear slope. The standard model for fitting calibration peaks. |
|
Gaussian signal on a step-function background. |
|
Gaussian signal on a linear background. |
|
Gaussian signal on an exponential background. |
|
Gaussian signal on an exGaussian background. |
|
Gaussian signal on a uniform background. |
|
Three Gaussian components on a double step background; used for complex multi-peak regions. |
Primitive distributions:
Distribution |
Description |
|---|---|
|
Normal (Gaussian) distribution. |
|
Exponentially modified Gaussian; models the low-energy tail of HPGe peaks due to incomplete charge collection. |
|
Crystal Ball function; a Gaussian with a power-law low-energy tail. |
|
Step (Heaviside-convolved-with-Gaussian) background. |
|
Exponential background. |
|
Linear background. |
|
Uniform (flat) distribution. |
|
Moyal distribution; approximates the Landau distribution for energy-loss processes. |
Polynomial of arbitrary degree. |
binned_fitting¶
pygama.math.binned_fitting provides a unified interface for fitting a
function to a histogram triple. The default cost function is an extended
binned log-likelihood fit (iminuit.cost.ExtendedBinnedNLL).
Function |
Description |
|---|---|
Fit a model to a histogram triple; returns best-fit parameter values,
uncertainties, and optionally the full |
|
Compute a reduced chi-squared statistic and p-value for a fit result. |
|
Fit a Gaussian to a histogram peak and return the mode, width, and maximum, useful for robust initial-parameter estimation. |
unbinned_fitting¶
pygama.math.unbinned_fitting provides the unbinned counterpart to
binned_fitting. The default cost function is the extended
unbinned negative log-likelihood (iminuit.cost.ExtendedUnbinnedNLL).
Unbinned fits are preferred when the dataset is small (fewer than a few thousand events) or when the bin-size choice would significantly affect the result.
Function |
Description |
|---|---|
Fit a model to an array of unbinned data values using extended unbinned negative log-likelihood minimisation. |
hpge_peak_fitting¶
pygama.math.hpge_peak_fitting contains higher-level routines that
combine the distribution and fitting machinery to fit the standard HPGe peak
model (hpge_peak) to data, with
automatic initial-parameter estimation.
Function |
Description |
|---|---|
Compute the FWHM of an HPGe peak from the fit parameters, propagating uncertainties analytically. |
|
Compute the full-width at a given fraction of the maximum. |
|
Return the mode (peak position) of the HPGe peak model. |
least_squares¶
pygama.math.least_squares provides simple linear least-squares helpers
used internally by pygama.pargen.energy_cal.
units¶
pygama.math.units exposes the unit registry (backed by pint) and a small set of unit-conversion helpers.
For the complete parameter reference see pygama.math.