| Larch: Data Analysis Tools for X-ray Spectroscopy

# 8.4. Fit Results and Outputs

Changed in version 0.9.34: `minimize()` returns a result group containing fit statistics.

After the fit has completed, several statistics are output and available to describe the quality of the fit and the estimated values for the Parameter values and uncertainties. The main statistics are written to group returned by `minimize()`, while the parameter values in paramgroup are updated to their best-fit values.

The estimated values, uncertainties, and correlations for each varied Parameter are written as attributes of that Parameter. Thus, after a fit, each variable Parameter `par` will be updated so that `par.value` will hold the estimated best-fit value, `par.stderr` will hold the estimated uncertainty (1-\(\sigma\) standard error), and `par.correl` will hold a dictionary of correlation values with the other variable Parameters.

General Fit statistics describing the quality of the fit and details about how the fit proceeded will be put into the result group with variable names and meanings as outlines in Table of Fit Statistics. For advanced users, the fitting class instance and result from lmfit are available.

> Table of Fit Statistics and Results contained in the return value of `minimize()`.

> Listed are the names and description of items in the fit result group returned by the `minimize()` function. Many of these items are directly from lmfit.

> > name | Description of Statistical Quantity or Output
> > --- | ---
> > nvarys | number of variable parameters in the fit
> > ndata | number of data points
> > nfree | ndata - nfree
> > chi_square | chi-square: \(\chi^2 = \sum_i^N [{\rm Resid}_i]^2\)
> > chi_reduced | reduced chi-square: \(\chi^2_{\nu}= {\chi^2} / {(N - N_{\rm varys})}\) |
> > aic | Akaike Information Criteria
> > bic | Bayesian Information Criteria
> > residual | final residual array
> > covar | covariance matrix (ordered according to var_names).
> > var_names | list of variable parameter names
> > params | lmfit Parameters
> > fitter | lmfit Minimizer
> > fit_details | lmfit MinimizerResult
> > nfev | number of evaluations of the fit residual function.
> > success | bool (True or False) for whether fit appeared to succeed.
> > errorbars | bool (True or False) for whether uncertainties were estimated.
> > message | text message from fit
> > lmdif_message | text message from Fortran least-squares function

Changed in version 0.9.34: `fit_report()` uses a result group returned by `minimize()`

`fit_report`(result, show_correl=True, min_correl=0.1)

  returns a fit report for a fit given a parameter group.

  Parameters: | result – fit result group, returned by `minimize()`. show_correl – flag (`True`/`False`) to show parameter correlations. min_correl – smallest absolute value of correlation to show.
  Returns: | string of fit report. This can be printed or stored.

A typical result from `fit_report()` would look like this:

    larch> print fit_report(result)
    [[Fit Statistics]]
        # function evals   = 28
        # data points      = 201
        # variables        = 4
        chi-square         = 0.50471
        reduced chi-square = 0.002562
        Akaike info crit   = -1195.4
        Bayesian info crit = -1182.2
    [[Variables]]
        amp:   12.0312835 +/- 0.076725 (0.64%) (init= 5)
        wid:   2.01663402 +/- 0.011920 (0.59%) (init= 1)
        off:   0.99188155 +/- 0.005226 (0.53%) (init= 0)
        cen:   1.49995279 +/- 0.010144 (0.68%) (init= 2)
    [[Correlations]] (unreported correlations are <  0.100)
        C(amp, off)                  = -0.730
        C(amp, wid)                  =  0.719
        C(wid, off)                  = -0.525