1#ifndef __JFIT__JGRADIENT__
2#define __JFIT__JGRADIENT__
22namespace JPP {
using namespace JFIT; }
41 virtual void apply(
const double step) = 0;
49 public std::shared_ptr<JParameter_t>
92 const int debug = 3) :
124 return numeric_limits<double>::max();
129 const size_t N = this->size();
134 for (
size_t i = 0; i != N; ++i) {
153 for (
double ds = 1.0; ds > 1.0e-3; ) {
155 this->
move(+1.0 * ds);
159 DEBUG(
"chi2[3] " << setw(4) << m <<
' ' <<
FIXED(12,5) << chi2[3] <<
' ' <<
FIXED(12,5) << ds << endl);
161 if (chi2[3] < chi2[2]) {
185 for ( ; m != 0; --m) {
186 this->
move(-1.0 * ds);
193 this->
move(-1.0 * ds);
195 if (chi2[2] < chi2[3]) {
203 const double f21 = chi2[2] - chi2[1];
204 const double f23 = chi2[2] - chi2[3];
206 const double xs = 0.5 * (f21 - f23) / (f23 + f21);
208 this->
move(+1.0 * xs * ds);
212 if (chi2[3] < chi2[2]) {
218 this->
move(-1.0 * xs * ds);
233 if (fabs(chi2[2] - chi2[0]) <
epsilon * (
absolute ? 1.0 : 0.5 * (fabs(chi2[0]) + fabs(chi2[2])))) {
245 for (
size_t i = 0; i != N; ++i){
256 for (
size_t i = 0; i != N; ++i){
258 H[i] = G[i] + dgg * H[i];
283 for (
const auto& i : *
this) {
284 out << i.name <<
' ';
287 out <<
"chi2" << endl;
317 const size_t N = this->size();
329 for (
size_t i = 0; i != N; ++i) {
330 if ((*
this)[i].name.size() > width) {
331 width = (*this)[i].name.size();
337 for (
size_t i = 0; i != N; ++i) {
339 if ((*
this)[i].value != 0.0) {
341 (*this)[i]->apply(+0.5 * (*
this)[i].value);
345 (*this)[i]->apply(-0.5 * (*
this)[i].value);
346 (*this)[i]->apply(-0.5 * (*
this)[i].value);
352 (*this)[i]->apply(+0.5 * (*
this)[i].value);
354 DEBUG(setw(width) << left << (*
this)[i].name << right <<
' ' <<
FIXED(12,5) << (*
this)[i].value <<
' ' <<
FIXED(12,5) <<
gradient[i] << endl);
366 DEBUG(setw(width) << left <<
"|gradient|" << right <<
' ' <<
FIXED(12,5) << V << endl);
369 for (
size_t i = 0; i != N; ++i) {
386 for (
size_t i = 0; i != this->size(); ++i) {
387 (*this)[ i ]->apply((*
this)[ i ].value *
gradient[ i ] * factor);
389 }
else if (factor < 0.0) {
390 for (
size_t i = this->size(); i != 0; --i) {
391 (*this)[i-1]->apply((*
this)[i-1].value *
gradient[i-1] * factor);
410 if (ys.size() == this->size()) {
412 for (
size_t i = 0; i != this->size(); ++i) {
413 (*this)[ i ]->apply(+ys[ i ]);
416 for (
const double y : ys) {
417 out <<
FIXED(12,5) << y <<
' ';
422 for (
size_t i = this->size(); i != 0; --i) {
423 (*this)[i-1]->apply(-ys[i-1]);
428 for (
const double x : xs) {
430 ys.push_back(x * (*
this)[ys.size()].value);
General purpose messaging.
#define DEBUG(A)
Message macros.
Auxiliary classes and methods for linear and iterative data regression.
double getChi2(const double P)
Get chi2 corresponding to given probability.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
size_t Nmax
maximum number of iterations
bool normalise
normalise gradient
void operator()(std::ostream &out, const T &getChi2, const std::vector< double > &xs) const
Make scan at current position.
void move(const double factor)
Move.
void scan(std::ostream &out, const T &getChi2, const std::vector< double > &xs, std::vector< double > &ys) const
Scan.
std::vector< double > gradient
bool absolute
absolute chi2
double evaluate(const T &getChi2)
Evaluate gradient.
double operator()(const T &getChi2)
Fit.
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.
size_t numberOfIterations
size_t Nextra
maximum number of extra steps
Auxiliary data structure for editable parameter.
JModifier_t(const std::string &name, JParameter_t *parameter, const double value)
Constructor.
Auxiliary data structure for fit parameter.
virtual void apply(const double step)=0
Apply step.
virtual ~JParameter_t()
Virtual destructor.
Auxiliary data structure for floating point format specification.