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