Jpp  18.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
JFIT::JGradient Struct Reference

Conjugate gradient fit. More...

#include <JGradient.hh>

Inheritance diagram for JFIT::JGradient:
std::vector< JModifier_t >

Public Member Functions

 JGradient (const size_t Nmax=std::numeric_limits< size_t >::max(), const size_t Nextra=0, const double epsilon=1.0e-4, const int debug=3)
 Constructor. More...
 
template<class T >
double operator() (const T &getChi2)
 Fit. More...
 

Public Attributes

size_t Nmax
 maximum number of iterations More...
 
size_t Nextra
 maximum number of extra steps More...
 
double epsilon
 epsilon More...
 
int debug
 debug More...
 

Private Member Functions

template<class T >
void evaluate (const T &getChi2)
 Evaluate gradient. More...
 
void move (const double factor)
 Move. More...
 

Private Attributes

std::vector< double > gradient
 
double chi2 [5]
 

Detailed Description

Conjugate gradient fit.

Definition at line 73 of file JGradient.hh.

Constructor & Destructor Documentation

JFIT::JGradient::JGradient ( const size_t  Nmax = std::numeric_limits<size_t>::max(),
const size_t  Nextra = 0,
const double  epsilon = 1.0e-4,
const int  debug = 3 
)
inline

Constructor.

The number of iterations and epsilon refer to the number of steps and the distance to the minimum, respectively.
The number of extra steps can be used to overcome a possible hurdle on the way.

Parameters
Nmaxmaximum number of iterations
Nextramaximum number of extra steps
epsilonepsilon
debugdebug

Definition at line 88 of file JGradient.hh.

91  :
92  Nmax (Nmax),
93  Nextra (Nextra),
95  debug (debug)
96  {}
size_t Nextra
maximum number of extra steps
Definition: JGradient.hh:266
double epsilon
epsilon
Definition: JGradient.hh:267
size_t Nmax
maximum number of iterations
Definition: JGradient.hh:265
int debug
debug
Definition: JGradient.hh:268

Member Function Documentation

template<class T >
double JFIT::JGradient::operator() ( const T getChi2)
inline

Fit.

The template parameter should provide for the following function operator.

   double operator()(int option);

The value of the option corresponds to the following cases.

  • 0 => step wise improvement of the chi2;
  • 1 => evaluation of the chi2 before the determination of the gradient of the chi2; and
  • 2 => evaluation of the derivative of the chi2 to each fit parameter.
Parameters
getChi2chi2 function

Definition at line 114 of file JGradient.hh.

115  {
116  using namespace std;
117  using namespace JPP;
118 
119  if (this->empty()) {
120  return numeric_limits<double>::max();
121  }
122 
123  this->evaluate(getChi2);
124 
125  const size_t N = this->size();
126 
127  vector<double> H(N);
128  vector<double> G(N);
129 
130  for (size_t i = 0; i != N; ++i) {
131  G[i] = -1.0 * gradient[i];
132  H[i] = G[i];
133  gradient[i] = H[i];
134  }
135 
136  size_t number_of_iterations = 0;
137 
138  for ( ; number_of_iterations != Nmax; ++number_of_iterations) {
139 
140  DEBUG("chi2[0] " << setw(4) << number_of_iterations << ' ' << FIXED(12,5) << chi2[0] << endl);
141 
142  // minimise chi2 in direction of gradient
143 
144  chi2[1] = chi2[0];
145  chi2[2] = chi2[1];
146 
147  size_t m = 0;
148 
149  for (double ds = 1.0; ds > 1.0e-3; ) {
150 
151  this->move(+1.0 * ds);
152 
153  chi2[3] = getChi2(0);
154 
155  DEBUG("chi2[3] " << setw(4) << m << ' ' << FIXED(12,5) << chi2[3] << ' ' << FIXED(12,5) << ds << endl);
156 
157  if (chi2[3] < chi2[2]) {
158 
159  chi2[1] = chi2[2];
160  chi2[2] = chi2[3];
161 
162  m = 0;
163 
164  continue;
165  }
166 
167  if (ds == 1.0) {
168 
169  if (m == 0) {
170  chi2[4] = chi2[3];
171  }
172 
173  if (m != Nextra) {
174 
175  ++m;
176 
177  continue;
178 
179  } else {
180 
181  for ( ; m != 0; --m) {
182  this->move(-1.0 * ds);
183  }
184 
185  chi2[3] = chi2[4];
186  }
187  }
188 
189  this->move(-1.0 * ds);
190 
191  if (chi2[2] < chi2[3]) {
192 
193  // final step based on parabolic interpolation through following points
194  //
195  // x1 = -1 * ds -> chi2[1]
196  // x2 = 0 * ds -> chi2[2]
197  // x3 = +1 * ds -> chi2[3]
198 
199  const double f21 = chi2[2] - chi2[1]; // f(x2) - f(x1)
200  const double f23 = chi2[2] - chi2[3]; // f(x2) - f(x3)
201 
202  const double xs = 0.5 * (f21 - f23) / (f23 + f21);
203 
204  this->move(+1.0 * xs * ds);
205 
206  chi2[3] = getChi2(0);
207 
208  if (chi2[3] < chi2[2]) {
209 
210  chi2[2] = chi2[3];
211 
212  } else {
213 
214  this->move(-1.0 * xs * ds);
215 
216  chi2[2] = getChi2(0);
217  }
218 
219  DEBUG("chi2[2] " << setw(4) << number_of_iterations << ' ' << FIXED(12,5) << chi2[2] << ' ' << SCIENTIFIC(12,5) << ds << endl);
220 
221  break;
222 
223  } else {
224 
225  ds *= 0.5;
226  }
227  }
228 
229  if (fabs(chi2[2] - chi2[0]) < epsilon * 0.5 * (fabs(chi2[0]) + fabs(chi2[2]))) {
230 
231  chi2[0] = chi2[2];
232 
233  break;
234  }
235 
236  this->evaluate(getChi2);
237 
238  double gg = 0.0;
239  double dgg = 0.0;
240 
241  for (size_t i = 0; i != N; ++i){
242  gg += G[i]*G[i];
243  dgg += (gradient[i] + G[i]) * gradient[i];
244  }
245 
246  if (gg == 0.0) {
247  break;
248  }
249 
250  dgg /= gg;
251 
252  for (size_t i = 0; i != N; ++i){
253  G[i] = -1.0 * gradient[i];
254  H[i] = G[i] + dgg * H[i];
255  gradient[i] = H[i];
256  }
257  }
258 
259  DEBUG("chi2[0] " << setw(4) << number_of_iterations << ' ' << FIXED(12,5) << chi2[0] << endl);
260 
261  return chi2[0];
262  }
void move(const double factor)
Move.
Definition: JGradient.hh:336
static const double H
Planck constant [eV s].
void evaluate(const T &getChi2)
Evaluate gradient.
Definition: JGradient.hh:275
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
size_t Nextra
maximum number of extra steps
Definition: JGradient.hh:266
double epsilon
epsilon
Definition: JGradient.hh:267
std::vector< double > gradient
Definition: JGradient.hh:349
size_t Nmax
maximum number of iterations
Definition: JGradient.hh:265
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
double chi2[5]
Definition: JGradient.hh:350
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:56
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
template<class T >
void JFIT::JGradient::evaluate ( const T getChi2)
inlineprivate

Evaluate gradient.

Definition at line 275 of file JGradient.hh.

276  {
277  using namespace std;
278  using namespace JPP;
279 
280  const size_t N = this->size();
281 
282  gradient.resize(N);
283 
284  for (std::vector<double>::iterator i = gradient.begin(); i != gradient.end(); ++i) {
285  *i = 0.0;
286  }
287 
288  chi2[0] = getChi2(1);
289 
290  size_t width = 1;
291 
292  for (size_t i = 0; i != N; ++i) {
293  if ((*this)[i].name.size() > width) {
294  width = (*this)[i].name.size();
295  }
296  }
297 
298  double V = 0.0;
299 
300  for (size_t i = 0; i != N; ++i) {
301 
302  if ((*this)[i].value != 0.0) {
303 
304  (*this)[i]->apply(+0.5 * (*this)[i].value);
305 
306  chi2[1] = getChi2(2);
307 
308  (*this)[i]->apply(-0.5 * (*this)[i].value);
309  (*this)[i]->apply(-0.5 * (*this)[i].value);
310 
311  chi2[2] = getChi2(2);
312 
313  gradient[i] = chi2[1] - chi2[2];
314 
315  (*this)[i]->apply(+0.5 * (*this)[i].value);
316 
317  DEBUG(setw(width) << left << (*this)[i].name << right << ' ' << FIXED(12,5) << (*this)[i].value << ' ' << FIXED(12,5) << gradient[i] << endl);
318 
319  } else {
320 
321  gradient[i] = 0.0;
322  }
323 
324  V += gradient[i] * gradient[i];
325  }
326 
327  DEBUG(setw(width) << left << "|gradient|" << right << ' ' << FIXED(12,5) << sqrt(V) << endl);
328  }
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
std::vector< double > gradient
Definition: JGradient.hh:349
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
double chi2[5]
Definition: JGradient.hh:350
double getChi2(const double P)
Get chi2 corresponding to given probability.
Definition: JFitToolkit.hh:56
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
void JFIT::JGradient::move ( const double  factor)
inlineprivate

Move.

Parameters
factorfactor

Definition at line 336 of file JGradient.hh.

337  {
338  if (factor > 0.0) {
339  for (size_t i = 0; i != this->size(); ++i) {
340  (*this)[ i ]->apply((*this)[ i ].value * gradient[ i ] * factor);
341  }
342  } else if (factor < 0.0) {
343  for (size_t i = this->size(); i != 0; --i) {
344  (*this)[i-1]->apply((*this)[i-1].value * gradient[i-1] * factor);
345  }
346  }
347  }
std::vector< double > gradient
Definition: JGradient.hh:349

Member Data Documentation

size_t JFIT::JGradient::Nmax

maximum number of iterations

Definition at line 265 of file JGradient.hh.

size_t JFIT::JGradient::Nextra

maximum number of extra steps

Definition at line 266 of file JGradient.hh.

double JFIT::JGradient::epsilon

epsilon

Definition at line 267 of file JGradient.hh.

int JFIT::JGradient::debug

debug

Definition at line 268 of file JGradient.hh.

std::vector<double> JFIT::JGradient::gradient
private

Definition at line 349 of file JGradient.hh.

double JFIT::JGradient::chi2[5]
private

Definition at line 350 of file JGradient.hh.


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