15 template<
typename this_t,
18 typename data_t=std::vector<datum_t>,
38 virtual void recompute_LL(std::vector<HYP>& hypotheses,
const data_t& human_data)
override {
39 assert(this->
which_data == std::addressof(human_data));
42 std::unordered_map<typename datum_t::data_t*, size_t> max_sizes;
43 for(
auto& d : human_data) {
44 if( (not max_sizes.contains(d.data)) or max_sizes[d.data] < d.ndata) {
45 max_sizes[d.data] = d.ndata;
49 this->
LL.reset(
new LL_t());
50 this->
LL->reserve(max_sizes.size());
53 for(
const auto& [dptr, sz] : max_sizes) {
58 #pragma omp parallel for 62 Vector data_lls = Vector::Zero(sz);
65 const auto& M = this->
P->at(h,0);
68 for(
size_t i=0;i<max_sizes[dptr];i++) {
70 d.push_back(dptr->at(i));
72 data_lls(i) = MyHypothesis::string_likelihood(M, d);
74 assert(not std::isnan(data_lls(i)));
78 this->
LL->at(dptr)[h] = std::move(data_lls);
91 virtual void recompute_P(std::vector<HYP>& hypotheses,
const data_t& human_data)
override {
92 assert(this->
which_data == std::addressof(human_data));
94 this->
P.reset(
new Predict_t(hypotheses.size(), 1));
96 #pragma omp parallel for 97 for(
size_t h=0;h<hypotheses.size();h++) {
100 auto ret = hypotheses[h].call();
103 this->
P->at(h,0) = std::move(ret);
114 std::map<typename HYP::output_t, double> model_predictions;
116 for(
int h=0;h<hposterior.rows();h++) {
117 if(hposterior(h,i) < 1e-6)
continue;
119 for(
const auto& [outcome,outcomelp] : this->
P->at(h,0)) {
120 model_predictions[outcome] += hposterior(h,i) * exp(outcomelp);
124 return model_predictions;
std::shared_ptr< LL_t > LL
Definition: BaseGrammarHypothesis.h:90
virtual std::map< typename HYP::output_t, double > compute_model_predictions(const data_t &human_data, const size_t i, const Matrix &hposterior) const override
In this variant, we need to always use P->at(h,0) since we only have one prediction stored for thunks...
Definition: ThunkGrammarHypothesis.h:112
virtual void set_hypotheses_and_data(std::vector< HYP > &hypotheses, const data_t &human_data)
This is the primary function for setting hypothese and data on construction.
Definition: BaseGrammarHypothesis.h:172
_Predict_t Predict_t
Definition: BaseGrammarHypothesis.h:58
virtual void recompute_LL(std::vector< HYP > &hypotheses, const data_t &human_data) override
Recompute LL[h,di] a hypothesis from each hypothesis and data point to a vector of prior responses...
Definition: ThunkGrammarHypothesis.h:38
Eigen::MatrixXf Matrix
Definition: EigenLib.h:18
Definition: BaseGrammarHypothesis.h:48
virtual size_t nhypotheses() const
A convenient function that uses C to say how many hypotheses.
Definition: BaseGrammarHypothesis.h:228
we don't need inputs/outputs for out MyHypothesis
Definition: MyHypothesis.h:6
volatile sig_atomic_t CTRL_C
ThunkGrammarHypothesis(std::vector< HYP > &hypotheses, const data_t *human_data)
Definition: ThunkGrammarHypothesis.h:30
Definition: HumanDatum.h:19
Definition: ThunkGrammarHypothesis.h:20
Super::LL_t LL_t
Definition: ThunkGrammarHypothesis.h:25
std::shared_ptr< Predict_t > P
Definition: BaseGrammarHypothesis.h:92
Just a little wrapper to allow vectors to be handled as 2D arrays, which simplifie some stuff in Gram...
Definition: Vector2D.h:14
const data_t * which_data
Definition: BaseGrammarHypothesis.h:99
std::vector< Args... > data_t
Definition: Bayesable.h:39
std::unordered_map< typename MyHumanDatum ::std::vector< MyHumanDatum > *, std::vector< Vector > > LL_t
Definition: BaseGrammarHypothesis.h:61
virtual void recompute_P(std::vector< HYP > &hypotheses, const data_t &human_data) override
For a thunk, the predictions don't depend on the data.
Definition: ThunkGrammarHypothesis.h:91
This class does grammar inference with some collection of HumanData and fixed set of hypotheses...
Super::Predict_t Predict_t
Definition: ThunkGrammarHypothesis.h:26
Eigen::VectorXf Vector
Definition: EigenLib.h:17