26 unsigned int size =
y.GetNoElements();
27 const double * yy =
y.GetMatrixArray();
61 std::vector<double> values(
X(),
X()+
NDim());
64 r[
"stepsizes"] = stepSizes;
65 r[
"initialparams"] = values;
68 bool optimxloaded = FALSE;
69 r[
"optimxloaded"] = optimxloaded;
70 r.Execute(
"optimxloaded<-library(optimx,logical.return=TRUE)");
72 int ibool =
r.Eval(
"optimxloaded");
73 if (ibool==1) optimxloaded=
kTRUE;
79 if (optimxloaded==
kTRUE) {
82 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());
86 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());
95 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());
99 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());
103 std::cout <<
"Calling R with command " << cmd << std::endl;
104 r.Execute(cmd.
Data());
109 r.Execute(
"par<-coef(result)");
111 r.Execute(
"hess<-attr(result,\"details\")[,\"nhatend\"]");
113 r.Execute(
"hess<-sapply(hess,function(x) x)");
115 r.Execute(
"hess<-matrix(hess,c(ndim,ndim))");
117 r.Execute(
"cov<-solve(hess)");
119 r.Execute(
"errors<-sqrt(abs(diag(cov)))");
124 r.Execute(
"par<-result$par");
125 r.Execute(
"hess<-result$hessian");
126 r.Execute(
"cov<-solve(hess)");
127 r.Execute(
"errors<-sqrt(abs(diag(cov)))");
132 std::vector<double> vectorPar =
r[
"par"];
140 std::vector<double>
err =
r[
"errors"];
154 const double *min=vectorPar.data();
157 std::cout<<
"Value at minimum ="<<
MinValue()<<std::endl;
164 unsigned int ndim =
NDim();
166 if (i > ndim || j > ndim)
return 0;
175 unsigned int ndim =
NDim();
177 if (i > ndim || j > ndim)
return 0;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
const char Option_t
Option string (const char).
TMatrixT< Double_t > TMatrixD
TVectorT< Double_t > TVectorD
unsigned int NDim() const override
number of dimensions
void SetMinValue(double val)
void SetFinalValues(const double *x, const MinimTransformFunction *func=nullptr)
double MinValue() const override
return minimum function value
virtual const double * StepSizes() const
accessor methods
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
const ROOT::Math::IMultiGradFunction * GradObjFunction() const
return pointer to used gradient object function (NULL if gradient is not supported)
const double * X() const override
return pointer to X values at the minimum
double Tolerance() const
Absolute tolerance.
unsigned int MaxIterations() const
Max iterations.
int PrintLevel() const
Set print level.
double HessMatrix(unsigned int i, unsigned int j) const
Returns the ith jth component of the Hessian matrix.
TMatrixD fCovMatrix
covariant matrix
std::vector< double > fErrors
vector of parameter errors
bool Minimize() override
Function to find the minimum.
std::string fMethod
minimizer method to be used, must be of a type listed in R optim or optimx descriptions
unsigned int NCalls() const override
Returns the number of function calls.
double CovMatrix(unsigned int ivar, unsigned int jvar) const override
return covariance matrices element for variables ivar,jvar if the variable is fixed the return value ...
RMinimizer(Option_t *method)
Default constructor.
TMatrixD fHessMatrix
Hessian matrix.
This is a class to pass functions from ROOT to R.
ROOT R was implemented using the R Project library and the modules Rcpp and RInside.
static TRInterface & Instance()
static method to get an TRInterface instance reference
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
IGradientFunctionMultiDim IMultiGradFunction
const ROOT::Math::IMultiGenFunction * gFunction
function wrapper for the function to be minimized
IMultiGenFunctionTempl< double > IMultiGenFunction
double minfunction(const std::vector< double > &x)
function to return the function values at point x
TVectorD mingradfunction(TVectorD y)
function to return the gradient values at point y
const ROOT::Math::IMultiGradFunction * gGradFunction
function wrapper for the gradient of the function to be minimized
int gNCalls
integer for the number of function calls