foresight_fit
foresight_fit, gnuplot's
fit: nonlinear least squares by the Marquardt-Levenberg method.
A translation of gnuplot 6.0's fit.c and matrix.c, so that a fit gives gnuplot's parameters, errors and iterations:
- the parameters are prescaled by their initial values (each one starts at +-1; a zero one at 1e-30, unscaled);
- the model derivatives are one-sided differences, the step 0.001 of each scaled parameter;
- each step solves the least squares problem of the Jacobian over the errors, augmented by lambda times the identity, by Givens rotations; a step that does not increase chi-square is taken and lambda divided by 10, else lambda is multiplied by 10 and the step tried again; lambda starts at the root mean square of the Jacobian entries;
- the fit stops when chi-square decreases by no more than
limittimes itself (pluslimit_abs), aftermaxiteriterations if set, when lambda exceeds 1e20 or chi-square is zero; - the parameter errors are the square roots of the diagonal of (J^T J)^-1, scaled by sqrt(chi-square / ndf) when the data carry no errors or
errorscalingholds (gnuplot's default); the correlations follow from the same matrix.
levenberg_marquardt fits any extension of fit_model (values at the data points for a parameter vector; the script fits user functions through it); fit fits a Fortran function of x; fit_report writes gnuplot's report.
Source: src/lib/foresight_fit.F90
Dependencies
Contents
- fit_settings
- fit_result
- fit_model
- function_model
- fit
- function_values
- model_slopes
- function_slopes
- levenberg_marquardt
- append
- givens
- invert_rtr
- fit_report
- sumsq
- chisq_cdf
- cformat
- field
- results_text
Variables
| Name | Type | Attributes | Description |
|---|---|---|---|
NEARLY_ZERO | real(kind=R8P) | parameter | gnuplot's floor of a parameter and of chi-square. |
DELTA | real(kind=R8P) | parameter | Relative step of the numerical derivatives. |
MIN_LAMBDA | real(kind=R8P) | parameter | lambda is not decreased below. |
MAX_LAMBDA | real(kind=R8P) | parameter | The fit stops above. |
Derived Types
fit_settings
gnuplot's set fit options.
Components
| Name | Type | Attributes | Description |
|---|---|---|---|
limit | real(kind=R8P) | Relative change of chi-square that stops the fit (limit, FIT_LIMIT). | |
limit_abs | real(kind=R8P) | Absolute part of the stopping change (limit_abs). | |
maxiter | integer(kind=I4P) | Most iterations, 0 for no limit (maxiter, FIT_MAXITER). | |
start_lambda | real(kind=R8P) | First lambda, 0 for gnuplot's estimate (start_lambda). | |
lambda_factor | real(kind=R8P) | lambda factor up and down (lambda_factor). | |
prescale | logical | Parameters scaled by their initial values (prescale). | |
errorscaling | logical | Errors scaled by the reduced chi-square (errorscaling). |
fit_result
Outcome of a fit: gnuplot's FIT_* values, the parameters and their errors.
Components
| Name | Type | Attributes | Description |
|---|---|---|---|
params | real(kind=R8P) | allocatable | Fitted parameters. |
errors | real(kind=R8P) | allocatable | Asymptotic standard errors (a_err), 0 if not computable. |
correlation | real(kind=R8P) | allocatable | Correlations of the parameters, 0 if not computable. |
wssr | real(kind=R8P) | Weighted sum of squared residuals (FIT_WSSR). | |
last_wssr | real(kind=R8P) | WSSR of the iteration before the last. | |
stdfit | real(kind=R8P) | sqrt(WSSR / ndf) (FIT_STDFIT). | |
p | real(kind=R8P) | p-value of chi-square (FIT_P). | |
ndf | integer(kind=I4P) | Degrees of freedom (FIT_NDF). | |
niter | integer(kind=I4P) | Iterations (FIT_NITER). | |
converged | logical | Converged (FIT_CONVERGED). | |
has_errors | logical | Errors and correlations computed. | |
weighted | logical | The data carried errors. | |
stop_text | character(len=:) | allocatable | Why the fit stopped, gnuplot's sentence. |
progress | character(len=:) | allocatable | gnuplot's one-line progress report, iteration after iteration. |
message | character(len=:) | allocatable | Failure, empty on success. |
fit_model
A model fitted by levenberg_marquardt: its values at the data points for a parameter vector. An object, not a procedure argument: an internal procedure passed as an argument costs gfortran an executable stack (trampoline).
Inheritance
Attributes: abstract
Type-Bound Procedures
| Name | Attributes | Description |
|---|---|---|
values | pass(self) | Values at the data points. |
slopes | pass(self) | Slopes in x at the data points (xyerrors). |
function_model
A Fortran function of x at given abscissae, for fit.
Inheritance
Extends: fit_model
Components
| Name | Type | Attributes | Description |
|---|---|---|---|
f | procedure(model_function) | pointer, nopass | Model. |
x | real(kind=R8P) | allocatable | Abscissae. |
Type-Bound Procedures
| Name | Attributes | Description |
|---|---|---|
values | pass(self) | Values at the abscissae. |
slopes | pass(self) | Slopes at the abscissae. |
Subroutines
fit
Fit the Fortran function model(x, params) to the points (x, y), from the initial params (updated), as gnuplot fit f(x) 'data' using 1:2[:3] via ...: err the errors of y (unit weights if absent). Silent unless report is given: '-' writes gnuplot's report to the standard error, as gnuplot's terminal, a file name writes it there (gnuplot's fit.log). names name the parameters in the report (p1, p2, ... if absent). On failure result%message says why and params keep their initial values.
subroutine fit(model, x, y, params, names, err, settings, result, report)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
model | procedure(model_function) | Model. | ||
x | real(kind=R8P) | in | Abscissae. | |
y | real(kind=R8P) | in | Ordinates. | |
params | real(kind=R8P) | inout | Parameters: initial values in, fitted out. | |
names | character(len=*) | in | optional | Parameter names. |
err | real(kind=R8P) | in | optional | Errors of y. |
settings | type(fit_settings) | in | optional | Fit options. |
result | type(fit_result) | out | optional | Outcome. |
report | character(len=*) | in | optional | Report: '-' standard error, else a file. |
Call graph
function_values
The model function at every abscissa.
subroutine function_values(self, p, f, ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
self | class(function_model) | inout | Model. | |
p | real(kind=R8P) | in | Parameters. | |
f | real(kind=R8P) | out | Values. | |
ok | logical | out | All defined. |
model_slopes
Slopes in x of the model at the data points for the parameters p (its values f there): none by default.
subroutine model_slopes(self, p, f, dfdx, ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
self | class(fit_model) | inout | Model. | |
p | real(kind=R8P) | in | Parameters. | |
f | real(kind=R8P) | in | Model values. | |
dfdx | real(kind=R8P) | out | Slopes. | |
ok | logical | out | Every slope defined. |
function_slopes
Slopes of the model function at the abscissae, gnuplot's calc_derivatives: one-sided, the step max(0.001 |x|, 8e-8 (|x| + 1e-8)).
subroutine function_slopes(self, p, f, dfdx, ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
self | class(function_model) | inout | Model. | |
p | real(kind=R8P) | in | Parameters. | |
f | real(kind=R8P) | in | Model values. | |
dfdx | real(kind=R8P) | out | Slopes. | |
ok | logical | out | Every slope defined. |
levenberg_marquardt
Fit model (values at the data points for a parameter vector) to the data z of errors err from the initial parameters a0, as gnuplot's regress (see the module notes); weighted tells the errors were given (for the error scaling and the report). With the errors xerr of the abscissae (gnuplot's xyerrors), each point's error is the effective one, sqrt(err^2 + (f' xerr)^2), the slope f' of the model at the parameters being tried (Orear's effective variance, gnuplot's effective_error).
subroutine levenberg_marquardt(model, z, err, a0, settings, result, weighted, xerr)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
model | class(fit_model) | inout | Model. | |
z | real(kind=R8P) | in | Data. | |
err | real(kind=R8P) | in | Errors of the data. | |
a0 | real(kind=R8P) | in | Initial parameters. | |
settings | type(fit_settings) | in | Options. | |
result | type(fit_result) | out | Outcome. | |
weighted | logical | in | The data carry errors. | |
xerr | real(kind=R8P) | in | optional | Errors of the abscissae. |
Call graph
append
Append more to text through a temporary (no text = text//more: see foresight#3, bug 1).
Attributes: pure
subroutine append(text, more)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=:) | inout | allocatable | Text. |
more | character(len=*) | in | Appended text. |
Call graph
givens
gnuplot's Givens: the QR decomposition of c (N x n, N >= n) by Givens rotations in place, R in its top n rows, d rotated along; with solve, x solves R x + d = 0 by back substitution. ok false on a singular matrix.
Attributes: pure
subroutine givens(c, d, solve, x, ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
c | real(kind=R8P) | inout | Matrix. | |
d | real(kind=R8P) | inout | Right-hand side. | |
solve | logical | in | Solve for x. | |
x | real(kind=R8P) | out | allocatable | Solution. |
ok | logical | out | Not singular. |
Call graph
invert_rtr
gnuplot's Invert_RtR: (R^T R)^-1 of the upper triangular r (n x n), its lower triangle in inv (the upper one mirrored). ok false on a zero diagonal.
Attributes: pure
subroutine invert_rtr(r, inv, ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
r | real(kind=R8P) | in | Upper triangular matrix. | |
inv | real(kind=R8P) | out | Inverse of R^T R. | |
ok | logical | out | Regular. |
Call graph
fit_report
Write gnuplot's fit report of result, for the parameters names: on the standard error (destination '-', gnuplot's terminal output: with progress the progress lines (gnuplot's brief), then the stop sentence and the results) or appended to the file destination (gnuplot's fit.log: a header naming the data source and the function_text, the first and last progress lines, the results).
subroutine fit_report(result, names, destination, source, function_text, progress)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
result | type(fit_result) | in | Outcome. | |
names | character(len=*) | in | Parameter names. | |
destination | character(len=*) | in | '-' or a file name. | |
source | character(len=*) | in | Data description. | |
function_text | character(len=*) | in | Fitted function. | |
progress | logical | in | Terminal: with the progress lines. |
Call graph
Functions
sumsq
Sum of the squares of x, compensated (Neumaier), as gnuplot's sumsq_vec.
Attributes: pure
Returns: real(kind=R8P)
function sumsq(x) result(s)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | in | Values. |
chisq_cdf
Chi-square distribution of dof degrees of freedom at chisq: the regularized lower incomplete gamma function P(dof / 2, chisq / 2) (series below a + 1, continued fraction above).
Attributes: pure
Returns: real(kind=R8P)
function chisq_cdf(dof, chisq) result(p)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
dof | integer(kind=I4P) | in | Degrees of freedom. | |
chisq | real(kind=R8P) | in | Chi-square. |
Call graph
cformat
v formatted by the printf format (%g, %e, %f with flags, width, precision), from its 15 significant digits (foresight_format), as C would print it.
Returns: character(len=:)
function cformat(format, v) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
format | character(len=*) | in | Format. | |
v | real(kind=R8P) | in | Value. |
Call graph
field
text left-aligned in width characters, cut beyond (printf %-W.Ws).
Attributes: pure
Returns: character(len=:)
function field(text, width) result(padded)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=*) | in | Text. | |
width | integer(kind=I4P) | in | Width. |
Call graph
results_text
gnuplot's final results (show_results): sums of squares, degrees of freedom, the parameters and their errors, the correlation matrix.
Returns: character(len=:)
function results_text(result, names) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
result | type(fit_result) | in | Outcome. | |
names | character(len=*) | in | Parameter names. |
Call graph