rminuit2_par: Function Minimization with Minuit2

View source: R/rminuit2.R

rminuit2_parR Documentation

Function Minimization with Minuit2

Description

Performs function minimization using Minuit2

Usage

rminuit2_par(
  mll,
  start,
  err = NULL,
  lower = NULL,
  upper = NULL,
  fix = NULL,
  opt = "h",
  maxcalls = 0L,
  nsigma = 1,
  ...
)

Arguments

mll

The function to be minimized, which must have as first argument a numeric vector of the parameters to be optimized. Futher arguments can be specified as optional arguments in rminuit2_par or in the environment passed using the envir argument.

The function can also be an external pointer to a C++ function compiled using the inline interface. For more info about implementing the objective function as compiled C++ code, see the lbfgs package vignette.

The full potential of this package is attained when the function corresponds to the negative logarithm of a likelihood or minus-log-likelihood (MLL).

start

numeric vector, initial values of the function parameters

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

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

...

extra arguments for the mll function. If the mll function is implemented in C++ then the extra arguments are collected in an environment, which is passed to the function.

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

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)

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

Examples

#
# Rosenbrock Banana function, to be minimized vs. 2 paramaters
#
rosenbrock <- function(par, a, b) {
  x <- par[1]
  y <- par[2]
  (a-x)^2 + b*(y-x^2)^2
}

# minimize Rosenbrock Banana function, also setting parameters a, b
fit.rc <- rminuit2_par(rosenbrock, c(x=0.7, y=1.2), a=1, b=100)

# print fitted parameters
fit.rc$par

#
# simulate model y = a*exp(-x/b)
#
x = seq(0, 1, length.out=31)
y.func = function(x, par) par[1]*exp(-x/par[2])

# simulate data with Gaussian errors for specific model
model.par = c(a=2.3, b=0.47)
y.err = 0.01
y = y.func(x, par=model.par) + rnorm(sd=y.err, n=length(x))

# negative log-likelihood for model
halfchisq = function(par, x, y, y.err) {
  sum( (y - y.func(x, par))^2 / (2 * y.err^2) )
}

# fit model on data, ask to compute Minos errors too
fit.rc = rminuit2_par(halfchisq, c(a=1, b=10), opt="hm", x=x, y=y, y.err=y.err)

# chi square / number of degrees of freedom
cbind(chisq=2*fit.rc$fval, ndof=length(x) - length(model.par))

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.