Jpp
Classes | Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
JFIT::JGandalf< JModel_t > Class Template Reference

Fit method based on the Levenberg-Marquardt method. More...

#include <JGandalf.hh>

Inheritance diagram for JFIT::JGandalf< JModel_t >:
JEEP::JMessage< JGandalf< JModel_t > >

Classes

struct  result_type
 Data structure for return value of fit function. More...
 

Public Types

typedef JModel_t::parameter_type parameter_type
 Data type of fit parameter. More...
 

Public Member Functions

 JGandalf ()
 Default constructor. More...
 
template<class JFunction_t , class T1 , class T2 >
result_type operator() (const JFunction_t &fit, T1 __begin1, T1 __end1, T2 __begin2, T2 __end2)
 Multi-dimensional fit of two data sets. More...
 
template<class JFunction_t , class T >
result_type operator() (const JFunction_t &fit, T __begin, T __end)
 Multi-dimensional fit of one data set. More...
 

Public Attributes

double lambda
 
JModel_t value
 
JModel_t error
 
std::vector< parameter_typeparameters
 
int numberOfIterations
 
JMATH::JMatrixNS H
 

Static Public Attributes

static int MAXIMUM_ITERATIONS = 1000
 maximal number of iterations More...
 
static double EPSILON = 1.0e-3
 maximal distance to minimum More...
 
static double LAMBDA_MIN = 0.01
 minimal value control parameter More...
 
static double LAMBDA_MAX = 100.0
 maximal value control parameter More...
 
static double LAMBDA_UP = 9.0
 multiplication factor control parameter More...
 
static double LAMBDA_DOWN = 11.0
 multiplication factor control parameter More...
 
static double PIVOT = 1.0e-3
 minimal value diagonal element of matrix More...
 
static int debug
 debug level (default is off). More...
 

Private Member Functions

void reset ()
 Reset. More...
 
template<class JFunction_t , class T >
void evaluate (const JFunction_t &fit, T __begin, T __end)
 Evaluate fit for given data set. More...
 

Private Attributes

result_type successor
 
JModel_t previous
 
std::vector< double > h
 

Detailed Description

template<class JModel_t>
class JFIT::JGandalf< JModel_t >

Fit method based on the Levenberg-Marquardt method.

The template argument refers to the model that should be fitted to the data. This data structure should have arithmetic capabalities.

The data member JGandalf::value corresponds to the start or final value of the model of the fit procedure and JGandalf::error to the uncertainties. The co-variance matrix is stored in data member JGandalf::H. The data member JGandalf::parameters is a list of pointers to those data members of the model that should be fitted. The template fit function should return the data type JGandalf::result_type which is composed of the values of the chi2 and gradient of a data point, respectively. The function operator returns the chi2 of the fit.

Definition at line 45 of file JGandalf.hh.

Member Typedef Documentation

◆ parameter_type

template<class JModel_t>
typedef JModel_t::parameter_type JFIT::JGandalf< JModel_t >::parameter_type

Data type of fit parameter.

Definition at line 56 of file JGandalf.hh.

Constructor & Destructor Documentation

◆ JGandalf()

template<class JModel_t>
JFIT::JGandalf< JModel_t >::JGandalf ( )
inline

Default constructor.

Definition at line 104 of file JGandalf.hh.

105  {}

Member Function Documentation

◆ operator()() [1/2]

template<class JModel_t>
template<class JFunction_t , class T1 , class T2 >
result_type JFIT::JGandalf< JModel_t >::operator() ( const JFunction_t &  fit,
T1  __begin1,
T1  __end1,
T2  __begin2,
T2  __end2 
)
inline

Multi-dimensional fit of two data sets.

The fit function should return the equivalent of chi2 for the current value of the model and the given data point as well as the partial derivatives.

Parameters
fitfit function
__begin1begin of first data set
__end1end of first data set
__begin2begin of second data set
__end2end of second data set
Returns
chi2

Definition at line 122 of file JGandalf.hh.

125  {
126  using namespace std;
127 
128  const int N = parameters.size();
129 
130  H.resize(N);
131  h.resize(N);
132 
133  previous = value;
134  error = JModel_t();
135  lambda = LAMBDA_MIN;
136 
137  result_type precursor = result_type(numeric_limits<double>::max(), JModel_t());
138 
140 
141  DEBUG("step: " << numberOfIterations << endl);
142 
143  reset();
144 
145  evaluate(fit, __begin1, __end1);
146  evaluate(fit, __begin2, __end2);
147 
148  DEBUG("lambda: " << SCIENTIFIC(12,5) << lambda << endl);
149  DEBUG("chi2: " << FIXED (12,5) << successor.chi2 << endl);
150 
151  if (successor.chi2 < precursor.chi2) {
152 
153  if (numberOfIterations != 0) {
154 
155  if (fabs(precursor.chi2 - successor.chi2) < EPSILON*fabs(precursor.chi2)) {
156  return successor;
157  }
158 
159  if (lambda > LAMBDA_MIN) {
160  lambda /= LAMBDA_DOWN;
161  }
162  }
163 
164  precursor = successor;
165  previous = value;
166 
167  } else {
168 
169  value = previous;
170  lambda *= LAMBDA_UP;
171 
172  if (lambda > LAMBDA_MAX) {
173  return precursor; // no improvement found
174  }
175 
176  reset();
177 
178  evaluate(fit, __begin1, __end1);
179  evaluate(fit, __begin2, __end2);
180  }
181 
182  DEBUG("Hesse matrix:" << endl);
183  DEBUG(H << endl);
184 
185 
186  // force definite positiveness
187 
188  for (int i = 0; i != N; ++i) {
189 
190  if (H(i,i) < PIVOT) {
191  H(i,i) = PIVOT;
192  }
193 
194  h[i] = 1.0 / sqrt(H(i,i));
195  }
196 
197 
198  // normalisation
199 
200  for (int i = 0; i != N; ++i) {
201  for (int j = 0; j != i; ++j) {
202  H(j,i) *= h[i] * h[j];
203  H(i,j) = H(j,i);
204  }
205  }
206 
207  for (int i = 0; i != N; ++i) {
208  H(i,i) = 1.0 + lambda;
209  }
210 
211 
212  try {
213  H.invert();
214  }
215  catch (const JException& error) {
216  ERROR("JGandalf: " << error.what() << endl);
217  return precursor;
218  }
219 
220 
221  for (int i = 0; i != N; ++i) {
222  DEBUG("u[" << noshowpos << i << "] = " << showpos << FIXED(15,5) << value.*parameters[i]);
223 
224  for (int j = 0; j != N; ++j) {
225  value.*parameters[i] -= H(i,j) * successor.gradient.*parameters[j] * h[i] * h[j];
226  }
227 
228  DEBUG(' ' << FIXED(15,5) << value.*parameters[i] << noshowpos << endl);
229 
230  error.*parameters[i] = h[i];
231  }
232 
233  }
234 
235  return precursor;
236  }

◆ operator()() [2/2]

template<class JModel_t>
template<class JFunction_t , class T >
result_type JFIT::JGandalf< JModel_t >::operator() ( const JFunction_t &  fit,
__begin,
__end 
)
inline

Multi-dimensional fit of one data set.

The fit function should return the equivalent of chi2 for the current value of the model and the given data point as well as the partial derivatives.

Parameters
fitfit function
__beginbegin of data
__endend of data
Returns
chi2

Definition at line 251 of file JGandalf.hh.

252  {
253  return (*this)(fit, __begin, __end, __end, __end);
254  }

◆ reset()

template<class JModel_t>
void JFIT::JGandalf< JModel_t >::reset ( )
inlineprivate

Reset.

Definition at line 276 of file JGandalf.hh.

277  {
278  successor = result_type();
279 
280  H.reset();
281  }

◆ evaluate()

template<class JModel_t>
template<class JFunction_t , class T >
void JFIT::JGandalf< JModel_t >::evaluate ( const JFunction_t &  fit,
__begin,
__end 
)
inlineprivate

Evaluate fit for given data set.

Parameters
fitfit function
__beginbegin of data
__endend of data

Definition at line 292 of file JGandalf.hh.

293  {
294  for (T hit = __begin; hit != __end; ++hit) {
295 
296  const result_type& result = fit(value, *hit);
297 
298  successor.chi2 += result.chi2;
299  successor.gradient += result.gradient;
300 
301  for (unsigned int i = 0; i != parameters.size(); ++i) {
302  for (unsigned int j = i; j != parameters.size(); ++j) {
303  H(i,j) += result.gradient.*parameters[i] * result.gradient.*parameters[j];
304  }
305  }
306  }
307  }

Member Data Documentation

◆ MAXIMUM_ITERATIONS

template<class JModel_t>
int JFIT::JGandalf< JModel_t >::MAXIMUM_ITERATIONS = 1000
static

maximal number of iterations

maximal number of iterations.

Definition at line 257 of file JGandalf.hh.

◆ EPSILON

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::EPSILON = 1.0e-3
static

maximal distance to minimum

maximal distance to minimum.

Definition at line 258 of file JGandalf.hh.

◆ LAMBDA_MIN

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::LAMBDA_MIN = 0.01
static

minimal value control parameter

Definition at line 259 of file JGandalf.hh.

◆ LAMBDA_MAX

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::LAMBDA_MAX = 100.0
static

maximal value control parameter

Definition at line 260 of file JGandalf.hh.

◆ LAMBDA_UP

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::LAMBDA_UP = 9.0
static

multiplication factor control parameter

Definition at line 261 of file JGandalf.hh.

◆ LAMBDA_DOWN

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::LAMBDA_DOWN = 11.0
static

multiplication factor control parameter

Definition at line 262 of file JGandalf.hh.

◆ PIVOT

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::PIVOT = 1.0e-3
static

minimal value diagonal element of matrix

Definition at line 263 of file JGandalf.hh.

◆ lambda

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::lambda

Definition at line 265 of file JGandalf.hh.

◆ value

template<class JModel_t>
JModel_t JFIT::JGandalf< JModel_t >::value

Definition at line 266 of file JGandalf.hh.

◆ error

template<class JModel_t>
JModel_t JFIT::JGandalf< JModel_t >::error

Definition at line 267 of file JGandalf.hh.

◆ parameters

template<class JModel_t>
std::vector<parameter_type> JFIT::JGandalf< JModel_t >::parameters

Definition at line 268 of file JGandalf.hh.

◆ numberOfIterations

template<class JModel_t>
int JFIT::JGandalf< JModel_t >::numberOfIterations

Definition at line 269 of file JGandalf.hh.

◆ H

template<class JModel_t>
JMATH::JMatrixNS JFIT::JGandalf< JModel_t >::H

Definition at line 270 of file JGandalf.hh.

◆ successor

template<class JModel_t>
result_type JFIT::JGandalf< JModel_t >::successor
private

Definition at line 309 of file JGandalf.hh.

◆ previous

template<class JModel_t>
JModel_t JFIT::JGandalf< JModel_t >::previous
private

Definition at line 310 of file JGandalf.hh.

◆ h

template<class JModel_t>
std::vector<double> JFIT::JGandalf< JModel_t >::h
private

Definition at line 311 of file JGandalf.hh.

◆ debug

int JEEP::JMessage< JGandalf< JModel_t > >::debug
staticinherited

debug level (default is off).

Definition at line 45 of file JMessage.hh.


The documentation for this class was generated from the following file:
JFIT::JGandalf::LAMBDA_MAX
static double LAMBDA_MAX
maximal value control parameter
Definition: JGandalf.hh:260
FIXED
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:481
JFIT::JGandalf::numberOfIterations
int numberOfIterations
Definition: JGandalf.hh:269
JFIT::JGandalf::LAMBDA_DOWN
static double LAMBDA_DOWN
multiplication factor control parameter
Definition: JGandalf.hh:262
JFIT::JGandalf::reset
void reset()
Reset.
Definition: JGandalf.hh:276
JFIT::JGandalf::evaluate
void evaluate(const JFunction_t &fit, T __begin, T __end)
Evaluate fit for given data set.
Definition: JGandalf.hh:292
JFIT::JGandalf::MAXIMUM_ITERATIONS
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JGandalf.hh:257
JFIT::ERROR
Definition: JFitStatus.hh:18
JFIT::JGandalf::EPSILON
static double EPSILON
maximal distance to minimum
Definition: JGandalf.hh:258
JFIT::JGandalf::h
std::vector< double > h
Definition: JGandalf.hh:311
JTOOLS::j
int j
Definition: JPolint.hh:634
JFIT::JGandalf::error
JModel_t error
Definition: JGandalf.hh:267
JFIT::JGandalf::lambda
double lambda
Definition: JGandalf.hh:265
JFIT::JGandalf::PIVOT
static double PIVOT
minimal value diagonal element of matrix
Definition: JGandalf.hh:263
JFIT::JGandalf::LAMBDA_MIN
static double LAMBDA_MIN
minimal value control parameter
Definition: JGandalf.hh:259
JTOOLS::result
return result
Definition: JPolint.hh:695
JFIT::JGandalf::parameters
std::vector< parameter_type > parameters
Definition: JGandalf.hh:268
SCIENTIFIC
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:518
JFIT::JGandalf::value
JModel_t value
Definition: JGandalf.hh:266
JMATH::JMatrixND::reset
JMatrixND & reset()
Set matrix to the null matrix.
Definition: JMatrixND.hh:372
JFIT::JGandalf::result_type::gradient
JModel_t gradient
d(chi2)/d(...)
Definition: JGandalf.hh:97
JMATH::JMatrixNS::invert
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:80
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
JFIT::JGandalf::result_type::chi2
double chi2
chi2
Definition: JGandalf.hh:96
JFIT::JGandalf::previous
JModel_t previous
Definition: JGandalf.hh:310
JFIT::JGandalf::LAMBDA_UP
static double LAMBDA_UP
multiplication factor control parameter
Definition: JGandalf.hh:261
JFIT::JGandalf::successor
result_type successor
Definition: JGandalf.hh:309
JMATH::JMatrixND::resize
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:359
JFIT::JGandalf::H
JMATH::JMatrixNS H
Definition: JGandalf.hh:270