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) {
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;
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;
#define MATH_ERROR_MSGVAL(loc, txt, x)
#define MATH_ERROR_MSG(loc, str)
Binding & operator=(OUT(*fun)(void))
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
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...
virtual unsigned int NPoints() const
return the number of data points used in evaluating the function
virtual unsigned int NCalls() const
return the total number of function calls (overrided if needed)
virtual double MinValue() const
return minimum function value
virtual unsigned int NPar() const
total number of parameter defined
void SetMinValue(double val)
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
virtual unsigned int NDim() const
number of dimensions
virtual std::string VariableName(unsigned int ivar) const
get name of variables (override if minimizer support storing of variable names)
virtual const double * X() const
return pointer to X values at the minimum
void SetFinalValues(const double *x)
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=0)
GSLMultiFit, internal class for implementing GSL non linear least square GSL fitting.
int TestGradient(double absTol) const
test gradient (ask from solver gradient vector)
int TestDelta(double absTol, double relTol) const
test using abs and relative tolerance |dx| < absTol + relTol*|x| for every component
const double * Gradient() const
gradient value at the minimum
const double * CovarMatrix() const
return covariance matrix of the parameters
const double * X() const
parameter values at the minimum
int Set(const std::vector< Func > &funcVec, const double *x)
set the solver parameters
const ROOT::Math::FitMethodFunction * fChi2Func
virtual bool Minimize()
method to perform the minimization
std::vector< double > fErrors
std::vector< LSResidualFunc > fResiduals
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...
std::vector< double > fCovMatrix
virtual int CovMatrixStatus() const
return covariance matrix status
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
GSLNLSMinimizer(int type=0)
Default constructor.
ROOT::Math::GSLMultiFit * fGSLMultiFit
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
~GSLNLSMinimizer()
Destructor (no operations)
Documentation for the abstract class IBaseFunctionMultiDim.
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
LSResidualFunc class description.
static int DefaultPrintLevel()
static double DefaultTolerance()
static int DefaultMaxIterations()
double Tolerance() const
absolute tolerance
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
unsigned int MaxIterations() const
max iterations
void SetPrintLevel(int level)
set print level
int PrintLevel() const
minimizer configuration parameters
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
Namespace for new Math classes and functions.
BasicFitMethodFunction< ROOT::Math::IMultiGenFunction > FitMethodFunction
IMultiGenFunctionTempl< double > IMultiGenFunction
Namespace for new ROOT classes and functions.