BCIntegrate.cxx
166 BCIntegrate::BCIntegrate(const std::string& filename, const std::string& name, bool loadObservables) :
315 if (!(fIntegrationMethodCurrent == BCIntegrate::kIntDefault) && !(fIntegrationMethodCurrent == BCIntegrate::kIntEmpty))
377 BCLog::OutError(Form("BCIntegrate::Integrate : Invalid integration method: %d", fIntegrationMethodCurrent));
405 void BCIntegrate::SetBestFitParameters(const std::vector<double>& x, const double& new_value, double& old_value)
420 void BCIntegrate::LogOutputAtStartOfIntegration(BCIntegrationMethod type, BCCubaMethod cubatype)
475 void BCIntegrate::LogOutputAtEndOfIntegration(double integral, double absprecision, double relprecision, int nIterations)
484 void BCIntegrate::LogOutputAtIntegrationStatusUpdate(BCIntegrationMethod type, double integral, double absprecision, int nIterations)
486 BCLog::OutDetail(Form("%s. Iteration %i, integral: %e +- %e.", DumpIntegrationMethod(type).c_str(), nIterations, integral, absprecision));
490 double BCIntegrate::Integrate(BCIntegrationMethod type, tRandomizer randomizer, tEvaluator evaluator, tIntegralUpdater updater,
511 // or precision is not reached and the number of iterations is lower than maximum number of iterations
512 while ((absprecision > std::max(GetAbsolutePrecision(), GetRelativePrecision() * integral) && GetNIterations() < GetNIterationsMax())
547 BCLog::OutWarning("BCIntegrate::Integrate: Did not converge within maximum number of iterations");
557 double BCIntegrate::EvaluatorMC(std::vector<double>& sums, const std::vector<double>& point, bool& accepted)
572 void BCIntegrate::IntegralUpdaterMC(const std::vector<double>& sums, const int& nIterations, double& integral, double& absprecision)
578 absprecision = sqrt((1.0 / (nIterations - 1)) * (sums[2] * sums[2] * sums[1] / double(nIterations) - integral * integral));
594 BCLog::OutError(Form("BCIntegrate::CheckMarginalizationAvailability. Invalid marginalization method: %d.", type));
614 BCLog::OutError(Form("BCIntegrate::Marginalize : Too few (%d) indices supplied for histogram dimension (%d)", (int)index.size(), hist->GetDimension()));
621 BCLog::OutError(Form("BCIntegrate::Marginalize : Parameter index (%d) out of bound.", index[i]));
627 BCLog::OutError(Form("BCIntegrate::Marginalize : Parameter index (%d) appears more than once", index[i]));
645 BCLog::OutError(Form("BCIntegrate::MarginalizeAll : Marginalization method not implemented \'%s\'. Aborting.", DumpCurrentMarginalizationMethod().c_str()));
650 if (!(fMarginalizationMethodCurrent == BCIntegrate::kMargDefault) && !(fMarginalizationMethodCurrent == BCIntegrate::kMargEmpty))
679 fBestFitParameterErrors.assign(fBestFitParameters.size(), std::numeric_limits<double>::infinity());
695 BCLog::OutWarning("BCIntegrate::MarginalizeAll : Grid marginalization only works for 1D and 2D problems.");
699 BCLog::OutWarning("BCIntegrate::MarginalizeAll : Grid marginalization will not store resutls for user-defined observables.");
726 if (GetParameters().GetNFreeParameters() == 1) { // Marginalize the free parameter to a 1D histogram
762 fH1Marginalized[i] = fH2Marginalized[i][j]->ProjectionX(Form("h1_%s_parameter_%i", GetSafeName().data() , i));
765 fH1Marginalized[j] = fH2Marginalized[i][j]->ProjectionY(Form("h1_%s_parameter_%i", GetSafeName().data() , j));
807 BCLog::OutError(Form("BCIntegrate::MarginalizeAll : Invalid marginalization method: %d", GetMarginalizationMethod()));
840 TH1* BCIntegrate::GetSlice(std::vector<unsigned> indices, unsigned& nIterations, double& log_max_val, const std::vector<double> parameters, int nbins, bool normalize)
881 h = dynamic_cast<TH2*>(slice_NnD)->ProjectionX(Form("h1_slice_%s_%d", GetSafeName().data(), indices[0]));
884 h = dynamic_cast<TH3*>(slice_NnD)->ProjectionX(Form("h1_slice_%s_%d", GetSafeName().data(), indices[0]));
905 if (std::find(indices.begin(), indices.end(), i) == indices.end() && !GetParameter(i).Fixed()) {
906 BCLog::OutError("BCIntegrate::GetSlice : All non-sliced parameters must be fixed or provided values in function call.");
924 h = GetParameter(indices[0]).CreateH1(Form("h1_slice_%s_%d", GetSafeName().data(), indices[0]));
929 h = GetParameter(indices[0]).CreateH2(Form("h2_slice_%s_%d_%d", GetSafeName().data(), indices[0], indices[1]), GetParameter(indices[1]));
934 h = GetParameter(indices[0]).CreateH3(Form("h3_slice_%s_%d_%d_%d", GetSafeName().data(), indices[0], indices[1], indices[2]), GetParameter(indices[1]), GetParameter(indices[2]));
998 TH2* BCIntegrate::GetSlice(unsigned index1, unsigned index2, unsigned& nIterations, double& log_max_val, const std::vector<double> parameters, int nbins, bool normalize)
1040 if (!(fOptimizationMethodCurrent == BCIntegrate::kOptDefault) && !(fOptimizationMethodCurrent == BCIntegrate::kOptEmpty))
1069 BCLog::OutError(Form("BCIntegrate::FindMode : Invalid mode finding method: %d", GetOptimizationMethod()));
1080 fBestFitParameterErrors.resize(GetBestFitParameters().size(), std::numeric_limits<double>::infinity());
1090 std::vector<double> BCIntegrate::FindMode(BCIntegrate::BCOptimizationMethod optmethod, std::vector<double> start)
1109 std::vector<double> BCIntegrate::FindModeMinuit(std::vector<double>& mode, std::vector<double>& errors, std::vector<double> start, int printlevel)
1151 std::copy(fMinimizer.min.Errors(), fMinimizer.min.Errors() + fParameters.Size(), errors.begin());
1157 std::vector<double> BCIntegrate::FindModeSA(std::vector<double>& mode, std::vector<double>& errors, std::vector<double> start)
1170 std::vector<double> x, y; // vectors for current state, new proposed state and best fit up to now
1171 double fval_x, fval_y, fval_mode; // function values at points x, y and mode (we save them rather than to re-calculate them every time)
1176 BCLog::OutWarning("BCIntegrate::FindModeSA : Start point not valid (mismatch of dimensions), set to center.");
1182 BCLog::OutWarning("BCIntegrate::FindModeSA : Start point not valid (parameter not inside valid range), set to center.");
1188 BCLog::OutWarning("BCIntegrate::FindModeSA : Start point fixed values not properly set. Forcing to fixed values.");
1294 std::vector<double> BCIntegrate::GetProposalPointSABoltzmann(const std::vector<double>& x, int t) const
1314 std::vector<double> BCIntegrate::GetProposalPointSACauchy(const std::vector<double>& x, int t) const
1356 std::vector<double> BCIntegrate::GetProposalPointSACustom(const std::vector<double>& /*x*/, int /*t*/) const
1358 BCLog::OutError("BCIntegrate::GetProposalPointSACustom : No custom proposal function defined");
1454 theta = map_theta[lo] + (u - map_u[lo]) / (map_u[up] - map_u[lo]) * (map_theta[up] - map_theta[lo]);
1477 std::vector<double> BCIntegrate::FindModeMCMC(std::vector<double>& mode, std::vector<double>& errors)
1764 BCLog::OutWarning("BCIntegrate::IntegrateSlice: Method only implemented for 1D, 2D, and 3D problems. Return -1.");
1787 BCLog::OutWarning("Laplace requires a successful run of minuit. Rerun with default starting point");
1804 BCLog::OutError("Cannot perform Laplace approximation without minuit's covariance. Return -1");
1890 std::string BCIntegrate::DumpMarginalizationMethod(BCIntegrate::BCMarginalizationMethod type) const
2036 if (i < GetBestFitParameterErrors().size() && GetBestFitParameterErrors()[i] != std::numeric_limits<double>::infinity())
2037 return BCEngineMCMC::GetBestFitSummary(i) + Form(" +- %.*g", GetVariable(i).GetPrecision(), GetBestFitParameterErrors()[i]);
BCEngineMCMC & operator=(const BCEngineMCMC &)
Copy-assignment operator.
Definition: BCEngineMCMC.cxx:191
BCIntegrate::BCMarginalizationMethod GetMarginalizationMethod() const
Definition: BCIntegrate.h:274
virtual TH1 * CreateH1(const std::string &name) const
Creates a 1D Histogram for this variable.
Definition: BCVariable.cxx:132
std::string DumpCubaIntegrationMethod() const
Return string with the name for the currently set Cuba integration type.
Definition: BCIntegrate.h:783
const std::vector< double > & GetBestFitParameterErrors() const
Returns the set of errors on the values of the parameters at the mode.
Definition: BCIntegrate.cxx:282
std::string DumpIntegrationMethod(BCIntegrationMethod type) const
Return string with the name for a given integration type.
Definition: BCIntegrate.cxx:1871
bool MetropolisPreRun()
Runs a pre run for the Metropolis algorithm.
Definition: BCEngineMCMC.cxx:1762
bool CheckMarginalizationAvailability(BCMarginalizationMethod type)
Check availability of integration routine for marginalization.
Definition: BCIntegrate.cxx:582
virtual double LogEval(const std::vector< double > &x)
Evaluate the natural logarithm of the Eval function.
Definition: BCIntegrate.h:555
bool fFlagIgnorePrevOptimization
Flag for ignoring older results of optimization.
Definition: BCIntegrate.h:838
void SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method)
Definition: BCIntegrate.h:465
virtual std::string GetBestFitSummary(unsigned i) const
Get string summarizing best fit for single variable.
Definition: BCIntegrate.cxx:2032
void SetBestFitParameters(const std::vector< double > &x)
Set best fit parameters values.
Definition: BCIntegrate.cxx:414
void LogOutputAtEndOfIntegration(double integral, double absprecision, double relprecision, int nIterations)
Helper method to output at end of integration.
Definition: BCIntegrate.cxx:475
std::vector< double > GetProposalPointSACauchy(const std::vector< double > &x, int t) const
Generates a new state in a neighbourhood around x that is to be accepted or rejected by the Simulated...
Definition: BCIntegrate.cxx:1314
void UpdateChainIndex(int chain)
Keep track of which chain is currently computed (within a thread).
Definition: BCEngineMCMC.cxx:3750
std::string DumpCurrentOptimizationMethod() const
Return string with the name for the currently set optimization type.
Definition: BCIntegrate.h:765
std::string DumpUsedOptimizationMethod() const
Return string with the name for the optimization type used to find the current mode.
Definition: BCIntegrate.h:771
double EvaluatorMC(std::vector< double > &sums, const std::vector< double > &point, bool &accepted)
Evaluates integrator.
Definition: BCIntegrate.cxx:557
double(BCIntegrate::* tEvaluator)(std::vector< double > &, const std::vector< double > &, bool &)
A pointer for a function that evaluates at a point.
Definition: BCIntegrate.h:216
Definition: libBAT_rdict.cxx:16
virtual void MarginalizePreprocess()
Method executed for before marginalization.
Definition: BCIntegrate.h:610
virtual void MarginalizePostprocess()
Method executed after marginalization.
Definition: BCIntegrate.h:616
std::vector< double > FindMode(std::vector< double > start=std::vector< double >())
Do the mode finding using a method set via SetOptimizationMethod.
Definition: BCIntegrate.cxx:1022
std::string DumpMarginalizationMethod(BCMarginalizationMethod type) const
Return string with the name for a given marginalization type.
Definition: BCIntegrate.cxx:1890
void PrintParameters(const std::vector< double > &P, void(*output)(const std::string &)=BCLog::OutSummary) const
Print parameters.
Definition: BCEngineMCMC.cxx:2881
double fSALogProb
Log probability of current simulated annealing iteration.
Definition: BCIntegrate.h:858
static void Out(BCLog::LogLevel loglevelfile, BCLog::LogLevel loglevelscreen, const std::string &message)
Writes string to the file and screen log if the log level is equal or greater than the minimum...
Definition: BCLog.cxx:70
virtual void ValueFromPositionInRange(std::vector< double > &p) const
Translate from unit interval to values in variable ranges, fixing fixed parameters along the way...
Definition: BCParameterSet.cxx:117
double SAHelperGetRadialCauchy() const
Generates the radial part of a n-dimensional Cauchy distribution.
Definition: BCIntegrate.cxx:1404
std::string DumpUsedMarginalizationMethod() const
Return string with the name for the marginalization type used.
Definition: BCIntegrate.h:753
double SATemperature(double t) const
Temperature annealing schedule for use with Simulated Annealing.
Definition: BCIntegrate.cxx:1251
static void IntegralUpdaterMC(const std::vector< double > &sums, const int &nIterations, double &integral, double &absprecision)
Updates info about integrator.
Definition: BCIntegrate.cxx:572
std::vector< double > GetProposalPointSABoltzmann(const std::vector< double > &x, int t) const
Generates a new state in a neighbourhood around x that is to be accepted or rejected by the Simulated...
Definition: BCIntegrate.cxx:1294
std::vector< double > fSAx
Current simulated annealing parameter point.
Definition: BCIntegrate.h:862
virtual std::vector< double > GetFixedValues(bool include_unfixed=true) const
Get vector of fixed values.
Definition: BCParameterSet.cxx:71
virtual bool IsWithinLimits(const std::vector< double > &x) const
Check if vector of values is within limits.
Definition: BCParameterSet.cxx:92
void SetCubaIntegrationMethod(BCCubaMethod type)
Set Cuba integration method.
Definition: BCIntegrate.cxx:1503
virtual void MCMCUserInitialize()
User hook called from MCMCInitialize().
Definition: BCEngineMCMC.h:1404
virtual bool IsAtFixedValues(const std::vector< double > &x) const
Check if fixed parameters in vector of values are at fixed values.
Definition: BCParameterSet.cxx:106
virtual unsigned int GetNFreeParameters() const
Definition: BCParameterSet.h:48
void SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method)
Definition: BCIntegrate.cxx:1488
virtual void PrintMarginalizationSummary() const
Print marginalization to log.
Definition: BCIntegrate.cxx:1997
std::string DumpOptimizationMethod(BCOptimizationMethod type) const
Return string with the name for a given optimization type.
Definition: BCIntegrate.cxx:1909
std::vector< double > SAHelperGetRandomPointOnHypersphere() const
Generates a uniform distributed random point on the surface of a fNvar-dimensional Hypersphere...
Definition: BCIntegrate.cxx:1363
double IntegrateLaplace()
Integrate using the Laplace approximation.
Definition: BCIntegrate.cxx:1782
virtual const std::vector< double > & GetBestFitParameterErrors() const
Definition: BCEngineMCMC.cxx:639
std::vector< double > GetProposalPointSA(const std::vector< double > &x, int t) const
Generates a new state in a neighbourhood around x that is to be accepted or rejected by the Simulated...
Definition: BCIntegrate.cxx:1282
BCEngineMCMC::Statistics fMCMCStatistics_AllChains
Statistics across all Markov chains.
Definition: BCEngineMCMC.h:1756
void SetNChains(unsigned n)
Sets the number of Markov chains which are run in parallel.
Definition: BCEngineMCMC.h:775
virtual double Eval(const std::vector< double > &x)=0
Evaluate the unnormalized probability at a point in parameter space.
double SAHelperSinusToNIntegral(int dim, double theta) const
Returns the Integral of sin^dim from 0 to theta.
Definition: BCIntegrate.cxx:1460
double GetRandomPoint(std::vector< double > &x)
Fills a vector of (flat) random numbers in the limits of the parameters and returns the probability a...
Definition: BCIntegrate.cxx:1006
virtual void PrintMarginalizationSummary() const
Print marginalization to log.
Definition: BCEngineMCMC.cxx:2809
std::vector< std::vector< TH2 * > > fH2Marginalized
Vector of 2D marginalized distributions.
Definition: BCEngineMCMC.h:1779
std::string DumpUsedIntegrationMethod() const
Return string with the name for the previously used integration type.
Definition: BCIntegrate.h:735
std::vector< TH1 * > fH1Marginalized
Vector of 1D marginalized distributions.
Definition: BCEngineMCMC.h:1775
void(* tIntegralUpdater)(const std::vector< double > &, const int &, double &, double &)
A pointer for a function that updates the integral and absolute precision.
Definition: BCIntegrate.h:220
std::string DumpCurrentIntegrationMethod() const
Return string with the name for the currently set integration type.
Definition: BCIntegrate.h:729
BCIntegrate::BCOptimizationMethod GetOptimizationMethod() const
Definition: BCIntegrate.h:264
virtual double SATemperatureCustom(double t) const
Temperature annealing schedule for use with Simulated Annealing.
Definition: BCIntegrate.cxx:1275
double fSATemperature
Current temperature of simulated annealing algorithm.
Definition: BCIntegrate.h:854
virtual const std::vector< double > & GetBestFitParameters() const
Definition: BCEngineMCMC.cxx:633
Definition: BCIntegrate.cxx:52
virtual std::vector< double > GetProposalPointSACustom(const std::vector< double > &x, int t) const
Generates a new state in a neighbourhood around x that is to be accepted or rejected by the Simulated...
Definition: BCIntegrate.cxx:1356
virtual std::string GetBestFitSummary(unsigned i) const
Get string summarizing best fit for single variable.
Definition: BCEngineMCMC.cxx:2792
virtual std::vector< double > GetRangeCenters() const
Get range centers, leaving fixed parameters at fixed values.
Definition: BCParameterSet.cxx:126
BCIntegrate & operator=(const BCIntegrate &)
Copy-assignment operator.
Definition: BCIntegrate.cxx:233
void GetRandomVectorInParameterSpace(std::vector< double > &x) const
Fills a vector of random numbers x[i] between fMin[i] and fMax[i] into a vector.
Definition: BCIntegrate.cxx:1013
double SATemperatureBoltzmann(double t) const
Temperature annealing schedule for use with Simulated Annealing.
Definition: BCIntegrate.cxx:1263
TH1 * GetSlice(std::vector< unsigned > indices, unsigned &nIterations, double &log_max_val, const std::vector< double > parameters=std::vector< double >(0), int nbins=0, bool normalize=true)
Returns a one-dimensional slice of the pdf at the point and along a specific direction.
Definition: BCIntegrate.cxx:840
void(BCIntegrate::* tRandomizer)(std::vector< double > &) const
A pointer for a function that chooses a next random point.
Definition: BCIntegrate.h:212
double SATemperatureCauchy(double t) const
Temperature annealing schedule for use with Simulated Annealing.
Definition: BCIntegrate.cxx:1269
virtual bool ApplyFixedValues(std::vector< double > &x) const
Change values to fixed values for fixed parameters.
Definition: BCParameterSet.cxx:153
std::string DumpCurrentMarginalizationMethod() const
Return string with the name for the currently set marginalization type.
Definition: BCIntegrate.h:747
void LogOutputAtIntegrationStatusUpdate(BCIntegrationMethod type, double integral, double absprecision, int nIterations)
Helper method to output integration status.
Definition: BCIntegrate.cxx:484
virtual double GetRangeWidth() const
Returns the range width of the variable values.
Definition: BCVariable.h:109
static BCLog::LogLevel GetLogLevelScreen()
Returns the minimum log level for screen output.
Definition: BCLog.h:88
void LogOutputAtStartOfIntegration(BCIntegrationMethod type, BCCubaMethod cubatype)
Helper method to output at beginning of integration.
Definition: BCIntegrate.cxx:420
bool CheckMarginalizationIndices(TH1 *hist, const std::vector< unsigned > &index)
Check that indices of parameters to marginalize w/r/t are correct.
Definition: BCIntegrate.cxx:601
virtual void ResetResults()
Reset all information on the best-fit parameters.
Definition: BCIntegrate.cxx:292
unsigned UpdateFrequency(unsigned N) const
return appropriate update interval
Definition: BCEngineMCMC.cxx:3639
virtual const std::vector< double > & GetBestFitParameters() const
Definition: BCIntegrate.cxx:274
void SetOptimizationMethod(BCIntegrate::BCOptimizationMethod method)
Definition: BCIntegrate.h:456