BCIntegrate.h
1 #ifndef __BCINTEGRATE__H
2 #define __BCINTEGRATE__H
3 
16 /*
17  * Copyright (C) 2007-2018, the BAT core developer team
18  * All rights reserved.
19  *
20  * For the licensing terms see doc/COPYING.
21  * For documentation see http://mpp.mpg.de/bat
22  */
23 
24 // ---------------------------------------------------------
25 
26 #include "BCEngineMCMC.h"
27 
28 #include <TMinuitMinimizer.h>
29 
30 #include <string>
31 
32 // forward declarations
33 class BCIntegrate;
34 
35 class TFile;
36 class TH1;
37 class TH2;
38 class TTree;
39 
42 namespace BCCubaOptions
43 {
44 
51 struct General {
53  int ncomp, flags, nregions, neval, fail;
54  double error, prob;
55 
56  General();
58 };
59 
62 struct Vegas : public General {
64  int nstart, nincrease, nbatch, gridno;
65 
66  Vegas();
68 };
69 
72 struct Suave : public General {
74  int nmin, nnew;
75  double flatness;
76 
77  Suave();
79 };
80 
83 struct Divonne : public General {
85  int key1, key2, key3, maxpass;
86  double border, maxchisq, mindeviation;
87 
88  Divonne();
90 };
91 
94 struct Cuhre : public General {
96  int key;
97 
98  Cuhre();
100 };
101 }
102 
103 namespace BCMinimizer
104 {
105 
108 class Adapter : public ROOT::Math::IMultiGenFunction
109 {
110 public:
112  Adapter(BCIntegrate& m);
113  virtual unsigned int NDim() const;
114  virtual ROOT::Math::IMultiGenFunction* Clone() const;
115 
116  BCIntegrate* m;
117  mutable std::vector<double> par;
118 private:
119  virtual double DoEval (const double* x) const;
121 };
122 
126 class Wrapper
127 {
128 public:
130  Wrapper(BCIntegrate& m);
131  void Init();
132  void Init(const std::vector<double>& start, int printlevel);
133  void Reset(BCIntegrate& m);
134 
135  TMinuitMinimizer min;
136  Adapter adapt;
138 };
139 }
140 
141 // ---------------------------------------------------------
142 
143 class BCIntegrate : public BCEngineMCMC
144 {
145 
146 public:
147 
159  NOptMethods
160  };
161 
171  NIntMethods
172  };
173 
182  NMargMethods
183  };
184 
191  NSAMethods
192  };
193 
202  NCubaMethods
203  };
204 
212  typedef void (BCIntegrate::*tRandomizer)(std::vector<double>&) const;
213 
216  typedef double (BCIntegrate::*tEvaluator)(std::vector<double>&, const std::vector<double>&, bool&);
217 
220  typedef void (*tIntegralUpdater)(const std::vector<double>&, const int&, double&, double&);
221 
230  BCIntegrate(const std::string& name = "model");
231 
238  BCIntegrate(const std::string& filename, const std::string& name, bool loadObservables = true);
239 
242  BCIntegrate(const BCIntegrate& other);
243 
246  BCIntegrate& operator=(const BCIntegrate&);
247 
250  virtual ~BCIntegrate() {};
251 
259  double GetIntegral() const
260  { return fIntegral; }
261 
265  { return fOptimizationMethodCurrent; }
266 
270  { return fIntegrationMethodCurrent; }
271 
275  { return fMarginalizationMethodCurrent; }
276 
280  { return fSASchedule; }
281 
285  void GetRandomVectorInParameterSpace(std::vector<double>& x) const;
286 
292  double GetRandomPoint(std::vector<double>& x);
293 
296  int GetNIterationsMin() const
297  { return fNIterationsMin; }
298 
301  int GetNIterationsMax() const
302  { return fNIterationsMax; }
303 
307  { return fNIterationsPrecisionCheck; }
308 
311  int GetNIterations() const
312  { return fNIterations; }
313 
316  double GetRelativePrecision() const
317  { return fRelativePrecision; }
318 
321  double GetAbsolutePrecision() const
322  { return fAbsolutePrecision; }
323 
327  { return fCubaIntegrationMethod; }
328 
332  { return fCubaVegasOptions; }
333 
337  { return fCubaSuaveOptions; }
338 
342  { return fCubaDivonneOptions; }
343 
347  { return fCubaCuhreOptions; }
348 
358  TH1* GetSlice(std::vector<unsigned> indices, unsigned& nIterations, double& log_max_val, const std::vector<double> parameters = std::vector<double>(0), int nbins = 0, bool normalize = true);
359 
369  TH1* GetSlice(const std::string& name, unsigned& nIterations, double& log_max_val, const std::vector<double> parameters = std::vector<double>(0), int nbins = 0, bool normalize = true)
370  { return GetSlice(fParameters.Index(name), nIterations, log_max_val, parameters, nbins, normalize); }
371 
381  TH1* GetSlice(unsigned index, unsigned& nIterations, double& log_max_val, const std::vector<double> parameters = std::vector<double>(0), int nbins = 0, bool normalize = true)
382  { return GetSlice(std::vector<unsigned>(1, index), nIterations, log_max_val, parameters, nbins, normalize); }
383 
394  TH2* GetSlice(const std::string& name1, const std::string& name2, unsigned& nIterations, double& log_max_val, const std::vector<double> parameters = std::vector<double>(0), int nbins = 0, bool normalize = true)
395  { return GetSlice(fParameters.Index(name1), fParameters.Index(name2), nIterations, log_max_val, parameters, nbins, normalize); }
396 
407  TH2* GetSlice(unsigned index1, unsigned index2, unsigned& nIterations, double& log_max_val, const std::vector<double> parameters = std::vector<double>(0), int nbins = 0, bool normalize = true);
408 
411  double GetError() const
412  { return fError; }
413 
418  TMinuitMinimizer& GetMinuit()
419  { return fMinimizer.min; }
420 
423  double GetSAT0() const
424  { return fSAT0; }
425 
428  double GetSATmin() const
429  { return fSATmin; }
430 
433  virtual const std::vector<double>& GetBestFitParameters() const;
434 
437  const std::vector<double>& GetBestFitParameterErrors() const;
438 
442  double GetLogMaximum() const
443  { return fLogMaximum; };
444 
452  { fFlagIgnorePrevOptimization = flag; }
453 
457  { fOptimizationMethodCurrent = method; }
458 
461  void SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method);
462 
466  { fMarginalizationMethodCurrent = method; }
467 
471  { fSASchedule = schedule; }
472 
475  void SetNIterationsMin(int niterations)
476  { fNIterationsMin = niterations; }
477 
480  void SetNIterationsMax(int niterations)
481  { fNIterationsMax = niterations; }
482 
485  void SetNIterationsPrecisionCheck(int niterations)
486  { fNIterationsPrecisionCheck = niterations; }
487 
491  void SetRelativePrecision(double relprecision)
492  { fRelativePrecision = relprecision; }
493 
496  void SetAbsolutePrecision(double absprecision)
497  { fAbsolutePrecision = absprecision; }
498 
501  void SetCubaIntegrationMethod(BCCubaMethod type);
502 
507  { fCubaVegasOptions = options; }
508 
513  { fCubaSuaveOptions = options; }
514 
519  { fCubaDivonneOptions = options; }
520 
525  { fCubaCuhreOptions = options; }
526 
530  void SetSAT0(double T0)
531  { fSAT0 = T0; }
532 
536  void SetSATmin(double Tmin)
537  { fSATmin = Tmin; }
548  virtual double Eval(const std::vector<double>& x) = 0;
549 
555  virtual double LogEval(const std::vector<double>& x)
556  { return log(Eval(x)); }
557 
560  double Normalize()
561  { return Integrate(); };
562 
567  double Integrate(BCIntegrationMethod intmethod);
568 
572  double Integrate();
573 
582  double Integrate(BCIntegrationMethod type, tRandomizer randomizer, tEvaluator evaluator, tIntegralUpdater updater, std::vector<double>& sums);
583 
586  double EvaluatorMC(std::vector<double>& sums, const std::vector<double>& point, bool& accepted);
587 
590  static void IntegralUpdaterMC(const std::vector<double>& sums, const int& nIterations, double& integral, double& absprecision);
591 
597  int MarginalizeAll();
598 
605  int MarginalizeAll(BCMarginalizationMethod margmethod);
606 
610  virtual void MarginalizePreprocess()
611  {};
612 
616  virtual void MarginalizePostprocess()
617  {};
618 
630  std::vector<double> FindMode(std::vector<double> start = std::vector<double>());
631 
638  std::vector<double> FindMode(BCIntegrate::BCOptimizationMethod optmethod, std::vector<double> start = std::vector<double>());
639 
645  double SATemperature(double t) const;
646 
651  double SATemperatureBoltzmann(double t) const;
652 
657  double SATemperatureCauchy(double t) const;
658 
664  virtual double SATemperatureCustom(double t) const;
665 
673  std::vector<double> GetProposalPointSA(const std::vector<double>& x, int t) const;
674 
681  std::vector<double> GetProposalPointSABoltzmann(const std::vector<double>& x, int t) const;
682 
689  std::vector<double> GetProposalPointSACauchy(const std::vector<double>& x, int t) const;
690 
698  virtual std::vector<double> GetProposalPointSACustom(const std::vector<double>& x, int t) const;
699 
704  std::vector<double> SAHelperGetRandomPointOnHypersphere() const;
705 
709  double SAHelperGetRadialCauchy() const;
710 
714  double SAHelperSinusToNIntegral(int dim, double theta) const;
715 
718  virtual void ResetResults();
719 
724  std::string DumpIntegrationMethod(BCIntegrationMethod type) const;
725 
729  std::string DumpCurrentIntegrationMethod() const
730  { return DumpIntegrationMethod(fIntegrationMethodCurrent); }
731 
735  std::string DumpUsedIntegrationMethod() const
736  { return DumpIntegrationMethod(fIntegrationMethodUsed); }
737 
742  std::string DumpMarginalizationMethod(BCMarginalizationMethod type) const;
743 
748  { return DumpMarginalizationMethod(fMarginalizationMethodCurrent); }
749 
753  std::string DumpUsedMarginalizationMethod() const
754  { return DumpMarginalizationMethod(fMarginalizationMethodUsed); }
755 
760  std::string DumpOptimizationMethod(BCOptimizationMethod type) const;
761 
765  std::string DumpCurrentOptimizationMethod() const
766  { return DumpOptimizationMethod(fOptimizationMethodCurrent); }
767 
771  std::string DumpUsedOptimizationMethod() const
772  { return DumpOptimizationMethod(fOptimizationMethodUsed); }
773 
778  std::string DumpCubaIntegrationMethod(BCCubaMethod type) const;
779 
783  std::string DumpCubaIntegrationMethod() const
784  { return DumpCubaIntegrationMethod(fCubaIntegrationMethod); }
785 
789  void SetBestFitParameters(const std::vector<double>& x);
790 
795  void SetBestFitParameters(const std::vector<double>& x, const double& new_value, double& old_value);
796 
800  bool CheckMarginalizationAvailability(BCMarginalizationMethod type);
801 
804  bool CheckMarginalizationIndices(TH1* hist, const std::vector<unsigned>& index);
805 
814  double IntegrateLaplace();
815 
818 protected:
821  unsigned IntegrationOutputFrequency() const;
822 
825  virtual void PrintBestFitSummary() const;
826 
830  virtual std::string GetBestFitSummary(unsigned i) const;
831 
834  virtual void PrintMarginalizationSummary() const;
835 
839 
842  double fSAT0;
843 
846  double fSATmin;
847 
851 
855 
858  double fSALogProb;
859 
862  std::vector<double> fSAx;
863 
866  void LogOutputAtStartOfIntegration(BCIntegrationMethod type, BCCubaMethod cubatype);
867 
870  void LogOutputAtIntegrationStatusUpdate(BCIntegrationMethod type, double integral, double absprecision, int nIterations);
871 
874  void LogOutputAtEndOfIntegration(double integral, double absprecision, double relprecision, int nIterations);
875 
879 
880 private:
881 
883  BCMinimizer::Wrapper fMinimizer;
884 
895  std::vector<double> FindModeMinuit(std::vector<double>& mode, std::vector<double>& errors, std::vector<double> start = std::vector<double>(0), int printlevel = -1);
896 
904  std::vector<double> FindModeMCMC(std::vector<double>& mode, std::vector<double>& errors);
905 
916  std::vector<double> FindModeSA(std::vector<double>& mode, std::vector<double>& errors, std::vector<double> start = std::vector<double>(0));
917 
921  double IntegrateCuba()
922  { return IntegrateCuba(fCubaIntegrationMethod); }
923 
928  double IntegrateCuba(BCCubaMethod cubatype);
929 
937  static int CubaIntegrand(const int* ndim, const double xx[], const int* ncomp, double ff[], void* userdata);
938 
942  double IntegrateSlice();
943 
946  BCIntegrate::BCOptimizationMethod fOptimizationMethodCurrent;
947 
951  BCIntegrate::BCOptimizationMethod fOptimizationMethodUsed;
952 
955  BCIntegrate::BCIntegrationMethod fIntegrationMethodCurrent;
956 
959  BCIntegrate::BCIntegrationMethod fIntegrationMethodUsed;
960 
963  BCIntegrate::BCMarginalizationMethod fMarginalizationMethodCurrent;
964 
967  BCIntegrate::BCMarginalizationMethod fMarginalizationMethodUsed;
968 
971  BCIntegrate::BCSASchedule fSASchedule;
972 
975  unsigned fNIterationsMin;
976 
979  unsigned fNIterationsMax;
980 
983  unsigned fNIterationsPrecisionCheck;
984 
987  int fNIterations;
988 
991  std::vector<double> fBestFitParameters;
992 
995  std::vector<double> fBestFitParameterErrors;
996 
999  double fLogMaximum;
1000 
1003  double fIntegral;
1004 
1006  double fRelativePrecision;
1007 
1009  double fAbsolutePrecision;
1010 
1013  double fError;
1014 
1016  BCCubaMethod fCubaIntegrationMethod;
1017 
1018  BCCubaOptions::Vegas fCubaVegasOptions;
1019  BCCubaOptions::Suave fCubaSuaveOptions;
1020  BCCubaOptions::Divonne fCubaDivonneOptions;
1021  BCCubaOptions::Cuhre fCubaCuhreOptions;
1022 };
1023 
1024 #endif
BCIntegrate::BCMarginalizationMethod GetMarginalizationMethod() const
Definition: BCIntegrate.h:274
Simulated annealing.
Definition: BCIntegrate.h:155
std::string DumpCubaIntegrationMethod() const
Return string with the name for the currently set Cuba integration type.
Definition: BCIntegrate.h:783
BCCubaMethod GetCubaIntegrationMethod() const
Definition: BCIntegrate.h:326
Use CUBA interface.
Definition: BCIntegrate.h:167
double GetIntegral() const
Definition: BCIntegrate.h:259
double GetLogMaximum() const
Returns the posterior at the mode.
Definition: BCIntegrate.h:442
TMinuitMinimizer & GetMinuit()
Definition: BCIntegrate.h:418
virtual double LogEval(const std::vector< double > &x)
Evaluate the natural logarithm of the Eval function.
Definition: BCIntegrate.h:555
Sample mean method.
Definition: BCIntegrate.h:166
bool fFlagIgnorePrevOptimization
Flag for ignoring older results of optimization.
Definition: BCIntegrate.h:838
void SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method)
Definition: BCIntegrate.h:465
Base struct to hold options shared by all CUBA algorithms.
Definition: BCIntegrate.h:51
void SetNIterationsMin(int niterations)
Definition: BCIntegrate.h:475
Wrapper to approximate RAII for TMinuitMinimizer that is not copyable.
Definition: BCIntegrate.h:126
int GetNIterations() const
Definition: BCIntegrate.h:311
void SetNIterationsPrecisionCheck(int niterations)
Definition: BCIntegrate.h:485
double Normalize()
Performs integration.
Definition: BCIntegrate.h:560
std::string DumpCurrentOptimizationMethod() const
Return string with the name for the currently set optimization type.
Definition: BCIntegrate.h:765
void SetCubaOptions(const BCCubaOptions::Vegas &options)
Set options for CUBA&#39;s Vegas.
Definition: BCIntegrate.h:506
std::string DumpUsedOptimizationMethod() const
Return string with the name for the optimization type used to find the current mode.
Definition: BCIntegrate.h:771
BCIntegrate::BCIntegrationMethod GetIntegrationMethod() const
Definition: BCIntegrate.h:269
Custom scheduler.
Definition: BCIntegrate.h:190
Hold Suave specific options.
Definition: BCIntegrate.h:72
A class for handling numerical operations for models.
Definition: BCIntegrate.h:143
Metropolis Hastings.
Definition: BCIntegrate.h:156
virtual void MarginalizePreprocess()
Method executed for before marginalization.
Definition: BCIntegrate.h:610
virtual void MarginalizePostprocess()
Method executed after marginalization.
Definition: BCIntegrate.h:616
No marginalization method set.
Definition: BCIntegrate.h:177
double GetError() const
Definition: BCIntegrate.h:411
double fSALogProb
Log probability of current simulated annealing iteration.
Definition: BCIntegrate.h:858
void SetCubaOptions(const BCCubaOptions::Divonne &options)
Set options for CUBA&#39;s Divonne.
Definition: BCIntegrate.h:518
int GetNIterationsMin() const
Definition: BCIntegrate.h:296
std::string DumpUsedMarginalizationMethod() const
Return string with the name for the marginalization type used.
Definition: BCIntegrate.h:753
Adapter to call Minuit on a user-defined function.
Definition: BCIntegrate.h:108
std::vector< double > fSAx
Current simulated annealing parameter point.
Definition: BCIntegrate.h:862
void SetSAT0(double T0)
Set starting temperature for Simulated Annealing.
Definition: BCIntegrate.h:530
void SetCubaOptions(const BCCubaOptions::Cuhre &options)
Set options for CUBA&#39;s Cuhre.
Definition: BCIntegrate.h:524
void SetCubaOptions(const BCCubaOptions::Suave &options)
Set options for CUBA&#39;s Suave.
Definition: BCIntegrate.h:512
int GetNIterationsPrecisionCheck() const
Definition: BCIntegrate.h:306
BCSASchedule
An enumerator for the Simulated Annealing schedule.
Definition: BCIntegrate.h:187
const BCCubaOptions::Divonne & GetCubaDivonneOptions() const
Definition: BCIntegrate.h:341
void SetFlagIgnorePrevOptimization(bool flag)
Definition: BCIntegrate.h:451
void SetAbsolutePrecision(double absprecision)
Set absolute precision of the numerical integation.
Definition: BCIntegrate.h:496
double GetRelativePrecision() const
Definition: BCIntegrate.h:316
BCOptimizationMethod
An enumerator for the mode finding algorithm.
Definition: BCIntegrate.h:153
Cauchy scheduler.
Definition: BCIntegrate.h:188
Laplace approximation.
Definition: BCIntegrate.h:169
BCCubaMethod
An enumerator for Cuba integration methods.
Definition: BCIntegrate.h:196
Hold Divonne specific options.
Definition: BCIntegrate.h:83
An engine class for Markov Chain Monte Carlo.
Definition: BCEngineMCMC.h:55
BCIntegrationMethod
An enumerator for integration algorithms.
Definition: BCIntegrate.h:164
std::string DumpUsedIntegrationMethod() const
Return string with the name for the previously used integration type.
Definition: BCIntegrate.h:735
void SetSASchedule(BCIntegrate::BCSASchedule schedule)
Definition: BCIntegrate.h:470
std::string DumpCurrentIntegrationMethod() const
Return string with the name for the currently set integration type.
Definition: BCIntegrate.h:729
BCIntegrate::BCOptimizationMethod GetOptimizationMethod() const
Definition: BCIntegrate.h:264
double fSATmin
Minimal/Threshold temperature for Simulated Annealing.
Definition: BCIntegrate.h:846
const BCCubaOptions::Suave & GetCubaSuaveOptions() const
Definition: BCIntegrate.h:336
Integration by gridding of parameter space.
Definition: BCIntegrate.h:168
double fSATemperature
Current temperature of simulated annealing algorithm.
Definition: BCIntegrate.h:854
Marginalization by gridding of parameter space.
Definition: BCIntegrate.h:180
void SetRelativePrecision(double relprecision)
Definition: BCIntegrate.h:491
TH1 * GetSlice(unsigned index, unsigned &nIterations, double &log_max_val, const std::vector< double > parameters=std::vector< double >(0), int nbins=0, bool normalize=true)
Returns a one-dimensional slice of the pdf at the point and along a specific direction.
Definition: BCIntegrate.h:381
Hold Cuhre specific options.
Definition: BCIntegrate.h:94
Hold Vegas specific options.
Definition: BCIntegrate.h:62
No optimization method set.
Definition: BCIntegrate.h:154
No integration method set.
Definition: BCIntegrate.h:165
double GetAbsolutePrecision() const
Definition: BCIntegrate.h:321
TH2 * GetSlice(const std::string &name1, const std::string &name2, unsigned &nIterations, double &log_max_val, const std::vector< double > parameters=std::vector< double >(0), int nbins=0, bool normalize=true)
Returns a two-dimensional slice of the pdf at the point and along two specified directions.
Definition: BCIntegrate.h:394
std::string DumpCurrentMarginalizationMethod() const
Return string with the name for the currently set marginalization type.
Definition: BCIntegrate.h:747
const BCCubaOptions::Cuhre & GetCubaCuhreOptions() const
Definition: BCIntegrate.h:346
double fSAT0
Starting temperature for Simulated Annealing.
Definition: BCIntegrate.h:842
double GetSATmin() const
Returns the Simulated Annealing threshhold temperature.
Definition: BCIntegrate.h:428
const BCCubaOptions::Vegas & GetCubaVegasOptions() const
Definition: BCIntegrate.h:331
Boltzman scheduler.
Definition: BCIntegrate.h:189
Sample mean Monte Carlo.
Definition: BCIntegrate.h:179
Metropolis Hastings.
Definition: BCIntegrate.h:178
void SetSATmin(double Tmin)
Set threshold temperature for Simulated Annealing.
Definition: BCIntegrate.h:536
int fSANIterations
Number of iterations for simualted annealing.
Definition: BCIntegrate.h:850
ROOT&#39;s Minuit.
Definition: BCIntegrate.h:157
void SetNIterationsMax(int niterations)
Definition: BCIntegrate.h:480
BCIntegrate::BCSASchedule GetSASchedule() const
Definition: BCIntegrate.h:279
TH1 * GetSlice(const std::string &name, unsigned &nIterations, double &log_max_val, const std::vector< double > parameters=std::vector< double >(0), int nbins=0, bool normalize=true)
Returns a one-dimensional slice of the pdf at the point and along a specific direction.
Definition: BCIntegrate.h:369
BCMarginalizationMethod
An enumerator for marginalization algorithms.
Definition: BCIntegrate.h:176
virtual ~BCIntegrate()
Destructor.
Definition: BCIntegrate.h:250
void SetOptimizationMethod(BCIntegrate::BCOptimizationMethod method)
Definition: BCIntegrate.h:456
bool fFlagMarginalized
flag indicating if the model was marginalized
Definition: BCIntegrate.h:878
double GetSAT0() const
Returns the Simulated Annealing starting temperature.
Definition: BCIntegrate.h:423
int GetNIterationsMax() const
Definition: BCIntegrate.h:301