KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sample_base.h
Go to the documentation of this file.
1 /* $Id: sample_base.h 17393 2014-08-23 20:19:14Z predoehl $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek, Ernesto Brau
18  * =========================================================================== */
19 
20 
21 #ifndef SAMPLE_BASE_H_INCLUDED
22 #define SAMPLE_BASE_H_INCLUDED
23 
24 #include <deque>
25 #include <boost/function.hpp>
26 #include <limits>
27 #include <string>
28 #include <map>
29 #include <m_cpp/m_vector.h>
30 #include <boost/optional.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/foreach.hpp>
33 
34 
35 /*
36  * ====================================================
37  * GENERAL SAMPLING STUFF
38  * ====================================================
39  */
40 
41 /* **********************************************
42  * STEP LOGGING
43  *
44  * These objects a logging system for the outcome
45  * of sampling moves. All moves are required to
46  * return a move log.
47  * **********************************************/
48 
54 template <class Model>
56 {
57 public:
59  type(""),
60  name(""),
61  accept(false),
62  lt(0),
63  extra(),
64  m_p(0)
65  {}
66 
67  Step_result(const char* type_, bool accept_, double lt_) :
68  type(type_),
69  name(""),
70  accept(accept_),
71  lt(lt_),
72  extra(),
73  m_p(0)
74  {}
75 
76  Step_result(const std::string& type_, const std::string name_, bool accept_, double lt_) :
77  type(type_),
78  name(name_),
79  accept(accept_),
80  lt(lt_),
81  extra(),
82  m_p(0)
83  {}
84 
85  Step_result(const std::string& type_, const std::string name_, bool accept_, double lt_, const Model* m) :
86  type(type_),
87  name(name_),
88  accept(accept_),
89  lt(lt_),
90  extra(),
91  m_p(m)
92  {}
93 
94  // step type (mh, hmc, other)
95  std::string type;
96  // step name (freeform, useful when many different steps of same type, or different sub-types of the same step, e.g. birth/death)
97  std::string name;
98 
99  // accepted or not?
100  bool accept;
101  // log target
102  double lt;
103 
104  // freeform key/value pairs
105  std::map<std::string, double> extra;
106 
107  // pointer to proposed model
108  const Model* m_p;
109 };
110 
111 template <class Model>
112 inline std::ostream& operator<<(std::ostream& ost, const Step_result<Model>& result)
113 {
114  std::string tmp = result.type;
115  boost::trim(tmp);
116  assert(!tmp.empty());
117 
118  // output full precision
119  ost.precision(std::numeric_limits<double>::digits10);
120 
121  // TYPE
122  tmp = result.type;
123  boost::trim(tmp);
124  if(tmp.empty())
125 // ost << ". "; // TODO: uncomment this after NIPS
126  ost << ".";
127  else
128 // ost << tmp << " "; // TODO: uncomment this after NIPS
129  ost << tmp << "";
130 
131  // NAME
132  tmp = result.name;
133  boost::trim(tmp);
134  if(tmp.empty())
135  ost << ". ";
136  else
137  ost << tmp << " ";
138 
139  ost << result.accept << " ";
140 
141  ost << result.lt;
142 
143  typedef std::map<std::string, double>::value_type Map_pair;
144 
145  BOOST_FOREACH(const Map_pair& pair, result.extra)
146  {
147  ost << ' ' << pair.first << '=' << pair.second;
148  }
149 
150  return ost;
151 }
152 
153 
159 template <class Model>
160 class Step_log : public std::deque<Step_result<Model> >
161 {
162 public:
163  Step_log() : std::deque<Step_result<Model> >(){}
164 
166  explicit Step_log(const Step_result<Model>& result) :
167  std::deque<Step_result<Model> >(1, result)
168  {}
169 
171  {
172  this->insert(this->end(), op.begin(), op.end());
173  return *this;
174  }
175 };
176 
177 template <class Model>
178 inline std::ostream& operator<<(std::ostream& ost, const Step_log<Model>& log)
179 {
180  typename Step_log<Model>::const_iterator it = log.begin();
181  for(; it != log.end(); it++)
182  {
183  ost << *it;
184 
185  if(it != log.end() - 1)
186  {
187  ost << "\n";
188  }
189  }
190 
191  return ost;
192 }
193 
194 
195 /* **********************************************
196  * MODEL FUNCTIONS
197  *
198  * These types will permit passing both functions
199  * and function objects to sampling algorithms.
200  *
201  * These all use the "Templated typedef" pattern
202  * see: http://www.gotw.ca/gotw/079.htm
203  *
204  * **********************************************/
205 
211 template <typename Model>
213 {
214  typedef boost::function1<double, const Model&> Type;
215 };
216 
217 /* **********************************************
218  * SAMPLER FUNCTIONS
219  *
220  * These types will permit passing both functions
221  * and functors as steps to a sampler.
222  *
223  * **********************************************/
224 
225 template <typename Model>
227 {
228  typedef boost::function2<Step_log<Model>, Model&, double> Type;
229 };
230 
231 /* **********************************************
232  * OTHER
233  * **********************************************/
234 
246 template <typename Model>
248 {
249  typedef typename boost::function2<void, const Model&, const Step_log<Model>&> Type;
250 };
251 
252 
253 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
254 
255 /*
256  * ====================================================
257  * INDEXABLE MODEL STUFF
258  * ====================================================
259  */
260 
267 template <typename Model>
269 {
270  typedef typename boost::function2<double, const Model&, size_t> Type;
271 };
272 
279 template <typename Model>
281 {
282  typedef typename boost::function3<void, Model&, size_t, double> Type;
283 };
284 
291 template <typename Model>
293 {
294  typedef typename boost::function3<void, Model&, size_t, double> Type;
295 };
296 
302 template <typename Model>
304 {
305  typedef typename boost::function1<size_t, const Model&> Type;
306 };
307 
314 template <typename Model>
316 {
317  typedef boost::function1<kjb::Vector, const Model&> Type;
318 };
319 
320 
321 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
322 
323 /*
324  * ====================================================
325  * METROPOLIS SAMPLING STUFF
326  * ====================================================
327  */
328 
335 {
336 public:
337  Mh_proposal_result(const std::string& type_ = std::string("")) : fwd_prob(0.0), rev_prob(0.0), type(type_), no_change(false)
338  {}
339 
345  Mh_proposal_result(double fp, double rp, const std::string& type_ = std::string("")) :
346  fwd_prob(fp),
347  rev_prob(rp),
348  type(type_),
349  no_change(false)
350  {}
351 
353  static Mh_proposal_result unchanged(const std::string& type_ = std::string(""))
354  {
355  Mh_proposal_result result(type_);
356  result.no_change = true;
357  return result;
358  }
359 
361  {
362  return fwd_prob == pr.fwd_prob && rev_prob == pr.rev_prob && no_change == pr.no_change;
363  }
364 
366  {
367  return !this->operator==(pr);
368  }
369 
371  {
372  fwd_prob += pr.fwd_prob;
373  rev_prob += pr.rev_prob;
374 
375  return *this;
376  }
377 
379  double fwd_prob;
381  double rev_prob;
382 
383  std::string type;
384 
386  bool no_change;
387 };
388 
390 {
391  Mh_proposal_result result(pr1);
392  result += pr2;
393  return result;
394 }
395 
396 
403 template <typename Model>
405 {
406  typedef boost::function2<Mh_proposal_result, const Model&, Model&> Type;
407 };
408 
409 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
410 
411 /*
412  * ====================================================
413  * GIBBS SAMPLING STUFF
414  * ====================================================
415  */
416 
423 template <typename Model>
425 {
426  typedef boost::function2<boost::optional<double>, Model&, size_t> Type;
427 };
428 
429 
430 #endif /*SAMPLE_BASE_H_INCLUDED */
431 
double fwd_prob
log forward proposal probability
Definition: sample_base.h:379
Mh_proposal_result(const std::string &type_=std::string(""))
Definition: sample_base.h:337
Indicates the result of an MH proposal. It is simply a pair of probabilities, forward and backward...
Definition: sample_base.h:334
Step_log< Model > & operator+=(const Step_log< Model > &op)
Definition: sample_base.h:170
Returns the dimension of the model.
Definition: sample_base.h:303
std::map< std::string, double > extra
Definition: sample_base.h:105
boost::function2< boost::optional< double >, Model &, size_t > Type
Definition: sample_base.h:426
Structure for returning results of a single sampling move.
Definition: sample_base.h:55
Definition: sample_base.h:160
bool no_change
proposal didn't modify the model
Definition: sample_base.h:386
std::string type
Definition: sample_base.h:95
double rev_prob
log reverse proposal probability
Definition: sample_base.h:381
static Mh_proposal_result unchanged(const std::string &type_=std::string(""))
named constructor - returns a proposal result indicating the model was not changed ...
Definition: sample_base.h:353
bool operator!=(const Mh_proposal_result &pr)
Definition: sample_base.h:365
bool accept
Definition: sample_base.h:100
Step_result()
Definition: sample_base.h:58
Definition: sample_base.h:404
boost::function3< void, Model &, size_t, double > Type
Definition: sample_base.h:282
boost::function1< double, const Model & > Type
Definition: sample_base.h:214
boost::function2< Mh_proposal_result, const Model &, Model & > Type
Definition: sample_base.h:406
Step_result(const std::string &type_, const std::string name_, bool accept_, double lt_)
Definition: sample_base.h:76
Moves the specified parameter of a model. For now, we assume all parameters are type double...
Definition: sample_base.h:292
Step_log()
Definition: sample_base.h:163
Definition: sample_base.h:315
std::string name
Definition: sample_base.h:97
boost::function1< kjb::Vector, const Model & > Type
Definition: sample_base.h:317
Definition: sample_base.h:212
Definition: sample_base.h:247
boost::function3< void, Model &, size_t, double > Type
Definition: sample_base.h:294
Mh_proposal_result & operator+=(const Mh_proposal_result &pr)
Definition: sample_base.h:370
Step_log(const Step_result< Model > &result)
construct from a single result object
Definition: sample_base.h:166
double lt
Definition: sample_base.h:102
Definition: sample_base.h:226
std::string type
Name of proposal type.
Definition: sample_base.h:383
Step_result(const std::string &type_, const std::string name_, bool accept_, double lt_, const Model *m)
Definition: sample_base.h:85
boost::function2< void, const Model &, const Step_log< Model > & > Type
Definition: sample_base.h:249
const Model * m_p
Definition: sample_base.h:108
Definition: sample_base.h:424
Gets the specified parameter of a model. For now, we assume all parameters are type double...
Definition: sample_base.h:268
boost::function2< Step_log< Model >, Model &, double > Type
Definition: sample_base.h:228
bool operator==(const Mh_proposal_result &pr)
Definition: sample_base.h:360
Sets the specified parameter of a model. For now, we assume all parameters are type double...
Definition: sample_base.h:280
Mh_proposal_result operator+(const Mh_proposal_result &pr1, const Mh_proposal_result &pr2)
Definition: sample_base.h:389
for m
Definition: APPgetLargeConnectedEdges.m:64
boost::function2< double, const Model &, size_t > Type
Definition: sample_base.h:270
Mh_proposal_result(double fp, double rp, const std::string &type_=std::string(""))
Definition: sample_base.h:345
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
boost::function1< size_t, const Model & > Type
Definition: sample_base.h:305
Step_result(const char *type_, bool accept_, double lt_)
Definition: sample_base.h:67