BayesianAnalysisToolkit  0.9.3
BCEngineMCMC.h
Go to the documentation of this file.
1 #ifndef __BCENGINEMCMC__H
2 #define __BCENGINEMCMC__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 "BCParameterSet.h"
27 
28 #include <vector>
29 
30 class BCH1D;
31 class BCH2D;
32 
33 // ROOT classes
34 class TH1D;
35 class TH2D;
36 class TTree;
37 class TRandom3;
38 
39 // ---------------------------------------------------------
40 
42 {
43 
44  public:
45 
51 
58  BCEngineMCMC();
59 
62  BCEngineMCMC(const BCEngineMCMC & enginemcmc);
63 
66  virtual ~BCEngineMCMC();
67 
74  BCEngineMCMC & operator = (const BCEngineMCMC & engineMCMC);
75 
82  unsigned MCMCGetNChains() const
83  { return fMCMCNChains; }
84 
87  unsigned MCMCGetNLag() const
88  { return fMCMCNLag; }
89 
92  const std::vector<unsigned> & MCMCGetNIterations() const
93  { return fMCMCNIterations; }
94 
97  unsigned MCMCGetCurrentIteration() const
98  { return fMCMCCurrentIteration; }
99 
102  unsigned MCMCGetCurrentChain() const
103  { return fMCMCCurrentChain; }
104 
110 
114  { return fMCMCFlagConvergenceGlobal; }
115 
118  unsigned MCMCGetNIterationsMax() const
119  { return fMCMCNIterationsMax; }
120 
123  unsigned MCMCGetNIterationsRun() const
124  { return fMCMCNIterationsRun; }
125 
129  { return fMCMCNIterationsPreRunMin; }
130 
133  unsigned MCMCGetNIterationsUpdate() const
134  { return fMCMCNIterationsUpdate; }
135 
139  { return fMCMCNIterationsUpdateMax; }
140 
143  std::vector<int> MCMCGetNTrialsTrue() const
144  { return fMCMCNTrialsTrue; }
145 
148  int MCMCGetNTrials() const
149  { return fMCMCNTrials; }
150 
154  const std::vector<double> & MCMCGetprobMean() const
155  { return fMCMCprobMean; }
156 
160  const std::vector<double> & MCMCGetVariance() const
161  { return fMCMCprobVar; }
162 
165  const std::vector<double> & MCMCGetTrialFunctionScaleFactor() const
167 
171  std::vector<double> MCMCGetTrialFunctionScaleFactor(unsigned ichain) const;
172 
177  double MCMCGetTrialFunctionScaleFactor(unsigned ichain, unsigned ipar);
178 
181  const std::vector<double> & MCMCGetx() const
182  { return fMCMCx; }
183 
187  std::vector<double> MCMCGetx(unsigned ichain);
188 
193  double MCMCGetx(unsigned ichain, unsigned ipar) const;
194 
197  const std::vector<double> & MCMCGetLogProbx() const
198  { return fMCMCprob; }
199 
203  double MCMCGetLogProbx(unsigned ichain);
204 
207  int MCMCGetPhase() const
208  { return fMCMCPhase; }
209 
212  const std::vector<double> & MCMCGetMaximumPoints() const
213  { return fMCMCxMax; }
214 
218  std::vector<double> MCMCGetMaximumPoint(unsigned i) const;
219 
222  const std::vector<double> & MCMCGetMaximumLogProb() const
223  { return fMCMCprobMax; }
224 
228  { return fMCMCFlagInitialPosition; }
229 
232  double MCMCGetRValueCriterion() const
233  { return fMCMCRValueCriterion; }
234 
239 
242  double MCMCGetRValue() const
243  { return fMCMCRValue; }
244 
248  double MCMCGetRValueParameters(unsigned i)
249  { return fMCMCRValueParameters.at(i); }
250 
252  bool MCMCGetRValueStrict() const
253  { return fMCMCRValueUseStrict; }
254 
257  bool MCMCGetFlagRun() const
258  { return fMCMCFlagRun; }
259 
264  TTree * MCMCGetMarkovChainTree(unsigned i)
265  { if (i < fMCMCTrees.size())
266  return fMCMCTrees.at(i);
267  else
268  return 0; }
269 
274  BCH1D * MCMCGetH1Marginalized(unsigned i);
275 
281  BCH2D * MCMCGetH2Marginalized(unsigned i, unsigned j);
282 
286  BCParameter * GetParameter(int index) const
287  { return fParameters.Get(index); }
288 
292  BCParameter * GetParameter(const char * name) const
293  { return fParameters.Get(name); }
294 
297  unsigned int GetNParameters() const
298  { return fParameters.Size(); }
299 
302  unsigned int GetNFixedParameters();
303 
306  unsigned int GetNFreeParameters();
307 
312  const std::vector<double> & GetBestFitParametersMarginalized() const;
313 
321  void MCMCSetTrialFunctionScaleFactor(std::vector<double> scale)
323 
326  void MCMCSetNChains(unsigned n);
327 
330  void MCMCSetNLag(unsigned n)
331  { fMCMCNLag = n; }
332 
335  void MCMCSetNIterationsMax(unsigned n)
336  { fMCMCNIterationsMax = n; }
337 
340  void MCMCSetNIterationsRun(unsigned n)
341  { fMCMCNIterationsRun = n; }
342 
347 
352  void MCMCSetNIterationsUpdate(unsigned n)
353  { fMCMCNIterationsUpdate = n; }
354 
362 
365  void MCMCSetMinimumEfficiency(double efficiency)
366  { fMCMCEfficiencyMin = efficiency; }
367 
370  void MCMCSetMaximumEfficiency(double efficiency)
371  { fMCMCEfficiencyMax = efficiency; }
372 
375  void MCMCSetRandomSeed(unsigned seed);
376 
379  void MCMCSetWriteChainToFile(bool flag)
380  { fMCMCFlagWriteChainToFile = flag; }
381 
384  void MCMCSetWritePreRunToFile(bool flag)
385  { fMCMCFlagWritePreRunToFile = flag; }
386 
390  void MCMCSetInitialPositions(const std::vector<double> &x0s);
391 
395  void MCMCSetInitialPositions(std::vector< std::vector<double> > x0s);
396 
400  { fMCMCFlagInitialPosition = flag; }
401 
406  { fMCMCFlagOrderParameters = flag; }
407 
409  void MCMCSetFlagFillHistograms(bool flag);
410 
412  void MCMCSetFlagPreRun(bool flag)
413  { fMCMCFlagPreRun = flag; }
414 
417  void MCMCSetRValueCriterion(double r)
418  { fMCMCRValueCriterion = r; }
419 
424 
426  void MCMCSetRValueStrict(bool strict=true)
427  { fMCMCRValueUseStrict = strict; }
428 
431  void MCMCSetMarkovChainTrees(const std::vector<TTree *> & trees);
432 
436 
441  int SetMarginalized(unsigned index, TH1D * h);
442 
448  int SetMarginalized(unsigned index1, unsigned index2, TH2D * h);
449 
452  void MCMCSetValuesDefault();
453 
456  void MCMCSetValuesQuick();
457 
460  void MCMCSetValuesDetail();
461 
465 #if 0
466 
472  void SetParameterRange(unsigned int index, double parmin, double parmax);
473 #endif
474 
477  void SetNbins(unsigned int nbins);
478 
487  void WriteMarkovChain(bool flag)
488  {
491  }
492 
493 #if 0
494 
496  void SetMarkovChainInitialPosition(const std::vector<double> & position)
497  { fXmetro0 = position; }
498 
501  void SetMarkovChainStepSize(double stepsize)
502  { fMarkovChainStepSize = stepsize; }
503 
506  void SetMarkovChainNIterations(int niterations)
507  { fMarkovChainNIterations = niterations;
508  fMarkovChainAutoN = false; }
509 
512  void SetMarkovChainAutoN(bool flag)
513  { fMarkovChainAutoN = flag; }
514 #endif
515 
523  void Copy(const BCEngineMCMC & enginemcmc);
524 
531  virtual int AddParameter(const char * name, double min, double max, const char * latexname = "");
532 
537  virtual int AddParameter(BCParameter* parameter);
538 
545  virtual void MCMCTrialFunction(unsigned ichain, std::vector<double> &x);
546 
554  virtual double MCMCTrialFunctionSingle(unsigned ichain, unsigned ipar);
555 
561  bool MCMCGetProposalPointMetropolis(unsigned chain, std::vector<double> &x);
562 
568  bool MCMCGetProposalPointMetropolis(unsigned chain, unsigned parameter, std::vector<double> &x);
569 
573  bool MCMCGetNewPointMetropolis(unsigned chain = 0);
574  bool MCMCGetNewPointMetropolis(unsigned chain, unsigned parameter);
575 
579 
583 
587 
591 
594  void MCMCInChainWriteChains();
595 
599  virtual double LogEval(const std::vector<double> & parameters);
600 
603  int MCMCMetropolis();
604 
607  int MCMCMetropolisPreRun();
608 
611  void MCMCResetRunStatistics();
612 
616 
620  int MCMCInitialize();
621 
625  virtual void ResetResults();
626 
632  void ClearParameters(bool hard=false)
633  { fParameters.Clear(hard); }
634 
641  virtual void MCMCIterationInterface();
642 
647  {}
648 
657  virtual void MCMCCurrentPointInterface(std::vector<double> & /*point*/, int /*ichain*/, bool /*accepted*/)
658  {}
659 
662  private:
663 
664 
667  typedef bool (BCEngineMCMC::*MCMCPointerToGetProposalPoint) (int chain, std::vector<double> xnew, std::vector<double> xold) const;
668 
672 
677  {
681  std::vector<double> xLocal;
682 
686  TRandom3 * rng;
687 
692  MCMCThreadLocalStorage(const unsigned & dim);
693 
695 
697 
698  virtual ~MCMCThreadLocalStorage();
699  };
700 
704  std::vector<MCMCThreadLocalStorage> fMCMCThreadLocalStorage;
705 
709  void SyncThreadStorage();
710 
712 
713  protected:
717 
720  unsigned fMCMCNChains;
721 
724  unsigned fMCMCNLag;
725 
729  std::vector<unsigned> fMCMCNIterations;
730 
735 
740 
744 
748 
753 
757 
761 
765 
769 
773  std::vector<int> fMCMCNTrialsTrue;
774 
777  unsigned fMCMCNTrials;
778 
782 
786 
790  std::vector<double> fMCMCTrialFunctionScaleFactor;
791 
795 
799 
803 
809  std::vector<double> fMCMCInitialPosition;
810 
814 
818 
824 
829 
835 
841  std::vector<double> fMCMCx;
842 
847  std::vector<double> fMCMCxMax;
848 
852  std::vector<double> fMCMCxMean;
853 
857  std::vector<double> fMCMCxVar;
858 
862  std::vector<double> fMCMCprob;
863 
867  std::vector<double> fMCMCprobMax;
868 
872  std::vector<double> fMCMCprobMean;
873 
877  std::vector<double> fMCMCprobVar;
878 
886 
890 
894 
897  double fMCMCRValue;
898 
900  std::vector<double> fMCMCRValueParameters;
901 
904  TRandom3 * fRandom;
905 
908  std::vector<TH1D *> fMCMCH1Marginalized;
909  std::vector<TH2D *> fMCMCH2Marginalized;
910 
914  std::vector<TTree *> fMCMCTrees;
915 
918  std::vector<double> fMCMCBestFitParameters;
919 
923 
926  std::vector<double> fMarginalModes;
927 };
928 
929 // ---------------------------------------------------------
930 
931 #endif