barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
geese-meat-likelihood_exhaust.hpp
Go to the documentation of this file.
1 
2 #ifndef GEESE_MEAT_LIKELIHOOD_EXHAUST_HPP
3 #define GEESE_MEAT_LIKELIHOOD_EXHAUST_HPP 1
4 // #include "../../barry.hpp"
5 // #include "geese-bones.hpp"
6 
8  const std::vector< double > & par
9 )
10 {
11 
12  INITIALIZED()
13 
14  // Splitting the probabilities
15  std::vector< double > par0(par.begin(), par.end() - nfunctions);
16  std::vector< double > par_root(par.end() - nfunctions, par.end());
17 
18  // Scaling root
19  for (auto& p : par_root)
20  p = std::exp(p)/(std::exp(p) + 1);
21 
22  // This is only worthwhile if the number of nodes is small
23  if (this->nnodes() > 6)
24  throw std::overflow_error("Too many nodes! Exhaust calculation of likelihood cannot be done for such cases.");
25 
26  if (this->nfuns() > 3)
27  throw std::overflow_error("Too many functions! Exhaust calculation of likelihood cannot be done for such cases.");
28 
29  // Computing all combinations ----------------------------------------------
30  PhyloArray base(nfuns(), nnodes());
31  for (auto& n : nodes)
32  {
33 
34  for (size_t i = 0u; i < nfuns(); ++i)
35  base(i, n.second.ord) = n.second.annotations[i];
36 
37  }
38 
39  PhyloPowerSet pset(base);//this->nfuns(), this->nnodes());
40  pset.add_rule(
41  rule_empty_free<PhyloArray,PhyloRuleData>,
43  );
44  pset.calc();
45 
46  // Inverse sequence
47  std::vector< size_t > preorder(this->sequence);
48  std::reverse(preorder.begin(), preorder.end());
49 
50  double totprob = 0.0;
51 
52  // This vector says whether the probability has to be included in
53  // the final likelihood or not.
54  for (size_t p = 0u; p < pset.size(); ++p)
55  {
56 
57  // ith state
58  const PhyloArray * s = &pset[p];
59 
60  // Following the sequence
61  double prob = 1.0;
62  std::vector< size_t > tmpstates(this->nfuns());
63 
64  Node * node;
65  for (auto& i : preorder)
66  {
67 
68  node = &nodes[i];
69  std::fill(tmpstates.begin(), tmpstates.end(), 0u);
70  s->get_col_vec(&tmpstates, node->ord, false);
71 
72  // Root node first
73  if (node->parent == nullptr)
74  {
75  // Since it is the root, the first probability is computed using
76  // the root only
77  for (auto k = 0u; k < this->nfuns(); ++k)
78  prob *= tmpstates[k] == 1u ? par_root[k] : (1.0 - par_root[k]);
79 
80  }
81  else if (node->is_leaf())
82  continue;
83 
84  // Computing the transition
85  PhyloArray transition(nfuns(), node->offspring.size());
86 
87  std::vector< double > bl(node->offspring.size(), 1.0);
88 
89  std::vector< bool > sl = vector_caster<bool,size_t>(tmpstates);
90 
91  transition.set_data(
92  new NodeData(bl, sl, node->duplication),
93  true
94  );
95 
96  // Filling the array
97  for (size_t a = 0u; a < nfuns(); ++a)
98  {
99 
100  for (size_t o = 0u; o < node->offspring.size(); ++o)
101  {
102 
103  if (s->get_cell(a, node->offspring[o]->id) == 1u)
104  transition(a, o) = 1u;
105 
106  }
107 
108  }
109 
110  prob *= this->model->likelihood(
111  par0,
112  transition,
113  node->narray[this->map_to_state_id[tmpstates]],
114  false
115  );
116 
117  }
118 
119  totprob += prob;
120  }
121 
122  return totprob;
123 
124 }
125 #endif
return * this
std::vector< size_t > sequence
size_t nfunctions
size_t nnodes() const noexcept
Number of nodes (interior + leaf)
Definition: geese-meat.hpp:437
std::map< size_t, Node > nodes
size_t nfuns() const noexcept
Number of functions analyzed.
Definition: geese-meat.hpp:430
double likelihood_exhaust(const std::vector< double > &par)
Data definition for the PhyloArray class.
Definition: geese-types.hpp:15
A single node for the model.
bool duplication
std::vector< Node * > offspring
Offspring nodes.
std::vector< size_t > narray
ID of the array in the model.
Node * parent
Parent node.
size_t ord
Order in which the node was created.
bool is_leaf() const noexcept
size_t i
#define INITIALIZED()
Definition: geese-bones.hpp:22
std::vector< std::pair< size_t, size_t > > PhyloRuleData
barry::BArrayDense< size_t, NodeData > PhyloArray
barry::PowerSet< PhyloArray, PhyloRuleData > PhyloPowerSet