1 #ifndef GEESE_MEAT_LIKELIHOOD_HPP
2 #define GEESE_MEAT_LIKELIHOOD_HPP 1
11 const size_t array_id,
12 std::vector< double > & totprob_n,
13 const std::vector< double > & par0,
14 const std::vector<std::vector<bool>> & states,
15 const std::vector< PhyloArray > & psets,
16 const std::vector< std::vector< size_t > > & locations,
17 const std::vector<geese::Node *> & node_offspring,
18 const double * psetprobs
22 const auto & x = psets[n];
25 throw std::logic_error(
"This is only supported for dense arrays.");
27 const size_t * location_x = &locations[n][0u];
30 double off_mult = 1.0;
32 for (
auto o = 0u; o < x.ncol(); ++o)
36 const Node * n_off = node_offspring[o];
42 for (
auto f = 0u; f < nfunctions; ++f)
79 off_mult *= *(psetprobs + n);
81 }
catch (std::exception & e) {
83 auto err = std::string(e.what());
85 std::string state_str =
"";
86 for (
const auto & ss : states[s])
87 state_str += std::to_string(ss) +
" ";
89 err =
"Error computing the likelihood at node " +
90 std::to_string(node_id) +
" with state " + state_str +
91 ". Error message:\n" +
94 throw std::runtime_error(err);
99 totprob_n[n] = off_mult;
104 const std::vector< double > & par,
106 bool use_reduced_sequence,
108 bool no_update_pset_probs
115 std::vector< double > par0(par.begin(), par.end() -
nfunctions);
116 std::vector< double > par_root(par.end() -
nfunctions, par.end());
119 for (
auto& p : par_root)
120 p = std::exp(p)/(std::exp(p) + 1);
125 if (!no_update_pset_probs)
126 model->update_pset_probs(par0, ncores);
129 const std::vector< size_t > & preseq = use_reduced_sequence ?
135 const auto & arrays2support = *(model->get_arrays2support());
136 const auto & psetprobs = *(model->get_pset_probs());
137 const auto & pset_locations = *(model->get_pset_locations());
139 for (
auto&
i : preseq)
143 if (this->
nodes[
i].is_leaf())
148 const size_t node_id = node.
id;
151 for (
size_t s = 0u; s < states.size(); ++s)
155 size_t array_id = node.
narray[s];
156 size_t support_id = arrays2support[array_id];
157 const double * psetsprobs_s = &psetprobs[pset_locations[support_id]];
160 const std::vector< PhyloArray > & psets =
161 *(model->get_pset(array_id));
163 std::vector< std::vector< size_t > > & locations =
pset_loc[support_id];
166 if (psets.size() < 128)
170 const auto & node_offspring = node.
offspring;
171 std::vector< double > totprob_n(psets.size(), 0.0);
172 for (
size_t n = 0u; n < psets.size(); ++n)
175 n, s,
nfunctions, node_id, array_id, totprob_n,
176 par0, states, psets, locations,
177 node_offspring, psetsprobs_s
184 #if defined(_OPENMP) || defined(__OPENMP)
185 #pragma omp simd reduction(+:nsp)
187 for (
size_t n = 0u; n < psets.size(); ++n)
194 if (node.
parent ==
nullptr)
197 for (
size_t s = 0u; s < states.size(); ++s)
205 tmpll *= states[s][k] ? par_root[k] : (1 - par_root[k]);
219 if (preseq.size() == 0u)
220 return as_log ? -std::numeric_limits<double>::infinity() : 1.0;
223 return as_log ? std::log(ll) : ll;
std::vector< size_t > reduced_sequence
std::vector< size_t > sequence
std::vector< std::vector< std::vector< size_t > > > pset_loc
Locations of columns.
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::map< size_t, Node > nodes
A single node for the model.
std::vector< size_t > annotations
Observed annotations (only defined for Geese)
size_t id
Id of the node (as specified in the input)
std::vector< Node * > offspring
Offspring nodes.
std::vector< size_t > narray
ID of the array in the model.
Node * parent
Parent node.
bool is_leaf() const noexcept
std::vector< double > subtree_prob
Induced subtree probabilities.
void pset_loop(size_t n, size_t s, size_t nfunctions, const size_t node_id, const size_t array_id, std::vector< double > &totprob_n, const std::vector< double > &par0, const std::vector< std::vector< bool >> &states, const std::vector< PhyloArray > &psets, const std::vector< std::vector< size_t > > &locations, const std::vector< geese::Node * > &node_offspring, const double *psetprobs)