Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
JROOT::JRootfit_t< JFs_t > Class Template Reference

Base class for result of ROOT Fit. More...

#include <JRootfit.hh>

Inheritance diagram for JROOT::JRootfit_t< JFs_t >:
JFIT::JGandalf< JModel_t > JEEP::JMessage< T > JROOT::JRootfit< JFs_t >

Public Types

typedef JFIT_LOCAL::JTypedef_t< JModel_t >::parameter_type parameter_type
 Data type of fit parameter. More...
 

Public Member Functions

const JFs_t & getFunction () const
 Get function. More...
 
size_t getNumberOfParameters () const
 Get number of parameters. More...
 
size_t getNumberOfFreeParameters () const
 Get number of free parameters. More...
 
size_t getN () const
 Get number of data points. More...
 
double getChi2 () const
 Get chi2. More...
 
int getNDF () const
 Get number of degrees of freedom. More...
 
double getValue (size_t i) const
 Get value of parameter at given index. More...
 
double getError (size_t i) const
 Get error of parameter at given index. 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...
 

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 bool EPSILON_ABSOLUTE = false
 set epsilon to absolute difference instead of relative 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...
 

Protected Member Functions

 JRootfit_t ()
 Default constructor. More...
 

Protected Attributes

size_t npx
 number of data points More...
 
double chi2
 chi2 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
 
   result_type   result
 
previous
 

Detailed Description

template<class JFs_t>
class JROOT::JRootfit_t< JFs_t >

Base class for result of ROOT Fit.

Definition at line 828 of file JRootfit.hh.

Member Typedef Documentation

◆ parameter_type

template<class JModel_t >
typedef JFIT_LOCAL::JTypedef_t<JModel_t>::parameter_type JFIT::JGandalf< JModel_t >::parameter_type
inherited

Data type of fit parameter.

Definition at line 95 of file JGandalf.hh.

Constructor & Destructor Documentation

◆ JRootfit_t()

template<class JFs_t >
JROOT::JRootfit_t< JFs_t >::JRootfit_t ( )
inlineprotected

Default constructor.

Definition at line 835 of file JRootfit.hh.

835  :
836  npx (0),
837  chi2(0.0)
838  {}
double chi2
chi2
Definition: JRootfit.hh:842
size_t npx
number of data points
Definition: JRootfit.hh:841

Member Function Documentation

◆ getFunction()

template<class JFs_t >
const JFs_t& JROOT::JRootfit_t< JFs_t >::getFunction ( ) const
inline

Get function.

Returns
function.

Definition at line 850 of file JRootfit.hh.

851  {
852  return this->value;
853  }
JModel_t value
value
Definition: JGandalf.hh:341

◆ getNumberOfParameters()

template<class JFs_t >
size_t JROOT::JRootfit_t< JFs_t >::getNumberOfParameters ( ) const
inline

Get number of parameters.

Returns
number of parameters

Definition at line 861 of file JRootfit.hh.

862  {
863  return JFs_t::parameters.size();
864  }

◆ getNumberOfFreeParameters()

template<class JFs_t >
size_t JROOT::JRootfit_t< JFs_t >::getNumberOfFreeParameters ( ) const
inline

Get number of free parameters.

Returns
number of free parameters

Definition at line 872 of file JRootfit.hh.

873  {
874  return this->parameters.size();
875  }
std::vector< parameter_type > parameters
fit parameters
Definition: JGandalf.hh:338

◆ getN()

template<class JFs_t >
size_t JROOT::JRootfit_t< JFs_t >::getN ( ) const
inline

Get number of data points.

Returns
number of data points

Definition at line 883 of file JRootfit.hh.

884  {
885  return npx;
886  }

◆ getChi2()

template<class JFs_t >
double JROOT::JRootfit_t< JFs_t >::getChi2 ( ) const
inline

Get chi2.

Returns
chi2

Definition at line 894 of file JRootfit.hh.

895  {
896  return chi2;
897  }

◆ getNDF()

template<class JFs_t >
int JROOT::JRootfit_t< JFs_t >::getNDF ( ) const
inline

Get number of degrees of freedom.

Returns
number of degrees of freedom

Definition at line 905 of file JRootfit.hh.

906  {
907  return (int) getN() - (int) getNumberOfFreeParameters();
908  }
size_t getNumberOfFreeParameters() const
Get number of free parameters.
Definition: JRootfit.hh:872
size_t getN() const
Get number of data points.
Definition: JRootfit.hh:883

◆ getValue()

template<class JFs_t >
double JROOT::JRootfit_t< JFs_t >::getValue ( size_t  i) const
inline

Get value of parameter at given index.

Parameters
iindex

Definition at line 916 of file JRootfit.hh.

917  {
918  return getParameter(this->value, i);
919  }
int getParameter(const std::string &text)
Get parameter number from text string.

◆ getError()

template<class JFs_t >
double JROOT::JRootfit_t< JFs_t >::getError ( size_t  i) const
inline

Get error of parameter at given index.

Parameters
iindex

Definition at line 927 of file JRootfit.hh.

928  {
929  return getParameter(this->error, i);
930  }
JModel_t error
error
Definition: JGandalf.hh:342

◆ operator()()

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

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

161  {
162  using namespace std;
163  using namespace JPP;
164 
165  // note that all model values should be assigned to the start value of the model before use
166  // because the actual list of model parameters can vary from fit to fit
167  // (e.g. if model consists of a container).
168 
169  const size_t N = parameters.size();
170 
171  V.resize(N);
172  h.resize(N);
173  x.resize(N);
174 
175  previous.result.chi2 = numeric_limits<double>::max();
176 
177  current.result.chi2 = numeric_limits<double>::max();
178  current.result.gradient = value;
179  current.result.gradient = zero;
180 
181  error = value;
182  error = zero;
183 
184  lambda = LAMBDA_MIN;
185 
187 
188  DEBUG("step: " << numberOfIterations << endl);
189 
190  reset();
191 
192  update(fit, __begin, __end, args...);
193 
194  DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
195  DEBUG("chi2: " << FIXED(12,5) << current.result.chi2 << endl);
196 
197  if (current.result.chi2 < previous.result.chi2) {
198 
199  if (numberOfIterations != 0) {
200 
201  const double tolerance = EPSILON * (EPSILON_ABSOLUTE ? 1.0 : fabs(previous.result.chi2));
202 
203  if (fabs(previous.result.chi2 - current.result.chi2) <= tolerance) {
204 
205  // normal end
206 
207  const result_type result = current.result;
208 
209  reset();
210 
211  update(fit, __begin, __end, args...);
212 
213  try {
214  V.invert();
215  }
216  catch (const exception& error) {}
217 
218  for (size_t i = 0; i != N; ++i) {
219  get(error, parameters[i]) = sqrt(V(i,i));
220  }
221 
222  return result;
223  }
224 
225  if (lambda > LAMBDA_MIN) {
226  lambda /= LAMBDA_DOWN;
227  }
228  }
229 
230  // store current values
231 
232  previous.value = value;
233  previous.result = current.result;
234 
235  } else {
236 
237  value = previous.value; // restore value
238 
239  lambda *= LAMBDA_UP;
240 
241  if (lambda > LAMBDA_MAX) {
242  break;
243  }
244 
245  reset();
246 
247  update(fit, __begin, __end, args...);
248  }
249 
250  DEBUG("Hesse matrix:" << endl << V << endl);
251 
252  // force definite positiveness
253 
254  for (size_t i = 0; i != N; ++i) {
255 
256  if (V(i,i) < PIVOT) {
257  V(i,i) = PIVOT;
258  }
259 
260  h[i] = 1.0 / sqrt(V(i,i));
261  }
262 
263  // normalisation
264 
265  for (size_t row = 0; row != N; ++row) {
266  for (size_t col = 0; col != row; ++col) {
267  V(row,col) *= h[row] * h[col];
268  V(col,row) = V(row,col);
269  }
270  }
271 
272  for (size_t i = 0; i != N; ++i) {
273  V(i,i) = 1.0 + lambda;
274  }
275 
276  // solve A x = b
277 
278  for (size_t col = 0; col != N; ++col) {
279  x[col] = h[col] * get(current.result.gradient, parameters[col]);
280  }
281 
282  try {
283  V.solve(x);
284  }
285  catch (const exception& error) {
286 
287  ERROR("JGandalf: " << error.what() << endl << V << endl);
288 
289  break;
290  }
291 
292  // update value
293 
294  for (size_t row = 0; row != N; ++row) {
295 
296  DEBUG("u[" << noshowpos << setw(3) << row << "] = " << showpos << FIXED(15,5) << get(value, parameters[row]));
297 
298  get(value, parameters[row]) -= h[row] * x[row];
299 
300  DEBUG(" -> " << FIXED(15,5) << get(value, parameters[row]) << noshowpos << endl);
301  }
302 
303  model(value);
304  }
305 
306  // abnormal end
307 
308  const result_type result = previous.result;
309 
310  value = previous.value; // restore value
311 
312  reset();
313 
314  update(fit, __begin, __end, args...);
315 
316  try {
317  V.invert();
318  }
319  catch (const exception& error) {}
320 
321  for (size_t i = 0; i != N; ++i) {
322  get(error, parameters[i]) = sqrt(V(i,i));
323  }
324 
325  return result;
326  }
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:66
double lambda
control parameter
Definition: JGandalf.hh:340
static double LAMBDA_MIN
minimal value control parameter
Definition: JGandalf.hh:332
JMATH::JVectorND x
Definition: JGandalf.hh:483
struct JFIT::JGandalf::@12 current
void reset()
Reset current parameters.
Definition: JGandalf.hh:349
static double LAMBDA_DOWN
multiplication factor control parameter
Definition: JGandalf.hh:335
static double LAMBDA_UP
multiplication factor control parameter
Definition: JGandalf.hh:334
int numberOfIterations
number of iterations
Definition: JGandalf.hh:339
static bool EPSILON_ABSOLUTE
set epsilon to absolute difference instead of relative
Definition: JGandalf.hh:331
std::vector< double > h
Definition: JGandalf.hh:482
static double get(const JModel_t &model, double JModel_t::*parameter)
Read/write access to parameter value by data member.
Definition: JGandalf.hh:412
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JGandalf.hh:329
static double PIVOT
minimal value diagonal element of Hesse matrix
Definition: JGandalf.hh:336
void update(const JFunction_t &fit, T __begin, T __end, Args ...args)
Recursive method to update current parameters.
Definition: JGandalf.hh:369
result_type result
Definition: JGandalf.hh:486
static double EPSILON
maximal distance to minimum
Definition: JGandalf.hh:330
JMATH::JMatrixNS V
Hesse matrix.
Definition: JGandalf.hh:343
static double LAMBDA_MAX
maximal value control parameter
Definition: JGandalf.hh:333
struct JFIT::JGandalf::@13 previous
void model(JModel_t &value)
Auxiliary function to constrain model during fit.
Definition: JGandalf.hh:56
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:446
void solve(JVectorND_t &u)
Get solution of equation A x = b.
Definition: JMatrixNS.hh:308
void invert()
Invert matrix according LDU decomposition.
Definition: JMatrixNS.hh:75

◆ reset()

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

Reset current parameters.

Definition at line 349 of file JGandalf.hh.

350  {
351  using namespace JPP;
352 
353  current.result.chi2 = 0.0;
354  current.result.gradient = zero;
355 
356  V.reset();
357  }
JMatrixND & reset()
Set matrix to the null matrix.
Definition: JMatrixND.hh:459

◆ update() [1/2]

template<class JModel_t >
template<class JFunction_t , class T , class ... Args>
void JFIT::JGandalf< JModel_t >::update ( const JFunction_t &  fit,
__begin,
__end,
Args ...  args 
)
inlineprivateinherited

Recursive method to update current parameters.

Parameters
fitfit function
__beginbegin of data
__endend of data
argsoptional data

Definition at line 369 of file JGandalf.hh.

370  {
371  for (T i = __begin; i != __end; ++i) {
372 
373  const result_type& result = fit(value, *i);
374 
375  current.result.chi2 += result.chi2;
376  current.result.gradient += result.gradient;
377 
378  for (size_t row = 0; row != parameters.size(); ++row) {
379  for (size_t col = row; col != parameters.size(); ++col) {
380  V(row,col) += get(result.gradient, parameters[row]) * get(result.gradient, parameters[col]);
381  }
382  }
383  }
384 
385  update(fit, args...);
386  }
JModel_t gradient
partial derivatives of chi2
Definition: JGandalf.hh:136

◆ update() [2/2]

template<class JModel_t >
template<class JFunction_t >
void JFIT::JGandalf< JModel_t >::update ( const JFunction_t &  fit)
inlineprivateinherited

Termination method to update current parameters.

Parameters
fitfit function

Definition at line 395 of file JGandalf.hh.

396  {
397  for (size_t row = 0; row != parameters.size(); ++row) {
398  for (size_t col = 0; col != row; ++col) {
399  V(row,col) = V(col,row);
400  }
401  }
402  }

◆ get() [1/6]

template<class JModel_t >
static double JFIT::JGandalf< JModel_t >::get ( const JModel_t model,
double JModel_t::*  parameter 
)
inlinestaticprivateinherited

Read/write access to parameter value by data member.

Parameters
modelmodel
parameterparameter
Returns
value

Definition at line 412 of file JGandalf.hh.

413  {
414  return model.*parameter;
415  }

◆ get() [2/6]

template<class JModel_t >
static double& JFIT::JGandalf< JModel_t >::get ( JModel_t model,
double JModel_t::*  parameter 
)
inlinestaticprivateinherited

Read/write access to parameter value by data member.

Parameters
modelmodel
parameterparameter
Returns
value

Definition at line 425 of file JGandalf.hh.

426  {
427  return model.*parameter;
428  }

◆ get() [3/6]

template<class JModel_t >
static double JFIT::JGandalf< JModel_t >::get ( const JModel_t model,
const size_t  index 
)
inlinestaticprivateinherited

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 438 of file JGandalf.hh.

439  {
440  return model[index];
441  }

◆ get() [4/6]

template<class JModel_t >
static double& JFIT::JGandalf< JModel_t >::get ( JModel_t model,
const size_t  index 
)
inlinestaticprivateinherited

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 451 of file JGandalf.hh.

452  {
453  return model[index];
454  }

◆ get() [5/6]

template<class JModel_t >
static double JFIT::JGandalf< JModel_t >::get ( const JModel_t model,
const int  index 
)
inlinestaticprivateinherited

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 464 of file JGandalf.hh.

465  {
466  return model[index];
467  }

◆ get() [6/6]

template<class JModel_t >
static double& JFIT::JGandalf< JModel_t >::get ( JModel_t model,
const int  index 
)
inlinestaticprivateinherited

Read/write access to parameter value by index.

Parameters
modelmodel
indexindex
Returns
value

Definition at line 477 of file JGandalf.hh.

478  {
479  return model[index];
480  }

Member Data Documentation

◆ npx

template<class JFs_t >
size_t JROOT::JRootfit_t< JFs_t >::npx
protected

number of data points

Definition at line 841 of file JRootfit.hh.

◆ chi2

template<class JFs_t >
double JROOT::JRootfit_t< JFs_t >::chi2
protected

chi2

Definition at line 842 of file JRootfit.hh.

◆ MAXIMUM_ITERATIONS

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

maximal number of iterations

maximal number of iterations.

Definition at line 329 of file JGandalf.hh.

◆ EPSILON

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

maximal distance to minimum

maximal distance to minimum.

Definition at line 330 of file JGandalf.hh.

◆ EPSILON_ABSOLUTE

template<class JModel_t >
bool JFIT::JGandalf< JModel_t >::EPSILON_ABSOLUTE = false
staticinherited

set epsilon to absolute difference instead of relative

set epsilon to absolute difference instead of relative.

Definition at line 331 of file JGandalf.hh.

◆ LAMBDA_MIN

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

minimal value control parameter

Definition at line 332 of file JGandalf.hh.

◆ LAMBDA_MAX

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

maximal value control parameter

Definition at line 333 of file JGandalf.hh.

◆ LAMBDA_UP

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

multiplication factor control parameter

Definition at line 334 of file JGandalf.hh.

◆ LAMBDA_DOWN

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

multiplication factor control parameter

Definition at line 335 of file JGandalf.hh.

◆ PIVOT

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

minimal value diagonal element of Hesse matrix

minimal value diagonal element of matrix

Definition at line 336 of file JGandalf.hh.

◆ parameters

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

fit parameters

Definition at line 338 of file JGandalf.hh.

◆ numberOfIterations

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

number of iterations

Definition at line 339 of file JGandalf.hh.

◆ lambda

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

control parameter

Definition at line 340 of file JGandalf.hh.

◆ value

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

value

Definition at line 341 of file JGandalf.hh.

◆ error

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

error

Definition at line 342 of file JGandalf.hh.

◆ V

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

Hesse matrix.

Definition at line 343 of file JGandalf.hh.

◆ h

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

Definition at line 482 of file JGandalf.hh.

◆ x

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

Definition at line 483 of file JGandalf.hh.

◆ result

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

Definition at line 486 of file JGandalf.hh.

◆ 

struct { ... } JFIT::JGandalf< JModel_t >::current

◆ 

struct { ... } JFIT::JGandalf< JModel_t >::previous

◆ debug

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: