mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-11-27 10:47:48 +01:00
note on using minimizers
This commit is contained in:
1 parent
60feb01972
commit
e0bfbd313e
1 file changed
+56
-22
+56
-22
@@ -4,12 +4,18 @@ Fitting
|
||||
.. py:currentmodule:: aare
|
||||
|
||||
Aare fits one-dimensional scans and three-dimensional pixel data with
|
||||
Minuit2 or a built-in Levenberg-Marquardt solver. Create a model object and
|
||||
call its :meth:`fit` method::
|
||||
Minuit2's Migrad and Fumili or with the built-in Levenberg-Marquardt and
|
||||
variable projection solvers. Create a model object and call its :meth:`fit`
|
||||
method::
|
||||
|
||||
model = Gaussian(compute_errors=True)
|
||||
result = model.fit(x, y, y_err)
|
||||
|
||||
``y_err`` holds the standard deviation of each point and weights the
|
||||
residuals; points with ``y_err == 0`` are left out of the fit, which skips
|
||||
empty histogram bins when ``y_err = sqrt(y)``. Without ``y_err`` every point
|
||||
has unit weight.
|
||||
|
||||
The model object is also callable, which evaluates it at the supplied points::
|
||||
|
||||
fitted_y = model(x, result["par"])
|
||||
@@ -21,12 +27,14 @@ accepts the same model objects when a functional interface is preferred.
|
||||
|
||||
For three-dimensional data, pass an array with shape
|
||||
``(rows, columns, scan_points)`` and select the worker count with
|
||||
``n_threads``::
|
||||
``n_threads`` (default 4)::
|
||||
|
||||
result = model.fit(x, image_data, image_errors, n_threads=8)
|
||||
|
||||
The result dictionary contains ``par`` and ``chi2``. It also contains
|
||||
``par_err`` when ``compute_errors`` is enabled.
|
||||
The result dictionary contains ``par`` and ``chi2``, with shapes ``(npar,)``
|
||||
and ``(1,)`` for a scan or ``(rows, columns, npar)`` and ``(rows, columns)``
|
||||
for a cube. It also contains ``par_err`` when ``compute_errors`` is enabled.
|
||||
A pixel that fails to converge returns zeros for all three.
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -41,6 +49,15 @@ The result dictionary contains ``par`` and ``chi2``. It also contains
|
||||
Choosing the minimizer
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. note ::
|
||||
|
||||
The built-in VarPro solver is up tp 10x faster than Minuit2
|
||||
, but it (and the built-in Levenberg-Marquardt solver) do not support the full
|
||||
range of Minuit2 features. Test VarPro and use Migrad if encountering issues.
|
||||
Minuit2::Fumili and the built in Levenberg-Marquardt are included for evaluation
|
||||
but might be redundant.
|
||||
|
||||
|
||||
Fits use Minuit2's Migrad by default. Three alternatives share the same
|
||||
interface and are selected with the ``minimizer`` argument or property::
|
||||
|
||||
@@ -59,23 +76,34 @@ interface and are selected with the ``minimizer`` argument or property::
|
||||
- ``Minimizer.LevenbergMarquardt``: a built-in damped Gauss-Newton solver with
|
||||
the analytic Jacobian of the model. It reflects steps at parameter limits
|
||||
and reuses its buffers between pixels, so data cubes are fitted without
|
||||
per-pixel allocations. Parameter errors are ``sqrt(diag((J^T J)^-1))``.
|
||||
per-pixel allocations. Parameter errors are ``sqrt(diag((J^T W J)^-1))``
|
||||
with ``W`` the weights from ``y_err``.
|
||||
- ``Minimizer.VarPro``: a built-in variable projection solver.
|
||||
Every bundled model is linear in some of its parameters (the amplitude of a
|
||||
Gaussian, the baseline and amplitude of an S-curve, every parameter of a
|
||||
polynomial). Each trial point solves those exactly and the
|
||||
Levenberg-Marquardt iteration runs over the remaining nonlinear parameters
|
||||
only, so it needs fewer evaluations than LevenbergMarquardt and cannot be
|
||||
misled by poor start values of the linear parameters, which it ignores.
|
||||
A pixel whose linear solution violates a limit on a linear parameter, or
|
||||
that does not converge, is refitted with LevenbergMarquardt. Parameter
|
||||
errors are the same Gauss-Newton estimates.
|
||||
Every bundled model is linear in some of its parameters: every parameter of
|
||||
a polynomial, the amplitude of a Gaussian, the amplitude and plateau of
|
||||
``GaussianErfcPlateau``, the background and amplitude of the charge-sharing
|
||||
models, and the background, amplitude and charge-sharing term of the
|
||||
S-curves. Each trial point solves those exactly and the Levenberg-Marquardt
|
||||
iteration runs over the remaining nonlinear parameters only, so it needs
|
||||
fewer evaluations than LevenbergMarquardt and is several times faster than
|
||||
Migrad. Start values of the linear parameters are not used, so
|
||||
``SetParameter`` has no effect on them; ``FixParameter`` does. A pixel
|
||||
whose linear solution violates a limit on a linear parameter, or that does
|
||||
not converge, is refitted with LevenbergMarquardt with a fresh
|
||||
``max_calls`` budget. Parameter errors are the same Gauss-Newton estimates.
|
||||
On data that barely constrains a model, VarPro can settle at a point where
|
||||
an amplitude is driven to zero and the parameters attached to it lose
|
||||
their meaning; such a pixel reports a valid fit with a poor ``chi2``.
|
||||
|
||||
All four converge to the same minimum on well-behaved data; the ``[fit]``
|
||||
test suite checks that. The bundled ``fit_benchmark`` compares their speed on
|
||||
Gaussian peaks, S-curves and a data cube. For very noisy data the Gauss-Newton
|
||||
steps of Fumili and LevenbergMarquardt converge more slowly, so compare the
|
||||
minimizers on representative data.
|
||||
Gaussian peaks, S-curves, polynomials, charge-sharing spectra and a data
|
||||
cube. On ill-conditioned data (few points, many parameters, or parameters
|
||||
that nearly trade off against each other) the minimizers stop at different
|
||||
points of a flat chi-squared valley, and Migrad in particular can settle on a
|
||||
parameter limit with a higher ``chi2`` than the others. Compare ``chi2``
|
||||
rather than the parameters when they disagree, fix or bound the poorly
|
||||
constrained parameters, and provide start values for the rest.
|
||||
|
||||
Parameters and errors
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
@@ -87,11 +115,13 @@ side open (the Minuit2 minimizers turn an open side into a wide finite one);
|
||||
free start values are clamped into their limits. The constructor arguments
|
||||
control the minimizer:
|
||||
|
||||
- ``max_calls`` caps the work per pixel: Minuit2 function calls for Migrad and
|
||||
Fumili, model evaluations for LevenbergMarquardt and VarPro,
|
||||
- ``max_calls`` (default 100) caps the work per pixel: Minuit2 function calls
|
||||
for Migrad and Fumili, model evaluations for LevenbergMarquardt and VarPro,
|
||||
which spend one evaluation per iteration (two when a step crosses a limit).
|
||||
``0`` selects Minuit's default budget. A pixel that does not converge within
|
||||
the budget returns zeros.
|
||||
``0`` selects Minuit's default budget of ``200 + 100 * npar + 5 * npar**2``.
|
||||
A pixel that does not converge within the budget returns zeros; the six-
|
||||
and eight-parameter models on noisy data need several hundred calls, so
|
||||
raise the default when such pixels fail.
|
||||
- ``tolerance`` is the EDM tolerance in Minuit's convention. Migrad,
|
||||
LevenbergMarquardt and VarPro stop when the estimated distance
|
||||
to the minimum is below ``0.002 * tolerance``, Fumili below
|
||||
@@ -100,6 +130,10 @@ control the minimizer:
|
||||
LevenbergMarquardt and VarPro ignore it.
|
||||
- ``compute_errors`` adds ``par_err`` to the result. Fixed parameters and
|
||||
parameters that end on a limit report an error of 0 with every minimizer.
|
||||
Migrad's Hesse errors include the residual-weighted second derivatives of
|
||||
the model, which the Gauss-Newton estimates of the other three omit; the
|
||||
two agree when the model describes the data (``chi2`` per degree of freedom
|
||||
near 1) and the Gauss-Newton errors come out smaller for a poorer fit.
|
||||
|
||||
Unknown parameter names raise ``RuntimeError``, bad indices ``IndexError``,
|
||||
and ``SetParLimits`` rejects a lower limit at or above the upper one.
|
||||
|
||||
Reference in new issue
Block a user