BCModel.h

Go to the documentation of this file.
00001 #ifndef __BCMODEL__H
00002 #define __BCMODEL__H
00003 
00019 /*
00020  * Copyright (C) 2008, Daniel Kollar and Kevin Kroeninger.
00021  * All rights reserved.
00022  *
00023  * For the licensing terms see doc/COPYING.
00024  */
00025 
00026 // ---------------------------------------------------------
00027 
00028 #include <vector>
00029 #include <string>
00030 
00031 #include "BAT/BCIntegrate.h"
00032 
00033 // ROOT classes
00034 class TH2D;
00035 class TGraph;
00036 class TCanvas;
00037 class TPostscript;
00038 
00039 //BAT classes
00040 class BCDataPoint;
00041 class BCDataSet;
00042 class BCParameter;
00043 class BCH1D;
00044 class BCH2D;
00045 
00046 const int MAXNDATAPOINTVALUES = 20;
00047 
00048 // ---------------------------------------------------------
00049 
00050 class BCModel : public BCIntegrate
00051 {
00052 
00053    public:
00054 
00056       /* @{ */
00057 
00060       BCModel();
00061 
00065       BCModel(const char * name);
00066 
00069       virtual ~BCModel();
00070 
00071       /* @} */
00072 
00074       /* @{ */
00075 
00078       std::string GetName()
00079          { return fName; };
00080 
00083       int GetIndex()
00084          { return fIndex; };
00085 
00088       double GetModelAPrioriProbability()
00089          { return fModelAPriori; };
00090 
00093       double GetModelAPosterioriProbability()
00094          { return fModelAPosteriori; };
00095 
00098       double GetNormalization()
00099          { return fNormalization; };
00100 
00103       BCDataSet* GetDataSet()
00104          { return fDataSet; };
00105 
00108       BCDataPoint* GetDataPointLowerBoundaries()
00109          { return fDataPointLowerBoundaries; };
00110 
00113       BCDataPoint* GetDataPointUpperBoundaries()
00114          { return fDataPointUpperBoundaries; };
00115 
00119       double GetDataPointLowerBoundary(unsigned int index)
00120          { return fDataPointLowerBoundaries -> GetValue(index); };
00121 
00125       double GetDataPointUpperBoundary(unsigned int index)
00126          { return fDataPointUpperBoundaries -> GetValue(index); };
00127 
00128       /*
00129        * Checks if the boundaries have been defined
00130        * @return true, if the boundaries have been set, false otherwise */
00131       bool GetFlagBoundaries();
00132 
00135       int GetNDataPoints();
00136 
00140       BCDataPoint * GetDataPoint(unsigned int index);
00141 
00144       unsigned int GetNDataPointsMinimum()
00145          { return fNDataPointsMinimum; };
00146 
00149       unsigned int GetNDataPointsMaximum()
00150          { return fNDataPointsMaximum; };
00151 
00154       unsigned int GetNParameters()
00155          { return fParameterSet ? fParameterSet -> size() : 0; };
00156 
00160       BCParameter* GetParameter(int index);
00161 
00165       BCParameter * GetParameter(const char * name);
00166 
00169       BCParameterSet * GetParameterSet()
00170          { return fParameterSet; };
00171 
00177       double GetBestFitParameter(unsigned int index)
00178          { return fBestFitParameters[index]; };
00179 
00180       double GetBestFitParameterError(unsigned int index)
00181       { return fBestFitParameterErrors[index]; }; 
00182 
00187       std::vector <double> GetBestFitParameters()
00188          { return fBestFitParameters; };
00189       
00190       std::vector <double> GetBestFitParameterErrors() 
00191          { return fBestFitParameterErrors; }; 
00192 
00198       double GetBestFitParameterMarginalized(unsigned int index)
00199          { return fBestFitParametersMarginalized[index]; };
00200 
00205       std::vector <double> GetBestFitParametersMarginalized()
00206          { return fBestFitParametersMarginalized; };
00207 
00210       TH2D * GetErrorBandXY()
00211          { return fErrorBandXY; };
00212 
00213       TH2D * GetErrorBandXY_yellow(double level=.68, int nsmooth=0);
00214 
00219       std::vector <double> GetErrorBand(double level);
00220 
00221       TGraph * GetErrorBandGraph(double level1, double level2);
00222 
00223       TGraph * GetFitFunctionGraph(std::vector <double> parameters);
00224 
00225       TGraph * GetFitFunctionGraph()
00226          { return this -> GetFitFunctionGraph(this -> GetBestFitParameters()); };
00227 
00228       TGraph * GetFitFunctionGraph(std::vector <double> parameters, double xmin, double xmax, int n=1000);
00229 
00230       bool GetFixedDataAxis(unsigned int index);
00231 
00232       /* @} */
00233 
00235       /* @{ */
00236 
00240       void SetName(const char * name)
00241          { fName = name; };
00242 
00246       void SetIndex(int index)
00247          { fIndex = index; };
00248 
00252       void SetParameterSet( BCParameterSet * parset )
00253          { fParameterSet = parset; };
00254 
00259       void SetModelAPrioriProbability(double probability)
00260          { fModelAPriori = probability; };
00261 
00266       void SetModelAPosterioriProbability(double probability)
00267          { fModelAPosteriori = probability; };
00268 
00273       void SetNormalization(double norm)
00274          { fNormalization = norm; };
00275 
00279       void SetDataSet(BCDataSet* dataset)
00280          { fDataSet = dataset; fNormalization = -1.0; };
00281 
00285       void SetSingleDataPoint(BCDataPoint * datapoint);
00286 
00287       void SetSingleDataPoint(BCDataSet * dataset, unsigned int index);
00288 
00291       void SetNDataPointsMinimum(unsigned int minimum)
00292          { fNDataPointsMinimum = minimum; };
00293 
00296       void SetNDataPointsMaximum(unsigned int maximum)
00297          { fNDataPointsMaximum = maximum; };
00298 
00299       void SetDataBoundaries(unsigned int index, double lowerboundary, double upperboundary, bool fixed=false);
00300 
00303       void SetErrorBandContinuous(bool flag);
00304 
00309       void SetNbins(const char * parname, int nbins);
00310 
00311       /* @} */
00312 
00314       /* @{ */
00315 
00322       int AddParameter(const char * name, double lowerlimit, double upperlimit);
00323 
00328       int AddParameter(BCParameter* parameter);
00329 
00335       double APrioriProbability(std::vector <double> parameters)
00336          { return exp( this->LogAPrioriProbability(parameters) ); };
00337 
00344       virtual double LogAPrioriProbability(std::vector <double> parameters)
00345          { return 0.; };
00346 
00351       double Likelihood(std::vector <double> parameter)
00352          { return exp( this->LogLikelihood(parameter) ); };
00353 
00359       virtual double LogLikelihood(std::vector <double> parameter);
00360 
00365       double ProbabilityNN(std::vector <double> parameter)
00366          { return exp( this->LogProbabilityNN(parameter) ); };
00367 
00373       double LogProbabilityNN(std::vector <double> parameter);
00374 
00379       double Probability(std::vector <double> parameter)
00380          { return exp( this->LogProbability(parameter) ); };
00381 
00386       double LogProbability(std::vector <double> parameter);
00387 
00395       double ConditionalProbabilityEntry(BCDataPoint * datapoint, std::vector <double> parameters)
00396          { return exp( this->LogConditionalProbabilityEntry(datapoint, parameters) ); };
00397 
00405       virtual double LogConditionalProbabilityEntry(BCDataPoint * datapoint, std::vector <double> parameters)
00406          { flag_ConditionalProbabilityEntry = false; return 0.; };
00407 
00413       virtual double SamplingFunction(std::vector <double> parameters);
00414 
00417       double Eval(std::vector <double> parameters)
00418          { return exp( this->LogEval(parameters) ); };
00419 
00422       double LogEval(std::vector <double> parameters);
00423 
00426       double EvalSampling(std::vector <double> parameters);
00427 
00430       double Normalize();
00431 
00437       int CheckParameters(std::vector <double> parameters);
00438 
00456       void FindMode(std::vector<double> start = std::vector<double>(0));
00457 
00463       void FindModeMinuit(std::vector<double> start = std::vector<double>(0), int printlevel = 1);
00464 
00467       void WriteMode(const char * file);
00468 
00471       int ReadMode(const char * file);
00472 
00475       int ReadMarginalizedFromFile(const char * file);
00476 
00479       int ReadErrorBandFromFile(const char * file);
00480 
00486       int MarginalizeAll();
00487 
00493       BCH1D * GetMarginalized(BCParameter * parameter);
00494 
00495       BCH1D * GetMarginalized(const char * name)
00496          { return this -> GetMarginalized(this -> GetParameter(name)); };
00497 
00504       BCH2D * GetMarginalized(BCParameter * parameter1, BCParameter * parameter2);
00505 
00506       BCH2D * GetMarginalized(const char * name1, const char * name2)
00507          { return this -> GetMarginalized(this -> GetParameter(name1), this -> GetParameter(name2)); };
00508 
00511       int PrintAllMarginalized1D(const char * filebase);
00512       int PrintAllMarginalized2D(const char * filebase);
00513       int PrintAllMarginalized(const char * file, unsigned int hdiv=1, unsigned int ndiv=1);
00514 
00518       virtual void CorrelateDataPointValues(std::vector<double> &x);
00519 
00525       double GetPvalueFromChi2(std::vector<double> par, int sigma_index);
00526 
00533       double GetPvalueFromChi2Johnson(std::vector<double> par);
00534 
00543       double GetChi2Johnson(std::vector<double> par, const int nBins=-1);
00544 
00555       double GetAvalueFromChi2Johnson(TTree* tree, TH1D* distribution=0);
00556 
00557       double GetPvalueFromChi2NDoF(std::vector<double> par, int sigma_index);
00558 
00559       BCH1D * CalculatePValue(std::vector<double> par, bool flag_histogram=false);
00560 
00561       /*
00562        * @return The p-value */
00563       double GetPValue()
00564          { return fPValue; };
00565 
00566       double GetPValueNDoF()
00567          { return fPValueNDoF; };
00568 
00569       double GetChi2NDoF()
00570          { return fChi2NDoF; };
00571 
00572       /*
00573        * Set maximum number of iterations in the MCMC pre-run of the p-value
00574        * evaluation using MCMC */
00575       void SetGoFNIterationsMax(int n)
00576          { fGoFNIterationsMax=n; };
00577 
00578       /*
00579        * Set number of iterations in the MCMC normal run of the p-value
00580        * evaluation using MCMC */
00581       void SetGoFNIterationsRun(int n)
00582          { fGoFNIterationsRun=n; };
00583 
00584       /*
00585        * Set number of chains in the MCMC of the p-value
00586        * evaluation using MCMC */
00587       void SetGoFNChains(int n)
00588          { fGoFNChains=n; };
00589 
00595       double HessianMatrixElement(BCParameter * parameter1, BCParameter * parameter2, std::vector<double> point);
00596 
00599       void PrintSummary();
00600 
00603       void PrintResults(const char * file);
00604 
00607       void PrintShortFitSummary(int chi2flag=0);
00608 
00612       void PrintHessianMatrix(std::vector<double> parameters);
00613 
00614       void FixDataAxis(unsigned int index, bool fixed);
00615 
00616 
00626       virtual double CDF(const std::vector<double>& parameters,  int index, bool lower=false)
00627       {return 0.0;}
00628 
00629 
00630    /* @} */
00631 
00632    protected:
00633 
00636       int fIndex;
00637 
00640       std::string fName;
00641 
00644       double fModelAPriori;
00645 
00648       double fModelAPosteriori;
00649 
00652       BCParameterSet * fParameterSet;
00653 
00656       BCDataSet * fDataSet;
00657 
00660       unsigned int fNDataPointsMinimum;
00661 
00664       unsigned int fNDataPointsMaximum;
00665 
00668       bool flag_ConditionalProbabilityEntry;
00669 
00672       double fPValue;
00673 
00674       double fChi2NDoF;
00675       double fPValueNDoF;
00676 
00679       bool flag_discrete;
00680 
00681       /*
00682        * Maximum number of iterations in the MCMC pre-run of the p-value
00683        * evaluation using MCMC */
00684       int fGoFNIterationsMax;
00685 
00686       /*
00687        * Number of iterations in the MCMC normal run of the p-value
00688        * evaluation using MCMC */
00689       int fGoFNIterationsRun;
00690 
00691       /*
00692        * Number of chains in the MCMC of the p-value
00693        * evaluation using MCMC */
00694       int fGoFNChains;
00695 
00696    private:
00697 
00700       BCDataPoint * VectorToDataPoint(std::vector<double> data);
00701 
00704       int CompareStrings(const char * string1, const char * string2);
00705 
00708       double fNormalization;
00709 
00715       int NumberBins(){
00716          return (int)(exp(0.4 * log(this -> GetNDataPoints())) + 2);
00717       }
00718 
00719 };
00720 
00721 // ---------------------------------------------------------
00722 
00723 typedef std::vector<BCModel*> BCModelContainer;
00724 
00725 // ---------------------------------------------------------
00726 
00727 #endif

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