2 #ifndef GEESE_MEAT_PREDICT_EXHAUST_HPP
3 #define GEESE_MEAT_PREDICT_EXHAUST_HPP 1
6 const std::vector< double > & par
13 throw std::overflow_error(
"Too many nodes! Exhaust calculation of likelihood cannot be done for such cases.");
15 if (this->
nfuns() > 2)
16 throw std::overflow_error(
"Too many functions! Exhaust calculation of prediction cannot be done for such cases.");
20 std::vector< size_t > preorder(this->
sequence);
21 std::reverse(preorder.begin(), preorder.end());
29 std::vector< size_t > missing_vec(
nfuns(), 9u);
30 for (
auto &
i : annotated_ids) {
49 const std::vector< double > & par,
50 const std::vector< size_t > & preorder
54 std::vector< double > par_terms(par.begin(), par.end() -
nfuns());
55 std::vector< double > par_root(par.end() -
nfuns(), par.end());
58 for (
auto& p : par_root)
59 p = std::exp(p)/(std::exp(p) + 1);
61 double baseline_likelihood = this->
likelihood(par);
69 for (
size_t f = 0u; f <
nfuns(); ++f)
70 base(f, n.second.ord) = n.second.annotations[f];
76 rule_empty_free<PhyloArray,PhyloRuleData>,
82 std::vector< double > expected(
nnodes() *
nfuns(), 0.0);
86 for (
size_t p = 0u; p < pset.size(); ++p)
93 double current_prob = 1.0;
94 for (
auto & o: preorder)
104 auto par_state = s->get_col_vec(n.
ord,
false);
110 for (
size_t f = 0u; f <
nfuns(); ++f)
111 current_prob *= par_state[f] ? par_root[f] : (1.0 - par_root[f]);
120 for (
size_t f = 0u; f <
nfuns(); ++f)
121 tmparray.D_ptr()->states[f] = par_state[f] == 1u;
127 for (
size_t f = 0u; f <
nfuns(); ++f)
130 if (s->operator()(f, off->ord) == 1u)
131 tmparray(f, loc) = 1u;
133 tmparray.rm_cell(f, loc);
143 current_prob *= model->likelihood(par_terms, tmparray, -1,
false);
149 for (
auto & n:
nodes)
150 for (
size_t j = 0u;
j <
nfuns(); ++
j)
151 expected[n.second.ord +
j *
nnodes()] += s->operator()(
j, n.second.ord) * current_prob/
157 std::vector< std::vector< double > >
res(
nnodes());
158 std::vector< double > zerovec(
nfuns(), 0.0);
159 for (
auto & n:
nodes)
161 res[n.second.ord] = zerovec;
162 for (
size_t i = 0u;
i <
nfuns(); ++
i)
163 res[n.second.ord][
i] = expected[n.second.ord +
i *
nnodes()];
std::vector< size_t > sequence
size_t nnodes() const noexcept
Number of nodes (interior + leaf)
double likelihood(const std::vector< double > &par, bool as_log=false, bool use_reduced_sequence=true, size_t ncores=1u, bool no_update_pset_probs=false)
std::vector< std::vector< double > > predict_exhaust_backend(const std::vector< double > &par, const std::vector< size_t > &preorder)
std::vector< size_t > get_annotated_nodes() const
Returns the ids of the nodes with at least one annotation.
void update_annotations(size_t nodeid, std::vector< size_t > newann)
std::vector< std::vector< double > > predict_exhaust(const std::vector< double > &par)
std::map< size_t, Node > nodes
size_t nfuns() const noexcept
Number of functions analyzed.
A single node for the model.
std::vector< size_t > annotations
Observed annotations (only defined for Geese)
PhyloArray array
Array of the node.
std::vector< Node * > offspring
Offspring nodes.
Node * parent
Parent node.
size_t ord
Order in which the node was created.
bool is_leaf() const noexcept
std::vector< std::pair< size_t, size_t > > PhyloRuleData
barry::BArrayDense< size_t, NodeData > PhyloArray
barry::PowerSet< PhyloArray, PhyloRuleData > PhyloPowerSet