20 #include "gsl/gsl_errno.h" 42 FitTransformFunction(
const FitMethodFunction & f,
const std::vector<EMinimVariableType> & types,
const std::vector<double> & values,
43 const std::map<
unsigned int, std::pair<double, double> > & bounds) :
45 fOwnTransformation(true),
47 fTransform(new MinimTransformFunction( new MultiNumGradFunction(f), types, values, bounds) ),
48 fGrad(
std::vector<double>(f.NDim() ) )
55 FitTransformFunction(
const FitMethodFunction & f, MinimTransformFunction *transFunc ) :
57 fOwnTransformation(false),
59 fTransform(transFunc),
60 fGrad(
std::vector<double>(f.NDim() ) )
65 ~FitTransformFunction() {
66 if (fOwnTransformation) {
73 virtual double DataElement(
const double *
x,
unsigned i,
double * g = 0)
const {
75 const double * xExt = fTransform->Transformation(x);
76 if ( g == 0)
return fFunc.DataElement( xExt, i );
78 double val = fFunc.DataElement( xExt, i, &fGrad[0]);
80 fTransform->GradientTransformation( x, &fGrad.front(), g);
91 unsigned int NDim()
const {
92 return fTransform->NDim();
95 unsigned int NTot()
const {
96 return fTransform->NTot();
100 const double * Transformation(
const double * x)
const {
return fTransform->Transformation(x); }
104 void InvTransformation(
const double * xext,
double * xint)
const { fTransform->InvTransformation(xext,xint); }
107 void InvStepTransformation(
const double * x,
const double * sext,
double * sint)
const { fTransform->InvStepTransformation(x,sext,sint); }
110 void GradientTransformation(
const double * x,
const double *gext,
double * gint)
const { fTransform->GradientTransformation(x,gext,gint); }
112 void MatrixTransformation(
const double * x,
const double *cint,
double * cext)
const { fTransform->MatrixTransformation(x,cint,cext); }
117 FitTransformFunction(
const FitTransformFunction& rhs);
118 FitTransformFunction& operator=(
const FitTransformFunction& rhs);
120 double DoEval(
const double * x)
const {
121 return fFunc( fTransform->Transformation(x) );
124 bool fOwnTransformation;
126 MinimTransformFunction * fTransform;
127 mutable std::vector<double> fGrad;
142 const gsl_multifit_fdfsolver_type * gsl_type = 0;
143 if (type == 1) gsl_type = gsl_multifit_fdfsolver_lmsder;
144 if (type == 2) gsl_type = gsl_multifit_fdfsolver_lmder;
152 if (niter <= 0) niter = 100;
178 if (
PrintLevel() > 0) std::cout <<
"GSLNLSMinimizer: Invalid function set - only Chi2Func supported" << std::endl;
186 for (
unsigned int i = 0; i <
fSize; ++i) {
196 return SetFunction(static_cast<const ROOT::Math::IMultiGenFunction &>(func) );
207 MATH_ERROR_MSG(
"GSLNLSMinimizer::Minimize",
"Function has not been set");
211 unsigned int npar =
NPar();
212 unsigned int ndim =
NDim();
213 if (npar == 0 || npar < ndim) {
214 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Wrong number of parameters",npar);
219 std::vector<double> startValues;
225 std::unique_ptr<FitTransformFunction> trFunc;
227 trFunc.reset(
new FitTransformFunction(*
fChi2Func, trFuncRaw) );
229 for (
unsigned int ires = 0; ires <
fResiduals.size(); ++ires) {
233 assert(npar == trFunc->NTot() );
236 if (debugLevel >=1 ) std::cout <<
"Minimize using GSLNLSMinimizer " << std::endl;
246 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Error setting the residual functions ",iret);
250 if (debugLevel >=1 ) std::cout <<
"GSLNLSMinimizer: " <<
fGSLMultiFit->
Name() <<
" - start iterating......... " << std::endl;
253 unsigned int iter = 0;
255 bool minFound =
false;
259 if (debugLevel >=1) {
260 std::cout <<
"----------> Iteration " << iter <<
" / " <<
MaxIterations() <<
" status " << gsl_strerror(status) << std::endl;
262 if (trFunc.get()) x = trFunc->Transformation(x);
263 int pr = std::cout.precision(18);
264 std::cout <<
" FVAL = " << (*fChi2Func)(
x) << std::endl;
265 std::cout.precision(pr);
266 std::cout <<
" X Values : ";
267 for (
unsigned int i = 0; i <
NDim(); ++i)
269 std::cout << std::endl;
276 if (status == GSL_SUCCESS) {
282 if ( minFound && status2 != GSL_SUCCESS) {
292 if (debugLevel >=1) {
293 std::cout <<
" after Gradient and Delta tests: " << gsl_strerror(status);
294 if (
fEdm > 0) std::cout <<
", edm is: " <<
fEdm;
295 std::cout << std::endl;
311 if (x == 0)
return false;
330 std::copy(cov, cov + ndim*ndim,
fCovMatrix.begin() );
333 for (
unsigned int i = 0; i < ndim; ++i)
339 if (debugLevel >=1 ) {
340 std::cout <<
"GSLNLSMinimizer: Minimum Found" << std::endl;
341 int pr = std::cout.precision(18);
342 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
343 std::cout <<
"Edm = " <<
fEdm << std::endl;
344 std::cout.precision(pr);
345 std::cout <<
"NIterations = " << iter << std::endl;
347 for (
unsigned int i = 0; i <
NDim(); ++i)
348 std::cout << std::setw(12) <<
VariableName(i) <<
" = " << std::setw(12) <<
X()[i] <<
" +/- " << std::setw(12) <<
fErrors[i] << std::endl;
354 if (debugLevel >=1 ) {
355 std::cout <<
"GSLNLSMinimizer: Minimization did not converge" << std::endl;
356 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
358 std::cout <<
"Niterations = " << iter << std::endl;
373 unsigned int ndim =
NDim();
375 if (i > ndim || j > ndim)
return 0;
384 if (
fStatus != GSL_SUCCESS)
return 1;
virtual const double * X() const
return pointer to X values at the minimum
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
~GSLNLSMinimizer()
Destructor (no operations)
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
std::vector< double > fErrors
Namespace for new ROOT classes and functions.
const ROOT::Math::FitMethodFunction * fChi2Func
virtual double CovMatrix(unsigned int, unsigned int) const
return covariance matrices elements if the variable is fixed the matrix is zero The ordering of the v...
GSLMultiFit, internal class for implementing GSL non linear least square GSL fitting.
virtual int CovMatrixStatus() const
return covariance matrix status
int TestDelta(double absTol, double relTol) const
test using abs and relative tolerance |dx| < absTol + relTol*|x| for every component ...
virtual unsigned int NPoints() const
return the number of data points used in evaluating the function
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=0)
ROOT::Math::GSLMultiFit * fGSLMultiFit
virtual double MinValue() const
return minimum function value
int Set(const std::vector< Func > &funcVec, const double *x)
set the solver parameters
std::vector< double > fCovMatrix
virtual std::string VariableName(unsigned int ivar) const
get name of variables (override if minimizer support storing of variable names)
int PrintLevel() const
minimizer configuration parameters
#define MATH_ERROR_MSGVAL(loc, str, x)
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
const double * CovarMatrix() const
return covariance matrix of the parameters
void SetMinValue(double val)
virtual unsigned int NPar() const
total number of parameter defined
unsigned int MaxIterations() const
max iterations
const double * Gradient() const
gradient value at the minimum
virtual unsigned int NDim() const
number of dimensions
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
#define MATH_ERROR_MSG(loc, str)
double Tolerance() const
absolute tolerance
Documentation for the abstract class IBaseFunctionMultiDim.
GSLNLSMinimizer(int type=0)
Default constructor.
void SetFinalValues(const double *x)
static int DefaultPrintLevel()
virtual bool Minimize()
method to perform the minimization
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
* x
Deprecated and error prone model selection interface.
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
static int DefaultMaxIterations()
LSResidualFunc class description.
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
const double * X() const
parameter values at the minimum
IMultiGenFunctionTempl< double > IMultiGenFunction
std::vector< LSResidualFunc > fResiduals
Namespace for new Math classes and functions.
virtual unsigned int NCalls() const
return the total number of function calls (overrided if needed)
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
void SetPrintLevel(int level)
set print level
static double DefaultTolerance()
virtual double DataElement(const double *x, unsigned int i, double *g=0) const =0
method returning the data i-th contribution to the fit objective function For example the residual fo...
BasicFitMethodFunction< ROOT::Math::IMultiGenFunction > FitMethodFunction
int TestGradient(double absTol) const
test gradient (ask from solver gradient vector)