BCModel.h
1 #ifndef __BCMODEL__H
2 #define __BCMODEL__H
3 
17 /*
18  * Copyright (C) 2007-2018, the BAT core developer team
19  * All rights reserved.
20  *
21  * For the licensing terms see doc/COPYING.
22  * For documentation see http://mpp.mpg.de/bat
23  */
24 
25 // ---------------------------------------------------------
26 
27 #include "BCH1D.h"
28 #include "BCH2D.h"
29 #include "BCIntegrate.h"
30 #include "BCDataSet.h"
31 
32 #include <string>
33 
34 //BAT classes
35 class BCPriorModel;
36 
37 // ---------------------------------------------------------
38 
39 class BCModel : public BCIntegrate
40 {
41 
42 public:
43 
50  BCModel(const std::string& name = "model");
51 
54  BCModel(const BCModel& bcmodel);
55 
62  BCModel(const std::string& filename, const std::string& name, bool loadObservables = true);
63 
66  BCModel& operator=(const BCModel&);
67 
70  virtual ~BCModel();
71 
79  { return fDataSet; }
80 
83  unsigned GetNDataPoints() const
84  { return (fDataSet) ? fDataSet->GetNDataPoints() : 0; }
85 
89  int GetNDoF() const
91 
94  virtual BCPriorModel* GetPriorModel(bool prepare = true, bool call_likelihood = false);
95 
101  virtual BCH1D GetPrior(unsigned index);
102 
109  virtual BCH2D GetPrior(unsigned index1, unsigned index2);
110 
114  { return fBCH1DPriorDrawingOptions; }
115 
119  { return fBCH2DPriorDrawingOptions; }
120 
125 
130 
134  bool GetDrawPriorFirst() const
135  { return fDrawPriorFirst; }
136 
145  void SetDataSet(BCDataSet* dataset)
146  { fDataSet = dataset; }
147 
151 
155  void SetDrawPriorFirst(bool b = true)
156  { fDrawPriorFirst = b; }
157 
168  virtual double APrioriProbability(const std::vector<double>& parameters)
169  { return exp(this->LogAPrioriProbability(parameters)); }
170 
178  virtual double LogAPrioriProbability(const std::vector<double>& parameters)
179  { fFactorizedPrior = true; return fParameters.GetLogPrior(parameters); }
180 
185  virtual double Likelihood(const std::vector<double>& params)
186  { return exp(LogLikelihood(params)) ; }
187 
193  virtual double LogLikelihood(const std::vector<double>& params) = 0;
194 
199  virtual double ProbabilityNN(const std::vector<double>& params)
200  { return exp(LogProbabilityNN(params)); }
201 
207  virtual double LogProbabilityNN(const std::vector<double>& parameters);
208 
213  virtual double Probability(const std::vector<double>& parameters)
214  { return exp(LogProbability(parameters)); }
215 
220  virtual double LogProbability(const std::vector<double>& parameters);
221 
227  virtual double SamplingFunction(const std::vector<double>& parameters);
228 
231  virtual double Eval(const std::vector<double>& parameters)
232  { return exp(LogEval(parameters)); }
233 
236  virtual double LogEval(const std::vector<double>& parameters)
237  { return LogProbabilityNN(parameters); }
238 
243  virtual void InitializeMarkovChainTree(bool replacetree = false, bool replacefile = false);
244 
251  double HessianMatrixElement(unsigned index1, unsigned index2, const std::vector<double>& point);
252 
255  void PrintShortFitSummary();
256 
260  void PrintHessianMatrix(std::vector<double> parameters);
261 
270  virtual unsigned PrintKnowledgeUpdatePlots(const std::string& filename, unsigned hdiv = 1, unsigned vdiv = 1, bool call_likelihood = false);
271 
272 
273  /* /\** */
274  /* * Print priors to file */
275  /* * @param filename name of file to print to */
276  /* * @param hdiv number of horizontal divisions on page */
277  /* * @param vdiv number of vertical divisions on page */
278  /* * @param call_likelihood whether to call model likelihood function to prepare for calculating observables */
279  /* * @return number of plots printed. *\/ */
280  /* virtual unsigned PrintPriors(const std::string& filename, unsigned hdiv = 1, unsigned vdiv = 1, bool call_likelihood = false); */
281 
284  // declare BCPriorModel a friend
285  friend class BCPriorModel;
286 
287 protected:
288 
292 
296 
300 
304 
308 
312 
316 
320 
321 };
322 
323 // ---------------------------------------------------------
324 
325 #endif
int GetNDoF() const
Definition: BCModel.h:89
virtual BCPriorModel * GetPriorModel(bool prepare=true, bool call_likelihood=false)
Definition: BCModel.cxx:223
virtual double LogLikelihood(const std::vector< double > &params)=0
Calculates natural logarithm of the likelihood.
BCH1D fBCH1DPosteriorDrawingOptions
knowledge update plot 1D posterior options.
Definition: BCModel.h:307
Class for sampling from prior of a BCModel.
Definition: BCPriorModel.h:30
virtual double LogProbability(const std::vector< double > &parameters)
Returns natural logarithm of the a posteriori probability given a set of parameter values...
Definition: BCModel.cxx:105
virtual double LogEval(const std::vector< double > &parameters)
Overloaded function to evaluate integral.
Definition: BCModel.h:236
A class for handling numerical operations for models.
Definition: BCIntegrate.h:143
void SetDataSet(BCDataSet *dataset)
Sets the data set.
Definition: BCModel.h:145
bool fDrawPriorFirst
flag for ordering of drawing of prior and posterior in knowledge update plots.
Definition: BCModel.h:315
The base class for all user-defined models.
Definition: BCModel.h:39
virtual double ProbabilityNN(const std::vector< double > &params)
Returns the likelihood times prior probability given a set of parameter values.
Definition: BCModel.h:199
A class for handling 2D distributions.
Definition: BCH2D.h:37
virtual double Eval(const std::vector< double > &parameters)
Overloaded function to evaluate integral.
Definition: BCModel.h:231
void PrintHessianMatrix(std::vector< double > parameters)
Prints matrix elements of the Hessian matrix.
Definition: BCModel.cxx:195
A class representing a set of data points.
Definition: BCDataSet.h:39
void PrintShortFitSummary()
Prints a short summary of the fit results on the screen.
Definition: BCModel.cxx:178
void SetDrawPriorFirst(bool b=true)
Set drawing of prior first (true) or posterior first (false) for knowledge update plots...
Definition: BCModel.h:155
unsigned GetNDataPoints() const
Definition: BCDataSet.h:75
virtual double LogProbabilityNN(const std::vector< double > &parameters)
Returns the natural logarithm of likelihood times prior probability given a set of parameter values...
Definition: BCModel.cxx:88
BCH1D fBCH1DPriorDrawingOptions
knowledge update plot 1D prior options.
Definition: BCModel.h:299
double HessianMatrixElement(unsigned index1, unsigned index2, const std::vector< double > &point)
Calculates the matrix element of the Hessian matrix.
Definition: BCModel.cxx:136
virtual unsigned int GetNFreeParameters() const
BCModel & operator=(const BCModel &)
Copy-assignment operator.
Definition: BCModel.cxx:66
virtual double GetLogPrior(const std::vector< double > &parameters) const
Get log of prior; assumes independent priors given for all parameters in set.
BCModel(const std::string &name="model")
Default constructor.
Definition: BCModel.cxx:29
virtual void InitializeMarkovChainTree(bool replacetree=false, bool replacefile=false)
Initialize the trees containing the Markov chains and parameter info.
Definition: BCModel.cxx:117
unsigned GetNDataPoints() const
Definition: BCModel.h:83
BCH2D & GetBCH2DPriorDrawingOptions()
Definition: BCModel.h:118
BCH2D fBCH2DPosteriorDrawingOptions
knowledge update plot 2D posterior options.
Definition: BCModel.h:311
BCKnowledgeUpdateDrawingStyle
An enumerator for the knowledge update drawing style presets.
Definition: BCAux.h:119
Simple line-drawn histograms.
Definition: BCAux.h:120
virtual unsigned PrintKnowledgeUpdatePlots(const std::string &filename, unsigned hdiv=1, unsigned vdiv=1, bool call_likelihood=false)
Print a comparison of the prior knowledge to the posterior knowledge for each parameter.
Definition: BCModel.cxx:335
BCH2D fBCH2DPriorDrawingOptions
knowledge update plot 2D prior options.
Definition: BCModel.h:303
BCH1D & GetBCH1DPriorDrawingOptions()
Definition: BCModel.h:113
virtual ~BCModel()
Destructor.
Definition: BCModel.cxx:82
A class for handling 1D distributions.
Definition: BCH1D.h:34
virtual double LogAPrioriProbability(const std::vector< double > &parameters)
Returns natural logarithm of the prior probability.
Definition: BCModel.h:178
virtual double APrioriProbability(const std::vector< double > &parameters)
Returns the prior probability.
Definition: BCModel.h:168
BCDataSet * fDataSet
A data set.
Definition: BCModel.h:291
BCParameterSet fParameters
Parameter settings.
BCPriorModel * fPriorModel
BCPriorModel object for drawing of knowledge update, and saving of samples according to prior...
Definition: BCModel.h:295
virtual double SamplingFunction(const std::vector< double > &parameters)
Sampling function used for importance sampling.
Definition: BCModel.cxx:127
BCH2D & GetBCH2DPosteriorDrawingOptions()
Definition: BCModel.h:128
void SetKnowledgeUpdateDrawingStyle(BCAux::BCKnowledgeUpdateDrawingStyle style=BCAux::kKnowledgeUpdateDefaultStyle)
Set default drawing options for knowledge update plots.
Definition: BCModel.cxx:484
virtual double Probability(const std::vector< double > &parameters)
Returns the a posteriori probability given a set of parameter values.
Definition: BCModel.h:213
BCDataSet * GetDataSet()
Definition: BCModel.h:78
virtual BCH1D GetPrior(unsigned index)
Get prior of a variable as a BCH1D.
Definition: BCModel.cxx:234
bool fFactorizedPrior
flag for whether factorized prior has been used.
Definition: BCModel.h:319
virtual double Likelihood(const std::vector< double > &params)
Returns the likelihood.
Definition: BCModel.h:185
bool GetDrawPriorFirst() const
Definition: BCModel.h:134
BCH1D & GetBCH1DPosteriorDrawingOptions()
Definition: BCModel.h:123