Fleet  0.0.9
Inference in the LOT
VectorNormalHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "EigenLib.h"
4 #include "Interfaces/MCMCable.h"
5 \
16  // This represents a vector of reals, defaultly here just unit normals.
17  // This gets used in GrammarHypothesis to store both the grammar values and
18  // parameters for the model.
19 public:
21 // using datum_t = _datum_t;
22 // using data_t = _data_t;
23 
24  double MEAN = 0.0;
25  double SD = 1.0;
26  double PROPOSAL_SCALE = 0.20;
27 
29 
30  // whether each element of value is constant or not?
31  // This is useful because sometimes we don't want to do MCMC on some parts of the grammar
32  std::vector<bool> can_propose;
33 
35  VectorNormalHypothesis(int n) { set_size(n); }
36 
37  double operator()(int i) const {
38  // So we can treat this hypothesis like a vector
39  return value(i);
40  }
41 
42  void set(int i, double v) {
43  value(i) = v;
44  }
45 
50  void set_can_propose(size_t i, bool b) {
51  can_propose[i] = b;
52 
53  // let's just check they're not all can't propose
54  if(std::all_of(can_propose.begin(), can_propose.end(), [](bool v) { return not v; })) {
55  assert(false && "*** Cannot set everything to no-propose or else Vector[Half]NormalHypothesis breaks! (loops infinitely on propose)");
56  }
57 
58  }
59 
60  void set_size(size_t n) {
61  value = Vector::Zero(n);
62  can_propose.resize(n,true);
63  }
64 
65  size_t size() const {
66  return value.size();
67  }
68 
69  virtual double compute_prior() override {
70  // Defaultly a unit normal
71  this->prior = 0.0;
72 
73  for(auto i=0;i<value.size();i++) {
74  this->prior += normal_lpdf((double)value(i), this->MEAN, this->SD);
75  }
76 
77  return this->prior;
78  }
79 
80  virtual double compute_likelihood(const data_t& data, const double breakout=-infinity) override {
81  throw YouShouldNotBeHereError("*** Should not call likelihood here");
82  }
83 
84 
85  virtual std::optional<std::pair<self_t,double>> propose() const override {
86  self_t out = *this;
87 
88  // choose an index
89  // (NOTE -- if can_propose is all false, this might loop infinitely...)
90  // but we should have caught that up in set_can_propose
91  size_t i;
92  do {
93  i = myrandom(value.size());
94  } while(!can_propose.at(i));
95 
96  // propose to one coefficient w/ SD of 0.1
97  out.value(i) = value(i) + PROPOSAL_SCALE*random_normal();
98 
99  // everything is symmetrical so fb=0
100  return std::make_pair(out, 0.0);
101  }
102 
103  virtual self_t restart() const override {
104  self_t out = *this;
105  for(auto i=0;i<value.size();i++) {
106  if(out.can_propose.at(i)) {// we don't want to change parameters unless we can propose to them
107  out.value(i) = MEAN + SD*random_normal();
108  }
109  }
110  return out;
111  }
112 
113  virtual size_t hash() const override {
114  if(value.size() == 0) return 0; // hmm what to do here?
115 
116  size_t output = std::hash<double>{}(value(0));
117  for(auto i=1;i<value.size();i++) {
118  hash_combine(output, std::hash<double>{}(value(i)));
119  }
120  return output;
121  }
122 
123  virtual bool operator==(const self_t& h) const override {
124  return value.size() == h.value.size() and value == h.value;
125  }
126 
127  virtual std::string string(std::string prefix="") const override {
128  std::string out = prefix+"<";
129  for(auto i=0;i<value.size();i++) {
130  out += str(value(i))+",";
131  }
132  out.erase(out.size()-1);
133  out += ">";
134  return out;
135  }
136 };
137 
T myrandom(T max)
Definition: Random.h:176
virtual std::string string(std::string prefix="") const override
Definition: VectorNormalHypothesis.h:127
virtual size_t hash() const override
Definition: VectorNormalHypothesis.h:113
size_t size() const
Definition: VectorNormalHypothesis.h:65
void set_can_propose(size_t i, bool b)
Set whether we can propose to each element of b or not.
Definition: VectorNormalHypothesis.h:50
virtual bool operator==(const self_t &h) const override
Definition: VectorNormalHypothesis.h:123
Definition: MCMCable.h:14
Definition: Errors.h:18
virtual self_t restart() const override
Definition: VectorNormalHypothesis.h:103
std::string str(BindingTree *t)
Definition: BindingTree.h:195
VectorNormalHypothesis()
Definition: VectorNormalHypothesis.h:34
double random_normal(double mu=0, double sd=1.0)
Definition: Random.h:39
constexpr double infinity
Definition: Numerics.h:20
Vector value
Definition: VectorNormalHypothesis.h:28
Definition: VectorNormalHypothesis.h:15
virtual std::optional< std::pair< self_t, double > > propose() const override
Definition: VectorNormalHypothesis.h:85
void set_size(size_t n)
Definition: VectorNormalHypothesis.h:60
std::vector< Args... > data_t
Definition: Bayesable.h:39
virtual double compute_likelihood(const data_t &data, const double breakout=-infinity) override
Compute the likelihood of a collection of data, by calling compute_single_likelihood on each...
Definition: VectorNormalHypothesis.h:80
std::vector< bool > can_propose
Definition: VectorNormalHypothesis.h:32
double operator()(int i) const
Definition: VectorNormalHypothesis.h:37
VectorNormalHypothesis(int n)
Definition: VectorNormalHypothesis.h:35
A class is MCMCable if it is Bayesable and lets us propose, restart, and check equality (which MCMC d...
T normal_lpdf(T x, T mu=0.0, T sd=1.0)
Definition: Random.h:45
Eigen::VectorXf Vector
Definition: EigenLib.h:17
virtual double compute_prior() override
Definition: VectorNormalHypothesis.h:69