BFGSFitter Class Reference

#include <BFGSFitter.h>

Inheritance diagram for BFGSFitter:

Inheritance graph
Fitter
[legend]
Collaboration diagram for BFGSFitter:

Collaboration graph
FitterStatedFCNFCNBaseFunctionBase
[legend]

List of all members.


Detailed Description

The minimization algorithm used is a complementary quasi newton algorithm, with BFGS (Broyden-Fletcher-Goldfarb-Shanno) update and hence the name BFGSFitter.

The entire minimization algorithm relies heavily on the description given in Numerical Optimization by Jorge Nocedal, Stephen J. Wright Publisher: Springer Verlag; (August 27, 1999) ISBN: 0387987932). In the following description we will refer to this book a number of times.

Author:
Kaustuv <kaustuv@stanford.edu>

Definition at line 33 of file BFGSFitter.h.


Public Member Functions

 BFGSFitter (const char *name)
 The constructor taking name of fitter as argument.
virtual bool calcBestFit ()
 Main driver routine for BFGS algorithm which has been used in computing the bets fit for the function.
virtual int calcCovariance (std::vector< std::vector< double > > &cov)
 Calculates the covariance matrix.
virtual int calcDegreesOfFreedom () const
 Returns the number of degrees of freedom in the fit.
Fitterclone () const
 Makes a copy of the receiving object.
virtual void copyFrom (const Fitter *other)
 Makes a copy of other.
void fillFreeParameters (std::vector< double > &) const
 Fills the vector with the free parameters values.
double function (const std::vector< double > &x) const
 The objective function.
StatedFCNgetFCN ()
 Returns the objective function object.
virtual const
std::vector< int > & 
getFixedFlags () const
 Sets the limits of the model function parameter values.
bool getUseErrors () const
 Returns true if error data from the DataSource will be used if available.
std::vector< double > gradient (const std::vector< double > &x) const
 The gradient of the objective function.
double gradp (const std::vector< double > &u, const std::vector< double > &p) const
 Efficient computation of gradient of the objective function with a vector p.
const std::vector
< double > & 
initIter () const
 Returns the initial value of the iterate.
double interpolate (const std::vector< double > &x0, const std::vector< double > &p, double Alphaim, double Alphai) const
 A cubic interpolation routine.
bool isCompatible (const FunctionBase *) const
 Returns true if the function is compatible with the objective function.
double iterParam (std::string name)
 Given a string, this function returns the value of the associated iteration parameter.
const std::string & name () const
 Returns the name of the fitter.
bool needsIntegrated () const
 Returns true if the Fitter needs integrated intervals.
virtual double objectiveValue () const
 Calculates the value of the objective function at the current set of parameters.
void setDataSource (const DataSource *source)
 Sets the source of data to be used.
virtual void setFCN (StatedFCN *fcn)
 Sets the objective function object.
virtual void setFitCut (TupleCut *cut)
 Sets the cut to limit range of fitting.
virtual void setFitRange (bool yes=true)
 Sets use of a fitting range on or off.
virtual void setFixedFlags (const std::vector< int > &flags)
 Sets the parameters that are to be held fixed during objective function minimization.
void setFunction (FunctionBase *function)
 Sets the model function.
int setInitIter (const std::vector< double > &xinit)
 Sets the initial value of the iterate, assuming it is given as a vector.
int setIterParam (std::string name, double value)
 Given a string and a double, this function sets the value of the associated iteration parameter.
void setLimits (const std::string &name, double lower, double upper)
 Sets the limits for the parameter of the model function with name name.
virtual void setLimits (unsigned int i, double lower, double upper)
 Sets the limits for the parameter of the model function indexed by i.
void setStepSize (const std::string &name, double size)
 Sets the minimization step size for model function parameter name.
virtual void setStepSize (unsigned int i, double size)
 Sets the step size for parameter of the minimization.
void setUseErrors (bool yes=true)
 Sets the fitter to use error data from the DataSource, if available.
double wolfeStep (const std::vector< double > &x0, const std::vector< double > &p) const
 Computes a step satisfying the Wolfe conditions.
double zoom (const std::vector< double > &x0, const std::vector< double > &p, double phi0, double dphi0, double Alphalo, double Alphahi) const
 A function which helps out Wolfe in deciding the step length.

Protected Attributes

StatedFCNm_fcn
 The objective function.
int m_max_iterations
 The maximum number of iterations allowed in attempting the fit.
std::string m_name
 The name of the fitter.

Private Attributes

double m_alpha1
 First step length to try and this must be less than Alpha_max.
double m_alpha_max
 Maximum step length to try, suggested value by Nocedal and Wright is alpha_max = 4.
double m_c1
 c1,c2 - constants such that 0 < c1 < c2 < 1 and they ensure that strong Wolfe conditions hold true.
double m_c2
double m_grad_cutoff
 The gradient cut-off parameter.
std::map
< std::string,
double * > 
m_iter_params
 Map of the various iteration parameters to their name.
std::vector
< std::vector
< double > > 
m_M
 The inverse of the quasi-Hessian.
double m_proj_cutoff
 The projection cut-off parameter.
double m_step_cutoff
 The step cut-off parameter.
std::vector< double > m_xinit
 The initial value to start the iteration from.

Constructor & Destructor Documentation

BFGSFitter ( const char *  name  ) 

The constructor taking name of fitter as argument.

Definition at line 60 of file BFGSFitter.cxx.

References BFGSFitter::m_alpha1, BFGSFitter::m_alpha_max, BFGSFitter::m_c1, BFGSFitter::m_c2, BFGSFitter::m_grad_cutoff, BFGSFitter::m_iter_params, BFGSFitter::m_proj_cutoff, and BFGSFitter::m_step_cutoff.

Referenced by BFGSFitter::clone().


Member Function Documentation

bool calcBestFit (  )  [virtual]

Main driver routine for BFGS algorithm which has been used in computing the bets fit for the function.

Todo:
Use if ( x!=x ) to test for nan.

Implements Fitter.

Definition at line 89 of file BFGSFitter.cxx.

References hippodraw::Numeric::eye(), Fitter::fillFreeParameters(), BFGSFitter::function(), BFGSFitter::gradient(), hippodraw::Numeric::innerProduct(), Fitter::m_fcn, BFGSFitter::m_grad_cutoff, BFGSFitter::m_M, Fitter::m_max_iterations, BFGSFitter::m_proj_cutoff, BFGSFitter::m_step_cutoff, BFGSFitter::m_xinit, hippodraw::Numeric::norm(), hippodraw::Numeric::outerProduct(), BFGSFitter::setInitIter(), and BFGSFitter::wolfeStep().

int calcCovariance ( std::vector< std::vector< double > > &  cov  )  [virtual]

Calculates the covariance matrix.

Returns EXIT_SUCCESS if a minima of the log likelihood functions is found, returns EXIT_FAILURE in case algorithm converges on other stationary points (i.e. on saddle points).

Reimplemented from Fitter.

Definition at line 472 of file BFGSFitter.cxx.

References hippodraw::Numeric::cholFactor(), BFGSFitter::m_M, and BFGSFitter::m_xinit.

int calcDegreesOfFreedom (  )  const [virtual, inherited]

Returns the number of degrees of freedom in the fit.

Definition at line 226 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by FunctionProjector::degreesOfFreedom(), and hippodraw::Python::export_Fitter().

Fitter * clone (  )  const [virtual]

Makes a copy of the receiving object.

Makes copy of receiving object by creating a new one with the only constructor.

Attention:
Objects created with this function may not be complete until the setFCN member function is called.

Implements Fitter.

Definition at line 80 of file BFGSFitter.cxx.

References BFGSFitter::BFGSFitter().

void copyFrom ( const Fitter other  )  [virtual, inherited]

Makes a copy of other.

Definition at line 47 of file Fitter.cxx.

References Fitter::m_fcn.

void fillFreeParameters ( std::vector< double > &  free_parms  )  const [inherited]

Fills the vector with the free parameters values.

Definition at line 139 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by LMFitter::calcBestFit(), and BFGSFitter::calcBestFit().

double function ( const std::vector< double > &  x  )  const

The objective function.

Definition at line 319 of file BFGSFitter.cxx.

References Fitter::m_fcn, and Fitter::objectiveValue().

Referenced by BFGSFitter::calcBestFit(), BFGSFitter::interpolate(), BFGSFitter::wolfeStep(), and BFGSFitter::zoom().

StatedFCN * getFCN (  )  [inherited]

Returns the objective function object.

Definition at line 70 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by hippodraw::Python::export_Fitter().

const vector< int > & getFixedFlags (  )  const [virtual, inherited]

Sets the limits of the model function parameter values.

Attempts to set the limits of the model function parameter values. This base class implementation throws a FitterException. Derived classes that support setting limits of the parameter values should override this member function. Returns a vector containing flags for which parameters are to be held fixed during objective function minimization.

Definition at line 153 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by MinuitMigrad::initialize().

bool getUseErrors (  )  const [inherited]

Returns true if error data from the DataSource will be used if available.

Definition at line 116 of file Fitter.cxx.

References Fitter::m_fcn.

vector< double > gradient ( const std::vector< double > &  x  )  const

The gradient of the objective function.

Definition at line 351 of file BFGSFitter.cxx.

References Fitter::m_fcn, and Fitter::objectiveValue().

Referenced by BFGSFitter::calcBestFit().

double gradp ( const std::vector< double > &  u,
const std::vector< double > &  p 
) const

Efficient computation of gradient of the objective function with a vector p.

Definition at line 404 of file BFGSFitter.cxx.

References Fitter::m_fcn, and Fitter::objectiveValue().

Referenced by BFGSFitter::interpolate(), BFGSFitter::wolfeStep(), and BFGSFitter::zoom().

const vector< double > & initIter (  )  const

Returns the initial value of the iterate.

Definition at line 453 of file BFGSFitter.cxx.

References BFGSFitter::m_xinit.

double interpolate ( const std::vector< double > &  x0,
const std::vector< double > &  p,
double  Alphaim,
double  Alphai 
) const

A cubic interpolation routine.

Definition at line 287 of file BFGSFitter.cxx.

References BFGSFitter::function(), and BFGSFitter::gradp().

Referenced by BFGSFitter::zoom().

bool isCompatible ( const FunctionBase function  )  const [inherited]

Returns true if the function is compatible with the objective function.

Definition at line 77 of file Fitter.cxx.

References Fitter::m_fcn.

double iterParam ( std::string  name  ) 

Given a string, this function returns the value of the associated iteration parameter.

Names of the various iteration parameters have been chosen according to the following simple rule. m_foo_bar is called foo_bar.

Definition at line 494 of file BFGSFitter.cxx.

References BFGSFitter::m_iter_params, and Fitter::m_max_iterations.

const std::string & name (  )  const [inherited]

Returns the name of the fitter.

Definition at line 54 of file Fitter.cxx.

References Fitter::m_name.

Referenced by hippodraw::Python::export_Fitter(), and MinuitMigrad::initialize().

bool needsIntegrated (  )  const [inherited]

Returns true if the Fitter needs integrated intervals.

Definition at line 128 of file Fitter.cxx.

References Fitter::m_fcn.

double objectiveValue (  )  const [virtual, inherited]

Calculates the value of the objective function at the current set of parameters.

Definition at line 219 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by LMFitter::calcBestFit(), hippodraw::Python::export_Fitter(), BFGSFitter::function(), BFGSFitter::gradient(), BFGSFitter::gradp(), and FunctionProjector::objectiveValue().

void setDataSource ( const DataSource source  )  [inherited]

Sets the source of data to be used.

Definition at line 97 of file Fitter.cxx.

References Fitter::m_fcn, and Fitter::setUseErrors().

void setFCN ( StatedFCN fcn  )  [virtual, inherited]

Sets the objective function object.

Sets the objective function object to be used and takes possession of it. That is, will delete an existing object, if there is one, and will delete the object in this object's destructor.

Attention:
A derived class that has special requirements, such as need for partial derivatives with respect to the parameters should override this method to set those requirements.

Reimplemented in LMFitter.

Definition at line 61 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by LMFitter::setFCN().

void setFitCut ( TupleCut cut  )  [virtual, inherited]

Sets the cut to limit range of fitting.

Definition at line 240 of file Fitter.cxx.

References Fitter::m_fcn.

void setFitRange ( bool  yes = true  )  [virtual, inherited]

Sets use of a fitting range on or off.

Definition at line 247 of file Fitter.cxx.

References Fitter::m_fcn.

void setFixedFlags ( const std::vector< int > &  flags  )  [virtual, inherited]

Sets the parameters that are to be held fixed during objective function minimization.

Definition at line 146 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by hippodraw::Python::export_Fitter().

void setFunction ( FunctionBase function  )  [inherited]

Sets the model function.

Definition at line 88 of file Fitter.cxx.

References Fitter::m_fcn.

int setInitIter ( const std::vector< double > &  xinit  ) 

Sets the initial value of the iterate, assuming it is given as a vector.

Definition at line 458 of file BFGSFitter.cxx.

References BFGSFitter::m_xinit.

Referenced by BFGSFitter::calcBestFit().

int setIterParam ( std::string  name,
double  value 
)

Given a string and a double, this function sets the value of the associated iteration parameter.

Names of the various parameters have been chosen according to the following simple rule. m_foo_bar is called foo_bar.

Definition at line 514 of file BFGSFitter.cxx.

References BFGSFitter::m_iter_params, and Fitter::m_max_iterations.

void setLimits ( const std::string &  name,
double  lower,
double  upper 
) [inherited]

Sets the limits for the parameter of the model function with name name.

Definition at line 194 of file Fitter.cxx.

References Fitter::getParameterIndex(), and Fitter::setLimits().

void setLimits ( unsigned int  i,
double  lower,
double  upper 
) [virtual, inherited]

Sets the limits for the parameter of the model function indexed by i.

This base class implementation throws a std::runtime_error. Derived classes that implement this function should override it.

Reimplemented in MinuitMigrad.

Definition at line 160 of file Fitter.cxx.

References Fitter::m_name.

Referenced by hippodraw::Python::export_Fitter(), and Fitter::setLimits().

void setStepSize ( const std::string &  name,
double  size 
) [inherited]

Sets the minimization step size for model function parameter name.

Definition at line 212 of file Fitter.cxx.

References Fitter::getParameterIndex(), and Fitter::setStepSize().

void setStepSize ( unsigned int  i,
double  size 
) [virtual, inherited]

Sets the step size for parameter of the minimization.

This base class implementation throws FitterException. Derived classes that support setting step size should override this member function.

Reimplemented in MinuitMigrad.

Definition at line 202 of file Fitter.cxx.

References Fitter::m_name.

Referenced by hippodraw::Python::export_Fitter(), and Fitter::setStepSize().

void setUseErrors ( bool  yes = true  )  [inherited]

Sets the fitter to use error data from the DataSource, if available.

Definition at line 107 of file Fitter.cxx.

References Fitter::m_fcn.

Referenced by Fitter::setDataSource().

double wolfeStep ( const std::vector< double > &  x0,
const std::vector< double > &  p 
) const

Computes a step satisfying the Wolfe conditions.

Based on Algorithm 3.2 (Line Search Algorithm) on pp. 58--60 of "Numerical Optimization" by Jorge Nocedal and Stephen J. Wright

Todo:
Take out error message and send signal to application.

Definition at line 171 of file BFGSFitter.cxx.

References BFGSFitter::function(), BFGSFitter::gradp(), BFGSFitter::m_alpha1, BFGSFitter::m_alpha_max, BFGSFitter::m_c1, BFGSFitter::m_c2, and BFGSFitter::zoom().

Referenced by BFGSFitter::calcBestFit().

double zoom ( const std::vector< double > &  x0,
const std::vector< double > &  p,
double  phi0,
double  dphi0,
double  Alphalo,
double  Alphahi 
) const

A function which helps out Wolfe in deciding the step length.

Based on Algorithm 3.3 (zoom) on pg. 60 of "Numerical Optimization" by Jorge Nocedal and Stephen J. Wright

Definition at line 234 of file BFGSFitter.cxx.

References BFGSFitter::function(), BFGSFitter::gradp(), BFGSFitter::interpolate(), BFGSFitter::m_c1, and BFGSFitter::m_c2.

Referenced by BFGSFitter::wolfeStep().


Member Data Documentation

double m_alpha1 [private]

First step length to try and this must be less than Alpha_max.

(typically Alpha1 = 1 for Newton's method and relatives)

Definition at line 85 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), and BFGSFitter::wolfeStep().

double m_alpha_max [private]

Maximum step length to try, suggested value by Nocedal and Wright is alpha_max = 4.

Definition at line 81 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), and BFGSFitter::wolfeStep().

double m_c1 [private]

c1,c2 - constants such that 0 < c1 < c2 < 1 and they ensure that strong Wolfe conditions hold true.

Strong Wolfe conditions:

f(x_k + Alpha_k * p_k) <= f(x_k) + c1 * Alpha_k * grad f(x_k)'*p_k (1) |grad f(x_k + Alpha_k * p_k)'*p_k| >= c2 * grad f(x_k)' * p_k (2)

The lower the c2 value, the closer you are asking the algorithm to get to an actual local minimum. The lower the c1 value, the sole demand being made is that the direction be a direction of descent.

Nocedal and Wright suggest use of following: c1 = 1e-4; c2 = 0.9;

Definition at line 77 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), BFGSFitter::wolfeStep(), and BFGSFitter::zoom().

double m_c2 [private]

Definition at line 77 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), BFGSFitter::wolfeStep(), and BFGSFitter::zoom().

StatedFCN* m_fcn [protected, inherited]

The objective function.

Definition at line 59 of file Fitter.h.

Referenced by LMFitter::calcAlpha(), MinuitMigrad::calcBestFit(), LMFitter::calcBestFit(), BFGSFitter::calcBestFit(), Fitter::calcDegreesOfFreedom(), MinuitMigrad::checkIndex(), Fitter::copyFrom(), Fitter::fillFreeParameters(), Fitter::Fitter(), BFGSFitter::function(), Fitter::getFCN(), Fitter::getFixedFlags(), Fitter::getParameterIndex(), Fitter::getUseErrors(), BFGSFitter::gradient(), BFGSFitter::gradp(), MinuitMigrad::initialize(), MinuitMigrad::initLimits(), Fitter::isCompatible(), Fitter::needsIntegrated(), Fitter::objectiveValue(), Fitter::setDataSource(), Fitter::setFCN(), Fitter::setFitCut(), Fitter::setFitRange(), Fitter::setFixedFlags(), Fitter::setFunction(), Fitter::setUseErrors(), and Fitter::~Fitter().

double m_grad_cutoff [private]

The gradient cut-off parameter.

If two successive iterations change the the norm of the gradient of Likelihood function less than this quantity, then iteration is terminated

Definition at line 50 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), and BFGSFitter::calcBestFit().

std::map< std::string, double * > m_iter_params [private]

Map of the various iteration parameters to their name.

Definition at line 88 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), BFGSFitter::iterParam(), and BFGSFitter::setIterParam().

std::vector< std::vector< double > > m_M [private]

The inverse of the quasi-Hessian.

Definition at line 42 of file BFGSFitter.h.

Referenced by BFGSFitter::calcBestFit(), and BFGSFitter::calcCovariance().

int m_max_iterations [protected, inherited]

The maximum number of iterations allowed in attempting the fit.

Definition at line 62 of file Fitter.h.

Referenced by LMFitter::calcBestFit(), BFGSFitter::calcBestFit(), BFGSFitter::iterParam(), and BFGSFitter::setIterParam().

std::string m_name [protected, inherited]

The name of the fitter.

Definition at line 51 of file Fitter.h.

Referenced by MinuitMigrad::checkIndex(), Fitter::name(), Fitter::setLimits(), and Fitter::setStepSize().

double m_proj_cutoff [private]

The projection cut-off parameter.

If the projection of step on the gradient is less than this quantity, then iteration is terminated

Definition at line 59 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), and BFGSFitter::calcBestFit().

double m_step_cutoff [private]

The step cut-off parameter.

If the two successive iterations result in the norm of difference of iterates less than this quantity, then iteration is terminated

Definition at line 55 of file BFGSFitter.h.

Referenced by BFGSFitter::BFGSFitter(), and BFGSFitter::calcBestFit().

std::vector< double > m_xinit [private]

The initial value to start the iteration from.

Definition at line 45 of file BFGSFitter.h.

Referenced by BFGSFitter::calcBestFit(), BFGSFitter::calcCovariance(), BFGSFitter::initIter(), and BFGSFitter::setInitIter().


The documentation for this class was generated from the following files:
Generated for HippoDraw Class Library by doxygen