Fleet  0.0.9
Inference in the LOT
MyHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "InnerHypothesis.h"
4 #include "Lexicon.h"
5 
6 class MyHypothesis : public Lexicon<MyHypothesis, std::string, InnerHypothesis, BindingTree*, std::string> {
7  // Takes a node (in a bigger tree) and a word
9  using Super::Super; // inherit the constructors
10 public:
11 
12  void clear_cache() {
13  for(auto& [k,f] : factors) {
14  f.clear_cache();
15  }
16  }
17 
18  double compute_likelihood(const data_t& data, const double breakout=-infinity) override {
19  // TODO: Can set to null so that we get an error on recurse
20  for(auto& [k, f] : factors)
21  f.program.loader = this;
22 
23  // make sure everyone's cache is right on this data
24  for(auto& [k, f] : factors) {
25  //f.clear_cache(); // if we always want to recompute (e.g. if using recursion) -- normally we don't want this!
26  f.cached_call(data);
27 
28  // now if anything threw an error, break out, we don't have to compute
29  if(f.got_error) {
30  return likelihood = -infinity;
31 
32  }
33  }
34 
35 
36  // The likelihood here samples from all words that are true
37  likelihood = 0.0;
38  for(size_t di=0;di<data.size();di++) {
39  auto& d = data[di];
40 
41  // see how many factors are true:
42  bool wtrue = false; // was w true?
43  int ntrue = 0; // how many are true?
44 
45  // see which words are permitted
46  for(const auto& w : words) {
47 
48  auto b = factors[w].cache.at(di);
49 
50  ntrue += 1*b;
51 
52  if(d.output == w)
53  wtrue = b;
54 
55 
56  //print(">>>", w, b);
57  //if(b) {
58  // print("Word is true:", w);
59  //}
60  }
61 
62  // Noisy size-principle likelihood
63  likelihood += (double(NDATA)/double(data.size())) * log( (wtrue ? d.reliability/ntrue : 0.0) +
64  (1.0-d.reliability)/words.size());
65 
66  if(likelihood < breakout)
67  return likelihood = -infinity;
68 
69  }
70 
71  return likelihood;
72  }
73 
74  virtual void show(std::string prefix="") override {
75 
76  extern MyHypothesis::data_t target_precisionrecall_data;
77  extern MyHypothesis target;
78 
79  std::lock_guard guard(output_lock);
80 
81  COUT std::setprecision(5) << prefix << NDATA TAB this->posterior TAB this->prior TAB this->likelihood TAB "";
82 
83 
84  // now we need to clear the caches and recompute -- NOTE this is very slow
85  target.clear_cache(); target.compute_posterior(target_precisionrecall_data);
86  this->clear_cache(); this->compute_posterior(target_precisionrecall_data);
87 
88  // when we print, we are going to compute overlap with each target item
89  for(auto& w : words) {
90  int nagree = 0;
91  int ntot = 0;
92 
93  for(size_t di=0;di<target_precisionrecall_data.size();di++) {
94 // auto& d = target_precisionrecall_data.at(di);
95 
96  if(factors[w].cache.at(di) == target.factors[w].cache.at(di)) {
97  ++nagree;
98  }
99  ++ntot;
100  }
101 
102  COUT float(nagree)/float(ntot) TAB "";
103  }
104 
105  this->clear_cache();
106 
107 
108  COUT QQ(this->string()) ENDL;
109 
110  }
111 
112  // Our restart will just choose one of the factors to restart
113 // [[nodiscard]] virtual MyHypothesis restart() const override {
114 // auto x = *this;
115 //
116 // auto which = myrandom(factors.size());
117 // x[words[which]] = x[words[which]].restart();
118 //
119 // return x;
120 // }
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137  /*
138  *
139  *
140  *
141  *
142  *
143  *
144  *
145  *
146  *
147  *
148  *
149  *
150  *
151  *
152  *
153  * Proposals for debugging him
154  *
155  *
156  *
157  * */
158 
159 // virtual std::string string(std::string prefix="") const override {
160 // /**
161 // * @brief Convert a lexicon to a string -- defaultly includes all arguments.
162 // * @return
163 // */
164 // return factors.at("him").string();
165 // }
166 // [[nodiscard]] virtual std::optional<std::pair<MyHypothesis,double>> propose() const override {
167 //
168 // // now go through and propose to those factors
169 // // (NOTE fb is always zero)
170 // // NOTE: This is not great because it doesn't copy like we might want...
171 // MyHypothesis x; double fb = 0.0;
172 // for(auto& [k,f] : factors) {
173 // if(k == "him") {
174 // auto p = f.propose();
175 // if(p){
176 // auto [h, _fb] = p.value();
177 // x.factors[k] = h;
178 // fb += _fb;
179 // }
180 // else {
181 // x.factors[k] = f; // on failed proposal just copy
182 // }
183 // } else {
184 // x.factors[k] = f;
185 // }
186 // }
187 // assert(x.factors.size() == factors.size());
188 //
189 // return std::make_pair(x,fb);
190 // }
191 // [[nodiscard]] virtual MyHypothesis restart() const override {
192 // auto x = *this;
193 //
194 // x["him"] = x["him"].restart();
195 //
196 // return x;
197 // }
198 
199 };
A lexicon stores an association of numbers (in a vector) to some other kind of hypotheses (typically ...
std::string QQ(const std::string &x)
Definition: Strings.h:190
double likelihood
Definition: Bayesable.h:42
OrderedLock output_lock
Definition: IO.h:31
virtual double compute_posterior(const data_t &data, const std::pair< double, double > breakoutpair=std::make_pair(-infinity, 1.0))
Compute the posterior, by calling prior and likelihood. This includes only a little bit of fanciness...
Definition: Bayesable.h:134
#define TAB
Definition: IO.h:19
double prior
Definition: Bayesable.h:41
int NDATA
Definition: Main.cpp:21
void clear_cache()
Definition: MyHypothesis.h:12
double posterior
Definition: Bayesable.h:43
we don&#39;t need inputs/outputs for out MyHypothesis
Definition: MyHypothesis.h:6
virtual void show(std::string prefix="") override
Definition: MyHypothesis.h:74
constexpr double infinity
Definition: Numerics.h:20
MyHypothesis target
Definition: Main.cpp:55
Definition: Lexicon.h:27
std::map< key_t, InnerHypothesis > factors
Definition: Lexicon.h:44
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: MyHypothesis.h:18
#define ENDL
Definition: IO.h:21
std::vector< Args... > data_t
Definition: Bayesable.h:38
#define COUT
Definition: IO.h:24
const std::vector< std::string > words
Definition: Main.cpp:15
Definition: LRUCache.h:19