00001 #ifndef __BCINTEGRATE__H 00002 #define __BCINTEGRATE__H 00003 00004 /*! 00005 * \class BCIntegrate 00006 * \brief A class for handling numerical operations for models. 00007 * \author Daniel Kollar 00008 * \author Kevin Kröninger 00009 * \version 1.0 00010 * \date 08.2008 00011 * \detail This is a base class for a model class. It contains 00012 * numerical methods to carry out the integration, marginalization, 00013 * peak finding etc. 00014 */ 00015 00016 /* 00017 * Copyright (C) 2008-2010, 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 00050 /** \name Enumerators */ 00051 /* @{ */ 00052 00053 /** 00054 * An enumerator for the integration algorithm */ 00055 enum BCIntegrationMethod { kIntMonteCarlo, kIntImportance, kIntMetropolis, kIntCuba, NIntMethods }; 00056 00057 /** 00058 * An enumerator for the marginalization algorithm */ 00059 enum BCMarginalizationMethod { kMargMonteCarlo, kMargMetropolis, NMargMethods }; 00060 00061 /** 00062 * An enumerator for the mode finding algorithm */ 00063 enum BCOptimizationMethod { kOptSA, kOptMetropolis, kOptMinuit, NOptMethods }; 00064 00065 /** 00066 * An enumerator for the Simulated Annealing schedule */ 00067 enum BCSASchedule { kSACauchy, kSABoltzmann, kSACustom, NSAMethods }; 00068 00069 /** 00070 * An enumerator for Cuba integration method */ 00071 enum BCCubaMethod { kCubaVegas, kCubaSuave, kCubaDivonne, kCubaCuhre }; 00072 00073 /* @} */ 00074 00075 /** \name Constructors and destructors */ 00076 /* @{ */ 00077 00078 /** 00079 * The default constructor */ 00080 BCIntegrate(); 00081 00082 /** 00083 * A constructor */ 00084 BCIntegrate(BCParameterSet * par); 00085 00086 /** 00087 * The default destructor */ 00088 virtual ~BCIntegrate(); 00089 00090 /* @} */ 00091 00092 /** \name Member functions (get) */ 00093 /* @{ */ 00094 00095 /** 00096 * @return The integration method */ 00097 BCIntegrate::BCIntegrationMethod GetIntegrationMethod() 00098 { return fIntegrationMethod; }; 00099 00100 /** 00101 * @return The marginalization method */ 00102 BCIntegrate::BCMarginalizationMethod GetMarginalizationMethod() 00103 { return fMarginalizationMethod; }; 00104 00105 /** 00106 * @return The current optimization method */ 00107 BCIntegrate::BCOptimizationMethod GetOptimizationMethod() 00108 { return fOptimizationMethod; }; 00109 00110 /** 00111 * @return The optimization method used to find the mode */ 00112 BCIntegrate::BCOptimizationMethod GetOptimizationMethodMode() 00113 { return fOptimizationMethodMode; }; 00114 00115 /** 00116 * @return The Simulated Annealing schedule */ 00117 BCIntegrate::BCSASchedule GetSASchedule() 00118 { return fSASchedule; }; 00119 00120 /** 00121 * Fills a vector of random numbers between 0 and 1 into a vector 00122 * @param A vector of doubles */ 00123 void GetRandomVector(std::vector <double> &x); 00124 00125 virtual void GetRandomVectorMetro(std::vector <double> &x); 00126 00127 /** 00128 * Fills a vector of (flat) random numbers in the limits of the parameters and returns 00129 * the probability at that point 00130 * @param x A vector of doubles 00131 * @return The (unnormalized) probability at the random point */ 00132 double GetRandomPoint(std::vector <double> &x); 00133 00134 /** 00135 * Fills a vector of random numbers in the limits of the parameters sampled by the sampling 00136 * function and returns the probability at that point 00137 * @param x A vector of doubles 00138 * @return The (unnormalized) probability at the random point */ 00139 double GetRandomPointImportance(std::vector <double> &x); 00140 00141 /** 00142 * Fills a vector of random numbers in the limits of the parameters sampled by the probality 00143 * function and returns the probability at that point (Metropolis) 00144 * @param x A vector of doubles */ 00145 void GetRandomPointMetro(std::vector <double> &x); 00146 00147 /** 00148 * Fills a vector of random numbers in the limits of the parameters sampled by the sampling 00149 * function and returns the probability at that point (Metropolis) 00150 * @param x A vector of doubles */ 00151 void GetRandomPointSamplingMetro(std::vector <double> &x); 00152 00153 /** 00154 * @return The number of iterations per dimension for the Monte Carlo integration */ 00155 int GetNiterationsPerDimension() 00156 { return fNiterPerDimension; }; 00157 00158 /** 00159 * @return Number of samples per 2D bin per variable in the Metropolis marginalization. */ 00160 int GetNSamplesPer2DBin() 00161 { return fNSamplesPer2DBin; }; 00162 00163 /** 00164 * @return The number of variables to integrate over */ 00165 int GetNvar() 00166 { return fNvar; }; 00167 00168 /** 00169 * @return The number of maximum iterations for Monte Carlo integration */ 00170 int GetNIterationsMax() 00171 { return fNIterationsMax; }; 00172 00173 /** 00174 * @return The number of iterations for the most recent Monte Carlo integration */ 00175 int GetNIterations() 00176 { return fNIterations; }; 00177 00178 /** 00179 * @return Requested relative precision of the numerical integation */ 00180 double GetRelativePrecision() 00181 { return fRelativePrecision; }; 00182 00183 /** 00184 * @return Requested absolute precision of the numerical integation */ 00185 double GetAbsolutePrecision() 00186 { return fAbsolutePrecision; }; 00187 00188 /** 00189 * @return Cuba Integration method */ 00190 BCCubaMethod GetCubaIntegrationMethod() 00191 { return fCubaIntegrationMethod; }; 00192 00193 /** 00194 * @return Minimum number of evaluations in Cuba integration */ 00195 int GetCubaMinEval() 00196 { return fCubaMinEval; }; 00197 00198 /** 00199 * @return Maximum number of evaluations in Cuba integration */ 00200 int GetCubaMaxEval() 00201 { return fCubaMaxEval; }; 00202 00203 /** 00204 * @return Verbosity level of Cuba integration */ 00205 int GetCubaVerbositylevel() 00206 { return fCubaVerbosity; }; 00207 00208 /** 00209 * @return Initial number of evaluations per iteration for Cuba Vegas */ 00210 int GetCubaVegasNStart() 00211 { return fCubaVegasNStart; }; 00212 00213 /** 00214 * @return Increase in number of evaluations per iteration for Cuba Vegas */ 00215 int GetCubaVegasNIncrease() 00216 { return fCubaVegasNIncrease; }; 00217 00218 /** 00219 * @return Number of new integrand evaluations in each subdivision for Cuba Suave */ 00220 int GetCubaSuaveNNew() 00221 { return fCubaSuaveNNew; }; 00222 00223 /** 00224 * @return Flatness for Cuba Suave */ 00225 double GetCubaSuaveFlatness() 00226 { return fCubaSuaveFlatness; } 00227 00228 /* 00229 * @return The uncertainty in the most recent Monte Carlo integration */ 00230 double GetError() 00231 { return fError; }; 00232 00233 /* 00234 * @return number of bins per dimension for the marginalized distributions */ 00235 int GetNbins() 00236 { return fNbins; }; 00237 00238 /* 00239 * @return Minuit used for mode finding */ 00240 TMinuit * GetMinuit(); 00241 00242 int GetMinuitErrorFlag() 00243 { return fMinuitErrorFlag; }; 00244 00245 /* 00246 * @return The ROOT tree containing the Markov chain */ 00247 TTree * GetMarkovChainTree() 00248 { return fMarkovChainTree; }; 00249 00250 /* 00251 * Returns the actual point in the markov chain */ 00252 std::vector<double> * GetMarkovChainPoint() 00253 { return &fXmetro1; }; 00254 00255 /** 00256 * Returns the iteration of the MCMC */ 00257 int * GetMCMCIteration() 00258 { return &fMCMCIteration; }; 00259 00260 /** 00261 * Returns the value of the loglikelihood at the point fXmetro1 */ 00262 double * GetMarkovChainValue() 00263 { return &fMarkovChainValue; }; 00264 00265 /** 00266 * Returns the Simulated Annealing starting temperature. */ 00267 double GetSAT0() 00268 { return fSAT0; } 00269 00270 /** 00271 * Returns the Simulated Annealing threshhold temperature. */ 00272 double GetSATmin() 00273 { return fSATmin; } 00274 00275 /* @} */ 00276 00277 /** \name Member functions (set) */ 00278 /* @{ */ 00279 00280 void SetMinuitArlist(double * arglist) 00281 { fMinuitArglist[0] = arglist[0]; 00282 fMinuitArglist[1] = arglist[1]; }; 00283 00284 void SetFlagIgnorePrevOptimization(bool flag) 00285 { fFlagIgnorePrevOptimization = flag; }; 00286 00287 /** 00288 * @param par The parameter set which gets translated into array 00289 * needed for the Monte Carlo integration */ 00290 void SetParameters(BCParameterSet * par); 00291 00292 /** 00293 * @param varlist A list of parameters */ 00294 void SetVarList(int * varlist); 00295 00296 /** 00297 * @param index The index of the variable to be set */ 00298 void SetVar(int index){fVarlist[index]=1;}; 00299 00300 /** 00301 * @param method The integration method */ 00302 void SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method); 00303 00304 /** 00305 * @param method The marginalization method */ 00306 void SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method) 00307 { fMarginalizationMethod = method; }; 00308 00309 /** 00310 * @param method The mode finding method */ 00311 void SetOptimizationMethod(BCIntegrate::BCOptimizationMethod method) 00312 { fOptimizationMethod = method; }; 00313 00314 /** 00315 * @param method The Simulated Annealing schedule */ 00316 void SetSASchedule(BCIntegrate::BCSASchedule schedule) 00317 { fSASchedule = schedule; }; 00318 00319 /** 00320 * @param niterations Number of iterations per dimension for Monte Carlo integration. */ 00321 void SetNiterationsPerDimension(int niterations) 00322 { fNiterPerDimension = niterations; }; 00323 00324 /** 00325 * @param n Number of samples per 2D bin per variable in the Metropolis marginalization. 00326 * Default is 100. */ 00327 void SetNSamplesPer2DBin(int n) 00328 { fNSamplesPer2DBin = n; }; 00329 00330 /** 00331 * @param niterations The maximum number of iterations for Monte Carlo integration */ 00332 void SetNIterationsMax(int niterations) 00333 { fNIterationsMax = niterations; }; 00334 00335 /** 00336 * @param relprecision The relative precision envisioned for Monte 00337 * Carlo integration */ 00338 void SetRelativePrecision(double relprecision) 00339 { fRelativePrecision = relprecision; }; 00340 00341 /** 00342 * Set absolute precision of the numerical integation */ 00343 void SetAbsolutePrecision(double absprecision) 00344 { fAbsolutePrecision = absprecision; }; 00345 00346 /** 00347 * Set Cuba integration method */ 00348 void SetCubaIntegrationMethod(BCCubaMethod type); 00349 00350 /** 00351 * Set minimum number of evaluations in Cuba integration */ 00352 void SetCubaMinEval(int n) 00353 { fCubaMinEval = n; }; 00354 00355 /** 00356 * Set maximum number of evaluations in Cuba integration */ 00357 void SetCubaMaxEval(int n) 00358 { fCubaMaxEval = n; }; 00359 00360 /** 00361 * Set verbosity level of Cuba integration */ 00362 void SetCubaVerbosityLevel(int n) 00363 { fCubaVerbosity = n; }; 00364 00365 /** 00366 * Set initial number of evaluations per iteration for Cuba Vegas */ 00367 void SetCubaVegasNStart(int n) 00368 { fCubaVegasNStart = n; }; 00369 00370 /** 00371 * Set increase in number of evaluations per iteration for Cuba Vegas */ 00372 void SetCubaVegasNIncrease(int n) 00373 { fCubaVegasNIncrease = n; }; 00374 00375 /** 00376 * Set number of new integrand evaluations in each subdivision for Cuba Suave */ 00377 void SetCubaSuaveNNew(int n) 00378 { fCubaSuaveNNew = n; }; 00379 00380 /** 00381 * Set flatness for Cuba Suave */ 00382 void SetCubaSuaveFlatness(double p) 00383 { fCubaSuaveFlatness = p; } 00384 00385 /** 00386 * @param n Number of bins per dimension for the marginalized 00387 * distributions. Default is 100. Minimum number allowed is 2. */ 00388 00389 /** 00390 * Set the number of bins for the marginalized distribution of a 00391 * parameter. 00392 * @param nbins Number of bins (default = 100) 00393 * @param index Index of the parameter. */ 00394 void SetNbins(int nbins, int index = -1); 00395 00396 // void SetNbins(int n); 00397 // void SetNbinsX(int n); 00398 // void SetNbinsY(int n); 00399 00400 /* 00401 * Turn on or off the filling of the error band during the MCMC run. 00402 * @param flag set to true for turning on the filling */ 00403 void SetFillErrorBand(bool flag = true) 00404 { fFillErrorBand=flag; }; 00405 00406 /* 00407 * Turn off filling of the error band during the MCMC run. 00408 * This method is equivalent to SetFillErrorBand(false) */ 00409 void UnsetFillErrorBand() 00410 { fFillErrorBand=false; }; 00411 00412 /** 00413 * Sets index of the x values in function fits. 00414 * @param index Index of the x values */ 00415 void SetFitFunctionIndexX(int index) 00416 { fFitFunctionIndexX = index; }; 00417 00418 /** 00419 * Sets index of the y values in function fits. @param index Index 00420 * of the y values */ 00421 void SetFitFunctionIndexY(int index) 00422 { fFitFunctionIndexY = index; }; 00423 00424 void SetFitFunctionIndices(int indexx, int indexy) 00425 { this -> SetFitFunctionIndexX(indexx); 00426 this -> SetFitFunctionIndexY(indexy); }; 00427 00428 /** 00429 * Sets the data point containing the lower boundaries of possible 00430 * data values */ 00431 void SetDataPointLowerBoundaries(BCDataPoint * datasetlowerboundaries) 00432 { fDataPointLowerBoundaries = datasetlowerboundaries; }; 00433 00434 /** 00435 * Sets the data point containing the upper boundaries of possible 00436 * data values */ 00437 void SetDataPointUpperBoundaries(BCDataPoint * datasetupperboundaries) 00438 { fDataPointUpperBoundaries = datasetupperboundaries; }; 00439 00440 /** 00441 * Sets the lower boundary of possible data values for a particular 00442 * variable */ 00443 void SetDataPointLowerBoundary(int index, double lowerboundary) 00444 { fDataPointLowerBoundaries -> SetValue(index, lowerboundary); }; 00445 00446 /** 00447 * Sets the upper boundary of possible data values for a particular 00448 * variable */ 00449 void SetDataPointUpperBoundary(int index, double upperboundary) 00450 { fDataPointUpperBoundaries -> SetValue(index, upperboundary); }; 00451 00452 /** 00453 * Flag for writing Markov chain to ROOT file (true) or not (false) 00454 */ 00455 void WriteMarkovChain(bool flag) 00456 { fFlagWriteMarkovChain = flag; 00457 fMCMCFlagWriteChainToFile = flag; 00458 fMCMCFlagWritePreRunToFile = flag; }; 00459 00460 /** 00461 * Sets the ROOT tree containing the Markov chain */ 00462 void SetMarkovChainTree(TTree * tree) 00463 { fMarkovChainTree = tree; }; 00464 00465 /** 00466 * Sets the initial position for the Markov chain */ 00467 void SetMarkovChainInitialPosition(std::vector<double> position); 00468 00469 /** 00470 * Sets the step size for Markov chains */ 00471 void SetMarkovChainStepSize(double stepsize) 00472 { fMarkovChainStepSize = stepsize; }; 00473 00474 /** 00475 * Sets the number of iterations in the markov chain */ 00476 void SetMarkovChainNIterations(int niterations) 00477 { fMarkovChainNIterations = niterations; 00478 fMarkovChainAutoN = false; } 00479 00480 /** 00481 * Sets a flag for automatically calculating the number of iterations */ 00482 void SetMarkovChainAutoN(bool flag) 00483 { fMarkovChainAutoN = flag; }; 00484 00485 /** 00486 * Sets mode */ 00487 void SetMode(std::vector <double> mode); 00488 00489 /** 00490 * Sets errorband histogram */ 00491 void SetErrorBandHisto(TH2D * h) 00492 { fErrorBandXY = h; }; 00493 00494 /** 00495 * @param T0 new value for Simulated Annealing starting temperature. */ 00496 void SetSAT0(double T0) 00497 { fSAT0 = T0; } 00498 00499 /** 00500 * @param Tmin new value for Simulated Annealing threshold temperature. */ 00501 void SetSATmin(double Tmin) 00502 { fSATmin = Tmin; } 00503 00504 void SetFlagWriteSAToFile(bool flag) 00505 { fFlagWriteSAToFile = flag; }; 00506 00507 /* 00508 * Sets the tree containing the Simulated Annealing chain. */ 00509 void SetSATree(TTree * tree) 00510 { fTreeSA = tree; }; 00511 00512 /* 00513 * Getter for the tree containing the Simulated Annealing chain. */ 00514 TTree * GetSATree() 00515 { return fTreeSA; }; 00516 00517 /* 00518 * Initialization of the tree for the Simulated Annealing */ 00519 void InitializeSATree(); 00520 00521 /* @} */ 00522 00523 /** \name Member functions (miscellaneous methods) */ 00524 /* @{ */ 00525 00526 /** 00527 * Frees the memory for integration variables */ 00528 void DeleteVarList(); 00529 00530 /** 00531 * Sets all values of the variable list to a particular value 00532 * @v The value */ 00533 void ResetVarlist(int v); 00534 00535 /** 00536 * Set value of a particular integration variable to 0. 00537 * @param index The index of the variable */ 00538 void UnsetVar(int index) 00539 { fVarlist[index] = 0; }; 00540 00541 /** 00542 * Evaluate the unnormalized probability at a point in parameter space. 00543 * Method needs to be overloaded by the user. 00544 * @param x The point in parameter space 00545 * @return The unnormalized probability */ 00546 virtual double Eval(std::vector <double> x); 00547 00548 /** 00549 * Evaluate the natural logarithm of the Eval function. For better numerical 00550 * stability, this method should also be overloaded by the user. 00551 * @param x The point in parameter space 00552 * @return log(Eval(x)) */ 00553 virtual double LogEval(std::vector <double> x); 00554 00555 /** 00556 * Evaluate the sampling function at a point in parameter space. 00557 * Method needs to be overloaded by the user. 00558 * @param x The point in parameter space 00559 * @return The value of the sampling function */ 00560 virtual double EvalSampling(std::vector <double> x); 00561 00562 /** 00563 * Evaluate the natural logarithm of the EvalSampling function. 00564 * Method needs to be overloaded by the user. 00565 * @param x The point in parameter space 00566 * @return log(EvalSampling(x)) */ 00567 double LogEvalSampling(std::vector <double> x); 00568 00569 /** 00570 * Evaluate the un-normalized probability at a point in parameter space 00571 * and prints the result to the log. 00572 * @param x The point in parameter space 00573 * @return The un-normalized probability 00574 * @see Eval(std::vector <double> x) */ 00575 double EvalPrint(std::vector <double> x); 00576 00577 /** 00578 * Defines a fit function. 00579 * @param parameters A set of parameter values 00580 * @param x A vector of x-values 00581 * @return The value of the fit function at the x-values given a set of parameters */ 00582 virtual double FitFunction(std::vector <double> /*x*/, std::vector <double> /*parameters*/) 00583 { return 0.; }; 00584 00585 /** 00586 * Does the integration over the un-normalized probability. 00587 * @return The normalization value */ 00588 double Integrate(); 00589 00590 /** 00591 * Perfoms the Monte Carlo integration. For details see documentation. 00592 * @param x An initial point in parameter space 00593 * @param varlist A list of variables 00594 * @return The integral */ 00595 double IntegralMC(std::vector <double> x, int * varlist); 00596 00597 double IntegralMC(std::vector <double> x); 00598 00599 /** 00600 * Perfoms the Metropolis Monte Carlo integration. For details see documentation. 00601 * @param x An initial point in parameter space 00602 * @return The integral */ 00603 double IntegralMetro(std::vector <double> x); 00604 00605 /** 00606 * Perfoms the importance sampling Monte Carlo integration. For details see documentation. 00607 * @param x An initial point in parameter space 00608 * @return The integral */ 00609 double IntegralImportance(std::vector <double> x); 00610 00611 /** 00612 * Calculate integral using the Cuba library. For details see documentation. 00613 * @param method A short cut for the method 00614 * @param parameters_double A vector of parameters (double) 00615 * @param parameters_int A vector of parameters (int) 00616 * @return The integral */ 00617 double CubaIntegrate(BCIntegrate::BCCubaMethod method, std::vector<double> parameters_double, std::vector<double> parameters_int); 00618 00619 double CubaIntegrate(); 00620 00621 /** 00622 * Integrand for the Cuba library. 00623 * @param ndim The number of dimensions to integrate over 00624 * @param xx The point in parameter space to integrate over (scaled to 0 - 1 per dimension) 00625 * @param ncomp The number of components of the integrand (usually 1) 00626 * @param ff The function value 00627 * @return The integral */ 00628 // static void CubaIntegrand(const int * ndim, const double xx[], const int * ncomp, double ff[]); 00629 static int CubaIntegrand(const int * ndim, const double xx[], const int * ncomp, double ff[], void *userdata); 00630 00631 /** 00632 * Performs the marginalization with respect to one parameter. 00633 * @param parameter The parameter w.r.t. which the marginalization is performed 00634 * @return A histogram which contains the marginalized probability distribution (normalized to 1) */ 00635 TH1D * Marginalize(BCParameter * parameter); 00636 00637 /** 00638 * Performs the marginalization with respect to two parameters. 00639 * @param parameter1 The first parameter w.r.t. which the marginalization is performed 00640 * @param parameter2 The second parameter w.r.t. which the marginalization is performed 00641 * @return A histogram which contains the marginalized probability distribution (normalized to 1) */ 00642 TH2D * Marginalize(BCParameter * parameter1, BCParameter * parameter2); 00643 00644 /** 00645 * Performs the marginalization with respect to one parameter using 00646 * the simple Monte Carlo technique. 00647 * @param parameter The parameter w.r.t. which the marginalization is performed 00648 * @return A histogram which contains the marginalized probability distribution (normalized to 1) */ 00649 TH1D * MarginalizeByIntegrate(BCParameter * parameter); 00650 00651 /** 00652 * Performs the marginalization with respect to two parameters using 00653 * the simple Monte Carlo technique. 00654 * @param parameter1 The first parameter w.r.t. which the marginalization is performed 00655 * @param parameter2 The second parameter w.r.t. which the marginalization is performed 00656 * @return A histogram which contains the marginalized probability distribution (normalized to 1) */ 00657 TH2D * MarginalizeByIntegrate(BCParameter * parameter1, BCParameter * parameter2); 00658 00659 /** 00660 * Performs the marginalization with respect to one parameter using 00661 * the Metropolis algorithm. 00662 * @param parameter The parameter w.r.t. which the marginalization is performed 00663 * @return A histogram which contains the marginalized probability distribution (normalized to 1) */ 00664 TH1D * MarginalizeByMetro(BCParameter * parameter); 00665 00666 /** 00667 * Performs the marginalization with respect to two parameters using the Metropolis algorithm. 00668 * @param parameter1 The first parameter w.r.t. which the marginalization is performed 00669 * @param parameter2 The second parameter w.r.t. which the marginalization is performed 00670 * @return A histogram which contains the marginalized probability distribution (normalized to 1) */ 00671 TH2D * MarginalizeByMetro(BCParameter * parameter1, BCParameter * parameter2); 00672 00673 /** 00674 * Performs the marginalization with respect to every single parameter as well as with respect 00675 * all combinations to two parameters using the Metropolis algorithm. 00676 * @param name Basename for the histograms (e.g. model name) 00677 * @return Total number of marginalized distributions */ 00678 int MarginalizeAllByMetro(const char * name); 00679 00680 /** 00681 * @param parIndex1 Index of parameter 00682 * @return Pointer to 1D histogram (TH1D) of marginalized distribution wrt. parameter with given index. 00683 */ 00684 TH1D * GetH1D(int parIndex); 00685 00686 /** 00687 * @param parIndex1 Index of first parameter 00688 * @param parIndex2 Index of second parameter, with parIndex2>parIndex1 00689 * @return Index of the distribution in the vector of 2D distributions, which corresponds 00690 * to the combination of parameters with given indeces */ 00691 int GetH2DIndex(int parIndex1, int parIndex2); 00692 00693 /** 00694 * @param parIndex1 Index of first parameter 00695 * @param parIndex2 Index of second parameter, with parIndex2>parIndex1 00696 * @return Pointer to 2D histogram (TH2D) of marginalized distribution wrt. parameters with given indeces. 00697 */ 00698 TH2D * GetH2D(int parIndex1, int parIndex2); 00699 00700 /** 00701 * Initializes the Metropolis algorithm (for details see manual) */ 00702 void InitMetro(); 00703 00704 void SAInitialize(); 00705 00706 /** 00707 * Does the mode finding */ 00708 // void FindMode(); 00709 00710 /** 00711 * Does the mode finding using Minuit. If starting point is not specified, 00712 * finding will start from the center of the parameter space. 00713 * @param start point in parameter space from which the mode finding is started. 00714 * @param printlevel The print level. */ 00715 virtual void FindModeMinuit(std::vector<double> start = std::vector<double>(0), int printlevel = 1); 00716 00717 /** 00718 * Does the mode finding using Markov Chain Monte Carlo (prerun only!) */ 00719 void FindModeMCMC(); 00720 // void FindModeMCMC(int flag_run = 0); 00721 00722 /** 00723 * Does the mode finding using Simulated Annealing. If starting point 00724 * is not specified, finding will start from the center of the 00725 * parameter space. 00726 * @param start point in parameter space from thich the mode finding is started. */ 00727 void FindModeSA(std::vector<double> start = std::vector<double>(0)); 00728 00729 /** 00730 * Temperature annealing schedule for use with Simulated Annealing. 00731 * Delegates to the appropriate method according to 00732 * fSASchedule. 00733 * @param t iterator for lowering the temperature over time. */ 00734 double SATemperature(double t); 00735 00736 /** 00737 * Temperature annealing schedule for use with Simulated Annealing. 00738 * This method is used for Boltzmann annealing schedule. 00739 * @param t iterator for lowering the temperature over time. */ 00740 double SATemperatureBoltzmann(double t); 00741 00742 /** 00743 * Temperature annealing schedule for use with Simulated Annealing. 00744 * This method is used for Cauchy annealing schedule. 00745 * @param t iterator for lowering the temperature over time. */ 00746 double SATemperatureCauchy(double t); 00747 00748 /** 00749 * Temperature annealing schedule for use with Simulated Annealing. 00750 * This is a virtual method to be overridden by a user-defined 00751 * custom temperature schedule. 00752 * @param t iterator for lowering the temperature over time. */ 00753 virtual double SATemperatureCustom(double t); 00754 00755 /** 00756 * Generates a new state in a neighbourhood around x that is to be 00757 * accepted or rejected by the Simulated Annealing algorithm. 00758 * Delegates the generation to the appropriate method according 00759 * to fSASchedule. 00760 * @param x last state. 00761 * @param t time iterator to determine current temperature. */ 00762 std::vector<double> GetProposalPointSA(std::vector<double> x, int t); 00763 00764 /** 00765 * Generates a new state in a neighbourhood around x that is to be 00766 * accepted or rejected by the Simulated Annealing algorithm. 00767 * This method is used for Boltzmann annealing schedule. 00768 * @param x last state. 00769 * @param t time iterator to determine current temperature. */ 00770 std::vector<double> GetProposalPointSABoltzmann(std::vector<double> x, int t); 00771 00772 /** 00773 * Generates a new state in a neighbourhood around x that is to be 00774 * accepted or rejected by the Simulated Annealing algorithm. 00775 * This method is used for Cauchy annealing schedule. 00776 * @param x last state. 00777 * @param t time iterator to determine current temperature. */ 00778 std::vector<double> GetProposalPointSACauchy(std::vector<double> x, int t); 00779 00780 /** 00781 * Generates a new state in a neighbourhood around x that is to be 00782 * accepted or rejected by the Simulated Annealing algorithm. 00783 * This is a virtual method to be overridden by a user-defined 00784 * custom proposal function. 00785 * @param x last state. 00786 * @param t time iterator to determine current temperature. */ 00787 virtual std::vector<double> GetProposalPointSACustom(std::vector<double> x, int t); 00788 00789 /** 00790 * Generates a uniform distributed random point on the surface of 00791 * a fNvar-dimensional Hypersphere. 00792 * Used as a helper to generate proposal points for Cauchy annealing. */ 00793 std::vector<double> SAHelperGetRandomPointOnHypersphere(); 00794 00795 /** 00796 * Generates the radial part of a n-dimensional Cauchy distribution. 00797 * Helper function for Cauchy annealing. */ 00798 double SAHelperGetRadialCauchy(); 00799 00800 /** 00801 * Returns the Integral of sin^dim from 0 to theta. 00802 * Helper function needed for generating Cauchy distributions. */ 00803 double SAHelperSinusToNIntegral(int dim, double theta); 00804 00805 00806 static void FCNLikelihood(int &npar, double * grad, double &fval, double * par, int flag); 00807 00808 /* 00809 * Method executed for every iteration of the MCMC. User's code should be 00810 * provided via overloading in the derived class*/ 00811 virtual void MCMCUserIterationInterface() 00812 {}; 00813 00814 /** 00815 * Reset all information on the best fit parameters. 00816 * @return An error code */ 00817 int IntegrateResetResults(); 00818 00819 std::string DumpIntegrationMethod(BCIntegrationMethod type); 00820 std::string DumpIntegrationMethod() 00821 { return DumpIntegrationMethod(fIntegrationMethod); }; 00822 00823 std::string DumpMarginalizationMethod(BCMarginalizationMethod type); 00824 std::string DumpMarginalizationMethod() 00825 { return DumpMarginalizationMethod(fMarginalizationMethod); }; 00826 00827 std::string DumpOptimizationMethod(BCOptimizationMethod type); 00828 std::string DumpOptimizationMethod() 00829 { return DumpOptimizationMethod(fOptimizationMethod); }; 00830 std::string DumpUsedOptimizationMethod() 00831 { return DumpOptimizationMethod(fOptimizationMethodMode); }; 00832 00833 std::string DumpCubaIntegrationMethod(BCCubaMethod type); 00834 std::string DumpCubaIntegrationMethod() 00835 { return DumpCubaIntegrationMethod(fCubaIntegrationMethod); }; 00836 00837 00838 /* @} */ 00839 00840 protected: 00841 00842 /** 00843 * Number of variables to integrate over. */ 00844 int fNvar; 00845 00846 /** 00847 * Number of bins per dimension for the marginalized distributions */ 00848 int fNbins; 00849 00850 /** 00851 * Number of samples per 2D bin per variable in the Metropolis 00852 * marginalization. */ 00853 int fNSamplesPer2DBin; 00854 00855 /** 00856 * Step size in the Markov chain relative to min and max */ 00857 double fMarkovChainStepSize; 00858 00859 int fMarkovChainNIterations; 00860 00861 bool fMarkovChainAutoN; 00862 00863 /** 00864 * data point containing the lower boundaries of possible data values */ 00865 BCDataPoint * fDataPointLowerBoundaries; 00866 00867 /** 00868 * data point containing the upper boundaries of possible data values */ 00869 BCDataPoint * fDataPointUpperBoundaries; 00870 00871 std::vector <bool> fDataFixedValues; 00872 00873 /** 00874 * A vector of best fit parameters estimated from the global 00875 * probability and the estimate on their uncertainties */ 00876 std::vector <double> fBestFitParameters; 00877 std::vector <double> fBestFitParameterErrors; 00878 00879 /** 00880 * A vector of best fit parameters estimated from the marginalized probability */ 00881 std::vector <double> fBestFitParametersMarginalized; 00882 00883 /** 00884 * Vector of TH1D histograms for marginalized probability distributions */ 00885 std::vector <TH1D *> fHProb1D; 00886 00887 /** 00888 * Vector of TH2D histograms for marginalized probability distributions */ 00889 std::vector <TH2D *> fHProb2D; 00890 00891 /** 00892 * Flag whether or not to fill the error band */ 00893 bool fFillErrorBand; 00894 00895 /** 00896 * The indices for function fits */ 00897 int fFitFunctionIndexX; 00898 int fFitFunctionIndexY; 00899 00900 /** 00901 * A flag for single point evaluation of the error "band" */ 00902 bool fErrorBandContinuous; 00903 std::vector <double> fErrorBandX; 00904 00905 /** 00906 * The error band histogram */ 00907 TH2D * fErrorBandXY; 00908 00909 /** 00910 * Number of X bins of the error band histogram */ 00911 int fErrorBandNbinsX; 00912 00913 /** 00914 * Nnumber of Y bins of the error band histogram */ 00915 int fErrorBandNbinsY; 00916 00917 /** 00918 * Minuit */ 00919 TMinuit * fMinuit; 00920 00921 double fMinuitArglist[2]; 00922 int fMinuitErrorFlag; 00923 00924 /** 00925 * Flag for ignoring older results of minimization */ 00926 double fFlagIgnorePrevOptimization; 00927 00928 /** 00929 * Flag for writing Markov chain to file */ 00930 bool fFlagWriteMarkovChain; 00931 00932 /** 00933 * ROOT tree containing the Markov chain */ 00934 TTree * fMarkovChainTree; 00935 00936 /** 00937 * Iteration of the MCMC */ 00938 int fMCMCIteration; 00939 00940 /** 00941 * Starting temperature for Simulated Annealing */ 00942 double fSAT0; 00943 00944 /** 00945 * Minimal/Threshold temperature for Simulated Annealing */ 00946 double fSATmin; 00947 00948 /** 00949 * Tree for the Simulated Annealing */ 00950 TTree * fTreeSA; 00951 00952 /** 00953 * Flag deciding whether to write SA to file or not. */ 00954 bool fFlagWriteSAToFile; 00955 00956 int fSANIterations; 00957 double fSATemperature; 00958 double fSALogProb; 00959 std::vector<double> fSAx; 00960 00961 private: 00962 00963 /** 00964 * Set of parameters for the integration. */ 00965 BCParameterSet * fx; 00966 00967 /** 00968 * Array containing the lower boundaries of the variables to integrate over. */ 00969 double * fMin; 00970 00971 /** 00972 * Array containing the upper boundaries of the variables to integrate over. */ 00973 double * fMax; 00974 00975 /** 00976 * List of variables containing a flag whether to integrate over them or not. */ 00977 int * fVarlist; 00978 00979 /** 00980 * Number of iteration per dimension for Monte Carlo integration. */ 00981 int fNiterPerDimension; 00982 00983 /** 00984 * Current integration method */ 00985 BCIntegrate::BCIntegrationMethod fIntegrationMethod; 00986 00987 /** 00988 * Current marginalization method */ 00989 BCIntegrate::BCMarginalizationMethod fMarginalizationMethod; 00990 00991 /** 00992 * Current mode finding method */ 00993 BCIntegrate::BCOptimizationMethod fOptimizationMethod; 00994 00995 /** 00996 * Method with which the global mode was found (can differ from 00997 * fOptimization method in case more than one algorithm is used). */ 00998 BCIntegrate::BCOptimizationMethod fOptimizationMethodMode; 00999 01000 /** 01001 * Current Simulated Annealing schedule */ 01002 BCIntegrate::BCSASchedule fSASchedule; 01003 01004 /** 01005 * Maximum number of iterations */ 01006 int fNIterationsMax; 01007 01008 /** 01009 * Number of iterations in the most recent Monte Carlo integation */ 01010 int fNIterations; 01011 01012 /** Requested relative precision of the integation */ 01013 double fRelativePrecision; 01014 01015 /** Requested relative precision of the integation */ 01016 double fAbsolutePrecision; 01017 01018 /** Cuba integration method */ 01019 BCCubaMethod fCubaIntegrationMethod; 01020 01021 /** Minimum number of evaluations in Cuba integration */ 01022 int fCubaMinEval; 01023 01024 /** Maximum number of evaluations in Cuba integration */ 01025 int fCubaMaxEval; 01026 01027 /** Verbosity level of Cuba integration */ 01028 int fCubaVerbosity; 01029 01030 /** Initial number of evaluations per iteration for Cuba Vegas */ 01031 int fCubaVegasNStart; 01032 01033 /** Increase in number of evaluations per iteration for Cuba Vegas */ 01034 int fCubaVegasNIncrease; 01035 01036 /** Number of new integrand evaluations in each subdivision for Cuba Suave */ 01037 int fCubaSuaveNNew; 01038 01039 /** Flatness for Cuba Suave */ 01040 double fCubaSuaveFlatness; 01041 01042 /** 01043 * The uncertainty in the most recent Monte Carlo integration */ 01044 double fError; 01045 01046 /** 01047 * The number of iterations in the Metropolis integration */ 01048 int fNmetro; 01049 int fNacceptedMCMC; 01050 01051 /** 01052 * A vector of points in parameter space used for the Metropolis algorithm */ 01053 std::vector <double> fXmetro0; 01054 01055 /** 01056 * A vector of points in parameter space used for the Metropolis algorithm */ 01057 std::vector <double> fXmetro1; 01058 01059 /** 01060 * A double containing the log likelihood value at the point fXmetro1 */ 01061 double fMarkovChainValue; 01062 01063 /* 01064 * Method executed for every iteration of the MCMC, overloaded from BCEngineMCMC. */ 01065 void MCMCIterationInterface(); 01066 01067 /* 01068 * Fill error band histogram for curreent iteration. This method is called from MCMCIterationInterface() */ 01069 void MCMCFillErrorBand(); 01070 01071 // friend class BCModelOutput; 01072 01073 }; 01074 01075 // --------------------------------------------------------- 01076 01077 #endif