BayesianAnalysisToolkit  0.9.3
BCIntegrate.h
Go to the documentation of this file.
1 #ifndef __BCINTEGRATE__H
2 #define __BCINTEGRATE__H
3 
16 /*
17  * Copyright (C) 2007-2013, 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 // ROOT classes
29 class TH1;
30 class TH1D;
31 class TH2D;
32 class TMinuit;
33 class TTree;
34 
40 namespace BCCubaOptions
41 {
42  struct General
43  {
45  double error, prob;
46  General();
47  protected:
48  ~General();
49  };
50 
51  struct Vegas : public General
52  {
54 
55  Vegas();
56  };
57 
58  struct Suave : public General
59  {
60  int nnew;
61  double flatness;
62 
63  Suave();
64  };
65 
66  struct Divonne : public General
67  {
70 
71  Divonne();
72  };
73 
74  struct Cuhre : public General
75  {
76  int key;
77 
78  Cuhre();
79  };
80 };
81 
82 // ---------------------------------------------------------
83 
84 class BCIntegrate : public BCEngineMCMC
85 {
86 
87 public:
88 
101 
111 
121 
125 
129 
137  typedef void (BCIntegrate::*tRandomizer)(std::vector<double> &) const;
138 
141  typedef double (BCIntegrate::*tEvaluator)(std::vector<double> &, const std::vector<double> &, bool &);
142 
145  typedef void (*tIntegralUpdater)(const std::vector<double> &, const int &, double &, double &);
146 
155  BCIntegrate();
156 
159  BCIntegrate(const BCIntegrate & bcintegrate);
160 
163  virtual ~BCIntegrate();
164 
171  BCIntegrate & operator = (const BCIntegrate & bcintegrate);
172 
179  int ReadMarginalizedFromFile(const char * file);
180 
188  BCH1D * GetMarginalized(const BCParameter * parameter);
189 
197  BCH1D * GetMarginalized(const char * name)
198  { return GetMarginalized(fParameters.Index(name)); }
199 
207  BCH1D * GetMarginalized(unsigned index);
208 
218  BCH2D * GetMarginalized(const BCParameter * parameter1, const BCParameter * parameter2);
219 
228  BCH2D * GetMarginalized(const char * name1, const char * name2)
229  { return GetMarginalized(fParameters.Index(name1), fParameters.Index(name2)); }
230 
239  BCH2D * GetMarginalized(unsigned index1, unsigned index2);
240 
243  int PrintAllMarginalized1D(const char * filebase);
244  int PrintAllMarginalized2D(const char * filebase);
245  int PrintAllMarginalized(const char * file, std::string options1d="BTsiB3CS1D0pdf0Lmeanmode", std::string options2d="BTfB3CS1meangmode", unsigned int hdiv=1, unsigned int ndiv=1);
246 
247 
250  double GetIntegral() const
251  { return fIntegral; }
252 
256  { return fOptimizationMethodCurrent; }
257 
261  { return fIntegrationMethodCurrent; }
262 
267 
271  { return fSASchedule; }
272 
276  void GetRandomVectorUnitHypercube(std::vector<double> &x) const;
277 
281  void GetRandomVectorInParameterSpace(std::vector<double> &x) const;
282 
288  double GetRandomPoint(std::vector<double> &x);
289 
292  int GetNIterationsMin() const
293  { return fNIterationsMin; }
294 
297  int GetNIterationsMax() const
298  { return fNIterationsMax; }
299 
303  { return fNIterationsPrecisionCheck; }
304 
308  { return fNIterationsOutput; }
309 
312  int GetNIterations() const
313  { return fNIterations; }
314 
317  double GetRelativePrecision() const
318  { return fRelativePrecision; }
319 
322  double GetAbsolutePrecision() const
323  { return fAbsolutePrecision; }
324 
328  { return fCubaIntegrationMethod; }
329 
334  { return fCubaVegasOptions; }
335 
337  { return fCubaSuaveOptions; }
338 
340  { return fCubaDivonneOptions; }
341 
343  { return fCubaCuhreOptions; }
344 
352  BCH1D* GetSlice(const BCParameter* parameter, const std::vector<double> parameters = std::vector<double>(0), int bins=0, bool flag_norm=true);
353 
361  BCH1D* GetSlice(const char * name, const std::vector<double> parameters = std::vector<double>(0), int nbins=0, bool flag_norm=true)
362  { return GetSlice(GetParameter(name), parameters, nbins, flag_norm); }
363 
372  BCH2D* GetSlice(const BCParameter* parameter1, const BCParameter* parameter2, const std::vector<double> parameters = std::vector<double>(0), int bins=0, bool flag_norm=true);
373 
382  BCH2D* GetSlice(const char* name1, const char* name2, const std::vector<double> parameters = std::vector<double>(0), int nbins=0, bool flag_norm=true);
383 
392  BCH2D* GetSlice(unsigned index1, unsigned index2, const std::vector<double> parameters = std::vector<double>(0), int nbins=0, bool flag_norm=true);
393 
396  double GetError() const
397  { return fError; }
398 
401  TMinuit * GetMinuit();
402 
405  int GetMinuitErrorFlag() const
406  { return fMinuitErrorFlag; }
407 
410  double GetSAT0() const
411  { return fSAT0; }
412 
415  double GetSATmin() const
416  { return fSATmin; }
417 
423  double GetBestFitParameter(unsigned index) const;
424 
430  double GetBestFitParameterError(unsigned index) const;
431 
435  double GetLogMaximum()
436  { return fLogMaximum; };
437 
442  const std::vector<double> & GetBestFitParameters() const
443  { return fBestFitParameters; }
444 
447  const std::vector<double> & GetBestFitParameterErrors() const
448  { return fBestFitParameterErrors; }
449 
457  void SetMinuitArlist(double * arglist)
458  { fMinuitArglist[0] = arglist[0];
459  fMinuitArglist[1] = arglist[1]; }
460 
464  { fFlagIgnorePrevOptimization = flag; }
465 
469  { fOptimizationMethodCurrent = method; }
470 
474 
478  { fMarginalizationMethodCurrent = method; }
479 
483  { fSASchedule = schedule; }
484 
487  void SetNIterationsMin(int niterations)
488  { fNIterationsMin = niterations; }
489 
492  void SetNIterationsMax(int niterations)
493  { fNIterationsMax = niterations; }
494 
497  void SetNIterationsPrecisionCheck(int niterations)
498  { fNIterationsPrecisionCheck = niterations; }
499 
502  void SetNIterationsOutput(int niterations)
503  { fNIterationsOutput = niterations; }
504 
508  void SetRelativePrecision(double relprecision)
509  { fRelativePrecision = relprecision; }
510 
513  void SetAbsolutePrecision(double absprecision)
514  { fAbsolutePrecision = absprecision; }
515 
519 
523  void SetCubaOptions(const BCCubaOptions::Vegas & options)
524  { fCubaVegasOptions = options; }
525 
526  void SetCubaOptions(const BCCubaOptions::Suave & options)
527  { fCubaSuaveOptions = options; }
528 
530  { fCubaDivonneOptions = options; }
531 
532  void SetCubaOptions(const BCCubaOptions::Cuhre & options)
533  { fCubaCuhreOptions = options; }
534 
537  void SetSAT0(double T0)
538  { fSAT0 = T0; }
539 
542  void SetSATmin(double Tmin)
543  { fSATmin = Tmin; }
544 
545  void SetFlagWriteSAToFile(bool flag)
546  { fFlagWriteSAToFile = flag; }
547 
550  TTree * GetSATree()
551  { return fTreeSA; }
552 
555  void InitializeSATree();
556 
567  virtual double Eval(const std::vector<double> &x);
568 
574  virtual double LogEval(const std::vector<double> &x);
575 
578  double Normalize()
579  { return Integrate(); };
580 
585  double Integrate(BCIntegrationMethod intmethod);
586 
591  double Integrate();
592 
602  double Integrate(BCIntegrationMethod type, tRandomizer randomizer, tEvaluator evaluator, tIntegralUpdater updater, std::vector<double> &sums);
603 
604  // todo document
605  double EvaluatorMC(std::vector<double> &sums, const std::vector<double> &point, bool &accepted);
606  static void IntegralUpdaterMC(const std::vector<double> &sums, const int &nIterations, double &integral, double &absprecision);
607 
615  static int CubaIntegrand(const int * ndim, const double xx[], const int * ncomp, double ff[], void *userdata);
616 #if 0
617  TH1D * Marginalize(BCIntegrationMethod type, unsigned index);
618 
619  TH2D * Marginalize(BCIntegrationMethod type, unsigned index1, unsigned index2);
620 
621  bool Marginalize(TH1* hist, BCIntegrationMethod type, const std::vector<unsigned> &index);
622 #endif
623 
628  int MarginalizeAll();
629 
636  int MarginalizeAll(BCMarginalizationMethod margmethod);
637 
641  virtual void MarginalizePreprocess()
642  {};
643 
647  virtual void MarginalizePostprocess()
648  {};
649 
652  void SAInitialize();
653 
665  std::vector<double> FindMode(std::vector<double> start = std::vector<double>());
666 
673  std::vector<double> FindMode(BCIntegrate::BCOptimizationMethod optmethod, std::vector<double> start = std::vector<double>());
674 
680  double SATemperature(double t);
681 
686  double SATemperatureBoltzmann(double t);
687 
692  double SATemperatureCauchy(double t);
693 
699  virtual double SATemperatureCustom(double t);
700 
708  std::vector<double> GetProposalPointSA(const std::vector<double> &x, int t);
709 
716  std::vector<double> GetProposalPointSABoltzmann(const std::vector<double> &x, int t);
717 
724  std::vector<double> GetProposalPointSACauchy(const std::vector<double> &x, int t);
725 
733  virtual std::vector<double> GetProposalPointSACustom(const std::vector<double> &x, int t);
734 
739  std::vector<double> SAHelperGetRandomPointOnHypersphere();
740 
744  double SAHelperGetRadialCauchy();
745 
749  double SAHelperSinusToNIntegral(int dim, double theta);
750 
751 
752  static void FCNLikelihood(int &npar, double * grad, double &fval, double * par, int flag);
753 
756  virtual void ResetResults();
757 
762  std::string DumpIntegrationMethod(BCIntegrationMethod type);
763 
769 
775 
781 
787 
793 
799 
805 
811 
816  std::string DumpCubaIntegrationMethod(BCCubaMethod type);
817 
823 
826  void SetBestFitParameters(const std::vector<double> &x)
827  { fBestFitParameters = x; }
828 
833  void SetBestFitParameters(const std::vector<double> &x, const double &new_value, double &old_value);
834 
838  unsigned GetNIntegrationVariables();
839 
844 
848 
851  bool CheckMarginalizationIndices(TH1* hist, const std::vector<unsigned> &index);
852 
855 protected:
856 
859  int fID;
860 
863  TMinuit * fMinuit;
864 
865  double fMinuitArglist[2];
867 
871 
874  double fSAT0;
875 
878  double fSATmin;
879 
882  TTree * fTreeSA;
883 
887 
890  double fSALogProb;
891  std::vector<double> fSAx;
892 
895  std::vector<BCH1D*> fMarginalized1D;
896 
899  std::vector<BCH2D*> fMarginalized2D;
900 
901  protected:
904  unsigned IntegrationOutputFrequency() const;
905 
912  void LogOutputAtIntegrationStatusUpdate(BCIntegrationMethod type, double integral, double absprecision, int nIterations);
913  void LogOutputAtEndOfIntegration(double integral, double absprecision, double relprecision, int nIterations);
914 
918  void Copy(const BCIntegrate & bcintegrate);
919 
923 
924 private:
925 
936  std::vector<double> FindModeMinuit(std::vector<double> &mode, std::vector<double> &errors, std::vector<double> start = std::vector<double>(0), int printlevel = 1);
937 
945  std::vector<double> FindModeMCMC(std::vector<double> &mode, std::vector<double> &errors);
946 
957  std::vector<double> FindModeSA(std::vector<double> &mode, std::vector<double> &errors, std::vector<double> start = std::vector<double>(0));
958 
962  double IntegrateCuba()
964 
969  double IntegrateCuba(BCCubaMethod cubatype);
970 
974  double IntegrateSlice();
975 
979 
984 
988 
992 
996 
1000 
1004 
1008 
1012 
1016 
1020 
1024 
1027  std::vector<double> fBestFitParameters;
1028 
1031  std::vector<double> fBestFitParameterErrors;
1032 
1035  double fLogMaximum;
1036 
1039  double fIntegral;
1040 
1043 
1046 
1049  double fError;
1050 
1057 
1058 };
1059 
1060 // ---------------------------------------------------------
1061 
1062 #endif
1063 
1064