Jpp  16.0.0-rc.2
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 = 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 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
 
struct {
   result_type   result
 
   JMATH::JMatrixNS   V
 
current
 
struct {
   JModel_t   value
 
   JModel_t   error
 
   result_type   result
 
   JMATH::JMatrixNS   V
 
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 52 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 63 of file JGandalf.hh.

Constructor & Destructor Documentation

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

Default constructor.

Definition at line 111 of file JGandalf.hh.

112  {}

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

Definition at line 128 of file JGandalf.hh.

129  {
130  using namespace std;
131  using namespace JPP;
132 
133  // note that all model values should be assigned to the start value of the model before use
134  // because the actual list of model parameters can vary from fit to fit
135  // (e.g. if model consists of a container).
136 
137  const size_t N = parameters.size();
138 
139  V.resize(N);
140  h.resize(N);
141 
142  previous.result.chi2 = numeric_limits<double>::max();
143 
144  previous.V.resize(N);
145 
146  current.result.chi2 = numeric_limits<double>::max();
147  current.result.gradient = value;
148  current.result.gradient = zero;
149 
150  current.V.resize(N);
151 
152  error = value;
153  error = zero;
154 
155  lambda = LAMBDA_MIN;
156 
157 
159 
160  DEBUG("step: " << numberOfIterations << endl);
161 
162  reset();
163 
164  update(fit, __begin, __end, args...);
165 
166  DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
167  DEBUG("chi2: " << FIXED(12,5) << current.result.chi2 << endl);
168 
169  if (current.result.chi2 < previous.result.chi2) {
170 
171  if (numberOfIterations != 0) {
172 
173  if (fabs(previous.result.chi2 - current.result.chi2) < EPSILON*fabs(previous.result.chi2)) {
174  return current.result; // normal end
175  }
176 
177  if (lambda > LAMBDA_MIN) {
178  lambda /= LAMBDA_DOWN;
179  }
180 
181  // store current values
182 
183  previous.value = value;
184  previous.error = error;
185  previous.result = current.result;
186 
187  previous.V.swap(V);
188  }
189 
190  } else {
191 
192  value = previous.value; // restore value
193 
194  lambda *= LAMBDA_UP;
195 
196  if (lambda > LAMBDA_MAX) {
197  break;
198  }
199 
200  reset();
201 
202  update(fit, __begin, __end, args...);
203  }
204 
205 
206  V.swap(current.V);
207 
208  DEBUG("Hesse matrix:" << endl << V << endl);
209 
210 
211  // force definite positiveness
212 
213  for (size_t i = 0; i != N; ++i) {
214 
215  if (V(i,i) < PIVOT) {
216  V(i,i) = PIVOT;
217  }
218 
219  h[i] = 1.0 / sqrt(V(i,i));
220  }
221 
222 
223  // normalisation
224 
225  for (size_t row = 0; row != N; ++row) {
226  for (size_t col = 0; col != row; ++col) {
227  V(row,col) *= h[row] * h[col];
228  V(col,row) = V(row,col);
229  }
230  }
231 
232  for (size_t i = 0; i != N; ++i) {
233  V(i,i) = 1.0 + lambda;
234  }
235 
236 
237  // invert Hesse matrix
238 
239  try {
240  V.invert();
241  }
242  catch (const JException& error) {
243 
244  ERROR("JGandalf: " << error.what() << endl << V << endl);
245 
246  break;
247  }
248 
249 
250  // update value and error
251 
252  for (size_t row = 0; row != N; ++row) {
253 
254  DEBUG("u[" << noshowpos << row << "] = " << showpos << FIXED(15,5) << get(value, parameters[row]));
255 
256  for (size_t col = 0; col != N; ++col) {
257 
258  V(row,col) *= h[row] * h[col];
259 
260  get(value, parameters[row]) -= V(row,col) * get(current.result.gradient, parameters[col]);
261  }
262 
263  DEBUG(" -> " << FIXED(15,5) << get(value, parameters[row]) << noshowpos << endl);
264 
265  get(error, parameters[row]) = h[row];
266  }
267  }
268 
269  // abnormal end
270 
271  value = previous.value; // restore value
272  error = previous.error; // restore error
273 
274  V.swap(previous.V); // restore Hesse matrix
275 
276  return previous.result;
277  }
double lambda
control parameter
Definition: JGandalf.hh:290
static double EPSILON
maximal distance to minimum
Definition: JGandalf.hh:281
JModel_t value
value
Definition: JGandalf.hh:291
static double PIVOT
minimal value diagonal element of Hesse matrix
Definition: JGandalf.hh:286
JMATH::JMatrixNS V
Hesse matrix.
Definition: JGandalf.hh:293
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
std::vector< parameter_type > parameters
fit parameters
Definition: JGandalf.hh:288
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:362
static double LAMBDA_MIN
minimal value control parameter
Definition: JGandalf.hh:282
static double LAMBDA_DOWN
multiplication factor control parameter
Definition: JGandalf.hh:285
void reset()
Reset current parameters.
Definition: JGandalf.hh:299
struct JFIT::JGandalf::@13 previous
void update(const JFunction_t &fit, T __begin, T __end, Args...args)
Recursive method to update current parameters.
Definition: JGandalf.hh:319
static double LAMBDA_UP
multiplication factor control parameter
Definition: JGandalf.hh:284
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:80
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
void swap(JMatrixND_t &A)
Swap matrices.
Definition: JMatrixND.hh:123
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JGandalf.hh:280
int numberOfIterations
number of iterations
Definition: JGandalf.hh:289
static double LAMBDA_MAX
maximal value control parameter
Definition: JGandalf.hh:283
JModel_t error
error
Definition: JGandalf.hh:292
struct JFIT::JGandalf::@12 current
std::vector< double > h
Definition: JGandalf.hh:432
template<class JModel_t>
void JFIT::JGandalf< JModel_t >::reset ( )
inlineprivate

Reset current parameters.

Definition at line 299 of file JGandalf.hh.

300  {
301  using namespace JPP;
302 
303  current.result.chi2 = 0.0;
304  current.result.gradient = zero;
305 
306  current.V.reset();
307  }
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
struct JFIT::JGandalf::@12 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 319 of file JGandalf.hh.

320  {
321  for (T i = __begin; i != __end; ++i) {
322 
323  const result_type& result = fit(value, *i);
324 
325  current.result.chi2 += result.chi2;
326  current.result.gradient += result.gradient;
327 
328  for (size_t row = 0; row != parameters.size(); ++row) {
329  for (size_t col = row; col != parameters.size(); ++col) {
330  current.V(row,col) += get(result.gradient, parameters[row]) * get(result.gradient, parameters[col]);
331  }
332  }
333  }
334 
335  update(fit, args...);
336  }
JModel_t value
value
Definition: JGandalf.hh:291
std::vector< parameter_type > parameters
fit parameters
Definition: JGandalf.hh:288
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:319
result_type result
Definition: JGandalf.hh:435
struct JFIT::JGandalf::@12 current
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 345 of file JGandalf.hh.

346  {
347  for (size_t row = 0; row != parameters.size(); ++row) {
348  for (size_t col = 0; col != row; ++col) {
349  current.V(row,col) = current.V(col,row);
350  }
351  }
352  }
std::vector< parameter_type > parameters
fit parameters
Definition: JGandalf.hh:288
struct JFIT::JGandalf::@12 current
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 362 of file JGandalf.hh.

363  {
364  return model.*parameter;
365  }
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 375 of file JGandalf.hh.

376  {
377  return model.*parameter;
378  }
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 388 of file JGandalf.hh.

389  {
390  return model[index];
391  }
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 401 of file JGandalf.hh.

402  {
403  return model[index];
404  }
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 414 of file JGandalf.hh.

415  {
416  return model[index];
417  }
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 427 of file JGandalf.hh.

428  {
429  return model[index];
430  }

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 280 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 281 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 282 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 283 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 284 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 285 of file JGandalf.hh.

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

minimal value diagonal element of Hesse matrix

minimal value diagonal element of matrix

Definition at line 286 of file JGandalf.hh.

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

fit parameters

Definition at line 288 of file JGandalf.hh.

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

number of iterations

Definition at line 289 of file JGandalf.hh.

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

control parameter

Definition at line 290 of file JGandalf.hh.

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

value

Definition at line 291 of file JGandalf.hh.

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

error

Definition at line 292 of file JGandalf.hh.

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

Hesse matrix.

Definition at line 293 of file JGandalf.hh.

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

Definition at line 432 of file JGandalf.hh.

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

Definition at line 435 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: