Friday, February 02, 2018

Fit example with rminuit2_expr

rminuit2_expr_exam1
In [2]:
library(rminuit2, quiet=TRUE)
In [4]:
#
# 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
In [5]:
#
# 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))
In [6]:
# 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)
In [10]:
# chi square / number of degrees of freedom
cat("chi square and number of degrees of freedom")
cbind(chisq=fit.rc$chisq, ndof=fit.rc$ndof)

# fitted parameters and their estimated uncertainties
cat("fit parameters: model, fit result, uncertainty, minos uncertainties\n")
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
cat("fit parameter correlation matrix\n")
fit.rc$cor
chi square and number of degrees of freedom
chisqndof
21.8938729
fit parameters: model, fit result, uncertainty, minos uncertainties
model.valuevalueerrorminos_posminos_neg
norm2.30 2.2942003 0.005197186 0.007353179 -0.007346653
tau0.47 0.4701774 0.001727393 0.002452698 -0.002433189
fit parameter correlation matrix
normtau
norm 1.0000000-0.7102571
tau-0.7102571 1.0000000

No comments:

La rovinosa cavalcata del PIL italiano