Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
MarkovChain.cxx
Go to the documentation of this file.
1// @(#)root/roostats:$Id$
2// Authors: Kevin Belasco 17/06/2009
3// Authors: Kyle Cranmer 17/06/2009
4/*************************************************************************
5 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class RooStats::MarkovChain
13 \ingroup Roostats
14
15Stores the steps in a Markov Chain of points. Allows user to access the
16weight and NLL value (if applicable) with which a point was added to the
17MarkovChain.
18
19*/
20
21#include "Rtypes.h"
22
23#include "TNamed.h"
25#include "RooDataSet.h"
26#include "RooArgSet.h"
27#include "RooRealVar.h"
29#include "RooDataHist.h"
30#include "THnSparse.h"
31
32using namespace std;
33
35
36using namespace RooFit;
37using namespace RooStats;
38
39static const char* WEIGHT_NAME = "weight_MarkovChain_local_";
40static const char* NLL_NAME = "nll_MarkovChain_local_";
41static const char* DATASET_NAME = "dataset_MarkovChain_local_";
42static const char* DEFAULT_NAME = "_markov_chain";
43static const char* DEFAULT_TITLE = "Markov Chain";
44
47{
48 fParameters = nullptr;
49 fDataEntry = nullptr;
50 fChain = nullptr;
51 fNLL = nullptr;
52}
53
56{
57 fParameters = nullptr;
58 fDataEntry = nullptr;
59 fChain = nullptr;
60 fNLL = nullptr;
61 SetParameters(parameters);
62}
63
64MarkovChain::MarkovChain(const char* name, const char* title,
65 RooArgSet& parameters) : TNamed(name, title)
66{
67 fParameters = nullptr;
68 fDataEntry = nullptr;
69 fChain = nullptr;
70 fNLL = nullptr;
71 SetParameters(parameters);
72}
73
75{
76 delete fChain;
77 delete fParameters;
78 delete fDataEntry;
79
80 fParameters = new RooArgSet();
81 fParameters->addClone(parameters);
82
83 // kbelasco: consider setting fDataEntry = fChain->get()
84 // to see if that makes it possible to get values of variables without
85 // doing string comparison
86 RooRealVar nll(NLL_NAME, "-log Likelihood", 0);
87
88 fDataEntry = new RooArgSet();
89 fDataEntry->addClone(parameters);
90 fDataEntry->addClone(nll);
92
94}
95
96void MarkovChain::Add(RooArgSet& entry, double nllValue, double weight)
97{
98 if (fParameters == nullptr)
99 SetParameters(entry);
101 fNLL->setVal(nllValue);
102 //kbelasco: this is stupid, but some things might require it, so be doubly sure
103 fChain->add(*fDataEntry, weight);
104 //fChain->add(*fDataEntry);
105}
106
108{
109 // Discards the first n accepted points.
110
111 if(fParameters == nullptr) SetParameters(*(RooArgSet*)otherChain.Get());
112 int counter = 0;
113 for( int i=0; i < otherChain.Size(); i++ ) {
114 RooArgSet* entry = (RooArgSet*)otherChain.Get(i);
115 counter += 1;
116 if( counter > burnIn ) {
117 AddFast( *entry, otherChain.NLL(), otherChain.Weight() );
118 }
119 }
120}
121void MarkovChain::Add(MarkovChain& otherChain, double discardEntries)
122{
123 // Discards the first entries. This is different to the definition of
124 // burn-in used in the Bayesian calculator where the first n accepted
125 // terms from the proposal function are discarded.
126
127 if(fParameters == nullptr) SetParameters(*(RooArgSet*)otherChain.Get());
128 double counter = 0.0;
129 for( int i=0; i < otherChain.Size(); i++ ) {
130 RooArgSet* entry = (RooArgSet*)otherChain.Get(i);
131 counter += otherChain.Weight();
132 if( counter > discardEntries ) {
133 AddFast( *entry, otherChain.NLL(), otherChain.Weight() );
134 }
135 }
136}
137
138void MarkovChain::AddFast(RooArgSet& entry, double nllValue, double weight)
139{
141 fNLL->setVal(nllValue);
142 //kbelasco: this is stupid, but some things might require it, so be doubly sure
143 fChain->addFast(*fDataEntry, weight);
144 //fChain->addFast(*fDataEntry);
145}
146
148{
149 RooArgSet args;
150 if (whichVars == nullptr) {
151 //args.add(*fParameters);
152 //args.add(*fNLL);
153 args.add(*fDataEntry);
154 } else {
155 args.add(*whichVars);
156 }
157
158 return RooFit::Detail::owningPtr<RooDataSet>(std::unique_ptr<RooAbsData>{fChain->reduce(args)});
159}
160
162 const RooCmdArg &arg3, const RooCmdArg &arg4,
163 const RooCmdArg &arg5, const RooCmdArg &arg6,
164 const RooCmdArg &arg7, const RooCmdArg &arg8) const
165{
166 return RooFit::Detail::owningPtr<RooDataSet>(
167 std::unique_ptr<RooAbsData>{fChain->reduce(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)});
168}
169
171{
172 RooArgSet args;
173 if (whichVars == nullptr) {
174 args.add(*fParameters);
175 //args.add(*fNLL);
176 //args.add(*fDataEntry);
177 } else {
178 args.add(*whichVars);
179 }
180
181 std::unique_ptr<RooAbsData> data{fChain->reduce(args)};
182 return RooFit::Detail::owningPtr(std::unique_ptr<RooDataHist>{static_cast<RooDataSet&>(*data).binnedClone()});
183}
184
186 const RooCmdArg &arg3, const RooCmdArg &arg4,
187 const RooCmdArg &arg5, const RooCmdArg &arg6,
188 const RooCmdArg &arg7, const RooCmdArg &arg8) const
189{
190 std::unique_ptr<RooAbsData> data{fChain->reduce(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)};
191 return RooFit::Detail::owningPtr(std::unique_ptr<RooDataHist>{static_cast<RooDataSet &>(*data).binnedClone()});
192}
193
195{
196 RooArgList axes;
197 if (whichVars == nullptr)
198 axes.add(*fParameters);
199 else
200 axes.add(*whichVars);
201
202 Int_t dim = axes.getSize();
203 std::vector<double> min(dim);
204 std::vector<double> max(dim);
205 std::vector<Int_t> bins(dim);
206 std::vector<const char *> names(dim);
207 Int_t i = 0;
208 for (auto const *var : static_range_cast<RooRealVar *>(axes)) {
209 names[i] = var->GetName();
210 min[i] = var->getMin();
211 max[i] = var->getMax();
212 bins[i] = var->numBins();
213 ++i;
214 }
215
216 THnSparseF* sparseHist = new THnSparseF("posterior", "MCMC Posterior Histogram",
217 dim, &bins[0], &min[0], &max[0]);
218
219 // kbelasco: it appears we need to call Sumw2() just to get the
220 // histogram to keep a running total of the weight so that Getsumw doesn't
221 // just return 0
222 sparseHist->Sumw2();
223
224 // Fill histogram
226 const RooArgSet* entry;
227 std::vector<double> x(dim);
228 for ( i = 0; i < size; i++) {
229 entry = fChain->get(i);
230
231 for (Int_t ii = 0; ii < dim; ii++) {
232 //LM: doing this is probably quite slow
233 x[ii] = entry->getRealValue( names[ii]);
234 sparseHist->Fill(x.data(), fChain->weight());
235 }
236 }
237
238 return sparseHist;
239}
240
241double MarkovChain::NLL(Int_t i) const
242{
243 // kbelasco: how to do this?
244 //fChain->get(i);
245 //return fNLL->getVal();
246 return fChain->get(i)->getRealValue(NLL_NAME);
247}
248
249double MarkovChain::NLL() const
250{
251 // kbelasco: how to do this?
252 //fChain->get();
253 //return fNLL->getVal();
254 return fChain->get()->getRealValue(NLL_NAME);
255}
256
258{
259 return fChain->weight();
260}
261
263{
264 fChain->get(i);
265 return fChain->weight();
266}
static const char * DATASET_NAME
static const char * NLL_NAME
static const char * DEFAULT_TITLE
static const char * WEIGHT_NAME
static const char * DEFAULT_NAME
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
char name[80]
Definition TGX11.cxx:110
THnSparseT< TArrayF > THnSparseF
Definition THnSparse.h:220
Abstract container object that can hold multiple RooAbsArg objects.
double getRealValue(const char *name, double defVal=0.0, bool verbose=false) const
Get value of a RooAbsReal stored in set with given name.
Int_t getSize() const
Return the number of elements in the collection.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooFit::OwningPtr< RooAbsData > reduce(const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={})
Create a reduced copy of this dataset.
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Named container for two doubles, two integers two object points and three string pointers that can be...
Definition RooCmdArg.h:26
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:57
const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
RooFit::OwningPtr< RooDataHist > binnedClone(const char *newName=nullptr, const char *newTitle=nullptr) const
Return binned clone of this dataset.
virtual void addFast(const RooArgSet &row, double weight=1.0, double weightError=0.0)
Add a data point, with its coordinates specified in the 'data' argset, to the data set.
void add(const RooArgSet &row, double weight, double weightError)
Add one ore more rows of data.
double weight() const override
Return event weight of current event.
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.
Stores the steps in a Markov Chain of points.
Definition MarkovChain.h:30
virtual void AddFast(RooArgSet &entry, double nllValue, double weight=1.0)
add an entry to the chain ONLY IF you have constructed with parameters or called SetParameters
virtual double NLL() const
get the NLL value of the current (last indexed) entry
virtual void AddWithBurnIn(MarkovChain &otherChain, Int_t burnIn=0)
add another markov chain
virtual RooFit::OwningPtr< RooDataHist > GetAsDataHist(RooArgSet *whichVars=nullptr) const
get this MarkovChain as a RooDataHist whose entries contain the values of whichVars.
virtual void Add(RooArgSet &entry, double nllValue, double weight=1.0)
safely add an entry to the chain
virtual double NLL(Int_t i) const
get the NLL value of entry at position i
virtual RooFit::OwningPtr< RooDataSet > GetAsDataSet(RooArgSet *whichVars=nullptr) const
get this MarkovChain as a RooDataSet whose entries contain the values of whichVars.
RooArgSet * fParameters
virtual THnSparse * GetAsSparseHist(RooAbsCollection *whichVars=nullptr) const
Get a clone of the markov chain on which this interval is based as a sparse histogram.
virtual void SetParameters(RooArgSet &parameters)
set which of your parameters this chain should store
virtual const RooArgSet * Get(Int_t i) const
get the entry at position i
Definition MarkovChain.h:51
virtual double Weight() const
get the weight of the current (last indexed) entry
virtual Int_t Size() const
get the number of steps in the chain
Definition MarkovChain.h:49
Long64_t Fill(const Double_t *x, Double_t w=1.)
Definition THnBase.h:149
Templated implementation of the abstract base THnSparse.
Definition THnSparse.h:206
Efficient multidimensional histogram.
Definition THnSparse.h:36
void Sumw2() override
Enable calculation of errors.
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)
Double_t x[n]
Definition legend1.C:17
OwningPtr< T > owningPtr(std::unique_ptr< T > &&ptr)
Internal helper to turn a std::unique_ptr<T> into an OwningPtr.
Definition Config.h:50
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
T * OwningPtr
An alias for raw pointers for indicating that the return type of a RooFit function is an owning point...
Definition Config.h:43
Namespace for the RooStats classes.
Definition Asimov.h:19
void SetParameters(const RooArgSet *desiredVals, RooArgSet *paramsToChange)