Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RooFit::MultiProcess & TestStatistics part 3: Minuit2::NumericalDerivator #8567

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions math/minuit2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ if(CMAKE_PROJECT_NAME STREQUAL ROOT)
Minuit2/ModularFunctionMinimizer.h
Minuit2/NegativeG2LineSearch.h
Minuit2/Numerical2PGradientCalculator.h
Minuit2/NumericalDerivator.h
Minuit2/ParametricFunction.h
Minuit2/ScanBuilder.h
Minuit2/ScanMinimizer.h
Expand Down Expand Up @@ -176,6 +177,7 @@ if(CMAKE_PROJECT_NAME STREQUAL ROOT)
src/ModularFunctionMinimizer.cxx
src/NegativeG2LineSearch.cxx
src/Numerical2PGradientCalculator.cxx
src/NumericalDerivator.cxx
src/ParametricFunction.cxx
src/ScanBuilder.cxx
src/SimplexBuilder.cxx
Expand Down
114 changes: 114 additions & 0 deletions math/minuit2/inc/Minuit2/NumericalDerivator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// @(#)root/mathcore:$Id$
// Authors: L. Moneta, J.T. Offermann, E.G.P. Bos 2013-2018
//
/**********************************************************************
* *
* Copyright (c) 2013 , LCG ROOT MathLib Team *
* *
**********************************************************************/
/*
* NumericalDerivator.h
*
* Original version created on: Aug 14, 2013
* Authors: L. Moneta, J. T. Offermann
* Modified version created on: Sep 27, 2017
* Author: E. G. P. Bos
*/

#ifndef ROOT_Minuit2_NumericalDerivator
#define ROOT_Minuit2_NumericalDerivator

#ifndef ROOT_Math_IFunctionfwd
#include <Math/IFunctionfwd.h>
#endif
egpbos marked this conversation as resolved.
Show resolved Hide resolved

#include <vector>
#include "Fit/ParameterSettings.h"
#include "Minuit2/SinParameterTransformation.h"
#include "Minuit2/SqrtUpParameterTransformation.h"
#include "Minuit2/SqrtLowParameterTransformation.h"
#include "Minuit2/MnMachinePrecision.h"

namespace ROOT {
namespace Minuit2 {

// Holds all necessary derivatives and associated numbers (per parameter) used in the NumericalDerivator class.
struct DerivatorElement {
double derivative;
double second_derivative;
double step_size;
};

class NumericalDerivator {
public:
explicit NumericalDerivator(bool always_exactly_mimic_minuit2 = true);
NumericalDerivator(const NumericalDerivator &other);
NumericalDerivator(double step_tolerance, double grad_tolerance, unsigned int ncycles, double error_level,
bool always_exactly_mimic_minuit2 = true);

void SetupDifferentiate(const ROOT::Math::IBaseFunctionMultiDim *function, const double *cx,
const std::vector<ROOT::Fit::ParameterSettings> &parameters);
std::vector<DerivatorElement> Differentiate(const ROOT::Math::IBaseFunctionMultiDim *function, const double *x,
const std::vector<ROOT::Fit::ParameterSettings> &parameters,
const std::vector<DerivatorElement> &previous_gradient);

DerivatorElement PartialDerivative(const ROOT::Math::IBaseFunctionMultiDim *function, const double *x,
const std::vector<ROOT::Fit::ParameterSettings> &parameters,
unsigned int i_component, DerivatorElement previous);
DerivatorElement FastPartialDerivative(const ROOT::Math::IBaseFunctionMultiDim *function,
const std::vector<ROOT::Fit::ParameterSettings> &parameters,
unsigned int i_component, const DerivatorElement &previous);
DerivatorElement operator()(const ROOT::Math::IBaseFunctionMultiDim *function, const double *x,
const std::vector<ROOT::Fit::ParameterSettings> &parameters, unsigned int i_component,
const DerivatorElement &previous);

double GetValue() const { return fVal; }
void SetStepTolerance(double value);
void SetGradTolerance(double value);
void SetNCycles(unsigned int value);
void SetErrorLevel(double value);

double Int2ext(const ROOT::Fit::ParameterSettings &parameter, double val) const;
double Ext2int(const ROOT::Fit::ParameterSettings &parameter, double val) const;
double DInt2Ext(const ROOT::Fit::ParameterSettings &parameter, double val) const;

void SetInitialGradient(const ROOT::Math::IBaseFunctionMultiDim *function,
const std::vector<ROOT::Fit::ParameterSettings> &parameters,
std::vector<DerivatorElement> &gradient);

private:
double fStepTolerance = 0.5;
double fGradTolerance = 0.1;
unsigned int fNCycles = 2;
double fUp = 1;
double fVal = 0;

std::vector<double> fVx, fVxExternal;
double fDfmin;
double fVrysml;

// MODIFIED: Minuit2 determines machine precision in a slightly different way than
// std::numeric_limits<double>::epsilon()). We go with the Minuit2 one.
ROOT::Minuit2::MnMachinePrecision fPrecision;

ROOT::Minuit2::SinParameterTransformation fDoubleLimTrafo;
ROOT::Minuit2::SqrtUpParameterTransformation fUpperLimTrafo;
ROOT::Minuit2::SqrtLowParameterTransformation fLowerLimTrafo;

private:
egpbos marked this conversation as resolved.
Show resolved Hide resolved
bool fAlwaysExactlyMimicMinuit2;

public:
bool AlwaysExactlyMimicMinuit2() const;
void SetAlwaysExactlyMimicMinuit2(bool flag);

private:
std::vector<double> fVxFValCache;
egpbos marked this conversation as resolved.
Show resolved Hide resolved
};

std::ostream &operator<<(std::ostream &out, const DerivatorElement &value);

} // namespace Minuit2
} // namespace ROOT

#endif // ROOT_Minuit2_NumericalDerivator
Loading