Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
PdfProposal.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////////////////////////////////////////////////////////////////////////////////
13
14/** \class RooStats::PdfProposal
15 \ingroup Roostats
16
17PdfProposal is a concrete implementation of the ProposalFunction interface.
18It proposes points across the parameter space in the distribution of the
19given PDF.
20
21To make Propose(xPrime, x) dependent on x, configure with
22PdfProposal::AddMapping(varToUpdate, valueToUse). For example, suppose we have:
23
24~~~{.cpp}
25// our parameter
26RooRealVar p("p", "p", 5, 0, 10);
27
28// create mean and sigma for gaussian proposal function
29RooRealVar meanP("meanP", "meanP", 0, 10);
30RooRealVar sigma("sigma", "sigma", 1, 0, 5);
31RooGaussian pGaussian("pGaussian", "pGaussian", p, meanP, sigma);
32
33// configure proposal function
34PdfProposal pdfProposal(pGaussian);
35pdfProposal.AddMapping(meanP, p); // each call of Propose(xPrime, x), meanP in
36 // the proposal function will be updated to
37 // the value of p in x. this will center the
38 // proposal function about x's p when
39 // proposing for xPrime
40
41// To improve performance, PdfProposal has the ability to cache a specified
42// number of proposals. If you don't call this function, the default cache size
43// is 1, which can be slow.
44pdfProposal.SetCacheSize(desiredCacheSize);
45~~~
46
47PdfProposal currently uses a fixed cache size. Adaptive caching methods are in the works
48for future versions.
49*/
50
51
52#include "Rtypes.h"
53
56#include "RooArgSet.h"
57#include "RooDataSet.h"
58#include "RooAbsPdf.h"
59#include "RooMsgService.h"
60#include "RooRealVar.h"
61
62#include <map>
63
65
66using namespace RooFit;
67using namespace RooStats;
68using namespace std;
69
70////////////////////////////////////////////////////////////////////////////////
71/// By default, PdfProposal does NOT own the PDF that serves as the
72/// proposal density function
73
75{
76 fPdf = nullptr;
77 fOwnsPdf = false;
78 fCacheSize = 1;
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// By default, PdfProposal does NOT own the PDF that serves as the
84/// proposal density function
85
87{
88 fPdf = &pdf;
89 fOwnsPdf = false;
90 fCacheSize = 1;
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// determine whether these two RooArgSets represent the same point
96
98{
99 if (x1.equals(x2)) {
100 for (auto const *r : static_range_cast<RooRealVar*>(x1))
101 if (r->getVal() != x2.getRealValue(r->GetName())) {
102 return false;
103 }
104 return true;
105 }
106 return false;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Populate xPrime with a new proposed point
111
113{
114 if (fLastX.empty()) {
115 // fLastX not yet initialized
117 // generate initial cache
119 if (!fMap.empty()) {
120 for (fIt = fMap.begin(); fIt != fMap.end(); fIt++)
121 fIt->first->setVal(fIt->second->getVal(&x));
122 }
123 fCache = std::unique_ptr<RooDataSet>{fPdf->generate(xPrime, fCacheSize)};
124 }
125
126 bool moved = false;
127 if (!fMap.empty()) {
128 moved = !Equals(fLastX, x);
129
130 // if we've moved, set the values of the variables in the PDF to the
131 // corresponding values of the variables in x, according to the
132 // mappings (i.e. let the variables in x set the given values for the
133 // PDF that will generate xPrime)
134 if (moved) {
135 // update the pdf parameters
137
138 for (fIt = fMap.begin(); fIt != fMap.end(); fIt++)
139 fIt->first->setVal(fIt->second->getVal(&x));
140
141 // save the new x in fLastX
143 }
144 }
145
146 // generate new cache if necessary
147 if (moved || fCachePosition >= fCacheSize) {
148 fCache = std::unique_ptr<RooDataSet>{fPdf->generate(xPrime, fCacheSize)};
149 fCachePosition = 0;
150 }
151
152 const RooArgSet* proposal = fCache->get(fCachePosition);
154 RooStats::SetParameters(proposal, &xPrime);
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Determine whether or not the proposal density is symmetric for
159/// points x1 and x2 - that is, whether the propabilty of reaching x2
160/// from x1 is equal to the probability of reaching x1 from x2
161
163{
164 // kbelasco: is there a better way to do this?
165 return false;
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Return the probability of proposing the point x1 given the starting
170/// point x2
171
173{
175 for (fIt = fMap.begin(); fIt != fMap.end(); fIt++)
176 fIt->first->setVal(fIt->second->getVal(&x2));
177 std::unique_ptr<RooArgSet> temp{fPdf->getObservables(x1)};
178 RooStats::SetParameters(&x1, temp.get());
179 return fPdf->getVal(&x1); // could just as well use x2
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// specify a mapping between a parameter of the proposal function and
184/// a parameter of interest. this mapping is used to set the value of
185/// proposalParam equal to the value of update to determine the
186/// proposal function.
187/// proposalParam is a parameter of the proposal function that must
188/// be set to the value of update (from the current point) in order to
189/// propose a new point.
190
192{
193 fMaster.add(*update.getParameters(static_cast<RooAbsData const*>(nullptr)));
194 if (update.getParameters(static_cast<RooAbsData const*>(nullptr))->empty())
196 fMap.insert(std::pair<RooRealVar*, RooAbsReal*>(&proposalParam, &update));
197}
static void update(gsl_integration_workspace *workspace, double a1, double b1, double area1, double error1, double a2, double b2, double area2, double error2)
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
Storage_t const & get() const
Const access to the underlying stl container.
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.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooFit::OwningPtr< RooDataSet > generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={})
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition RooAbsPdf.h:57
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:37
PdfProposal is a concrete implementation of the ProposalFunction interface.
Definition PdfProposal.h:30
std::map< RooRealVar *, RooAbsReal * >::iterator fIt
map of values in pdf to update
RooArgSet fMaster
the cached proposal data set
bool IsSymmetric(RooArgSet &x1, RooArgSet &x2) override
Determine whether or not the proposal density is symmetric for points x1 and x2 - that is,...
virtual void AddMapping(RooRealVar &proposalParam, RooAbsReal &update)
specify a mapping between a parameter of the proposal function and a parameter of interest.
bool fOwnsPdf
pointers to master variables needed for updates
virtual bool Equals(RooArgSet &x1, RooArgSet &x2)
whether we own the proposal density function
RooArgSet fLastX
pdf iterator
void Propose(RooArgSet &xPrime, RooArgSet &x) override
Populate xPrime with a new proposed point.
Int_t fCacheSize
the last point we were at
Int_t fCachePosition
how many points to generate each time
std::map< RooRealVar *, RooAbsReal * > fMap
the proposal density function
PdfProposal()
By default, PdfProposal does NOT own the PDF that serves as the proposal density function.
std::unique_ptr< RooDataSet > fCache
our position in the cached proposal data set
double GetProposalDensity(RooArgSet &x1, RooArgSet &x2) override
Return the probability of proposing the point x1 given the starting point x2.
ProposalFunction is an interface for all proposal functions that would be used with a Markov Chain Mo...
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
Namespace for the RooStats classes.
Definition Asimov.h:19
void SetParameters(const RooArgSet *desiredVals, RooArgSet *paramsToChange)