rminuit2_expr: Function Minimization and model fitting with Minuit2

rminuit2_exprR Documentation

Function Minimization and model fitting with Minuit2

Description

Fit data to a model performing minus log-likelihood minimization using Minuit2 and assuming Gaussian uncertainties

Usage

rminuit2_expr(
  formula,
  start,
  data = NULL,
  weights = NULL,
  errors = NULL,
  err = NULL,
  lower = NULL,
  upper = NULL,
  fix = NULL,
  lh = c("Gaussian"),
  opt = "h",
  maxcalls = 0L,
  nsigma = 1,
  rhs_vars = NULL,
  ...
)

Arguments

formula

expression describing the model to be fitted to data

start

initial values of the model parameters to be fitted

data

list or data.frame containing the data to be fitted to the model

weights

formula corresponding to weights to assign to the data observations

errors

formula corresponding to the uncertaintites of the data observations

err

numeric vector, expected uncertainty of function parameters

lower

numeric vector, lower bounds for the parameters (default none)

upper

numeric vector, upper bounds for the parameters (default none)

fix

boolean vector, each TRUE element fixes the corresponding parameter

lh

desired likelihood for the measurement errors, one of:

Gaussian:

measurements have Gaussian errors

opt

string, pass fit options, default "h" (compute HESSE errors)

v:

Verbose mode (not yet implemented)

h:

Run Hesse to estimate errors

m:

Get Minos errors

0:

Run Migrad with strategy 0

1:

Run Migrad with strategy 1

2:

Run Migrad with strategy 2

neither 1, 2, 3:

Run Migrad with strategy 1, and if it fails run with strategy 2

maxcalls

integer, maximum number of calls, defaults to 0 (no limit).

nsigma

numeric, number of standard deviations for Minos errors

rhs_vars

character vector with the name of all variables in the right-hand side (RHS) of formula. The code attempts to get the variables automatically from the formula expression if this argument is not provided, but may fail. For simple formula, most often there is no need to provide this argument.

...

extra arguments for the model, weights and error formulas

Value

A list with the following components:

fval:

Value of function at found minimum (1/2 * chi square if the function is the negative log-likelihood of a Gaussian likelihood.

Edm:

Estimated distance from the value of the function true minimum

par:

Fitted parameters

err:

Estimated uncertainties of fitted parameters

cov:

Covariance matrix of the fitted parameters

cor:

Correlation matrix of the fitted parameters

chisq:

Chi square (sum of the residuals divided by their estimated uncertainties) at the minimum.

ndof:

Number of degrees of freedom, equal to the number of observations minus the number of the optimized fit parameters

nobs:

Number of observations

err_minos_pos:

Minos-estimated positive parameters' uncertainties (if Minos errors were requested)

err_minos_neg:

Minos-estimated negative parameters' uncertainties (if Minos errors requested)

err_minos_pos_valid:

boolean vector, TRUE if Minos positive uncertainties are valid (if Minos errors were requested)

err_minos_neg_valid:

boolean vector, TRUE if Minos negative uncertainties are valid (if Minos errors were requested)

fun:

function corresponding to model with parameters initialized to the fitted values

fun_par:

function corresponding to model with parameters initialized to the fitted values, all parameters are passed as a possibly-named numeric vector in the last argument, which is named par

fun_mll:

Function that has been assembled to compute the likelihood, with a single argument par containing all fitted parameters, plus extra arguments for the extra parameters, if existing

fun_pulls:

Function returning the array of pulls (residual divided by estimated uncertainty), one for each observation. It has the same parameters as fun_mll

allOK:

TRUE if the fit converged and the parameters and their covariance are OK

MinosErrorsValid:

TRUE if the MINOS errors are all valid

IsValid:

TRUE if the fit minimization converged

IsValidFirstInvocation:

TRUE if Minuit strategy 1 succeeded (if it failed Minuit2 strategy 2 is performed)

IsAboveMaxEdm:

TRUE if the estimated distance from the true minimum is above the tolerance

HasReachedCallLimit:

TRUE if the maximum call limit was exceeded

HasValidParameters:

TRUE if the fitted parameters are considered valid

HasCovariance:

TRUE if a covariance matrix is returned

HasValidCovariance:

TRUE if the estimated covariance matrix is considered valid

HasAccurateCovar:

TRUE if the accuracy of the estimated covariance matrix is considered valid

HasPosDefCovar:

TRUE if the numerically computed covariance matrix is positive definite

HasMadePosDefCovar:

TRUE if the covariance matrix has been adjusted to make it positive definite

HesseFailed:

TRUE if the numeric computation of the HESSE matrix failed

Author(s)

Alberto Lusiani, alusiani@gmail.com

See Also

rminuit2 rminuit2_par

Examples

#
# simulate model y = a*exp(-x/b) + k
#
x = seq(0, 1, length.out=31)
y.func = function(x, norm, tau, const) norm*exp(-x/tau) + const

#
# simulate data with Gaussian errors for specific model
# the model includes norm and tau but not const, which is a fixed parameter
#
model.par = c(norm=2.3, tau=0.47)
model.extra.const = 4.7
y.err = 0.01
y = do.call(y.func, c(list(x), model.par, const=model.extra.const)) + rnorm(sd=y.err, n=length(x))

# fit model on data, ask to compute Minos errors too
fit.rc = rminuit2_expr(y ~ norm*exp(-x/tau) + const, c(norm=1, tau=10),
  data=data.frame(x=x, y=y, y.err=y.err), errors=y.err,
  lh="Gaussian", opt="hm", const=model.extra.const)

# chi square / number of degrees of freedom
cbind(chisq=fit.rc$chisq, ndof=fit.rc$ndof)

# fitted parameters and their estimated uncertainties
cbind(model.value=model.par, value=fit.rc$par, error=fit.rc$err,
      minos_pos=fit.rc$err_minos_pos, minos_neg=fit.rc$err_minos_neg)

# parameters' correlation matrix
fit.rc$cor


alusiani/rminuit2 documentation built on Aug. 7, 2024, 8:38 a.m.