2 #include "TRInterface.h" 22 return (*gFunction)(x.data());
56 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
58 r[
"minfunction"] = ROOT::R::TRFunctionExport(
minfunction);
62 std::vector<double> values(
X(),
X()+
NDim());
65 r[
"stepsizes"] = stepSizes;
66 r[
"initialparams"] = values;
69 bool optimxloaded = FALSE;
70 r[
"optimxloaded"] = optimxloaded;
71 r.Execute(
"optimxloaded<-library(optimx,logical.return=TRUE)");
73 int ibool = r.Eval(
"optimxloaded");
74 if (ibool==1) optimxloaded=kTRUE;
80 if (optimxloaded==kTRUE) {
83 cmd = TString::Format(
"result <- optimx( initialparams, minfunction,method='%s',control = list(ndeps=stepsizes,maxit=%d,trace=%d,abstol=%e),hessian=TRUE)",
fMethod.c_str(),
MaxIterations(),
PrintLevel(),
Tolerance());
87 cmd = TString::Format(
"result <- optimx( initialparams, minfunction,mingradfunction, method='%s', control = list(ndeps=stepsizes,maxit=%d,trace=%d,abstol=%e),hessian=TRUE)",
fMethod.c_str(),
MaxIterations(),
PrintLevel(),
Tolerance());
96 cmd = TString::Format(
"result <- optim( initialparams, minfunction,method='%s',control = list(ndeps=stepsizes,maxit=%d,trace=%d,abstol=%e),hessian=TRUE)",
fMethod.c_str(),
MaxIterations(),
PrintLevel(),
Tolerance());
100 cmd = TString::Format(
"result <- optim( initialparams, minfunction,mingradfunction, method='%s', control = list(ndeps=stepsizes,maxit=%d,trace=%d,abstol=%e),hessian=TRUE)",
fMethod.c_str(),
MaxIterations(),
PrintLevel(),
Tolerance());
104 std::cout <<
"Calling R with command " << cmd << std::endl;
105 r.Execute(cmd.Data());
110 r.Execute(
"par<-coef(result)");
112 r.Execute(
"hess<-attr(result,\"details\")[,\"nhatend\"]");
114 r.Execute(
"hess<-sapply(hess,function(x) x)");
116 r.Execute(
"hess<-matrix(hess,c(ndim,ndim))");
118 r.Execute(
"cov<-solve(hess)");
120 r.Execute(
"errors<-sqrt(abs(diag(cov)))");
125 r.Execute(
"par<-result$par");
126 r.Execute(
"hess<-result$hessian");
127 r.Execute(
"cov<-solve(hess)");
128 r.Execute(
"errors<-sqrt(abs(diag(cov)))");
133 std::vector<double> vectorPar = r[
"par"];
141 std::vector<double> err = r[
"errors"];
155 const double *min=vectorPar.data();
158 std::cout<<
"Value at minimum ="<<
MinValue()<<std::endl;
165 unsigned int ndim =
NDim();
167 if (i > ndim || j > ndim)
return 0;
176 unsigned int ndim =
NDim();
178 if (i > ndim || j > ndim)
return 0;
const ROOT::Math::IMultiGradFunction * gGradFunction
function wrapper for the gradient of the function to be minimized
virtual const double * X() const
return pointer to X values at the minimum
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Namespace for new ROOT classes and functions.
RMinimizer(Option_t *method)
Default constructor.
TMatrixD fCovMatrix
covariant matrix
std::string fMethod
minimizer method to be used, must be of a type listed in R optim or optimx descriptions ...
std::vector< double > fErrors
vector of parameter errors
virtual double MinValue() const
return minimum function value
virtual void Gradient(const T *x, T *grad) const
Evaluate all the vector of function derivatives (gradient) at a point x.
virtual TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t=-1)
Set size of the matrix to nrows x ncols New dynamic elements are created, the overlapping part of the...
you should not use this method at all Int_t y
int PrintLevel() const
minimizer configuration parameters
virtual bool Minimize()
Function to find the minimum.
virtual const double * StepSizes() const
accessor methods
virtual unsigned int NCalls() const
Returns the number of function calls.
int gNCalls
integer for the number of function calls
double HessMatrix(unsigned int i, unsigned int j) const
Returns the ith jth component of the Hessian matrix.
void SetMinValue(double val)
unsigned int MaxIterations() const
max iterations
virtual unsigned int NDim() const
number of dimensions
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
double Tolerance() const
absolute tolerance
virtual double CovMatrix(unsigned int ivar, unsigned int jvar) const
return covariance matrices element for variables ivar,jvar if the variable is fixed the return value ...
Element * GetMatrixArray()
Documentation for the abstract class IBaseFunctionMultiDim.
const ROOT::Math::IMultiGenFunction * gFunction
function wrapper for the function to be minimized
void SetFinalValues(const double *x)
Int_t GetNoElements() const
TMatrixD fHessMatrix
Hessian matrix.
* x
Deprecated and error prone model selection interface.
const ROOT::Math::IMultiGradFunction * GradObjFunction() const
return pointer to used gradient object function (NULL if gradient is not supported) ...
TVectorD mingradfunction(TVectorD y)
function to return the gradient values at point y
Namespace for new Math classes and functions.
you should not use this method at all Int_t Int_t z
double minfunction(const std::vector< double > &x)
function to return the function values at point x