NAME
Math::Histo::Fit - Curve Fitting Engine and Results for Math::Histo
SYNOPSIS
use Math::Histo;
my $h = Math::Histo->new(bins => 100, min => -5.0, max => 5.0);
# ... fill data ...
# Non-linear Gaussian fit via Levenberg-Marquardt
my $res = $h->fit(
model => 'gaussian',
max_iter => 500,
tol => 1e-8,
);
if ($res->status >= 0) {
print $res->summary;
my $params = $res->params; # [A, mu, sigma]
my $errors = $res->errors; # [sigma_A, sigma_mu, sigma_sigma]
my $cov = $res->cov_matrix; # 2D arrayref covariance matrix
printf("Chi2 / NDF: %.2f / %d (p-value: %.4g)\n", $res->chi2, $res->ndf, $res->p_value);
}
DESCRIPTION
Math::Histo::Fit provides non-linear least squares curve fitting using the Levenberg-Marquardt algorithm with adaptive damping, parameter box constraints, fixed parameters, and automatic moment-based initial guess heuristics.
Supported Built-in Models: - 'gaussian': f(x) = A * exp(-(x - mu)^2 / (2 * sigma^2)) - 'exponential': f(x) = A * exp(-lambda * x) + C - 'polynomial': f(x) = c0 + c1*x + c2*x^2 + ... (direct Linear LS) - 'breit_wigner': Breit-Wigner / Cauchy-Lorentz resonance peak - 'power_law': f(x) = A * (x - x0)^k
Math::Histo::Fit::Result METHODS
- status(): Convergence integer status (>= 0 is success).
- status_str(): Human-readable status name (e.g.
CONVERGED_FTOL,CONVERGED_EXACT). - iterations(): Iterations taken.
- n_params(): Number of parameters.
- params(): Arrayref of optimal fitted parameter values.
- errors(): Arrayref of parameter standard errors (sqrt(Cov_ii)).
- cov_matrix(): 2D arrayref covariance matrix.
- chi2(): Total Chi-Square value at minimum.
- ndf(): Degrees of freedom (N_bins - N_free_params).
- reduced_chi2(): Reduced Chi-Square (chi2 / ndf).
- p_value(): Goodness-of-fit upper tail probability P(Chi2 >= chi2_obs).
- aic(), bic(): Akaike and Bayesian Information Criteria.
- summary(): Formatted multiline diagnostics summary string.
SEE ALSO
libhistoCurve Fitting Guide: https://github.com/tsee/libhisto/blob/main/docs/curve_fitting_guide.md
AUTHOR
Steffen Mueller <cpan@steffen-mueller.net>
LICENSE
MIT License. Copyright (c) 2026 Steffen Mueller and libhisto contributors.