BCEngineMCMC.h

Go to the documentation of this file.
00001 #ifndef __BCENGINEMCMC__H
00002 #define __BCENGINEMCMC__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 // ROOT classes
00028 class TH1D;
00029 class TH2D;
00030 class TTree;
00031 class TRandom3;
00032 
00033 // ---------------------------------------------------------
00034 
00035 class BCEngineMCMC
00036 {
00037 
00038    public:
00039 
00041       /* @{ */
00042 
00045       BCEngineMCMC();
00046 
00050       BCEngineMCMC(int n);
00051 
00054       BCEngineMCMC(const BCEngineMCMC & enginemcmc);
00055 
00058       virtual ~BCEngineMCMC();
00059 
00060       /* @} */
00062       /* @{ */
00063 
00066       BCEngineMCMC & operator = (const BCEngineMCMC & engineMCMC);
00067 
00068       /* @} */
00070       /* @{ */
00071 
00072       /*
00073        * @return number of parameters of the Markov chain */
00074       int MCMCGetNParameters()
00075          { return fMCMCNParameters; };
00076 
00077       /*
00078        * @return number of Markov chains */
00079       int MCMCGetNChains()
00080          { return fMCMCNChains; };
00081 
00082       /*
00083        * @return lag of the Markov chains */
00084       int MCMCGetNLag()
00085          { return fMCMCNLag; };
00086 
00087       /*
00088        * @return number of iterations */
00089       std::vector <int> MCMCGetNIterations()
00090          { return fMCMCNIterations; };
00091 
00092       /*
00093        * @return pointer to the number of iterations */
00094       std::vector <int> * MCMCGetP2NIterations()
00095          { return &fMCMCNIterations; };
00096 
00097       /*
00098        * @return number of iterations needed for all chains to
00099        * converge simultaneously */
00100       int MCMCGetNIterationsConvergenceGlobal()
00101          { return fMCMCNIterationsConvergenceGlobal; };
00102 
00103       /*
00104        * @return flag if converged or not */
00105       bool MCMCGetFlagConvergenceGlobal()
00106          { return fMCMCFlagConvergenceGlobal; };
00107 
00108       /*
00109        * @return maximum number of iterations for a Markov chain */
00110       int MCMCGetNIterationsMax()
00111          { return fMCMCNIterationsMax; };
00112 
00113       /*
00114        * @return number of iterations for a Markov chain */
00115       int MCMCGetNIterationsRun()
00116          { return fMCMCNIterationsRun; };
00117 
00118       /*
00119        * @return number of iterations needed for burn-in. These
00120        * iterations are not included in fMCMCNIterations */
00121       int MCMCGetNIterationsBurnIn()
00122          { return fMCMCNIterationsBurnIn; };
00123 
00124       /*
00125        * @returns number of accepted trials for each chain */
00126       std::vector <int> MCMCGetNTrialsTrue()
00127          { return fMCMCNTrialsTrue; };
00128 
00129       /*
00130        * @returns number of not-accepted trials for each chain */
00131       std::vector <int> MCMCGetNTrialsFalse()
00132          { return fMCMCNTrialsFalse; };
00133 
00134       /*
00135        * @return mean value of the probability for each chain up to
00136        * the current iteration  */
00137       std::vector <double> MCMCGetMean()
00138          { return fMCMCMean; };
00139 
00140       /*
00141        * @return mean value of the probability for each chain up to
00142        * the current iteration */
00143       std::vector <double> MCMCGetVariance()
00144          { return fMCMCVariance; };
00145 
00146       /*
00147        * @return scale factor for all parameters and chains */
00148       std::vector <double> MCMCGetTrialFunctionScaleFactor()
00149          { return fMCMCTrialFunctionScaleFactor; };
00150 
00151       /*
00152        * @return scale factor for all parameters and achain.
00153        * @param ichain chain index */
00154       std::vector <double> MCMCGetTrialFunctionScaleFactor(int ichain);
00155 
00156       /*
00157        * @return scale factor for a parameter and a chain.
00158        * @param ichain chain index
00159        * @param ipar parameter index */
00160       double MCMCGetTrialFunctionScaleFactor(int ichain, int ipar);
00161 
00162       /*
00163        * @return current point of each Markov chain */
00164       std::vector <double> MCMCGetx()
00165          { return fMCMCx; };
00166 
00167       /*
00168        * @return pointer of each Markov chain */
00169       std::vector <double> * MCMCGetP2x()
00170          { return &fMCMCx; };
00171 
00172       /*
00173        * @param ichain index of the Markov chain
00174        * @return current point of the Markov chain */
00175       std::vector <double> MCMCGetx(int ichain);
00176 
00177       /*
00178        * @param ichain chain index
00179        * @param ipar parameter index
00180        * @return parameter of the Markov chain */
00181       double MCMCGetx(int ichain, int ipar);
00182 
00183       /*
00184        * @return log of the probability of the current points of each Markov chain */
00185       std::vector <double> MCMCGetLogProbx()
00186          { return fMCMCLogProbx; };
00187 
00188       /*
00189        * @return log of the probability of the current points of the Markov chain.
00190        * @param ichain chain index */
00191       double MCMCGetLogProbx(int ichain);
00192 
00193       /*
00194        * @return pointer to the log of the probability of the current points of each Markov chain */
00195       std::vector <double> * MCMCGetP2LogProbx()
00196          { return &fMCMCLogProbx; };
00197 
00198       /*
00199        * @return maximum points of each Markov chain */
00200       std::vector <double> MCMCGetMaximumPoints()
00201          { return fMCMCMaximumPoints; };
00202 
00203       /*
00204        * @return maximum point of  Markov chain
00205        * @param i The index of the Markov chain */
00206       std::vector <double> MCMCGetMaximumPoint(int i);
00207 
00208       /*
00209        * @return maximum (log) probability of each Markov chain */
00210       std::vector <double> MCMCGetMaximumLogProb()
00211          { return fMCMCMaximumLogProb; };
00212 
00213       /*
00214        * @return flag which defined initial position */
00215       int MCMCGetFlagInitialPosition()
00216          { return fMCMCFlagInitialPosition; };
00217 
00218       /*
00219        * @return R-value criterion */
00220       double MCMCGetRValueCriterion()
00221          { return fMCMCRValueCriterion; };
00222 
00223       /*
00224        * @return R-value criterion for parameters */
00225       double MCMCGetRValueParametersCriterion()
00226          { return fMCMCRValueParametersCriterion; };
00227 
00228       /*
00229        * @return R-value */
00230       double MCMCGetRValue()
00231          { return fMCMCRValue; };
00232 
00233       /*
00234        * @return R-value for a parameter
00235        * @param i parameter index */
00236       double MCMCGetRValueParameters(int i)
00237          { return fMCMCRValueParameters.at(i); };
00238 
00239       /*
00240        * @return the flag if MCMC has been performed or not */
00241       bool MCMCGetFlagRun()
00242       { return fMCMCFlagRun; };
00243 
00244       /*
00245        * Rtrieve the tree containing the Markov chain.
00246        * @param i index of the Markov chain
00247        * @return pointer to the tree */
00248       TTree * MCMCGetMarkovChainTree(int i)
00249          { return fMCMCTrees.at(i); };
00250 
00251       /*
00252        * Retrieve a histogram of the 1D marginalized distribution of a single parameter.
00253        * @param i index of the parameter
00254        * @return pointer to the histogram */
00255       TH1D * MCMCGetH1Marginalized(int i);
00256 
00257       /*
00258        * Retrieve a histogram of the 2D marginalized distribution for two parameters.
00259        * @param i index of the first parameter
00260        * @param j index of the second parameter
00261        * @return pointer to the histogram */
00262       TH2D * MCMCGetH2Marginalized(int i, int j);
00263 
00264       /*
00265        * Return the random number generator */
00266       TRandom3 * MCMCGetTRandom3()
00267       { return fMCMCRandom; };
00268 
00269       /* @} */
00271       /* @{ */
00272 
00273       /*
00274        * Set the scale factors for the trial functions
00275        * @param scale a vector of doubles containing the scale factors */
00276       void MCMCSetTrialFunctionScaleFactor(std::vector <double> scale)
00277          { fMCMCTrialFunctionScaleFactorStart = scale; };
00278 
00279       /*
00280        * Sets the number of Markov chains which are run in parallel. */
00281       void MCMCSetNChains(int n);
00282 
00283       /*
00284        * Sets the lag of the Markov chains */
00285       void MCMCSetNLag(int n)
00286       { fMCMCNLag = n; };
00287 
00288       /*
00289        * Sets the maximum number of iterations in the pre-run. */
00290       void MCMCSetNIterationsMax(int n)
00291          { fMCMCNIterationsMax = n; };
00292 
00293       /*
00294        * Sets the number of iterations. */
00295       void MCMCSetNIterationsRun(int n)
00296          { fMCMCNIterationsRun = n; };
00297 
00298       /*
00299        * Sets the number of iterations needed for burn-in. */
00300       void MCMCSetNIterationsBurnIn(int n)
00301          { fMCMCNIterationsBurnIn = n; };
00302 
00303       /*
00304        * Sets the minimum number of iterations in the pre-run */
00305       void MCMCSetNIterationsPreRunMin(int n)
00306          { fMCMCNIterationsPreRunMin = n; };
00307 
00308       /*
00309        * Sets the number of iterations in the pre-run after which an
00310        * update on the statistics (convergence, efficiency, etc.) is done.
00311        * @param n The number of iterations.*/
00312       void MCMCSetNIterationsUpdate(int n)
00313          { fMCMCNIterationsUpdate = n; };
00314 
00315       /*
00316        * Sets the maximum number of iterations in the pre-run after which an
00317        * update on the statistics (convergence, efficiency, etc.) is done.
00318        * If set to 0 no maximum is set.
00319        * @param n maximum number of iterations. */
00320       void MCMCSetNIterationsUpdateMax(int n)
00321          { fMCMCNIterationsUpdateMax = n; };
00322 
00323       /*
00324        * Sets the minimum efficiency required for a chain. */
00325       void MCMCSetMinimumEfficiency(double efficiency)
00326          { fMCMCEfficiencyMin = efficiency; };
00327 
00328       /*
00329        * Sets the maximum efficiency required for a chain. */
00330       void MCMCSetMaximumEfficiency(double efficiency)
00331          { fMCMCEfficiencyMax = efficiency; };
00332 
00333       /*
00334        * Sets flag to write Markov chains to file. */
00335       void MCMCSetWriteChainToFile(bool flag)
00336          { fMCMCFlagWriteChainToFile = flag; };
00337 
00338       /*
00339        * Sets the initial positions for all chains.
00340        * @param x0s initial positions for all chains. */
00341       void MCMCSetInitialPositions(std::vector<double> x0s);
00342 
00343       /*
00344        * Sets the initial positions for all chains.
00345        * @param x0s initial positions for all chains. */
00346       void MCMCSetInitialPositions(std::vector< std::vector<double> > x0s);
00347 
00348       /*
00349        * Sets flag which defines initial position.  */
00350       void MCMCSetFlagInitialPosition(int flag)
00351          { fMCMCFlagInitialPosition = flag; };
00352 
00353       /*
00354        * Sets the flag which controls the sequence parameters during the
00355        * running of the MCMC.  */
00356       void MCMCSetFlagOrderParameters(bool flag)
00357       { fMCMCFlagOrderParameters = flag; };
00358 
00359       /* Sets the flag for all parameters to either fill histograms or not. */
00360       void MCMCSetFlagFillHistograms(bool flag);
00361 
00362       /* Sets the flag for a single parameter to either fill histograms or not. */
00363       void MCMCSetFlagFillHistograms(int index, bool flag);
00364 
00365       /*
00366        * Sets the R-value criterion for convergence of all chains. */
00367       void MCMCSetRValueCriterion(double r)
00368          { fMCMCRValueCriterion = r; };
00369 
00370       /*
00371        * Sets the parameter R-value criterion for convergence of all chains */
00372       void MCMCSetRValueParametersCriterion(double r)
00373          { fMCMCRValueParametersCriterion = r; };
00374 
00375       /*
00376        * Sets the tree containing the Markov chains. */
00377       void MCMCSetMarkovChainTrees(std::vector <TTree *> trees);
00378 
00379       /*
00380        * Sets the histogram with 1D marginalized distributions for parameter.
00381        * @param i index of the parameter
00382        * @param h pointer to an existing histogram */
00383       int SetMarginalized(int index, TH1D * h);
00384 
00385       /*
00386        * Sets the histogram with 2D marginalized distributions for two parameters.
00387        * @param index1 index of the first parameter
00388        * @param index2 index of the second parameter
00389        * @param h pointer to an existing histogram */
00390       int SetMarginalized(int index1, int index2, TH2D * h);
00391 
00392       /*
00393        * Set the default values for the MCMC chain. */
00394       void MCMCSetValuesDefault();
00395 
00396       /*
00397        * Set the values for a quick MCMC run. */
00398       void MCMCSetValuesQuick();
00399 
00400       /*
00401        * Set the values for a detailed MCMC run. */
00402       void MCMCSetValuesDetail();
00403 
00404       /* @} */
00406       /* @{ */
00407 
00408       /*
00409        * Adds a parameter.
00410        * @param min minimum value of the parameter
00411        * @param max maximum value of the parameter
00412        * @return number of parameters after adding */
00413       int MCMCAddParameter(double min, double max);
00414 
00415       /*
00416        * Random walk trial function. The default trial function is a
00417        * Breit-Wigner. It can be overloaded by the user to set the trial
00418        * function.
00419        * @param chain the chain index
00420        * @param x point with the dimension fMCMCNParameters */
00421       virtual void MCMCTrialFunction(int chain, std::vector <double> &x);
00422 
00423       /*
00424        * Random walk trial function. The default trial function is a
00425        * Breit-Wigner. It can be overloaded by the user to set the trial
00426        * function.
00427        * @param ichain the chain index
00428        * @param iparameter the parameter index
00429        * @param x point with the dimension fMCMCNParameters */
00430       virtual void MCMCTrialFunctionSingle(int ichain, int iparameter, std::vector <double> &x);
00431 
00432       /*
00433        * Returns a trial point for the Metropolis algorithm.
00434        * @param chain chain index
00435        * @param x proposal point
00436        * @return flag indicating whether the new point lies within the allowed range */
00437       bool MCMCGetProposalPointMetropolis(int chain, std::vector <double> &x);
00438 
00439       /*
00440        * Returns a trial point for the Metropolis algorithm.
00441        * @param chain chain index
00442        * @param x proposal point
00443        * @return flag indicating whether the new point lies within the allowed range */
00444       bool MCMCGetProposalPointMetropolis(int chain, int parameter, std::vector <double> &x);
00445 
00446       /*
00447        * Generates a new point using the Metropolis algorithm.
00448        * @param chain chain index */
00449       bool MCMCGetNewPointMetropolis(int chain = 0);
00450       bool MCMCGetNewPointMetropolis(int chain, int parameter);
00451 
00452       /*
00453        * Updates statistics: find new maximum */
00454       void MCMCUpdateStatisticsCheckMaximum();
00455 
00456       /*
00457        * Updates statistics: fill marginalized distributions */
00458       void MCMCUpdateStatisticsFillHistograms();
00459 
00460       /*
00461        * Updates statistics: check convergence */
00462       void MCMCUpdateStatisticsTestConvergenceAllChains();
00463 
00464       /*
00465        * Updates statistics: write chains to file */
00466       void MCMCUpdateStatisticsWriteChains();
00467 
00468       /*
00469        * Needs to be overloaded in the derived class.
00470        * @return natural logarithm of the function to map with MCMC */
00471       virtual double LogEval(std::vector <double> parameters);
00472 
00473       /*
00474        * Runs Metropolis algorithm. */
00475       int MCMCMetropolis();
00476 
00477       /*
00478        * Runs a pre run for the Metropolis algorithm. */
00479       int MCMCMetropolisPreRun();
00480 
00481       /*
00482        * Resets the run statistics. */
00483       void MCMCResetRunStatistics();
00484 
00485       /*
00486        * Initializes Markov chains. */
00487       void MCMCInitializeMarkovChains();
00488 
00489       /*
00490        * Initializes the engine. */
00491       int MCMCInitialize();
00492 
00493       /*
00494        * Interface allowing to execute arbitrary code for each iteration
00495        * of the MCMC.  This method needs to be overloaded in the derived
00496        * class. */
00497       virtual void MCMCIterationInterface()
00498          {};
00499 
00500       /* @} */
00501 
00502    private:
00503 
00504       /*
00505        * Copies this BCEngineMCMC into another one. */
00506       void Copy(BCEngineMCMC & enginemcmc) const;
00507 
00508       /*
00509        * Defines a type of a pointer to a member function. */
00510       typedef bool (BCEngineMCMC::*MCMCPointerToGetProposalPoint) (int chain, std::vector <double> xnew, std::vector <double> xold) const;
00511 
00512       /*
00513        * Pointer to a member function */
00514       MCMCPointerToGetProposalPoint fMCMCPointerToGetProposalPoint;
00515 
00516    protected:
00517 
00518       /*
00519        * Number of parameters */
00520       int fMCMCNParameters;
00521 
00522       /*
00523        * Parameter boundaries */
00524       std::vector <double> fMCMCBoundaryMin;
00525       std::vector <double> fMCMCBoundaryMax;
00526 
00527       /*
00528        * Parameter flags for marginalization */
00529       std::vector <bool> fMCMCFlagsFillHistograms;
00530 
00531       /*
00532        * Number of Markov chains ran in parallel */
00533       int fMCMCNChains;
00534 
00535       /*
00536        * The lag for the Markov Chain */
00537       int fMCMCNLag;
00538 
00539       /*
00540        * Number of total iterations of the Markov chains. The length of
00541        * the vector is equal to fMCMCNChains. */
00542       std::vector<int> fMCMCNIterations;
00543 
00544       /*
00545        * Number of iterations for updating scale factors */
00546       int fMCMCNIterationsUpdate;
00547 
00548       /*
00549        * Maximum number of iterations for updating scale factors */
00550       int fMCMCNIterationsUpdateMax;
00551 
00552       /*
00553        * Number of iterations needed for all chains to convergence
00554        * simulaneously */
00555       int fMCMCNIterationsConvergenceGlobal;
00556 
00557       /*
00558        * Flag for convergence */
00559       bool fMCMCFlagConvergenceGlobal;
00560 
00561       /*
00562        * Maximum number of iterations for a Markov chain prerun */
00563       int fMCMCNIterationsMax;
00564 
00565       /*
00566        * Number of iterations for a Markov chain run */
00567       int fMCMCNIterationsRun;
00568 
00569       /*
00570        * Minimum number of iterations for the pre-run */
00571       int fMCMCNIterationsPreRunMin;
00572 
00573       /*
00574        * Number of iterations for burn-in. These iterations are not
00575        * included in fMCMCNIterations. */
00576       int fMCMCNIterationsBurnIn;
00577 
00578       /*
00579        * Number of accepted trials for each chain. The length of the
00580        * vector is equal to fMCMCNChains * fMCMCNParameters.  */
00581       std::vector<int> fMCMCNTrialsTrue;
00582 
00583       /*
00584        * Number of not accepted trials for each chain. The length of the
00585        * vector is equal to fMCMCNChains * fMCMCNParameters.  */
00586       std::vector<int> fMCMCNTrialsFalse;
00587 
00588       /*
00589        * The mean of all log prob values of each Markov chain. The
00590        * length of the vector is equal to fMCMCNChains. */
00591       std::vector <double> fMCMCMean;
00592 
00593       /*
00594        * The variance of all log prob values of each Markov chain. The
00595        * length of the vector is equal to fMCMCNChains. */
00596       std::vector <double> fMCMCVariance;
00597 
00598       /*
00599        * The mean of all parameters of each Markov chain. The length of
00600        * the vector is equal to fMCMCNChains * fMCMCNParameters. */
00601       std::vector <double> fMCMCMeanx;
00602 
00603       /*
00604        * The variance of all parameters of each Markov chain. The length
00605        * of the vector is equal to fMCMCNChains * fMCMCNParameters. */
00606       std::vector <double> fMCMCVariancex;
00607 
00608       /*
00609        * Flag to write Markov chains to file */
00610       bool fMCMCFlagWriteChainToFile;
00611 
00612       /*
00613        * Scales the width of the trial functions by a scale factor for
00614        * each parameter and chain */
00615       std::vector <double> fMCMCTrialFunctionScaleFactor;
00616 
00617 
00618       /*
00619        * Start values of the scale factors for the trial functions. */
00620       std::vector <double> fMCMCTrialFunctionScaleFactorStart;
00621 
00622       /*
00623        * Defines if a prerun has been performed or not */
00624       bool fMCMCFlagPreRun;
00625 
00626       /*
00627        * Defines if MCMC has been performed or not */
00628       bool fMCMCFlagRun;
00629 
00630       /*
00631        * The intial position of each Markov chain. The length of the
00632        * vectors is equal to fMCMCNChains * fMCMCNParameters. First, the
00633        * values of the first Markov chain are saved, then those of the
00634        * second and so on */
00635       std::vector <double> fMCMCInitialPosition;
00636 
00637       /*
00638        * The minimum required efficiency for MCMC */
00639       double fMCMCEfficiencyMin;
00640 
00641       /*
00642        * The maximum allowed efficiency for MCMC */
00643       double fMCMCEfficiencyMax;
00644 
00645       /*
00646        * Variable which defines the initial position. 0 (default) center
00647        * of the allowed region, (1) random initial position (2)
00648        * pre-defined intial position. */
00649       int fMCMCFlagInitialPosition;
00650 
00651       /*
00652        * Flag which controls the sequence parameters during the running
00653        * of the MCMC. */
00654       bool fMCMCFlagOrderParameters;
00655 
00656       /*
00657        * Flag which controls fill histograms during main run. */
00658       bool fMCMCFlagFillHistograms;
00659 
00660       /*
00661        * The current points of each Markov chain. The length of the
00662        * vectors is equal to fMCMCNChains * fMCMCNParameters. First, the
00663        * values of the first Markov chain are saved, then those of the
00664        * second and so on. */
00665       std::vector <double> fMCMCx;
00666 
00667       /*
00668        * A temporary vector for a single Markov chain */
00669       std::vector <double> fMCMCxLocal;
00670 
00671       /*
00672        * The log of the probability of the current points of each Markov
00673        * chain. The length of the vectors is fMCMCNChains. */
00674       std::vector<double> fMCMCLogProbx;
00675 
00676       /*
00677        * The maximum points of each Markov chain. The length of the vector
00678        * is fMCMCNChains * fMCMCNParameters. First, the values of the
00679        * first Markov chain are saved, then those of the second and so on. */
00680       std::vector <double> fMCMCMaximumPoints;
00681 
00682       /*
00683        * The maximum (log) probability of each Markov chain. The length of
00684        * the vector is fMCMCNChains. */
00685       std::vector <double> fMCMCMaximumLogProb;
00686 
00687       /*
00688        * The R-value criterion for convergence of log-likelihood*/
00689       double fMCMCRValueCriterion;
00690 
00691       /*
00692        * The R-value criterion for convergence of parameters */
00693       double fMCMCRValueParametersCriterion;
00694 
00695       /*
00696        * The R-value at which the chains did converge */
00697       double fMCMCRValue;
00698 
00699       /* The R-values for each parameter */
00700       std::vector <double> fMCMCRValueParameters;
00701 
00702       /*
00703        * Random number generator */
00704       TRandom3 * fMCMCRandom;
00705 
00706       /*
00707        * Number of bins per dimension for the marginalized distributions. */
00708       std::vector<int> fMCMCH1NBins;
00709 
00710       /*
00711        * An array of marginalized distributions */
00712       std::vector <TH1D *> fMCMCH1Marginalized;
00713       std::vector <TH2D *> fMCMCH2Marginalized;
00714 
00715       /*
00716        * The trees containing the Markov chains. The length of the vector
00717        * is fMCMCNChains. */
00718       std::vector<TTree *> fMCMCTrees;
00719 };
00720 
00721 // ---------------------------------------------------------
00722 
00723 #endif

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