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 - 'lognormal': f(x) = (A / (x*sigma*sqrt(2*pi))) * exp(-(ln(x) - mu)^2 / (2*sigma^2)) - 'gauss_linear': f(x) = A * exp(-(x - mu)^2 / (2*sigma^2)) + c0 + c1*x - 'weibull': f(x) = A * (k/lambda) * (x/lambda)^(k-1) * exp(-(x/lambda)^k) - 'gamma': f(x) = A * (x^(k-1) * exp(-x/theta)) / (Gamma(k) * theta^k) - 'poisson': f(x) = A * (lambda^x * exp(-lambda)) / Gamma(x + 1) - 'laplace': f(x) = (A / (2*b)) * exp(-|x - mu| / b)

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

AUTHOR

Steffen Mueller <cpan@steffen-mueller.net>

LICENSE

MIT License. Copyright (c) 2026 Steffen Mueller and libhisto contributors.