BCIntegrate.h

Go to the documentation of this file.
00001 #ifndef __BCINTEGRATE__H
00002 #define __BCINTEGRATE__H
00003 
00016 /*
00017  * Copyright (C) 2008, Daniel Kollar and Kevin Kroeninger.
00018  * All rights reserved.
00019  *
00020  * For the licensing terms see doc/COPYING.
00021  */
00022 
00023 // ---------------------------------------------------------
00024 
00025 #include <vector>
00026 
00027 #include <math.h>
00028 
00029 #include "BAT/BCEngineMCMC.h"
00030 #include "BAT/BCParameter.h"
00031 #include "BAT/BCDataPoint.h"
00032 
00033 // ROOT classes
00034 class TH1D;
00035 class TH2D;
00036 class TRandom3;
00037 class TMinuit;
00038 class TTree;
00039 
00040 
00041 #define BC_DEBUG 0
00042 
00043 // ---------------------------------------------------------
00044 
00045 class BCIntegrate : public BCEngineMCMC
00046 {
00047 
00048    public:
00049 
00051       /* @{ */
00052 
00055       enum BCIntegrationMethod { kIntMonteCarlo, kIntImportance, kIntMetropolis, kIntCuba };
00056 
00059       enum BCMarginalizationMethod { kMargMonteCarlo, kMargMetropolis };
00060 
00063       enum BCOptimizationMethod { kOptSA, kOptMetropolis, kOptMinuit };
00064       
00067       enum BCSASchedule { kSACauchy, kSABoltzmann, kSACustom };
00068 
00069       /* @} */
00070 
00072       /* @{ */
00073 
00076       BCIntegrate();
00077 
00080       BCIntegrate(BCParameterSet * par);
00081 
00084       virtual ~BCIntegrate();
00085 
00086       /* @} */
00087 
00089       /* @{ */
00090 
00093       BCIntegrate::BCIntegrationMethod GetIntegrationMethod()
00094          { return fIntegrationMethod; };
00095 
00098       BCIntegrate::BCMarginalizationMethod GetMarginalizationMethod()
00099          { return fMarginalizationMethod; };
00100 
00103       BCIntegrate::BCOptimizationMethod GetOptimizationMethod()
00104          { return fOptimizationMethod; };
00105       
00108       BCIntegrate::BCOptimizationMethod GetOptimizationMethodMode()
00109          { return fOptimizationMethodMode; };
00110       
00113       BCIntegrate::BCSASchedule GetSASchedule()
00114          { return fSASchedule; };
00115 
00119       void GetRandomVector(std::vector <double> &x);
00120 
00121       virtual void GetRandomVectorMetro(std::vector <double> &x);
00122 
00128       double GetRandomPoint(std::vector <double> &x);
00129 
00135       double GetRandomPointImportance(std::vector <double> &x);
00136 
00141       void GetRandomPointMetro(std::vector <double> &x);
00142 
00147       void GetRandomPointSamplingMetro(std::vector <double> &x);
00148 
00151       int GetNiterationsPerDimension()
00152          { return fNiterPerDimension; };
00153 
00156       int GetNSamplesPer2DBin()
00157          { return fNSamplesPer2DBin; };
00158 
00161       int GetNvar()
00162          { return fNvar; };
00163 
00166       int GetNIterationsMax()
00167          { return fNIterationsMax; };
00168 
00171       int GetNIterations()
00172          { return fNIterations; };
00173 
00176       double GetRelativePrecision()
00177          { return fRelativePrecision; };
00178 
00179       /*
00180        * @return The uncertainty in the most recent Monte Carlo integration */
00181       double GetError()
00182          { return fError; };
00183 
00184       /*
00185        * @return number of bins per dimension for the marginalized distributions */
00186       int GetNbins()
00187          { return fNbins; };
00188 
00189       /*
00190        * @return Minuit used for mode finding */
00191       TMinuit * GetMinuit();
00192 
00193       int GetMinuitErrorFlag()
00194          { return fMinuitErrorFlag; };
00195 
00196       /*
00197        * @return The ROOT tree containing the Markov chain */
00198       TTree * GetMarkovChainTree()
00199          { return fMarkovChainTree; };
00200 
00201       /*
00202        * Returns the actual point in the markov chain */
00203       std::vector<double> * GetMarkovChainPoint()
00204          { return &fXmetro1; };
00205 
00208       int * GetMCMCIteration()
00209          { return &fMCMCIteration; };
00210 
00213       double * GetMarkovChainValue()
00214          { return &fMarkovChainValue; };
00215 
00218       double GetSAT0()
00219          { return fSAT0; }
00220 
00223       double GetSATmin()
00224          { return fSATmin; }
00225 
00226       double * GetSAP2Temperature()
00227          { return &fSATemperature; };
00228 
00229       double * GetSAP2LogProb()
00230          { return &fSALogProb; }; 
00231       
00232       std::vector<double> * GetSAP2x()
00233          { return &fSAx; }; 
00234 
00235       int * GetSAP2NIterations()
00236          { return &fSANIterations; };
00237 
00238       /* @} */
00239 
00241       /* @{ */
00242 
00243       void SetMinuitArlist(double * arglist)
00244          { fMinuitArglist[0] = arglist[0];
00245            fMinuitArglist[1] = arglist[1]; };
00246 
00247       void SetFlagIgnorePrevOptimization(bool flag)
00248       { fFlagIgnorePrevOptimization = flag; }; 
00249 
00253       void SetParameters(BCParameterSet * par);
00254 
00257       void SetVarList(int * varlist);
00258 
00261       void SetVar(int index){fVarlist[index]=1;};
00262 
00265       void SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method);
00266 
00269       void SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method)
00270          { fMarginalizationMethod = method; };
00271 
00274       void SetOptimizationMethod(BCIntegrate::BCOptimizationMethod method)
00275          { fOptimizationMethod = method; };
00276 
00279       void SetSASchedule(BCIntegrate::BCSASchedule schedule)
00280          { fSASchedule = schedule; };
00281 
00284       void SetNiterationsPerDimension(int niterations)
00285          { fNiterPerDimension = niterations; };
00286 
00290       void SetNSamplesPer2DBin(int n)
00291          { fNSamplesPer2DBin = n; };
00292 
00295       void SetNIterationsMax(int niterations)
00296          { fNIterationsMax = niterations; };
00297 
00301       void SetRelativePrecision(double relprecision)
00302          { fRelativePrecision = relprecision; };
00303 
00313       void SetNbins(int nbins, int index = -1);
00314 
00315 //    void SetNbins(int n);
00316 //    void SetNbinsX(int n);
00317 //    void SetNbinsY(int n);
00318 
00319       /*
00320        * Turn on or off the filling of the error band during the MCMC run.
00321        * @param flag set to true for turning on the filling */
00322       void SetFillErrorBand(bool flag = true)
00323          { fFillErrorBand=flag; };
00324 
00325       /*
00326        * Turn off filling of the error band during the MCMC run.
00327        * This method is equivalent to SetFillErrorBand(false) */
00328       void UnsetFillErrorBand()
00329          { fFillErrorBand=false; };
00330 
00334       void SetFitFunctionIndexX(int index)
00335          { fFitFunctionIndexX = index; };
00336 
00340       void SetFitFunctionIndexY(int index)
00341          { fFitFunctionIndexY = index; };
00342 
00343       void SetFitFunctionIndices(int indexx, int indexy)
00344          { this -> SetFitFunctionIndexX(indexx);
00345             this -> SetFitFunctionIndexY(indexy); };
00346 
00350       void SetDataPointLowerBoundaries(BCDataPoint * datasetlowerboundaries)
00351          { fDataPointLowerBoundaries = datasetlowerboundaries; };
00352 
00356       void SetDataPointUpperBoundaries(BCDataPoint * datasetupperboundaries)
00357          { fDataPointUpperBoundaries = datasetupperboundaries; };
00358 
00362       void SetDataPointLowerBoundary(int index, double lowerboundary)
00363          { fDataPointLowerBoundaries -> SetValue(index, lowerboundary); };
00364 
00368       void SetDataPointUpperBoundary(int index, double upperboundary)
00369          { fDataPointUpperBoundaries -> SetValue(index, upperboundary); };
00370 
00374       void WriteMarkovChain(bool flag)
00375          { fFlagWriteMarkovChain = flag; fMCMCFlagWriteChainToFile = flag;};
00376 
00379       void SetMarkovChainTree(TTree * tree)
00380          { fMarkovChainTree = tree; };
00381 
00384       void SetMarkovChainInitialPosition(std::vector<double> position);
00385 
00388       void SetMarkovChainStepSize(double stepsize)
00389          { fMarkovChainStepSize = stepsize; };
00390 
00393       void SetMarkovChainNIterations(int niterations)
00394          { fMarkovChainNIterations = niterations;
00395            fMarkovChainAutoN = false; }
00396 
00399       void SetMarkovChainAutoN(bool flag)
00400          { fMarkovChainAutoN = flag; };
00401 
00404       void SetMode(std::vector <double> mode);
00405 
00408       void SetErrorBandHisto(TH2D * h)
00409          { fErrorBandXY = h; };
00410 
00413       void SetSAT0(double T0)
00414          { fSAT0 = T0; }
00415 
00418       void SetSATmin(double Tmin)
00419          { fSATmin = Tmin; }
00420 
00421       void SetFlagWriteSAToFile(bool flag)
00422       { fFlagWriteSAToFile = flag; }; 
00423 
00424       /*
00425        * Sets the tree containing the Markov chains. */
00426       void SetSATree(TTree* tree)
00427       { fTreeSA = tree; };
00428 
00429       /* @} */
00430 
00432       /* @{ */
00433 
00436       void DeleteVarList();
00437 
00441       void ResetVarlist(int v);
00442 
00446       void UnsetVar(int index)
00447          { fVarlist[index] = 0; };
00448 
00454       virtual double Eval(std::vector <double> x);
00455 
00461       virtual double LogEval(std::vector <double> x);
00462 
00468       virtual double EvalSampling(std::vector <double> x);
00469 
00475       double LogEvalSampling(std::vector <double> x);
00476 
00483       double EvalPrint(std::vector <double> x);
00484 
00490       virtual double FitFunction(std::vector <double> x, std::vector <double> parameters)
00491          { return 0.; };
00492 
00496       double Integrate();
00497 
00503       double IntegralMC(std::vector <double> x, int * varlist);
00504 
00505       double IntegralMC(std::vector <double> x);
00506 
00511       double IntegralMetro(std::vector <double> x);
00512 
00517       double IntegralImportance(std::vector <double> x);
00518 
00525       double CubaIntegrate(int method, std::vector<double> parameters_double, std::vector<double> parameters_int);
00526 
00527       double CubaIntegrate();
00528 
00536       static void CubaIntegrand(const int * ndim, const double xx[], const int * ncomp, double ff[]);
00537 
00542       TH1D * Marginalize(BCParameter * parameter);
00543 
00549       TH2D * Marginalize(BCParameter * parameter1, BCParameter * parameter2);
00550 
00556       TH1D * MarginalizeByIntegrate(BCParameter * parameter);
00557 
00564       TH2D * MarginalizeByIntegrate(BCParameter * parameter1, BCParameter * parameter2);
00565 
00571       TH1D * MarginalizeByMetro(BCParameter * parameter);
00572 
00578       TH2D * MarginalizeByMetro(BCParameter * parameter1, BCParameter * parameter2);
00579 
00585       int MarginalizeAllByMetro(const char * name);
00586 
00591       TH1D * GetH1D(int parIndex);
00592 
00598       int GetH2DIndex(int parIndex1, int parIndex2);
00599 
00605       TH2D * GetH2D(int parIndex1, int parIndex2);
00606 
00609       void InitMetro();
00610 
00611       void SAInitialize();
00612 
00615 //    void FindMode();
00616 
00622       virtual void FindModeMinuit(std::vector<double> start = std::vector<double>(0), int printlevel = 1);
00623 
00626       void FindModeMCMC();
00627 //    void FindModeMCMC(int flag_run = 0);
00628 
00634       void FindModeSA(std::vector<double> start = std::vector<double>(0));
00635 
00641       double SATemperature(double t);
00642 
00647       double SATemperatureBoltzmann(double t);
00648 
00653       double SATemperatureCauchy(double t);
00654       
00660       virtual double SATemperatureCustom(double t);
00661 
00669       std::vector<double> GetProposalPointSA(std::vector<double> x, int t);
00670 
00677       std::vector<double> GetProposalPointSABoltzmann(std::vector<double> x, int t);
00678 
00685       std::vector<double> GetProposalPointSACauchy(std::vector<double> x, int t);
00686 
00694       virtual std::vector<double> GetProposalPointSACustom(std::vector<double> x, int t);
00695 
00700       std::vector<double> SAHelperGetRandomPointOnHypersphere();
00701 
00705       double SAHelperGetRadialCauchy();
00706 
00710       double SAHelperSinusToNIntegral(int dim, double theta);
00711       
00712 
00713       static void FCNLikelihood(int &npar, double * grad, double &fval, double * par, int flag);
00714 
00715       /*
00716        * Method executed for every iteration of the MCMC. User's code should be
00717        * provided via overloading in the derived class*/
00718       virtual void MCMCUserIterationInterface()
00719          {};
00720 
00721       /* @} */
00722 
00723    private:
00724 
00727       BCParameterSet * fx;
00728 
00731       double * fMin;
00732 
00735       double * fMax;
00736 
00739       int * fVarlist;
00740 
00743       int fNiterPerDimension;
00744 
00747       BCIntegrate::BCIntegrationMethod fIntegrationMethod;
00748 
00751       BCIntegrate::BCMarginalizationMethod fMarginalizationMethod;
00752 
00755       BCIntegrate::BCOptimizationMethod fOptimizationMethod;
00756 
00760       BCIntegrate::BCOptimizationMethod fOptimizationMethodMode; 
00761 
00764       BCIntegrate::BCSASchedule fSASchedule;
00765 
00768       int fNIterationsMax;
00769 
00772       int fNIterations;
00773 
00776       double fRelativePrecision;
00777 
00780       double fError;
00781 
00784       int fNmetro;
00785       int fNacceptedMCMC;
00786 
00789       std::vector <double> fXmetro0;
00790 
00793       std::vector <double> fXmetro1;
00794 
00797       double fMarkovChainValue;
00798 
00799       /*
00800        * Method executed for every iteration of the MCMC, overloaded from BCEngineMCMC. */
00801       void MCMCIterationInterface();
00802 
00803       /*
00804        * Fill error band histogram for curreent iteration. This method is called from MCMCIterationInterface() */
00805       void MCMCFillErrorBand();
00806 
00807    protected:
00808 
00811       int fNvar;
00812 
00815       int fNbins;
00816 
00820       int fNSamplesPer2DBin;
00821 
00824       double fMarkovChainStepSize;
00825 
00826       int fMarkovChainNIterations;
00827 
00828       bool fMarkovChainAutoN;
00829 
00832       BCDataPoint * fDataPointLowerBoundaries;
00833 
00836       BCDataPoint * fDataPointUpperBoundaries;
00837 
00838       std::vector <bool> fDataFixedValues;
00839 
00842       TRandom3 * fRandom;
00843 
00847       std::vector <double> fBestFitParameters;
00848       std::vector <double> fBestFitParameterErrors;
00849 
00852       std::vector <double> fBestFitParametersMarginalized;
00853 
00856       std::vector <TH1D *> fHProb1D;
00857 
00860       std::vector <TH2D *> fHProb2D;
00861 
00864       bool fFillErrorBand;
00865 
00868       int fFitFunctionIndexX;
00869       int fFitFunctionIndexY;
00870 
00873       bool fErrorBandContinuous;
00874       std::vector <double> fErrorBandX;
00875 
00878       TH2D * fErrorBandXY;
00879 
00882       int fErrorBandNbinsX;
00883 
00886       int fErrorBandNbinsY;
00887 
00890       TMinuit * fMinuit;
00891 
00892       double fMinuitArglist[2];
00893       int fMinuitErrorFlag;
00894 
00897       double fFlagIgnorePrevOptimization; 
00898 
00901       bool fFlagWriteMarkovChain;
00902 
00905       TTree * fMarkovChainTree;
00906 
00909       int fMCMCIteration;
00910 
00913       double fSAT0;
00914 
00917       double fSATmin;
00918 
00921       TTree * fTreeSA; 
00922 
00925       bool fFlagWriteSAToFile; 
00926 
00927       int fSANIterations; 
00928       double fSATemperature; 
00929       double fSALogProb; 
00930       std::vector<double> fSAx; 
00931 
00932 };
00933 
00934 // ---------------------------------------------------------
00935 
00936 #endif

Generated on Thu Feb 11 11:29:30 2010 for BayesianAnalysisToolkit by  doxygen 1.5.1