Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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< 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 >
double 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 >
double 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-4
 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 = 0
 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

double chi2
 
JModel_t gradient
 
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

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

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

Default constructor.

Definition at line 104 of file JGandalf.hh.

105  {}

Member Function Documentation

template<class JModel_t>
template<class JFunction_t , class T1 , class T2 >
double 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  double chi2_old = numeric_limits<double>::max();
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) << chi2 << endl);
150 
151  if (chi2 < chi2_old) {
152 
153  if (numberOfIterations != 0) {
154 
155  if (fabs(chi2_old - chi2) < EPSILON*fabs(chi2_old)) {
156  return chi2;
157  }
158 
159  lambda /= LAMBDA_DOWN;
160  }
161 
162  chi2_old = chi2;
163  previous = value;
164 
165  } else {
166 
167  value = previous;
168  lambda *= LAMBDA_UP;
169 
170  if (lambda > LAMBDA_MAX) {
171  return chi2_old; // no improvement found
172  }
173 
174  reset();
175 
176  evaluate(fit, __begin1, __end1);
177  evaluate(fit, __begin2, __end2);
178  }
179 
180  DEBUG("Hesse matrix:" << endl);
181  DEBUG(H << endl);
182 
183 
184  // force definite positiveness
185 
186  for (int i = 0; i != N; ++i) {
187 
188  if (H(i,i) < PIVOT) {
189  H(i,i) = PIVOT;
190  }
191 
192  h[i] = 1.0 / sqrt(H(i,i));
193  }
194 
195 
196  // normalisation
197 
198  for (int i = 0; i != N; ++i) {
199  for (int j = 0; j != i; ++j) {
200  H(j,i) *= h[i] * h[j];
201  H(i,j) = H(j,i);
202  }
203  }
204 
205  for (int i = 0; i != N; ++i) {
206  H(i,i) = 1.0 + lambda;
207  }
208 
209 
210  try {
211  H.invert();
212  }
213  catch (const JException& error) {
214  ERROR("JGandalf: " << error.what() << endl);
215  return chi2_old;
216  }
217 
218 
219  for (int i = 0; i != N; ++i) {
220 
221  DEBUG("u[" << noshowpos << i << "] = " << showpos << FIXED(15,5) << value.*parameters[i]);
222 
223  for (int j = 0; j != N; ++j) {
224  value.*parameters[i] -= H(i,j) * gradient.*parameters[j] * h[i] * h[j];
225  }
226 
227  DEBUG(' ' << FIXED(15,5) << value.*parameters[i] << noshowpos << endl);
228 
229  error.*parameters[i] = h[i];
230  }
231  }
232 
233  return chi2_old;
234  }
double lambda
Definition: JGandalf.hh:264
static double EPSILON
maximal distance to minimum
Definition: JGandalf.hh:256
JModel_t value
Definition: JGandalf.hh:265
static double PIVOT
minimal value diagonal element of matrix
Definition: JGandalf.hh:261
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:461
std::vector< parameter_type > parameters
Definition: JGandalf.hh:267
static double LAMBDA_MIN
minimal value control parameter
Definition: JGandalf.hh:257
static double LAMBDA_DOWN
multiplication factor control parameter
Definition: JGandalf.hh:260
JModel_t previous
Definition: JGandalf.hh:311
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:98
void reset()
Reset.
Definition: JGandalf.hh:275
void evaluate(const JFunction_t &fit, T __begin, T __end)
Evaluate fit for given data set.
Definition: JGandalf.hh:292
JMATH::JMatrixNS H
Definition: JGandalf.hh:269
static double LAMBDA_UP
multiplication factor control parameter
Definition: JGandalf.hh:259
JModel_t gradient
Definition: JGandalf.hh:310
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JGandalf.hh:255
int numberOfIterations
Definition: JGandalf.hh:268
static double LAMBDA_MAX
maximal value control parameter
Definition: JGandalf.hh:258
JModel_t error
Definition: JGandalf.hh:266
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:498
void invert()
Invert matrix.
Definition: JMatrixNS.hh:61
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
std::vector< double > h
Definition: JGandalf.hh:312
template<class JModel_t>
template<class JFunction_t , class T >
double 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 249 of file JGandalf.hh.

250  {
251  return (*this)(fit, __begin, __end, __end, __end);
252  }
template<class JModel_t>
void JFIT::JGandalf< JModel_t >::reset ( )
inlineprivate

Reset.

Definition at line 275 of file JGandalf.hh.

276  {
277  chi2 = 0.0;
278  gradient = JModel_t();
279 
280  H.reset();
281  }
JMatrixND & reset()
Set matrix to the null matrix.
Definition: JMatrixND.hh:170
JMATH::JMatrixNS H
Definition: JGandalf.hh:269
JModel_t gradient
Definition: JGandalf.hh:310
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  chi2 += result.chi2;
299  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  }
JModel_t value
Definition: JGandalf.hh:265
std::vector< parameter_type > parameters
Definition: JGandalf.hh:267
JMATH::JMatrixNS H
Definition: JGandalf.hh:269
JModel_t gradient
Definition: JGandalf.hh:310

Member Data Documentation

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

maximal number of iterations

maximal number of iterations.

Definition at line 255 of file JGandalf.hh.

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

maximal distance to minimum

maximal distance to minimum.

Definition at line 256 of file JGandalf.hh.

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

minimal value control parameter

Definition at line 257 of file JGandalf.hh.

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

maximal value control parameter

Definition at line 258 of file JGandalf.hh.

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

multiplication factor control parameter

Definition at line 259 of file JGandalf.hh.

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

multiplication factor control parameter

Definition at line 260 of file JGandalf.hh.

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

minimal value diagonal element of matrix

Definition at line 261 of file JGandalf.hh.

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

Definition at line 264 of file JGandalf.hh.

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

Definition at line 265 of file JGandalf.hh.

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

Definition at line 266 of file JGandalf.hh.

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

Definition at line 267 of file JGandalf.hh.

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

Definition at line 268 of file JGandalf.hh.

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

Definition at line 269 of file JGandalf.hh.

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::chi2
private

Definition at line 309 of file JGandalf.hh.

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

Definition at line 310 of file JGandalf.hh.

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

Definition at line 311 of file JGandalf.hh.

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

Definition at line 312 of file JGandalf.hh.

template<class T>
int JEEP::JMessage< T >::debug = 0
staticinherited

debug level (default is off).

Definition at line 43 of file JMessage.hh.


The documentation for this class was generated from the following file: