Logo ROOT  
Reference Guide
Minimization.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example based in http://root.cern.ch/root/html/tutorials/fit/NumericalMinimization.C.html http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html

#include<TRInterface.h>
//in the next function the *double pointer must be changed by a TVectorD,
//because the pointer has no meaning in R enviroment.
Double_t RosenBrock(const TVectorD xx )
{
const Double_t x = xx[0];
const Double_t y = xx[1];
const Double_t tmp1 = y-x*x;
const Double_t tmp2 = 1-x;
return 100*tmp1*tmp1+tmp2*tmp2;
}
TVectorD RosenBrockGrad(const TVectorD xx )
{
const Double_t x = xx[0];
const Double_t y = xx[1];
TVectorD grad(2);
grad[0]=-400 * x * (y - x * x) - 2 * (1 - x);
grad[1]=200 * (y - x * x);
return grad;
}
{
//passsing RosenBrock function to R
r["RosenBrock"]=ROOT::R::TRFunctionExport(RosenBrock);
//passsing RosenBrockGrad function to R
r["RosenBrockGrad"]=ROOT::R::TRFunctionExport(RosenBrockGrad);
//the option "method" could be "Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN","Brent"
//the option "control" lets you put some constraints like
//"maxit" The maximum number of iterations.
//"abstol" The absolute convergence tolerance.
r.Execute("result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )");
//"reltol" Relative convergence tolerance.
//Getting results from R
TVectorD min=r.Eval("result$par");
std::cout.precision(8);
//printing results
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"Minimum x="<<min[0]<<" y="<<min[1]<<std::endl;
std::cout<<"Value at minimum ="<<RosenBrock(min)<<std::endl;
//using the gradient
r.Execute("optimHess(result$par, RosenBrock, RosenBrockGrad)");
r.Execute("hresult <- optim(c(-1.2,1), RosenBrock, NULL, method = 'BFGS', hessian = TRUE)");
//getting the min calculated with the gradient
TVectorD hmin=r.Eval("hresult$par");
//printing results
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"Minimization with the Gradient"<<std::endl;
std::cout<<"Minimum x="<<hmin[0]<<" y="<<hmin[1]<<std::endl;
std::cout<<"Value at minimum ="<<RosenBrock(hmin)<<std::endl;
}
Author
Omar Zapata

Definition in file Minimization.C.

ROOT::R::TRFunctionExport
Definition: TRFunctionExport.h:153
RooFit::Minimization
@ Minimization
Definition: RooGlobalFunc.h:67
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::R::TRInterface::Eval
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
Definition: TRInterface.cxx:78
ROOT::R::TRInterface
Definition: TRInterface.h:137
x
Double_t x[n]
Definition: legend1.C:17
ROOT::R::TRInterface::Execute
void Execute(const TString &code)
Method to eval R code.
Definition: TRInterface.cxx:97
TRInterface.h
y
Double_t y[n]
Definition: legend1.C:17
TVectorT
Definition: TMatrixTBase.h:78
Double_t
double Double_t
Definition: RtypesCore.h:59
ROOT::R::TRInterface::Instance
static TRInterface & Instance()
static method to get an TRInterface instance reference
Definition: TRInterface.cxx:185