Jpp  18.5.0
the software that should make you happy
 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 | Static 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 T , class... Args>
result_type operator() (const JFunction_t &fit, T __begin, T __end, Args...args)
 Multi-dimensional fit of multiple data sets. More...
 

Public Attributes

std::vector< parameter_typeparameters
 fit parameters More...
 
int numberOfIterations
 number of iterations More...
 
double lambda
 control parameter More...
 
JModel_t value
 value More...
 
JModel_t error
 error More...
 
JMATH::JMatrixNS V
 Hesse matrix. More...
 
result_type result
 

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 = 10.0
 multiplication factor control parameter More...
 
static double LAMBDA_DOWN = 10.0
 multiplication factor control parameter More...
 
static double PIVOT = std::numeric_limits<double>::epsilon()
 minimal value diagonal element of Hesse matrix More...
 
static int debug = 0
 debug level (default is off). More...
 

Private Member Functions

void reset ()
 Reset current parameters. More...
 
template<class JFunction_t , class T , class... Args>
void update (const JFunction_t &fit, T __begin, T __end, Args...args)
 Recursive method to update current parameters. More...
 
template<class JFunction_t >
void update (const JFunction_t &fit)
 Termination method to update current parameters. More...
 

Static Private Member Functions

static double get (const JModel_t &model, double JModel_t::*parameter)
 Read/write access to parameter value by data member. More...
 
static double & get (JModel_t &model, double JModel_t::*parameter)
 Read/write access to parameter value by data member. More...
 
static double get (const JModel_t &model, const size_t index)
 Read/write access to parameter value by index. More...
 
static double & get (JModel_t &model, const size_t index)
 Read/write access to parameter value by index. More...
 
static double get (const JModel_t &model, const int index)
 Read/write access to parameter value by index. More...
 
static double & get (JModel_t &model, const int index)
 Read/write access to parameter value by index. More...
 

Private Attributes

std::vector< double > h
 
JMATH::JVectorND x
 
struct {
   result_type   result
 
current
 
struct {
   JModel_t   value
 
   JModel_t   error
 
   result_type   result
 
previous
 

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 capabilities.

The data member JGandalf::value corresponds to the start c.q. 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::V.
The data member JGandalf::parameters constitutes a list of those parameters of the model that should actually be fitted.
For this, the model should contain the type definition for parameter_type.
Normally, this type definition corresponds to a pointer to a data member of the model.
Alternatively, the type definition can be size_t or int.
In that case, the model class should provide for the element access operator[].
The first template parameter in the function operator should provide for an implementation of the actual fit function.
This function should return the data type JGandalf::result_type.
This data type comprises the values of the chi2 and the gradient for a given data point.
The function operator returns the minimal chi2 and combined gradient of the data points.

Definition at line 53 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 64 of file JGandalf.hh.

Constructor & Destructor Documentation

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

Default constructor.

Definition at line 112 of file JGandalf.hh.

113  {}

Member Function Documentation

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

Multi-dimensional fit of multiple data sets.

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

Parameters
fitfit function
__beginbegin of data
__endend of data
argsoptional data
Returns
chi2 and gradient

Definition at line 129 of file JGandalf.hh.

130  {
131  using namespace std;
132  using namespace JPP;
133 
134  // note that all model values should be assigned to the start value of the model before use
135  // because the actual list of model parameters can vary from fit to fit
136  // (e.g. if model consists of a container).
137 
138  const size_t N = parameters.size();
139 
140  V.resize(N);
141  h.resize(N);
142  x.resize(N);
143 
144  previous.result.chi2 = numeric_limits<double>::max();
145 
146  current.result.chi2 = numeric_limits<double>::max();
147  current.result.gradient = value;
148  current.result.gradient = zero;
149 
150  error = value;
151  error = zero;
152 
153  lambda = LAMBDA_MIN;
154 
155 
157 
158  DEBUG("step: " << numberOfIterations << endl);
159 
160  reset();
161 
162  update(fit, __begin, __end, args...);
163 
164  DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
165  DEBUG("chi2: " << FIXED(12,5) << current.result.chi2 << endl);
166 
167  if (current.result.chi2 < previous.result.chi2) {
168 
169  if (numberOfIterations != 0) {
170 
171  if (fabs(previous.result.chi2 - current.result.chi2) < EPSILON*fabs(previous.result.chi2)) {
172 
173  const result_type result = current.result;
174 
175  reset();
176 
177  update(fit, __begin, __end, args...);
178 
179  try {
180  V.invert();
181  }
182  catch (const exception& error) {
183  V.reset();
184  }
185 
186  return result; // normal end
187  }
188 
189  if (lambda > LAMBDA_MIN) {
190  lambda /= LAMBDA_DOWN;
191  }
192 
193  // store current values
194 
195  previous.value = value;
196  previous.error = error;
197  previous.result = current.result;
198  }
199 
200  } else {
201 
202  value = previous.value; // restore value
203 
204  lambda *= LAMBDA_UP;
205 
206  if (lambda > LAMBDA_MAX) {
207  break;
208  }
209 
210  reset();
211 
212  update(fit, __begin, __end, args...);
213  }
214 
215  DEBUG("Hesse matrix:" << endl << V << endl);
216 
217  // force definite positiveness
218 
219  for (size_t i = 0; i != N; ++i) {
220 
221  if (V(i,i) < PIVOT) {
222  V(i,i) = PIVOT;
223  }
224 
225  h[i] = 1.0 / sqrt(V(i,i));
226  }
227 
228  // normalisation
229 
230  for (size_t row = 0; row != N; ++row) {
231  for (size_t col = 0; col != row; ++col) {
232  V(row,col) *= h[row] * h[col];
233  V(col,row) = V(row,col);
234  }
235  }
236 
237  for (size_t i = 0; i != N; ++i) {
238  V(i,i) = 1.0 + lambda;
239  }
240 
241  // solve A x = b
242 
243  for (size_t col = 0; col != N; ++col) {
244  x[col] = h[col] * get(current.result.gradient, parameters[col]);
245  }
246 
247  try {
248  V.solve(x);
249  }
250  catch (const exception& error) {
251 
252  ERROR("JGandalf: " << error.what() << endl << V << endl);
253 
254  break;
255  }
256 
257  // update value and error
258 
259  for (size_t row = 0; row != N; ++row) {
260 
261  DEBUG("u[" << noshowpos << setw(3) << row << "] = " << showpos << FIXED(15,5) << get(value, parameters[row]));
262 
263  get(value, parameters[row]) -= h[row] * x[row];
264  get(error, parameters[row]) = h[row];
265 
266  DEBUG(" -> " << FIXED(15,5) << get(value, parameters[row]) << noshowpos << endl);
267  }
268  }
269 
270  // abnormal end
271 
272  const result_type result = previous.result;
273 
274  value = previous.value; // restore value
275  error = previous.error; // restore error
276 
277  reset();
278 
279  update(fit, __begin, __end, args...);
280 
281  try {
282  V.invert();
283  }
284  catch (const exception& error) {
285  V.reset();
286  }
287 
288  return result;
289  }
double lambda
control parameter
Definition: JGandalf.hh:302
static double EPSILON
maximal distance to minimum
Definition: JGandalf.hh:293
JMATH::JVectorND x
Definition: JGandalf.hh:445
JModel_t value
value
Definition: JGandalf.hh:303
static double PIVOT
minimal value diagonal element of Hesse matrix
Definition: JGandalf.hh:298
JMATH::JMatrixNS V
Hesse matrix.
Definition: JGandalf.hh:305
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
JMatrixND & reset()
Set matrix to the null matrix.
Definition: JMatrixND.hh:456
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
std::vector< parameter_type > parameters
fit parameters
Definition: JGandalf.hh:300
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:443
static double LAMBDA_MIN
minimal value control parameter
Definition: JGandalf.hh:294
static double LAMBDA_DOWN
multiplication factor control parameter
Definition: JGandalf.hh:297
void reset()
Reset current parameters.
Definition: JGandalf.hh:311
#define ERROR(A)
Definition: JMessage.hh:66
void update(const JFunction_t &fit, T __begin, T __end, Args...args)
Recursive method to update current parameters.
Definition: JGandalf.hh:331
struct JFIT::JGandalf::@10 current
result_type result
Definition: JGandalf.hh:448
static double LAMBDA_UP
multiplication factor control parameter
Definition: JGandalf.hh:296
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:75
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
void solve(JVectorND_t &u)
Get solution of equation A x = b.
Definition: JMatrixNS.hh:308
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JGandalf.hh:292
int numberOfIterations
number of iterations
Definition: JGandalf.hh:301
static double LAMBDA_MAX
maximal value control parameter
Definition: JGandalf.hh:295
JModel_t error
error
Definition: JGandalf.hh:304
struct JFIT::JGandalf::@11 previous
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std::vector< double > h
Definition: JGandalf.hh:444
template<class JModel_t>
void JFIT::JGandalf< JModel_t >::reset ( )
inlineprivate

Reset current parameters.

Definition at line 311 of file JGandalf.hh.

312  {
313  using namespace JPP;
314 
315  current.result.chi2 = 0.0;
316  current.result.gradient = zero;
317 
318  V.reset();
319  }
JMATH::JMatrixNS V
Hesse matrix.
Definition: JGandalf.hh:305
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
JMatrixND & reset()
Set matrix to the null matrix.
Definition: JMatrixND.hh:456
struct JFIT::JGandalf::@10 current
template<class JModel_t>
template<class JFunction_t , class T , class... Args>
void JFIT::JGandalf< JModel_t >::update ( const JFunction_t &  fit,
T  __begin,
T  __end,
Args...  args 
)
inlineprivate

Recursive method to update current parameters.

Parameters
fitfit function
__beginbegin of data
__endend of data
argsoptional data

Definition at line 331 of file JGandalf.hh.

332  {
333  for (T i = __begin; i != __end; ++i) {
334 
335  const result_type& result = fit(value, *i);
336 
337  current.result.chi2 += result.chi2;
338  current.result.gradient += result.gradient;
339 
340  for (size_t row = 0; row != parameters.size(); ++row) {
341  for (size_t col = row; col != parameters.size(); ++col) {
342  V(row,col) += get(result.gradient, parameters[row]) * get(result.gradient, parameters[col]);
343  }
344  }
345  }
346 
347  update(fit, args...);
348  }
JModel_t value
value
Definition: JGandalf.hh:303
JMATH::JMatrixNS V
Hesse matrix.
Definition: JGandalf.hh:305
std::vector< parameter_type > parameters
fit parameters
Definition: JGandalf.hh:300
do set_variable OUTPUT_DIRECTORY $WORKDIR T
void update(const JFunction_t &fit, T __begin, T __end, Args...args)
Recursive method to update current parameters.
Definition: JGandalf.hh:331
struct JFIT::JGandalf::@10 current
result_type result
Definition: JGandalf.hh:448
template<class JModel_t>
template<class JFunction_t >
void JFIT::JGandalf< JModel_t >::update ( const JFunction_t &  fit)
inlineprivate

Termination method to update current parameters.

Parameters
fitfit function

Definition at line 357 of file JGandalf.hh.

358  {
359  for (size_t row = 0; row != parameters.size(); ++row) {
360  for (size_t col = 0; col != row; ++col) {
361  V(row,col) = V(col,row);
362  }
363  }
364  }
JMATH::JMatrixNS V
Hesse matrix.
Definition: JGandalf.hh:305
std::vector< parameter_type > parameters
fit parameters
Definition: JGandalf.hh:300
template<class JModel_t>
static double JFIT::JGandalf< JModel_t >::get ( const JModel_t &  model,
double JModel_t::*  parameter 
)
inlinestaticprivate

Read/write access to parameter value by data member.

Parameters
modelmodel
parameterparameter
Returns
value

Definition at line 374 of file JGandalf.hh.

375  {
376  return model.*parameter;
377  }
template<class JModel_t>
static double& JFIT::JGandalf< JModel_t >::get ( JModel_t &  model,
double JModel_t::*  parameter 
)
inlinestaticprivate

Read/write access to parameter value by data member.

Parameters
modelmodel
parameterparameter
Returns
value

Definition at line 387 of file JGandalf.hh.

388  {
389  return model.*parameter;
390  }
template<class JModel_t>
static double JFIT::JGandalf< JModel_t >::get ( const JModel_t &  model,
const size_t  index 
)
inlinestaticprivate

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 400 of file JGandalf.hh.

401  {
402  return model[index];
403  }
template<class JModel_t>
static double& JFIT::JGandalf< JModel_t >::get ( JModel_t &  model,
const size_t  index 
)
inlinestaticprivate

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 413 of file JGandalf.hh.

414  {
415  return model[index];
416  }
template<class JModel_t>
static double JFIT::JGandalf< JModel_t >::get ( const JModel_t &  model,
const int  index 
)
inlinestaticprivate

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 426 of file JGandalf.hh.

427  {
428  return model[index];
429  }
template<class JModel_t>
static double& JFIT::JGandalf< JModel_t >::get ( JModel_t &  model,
const int  index 
)
inlinestaticprivate

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 439 of file JGandalf.hh.

440  {
441  return model[index];
442  }

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 292 of file JGandalf.hh.

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 293 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 294 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 295 of file JGandalf.hh.

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

multiplication factor control parameter

Definition at line 296 of file JGandalf.hh.

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

multiplication factor control parameter

Definition at line 297 of file JGandalf.hh.

template<class JModel_t>
double JFIT::JGandalf< JModel_t >::PIVOT = std::numeric_limits<double>::epsilon()
static

minimal value diagonal element of Hesse matrix

minimal value diagonal element of matrix

Definition at line 298 of file JGandalf.hh.

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

fit parameters

Definition at line 300 of file JGandalf.hh.

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

number of iterations

Definition at line 301 of file JGandalf.hh.

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

control parameter

Definition at line 302 of file JGandalf.hh.

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

value

Definition at line 303 of file JGandalf.hh.

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

error

Definition at line 304 of file JGandalf.hh.

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

Hesse matrix.

Definition at line 305 of file JGandalf.hh.

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

Definition at line 444 of file JGandalf.hh.

template<class JModel_t>
JMATH::JVectorND JFIT::JGandalf< JModel_t >::x
private

Definition at line 445 of file JGandalf.hh.

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

Definition at line 448 of file JGandalf.hh.

struct { ... } JFIT::JGandalf< JModel_t >::current
struct { ... } JFIT::JGandalf< JModel_t >::previous
template<class T>
int JEEP::JMessage< T >::debug = 0
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: