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-simulate.hpp
Go to the documentation of this file.
1 #ifndef GEESE_MEAT_SIMULATE_HPP
2 #define GEESE_MEAT_SIMULATE_HPP 1
3 
4 inline void Geese::set_seed(const size_t & s) {
5  rengine->seed(s);
6 }
7 
8 inline std::vector< std::vector< size_t > > Geese::simulate(
9  const std::vector< double > & par
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 
23  // Making room
24  std::vector< std::vector< size_t > > res(nodes.size());
25 
26  // Inverse sequence
27  std::vector< size_t > preorder(this->sequence);
28  std::reverse(preorder.begin(), preorder.end());
29 
30  // Generating probabilities at the root-level (root state)
31  std::vector< double > rootp(states.size(), 1.0);
32  for (size_t i = 0u; i < rootp.size(); ++i)
33  {
34 
35  for (size_t j = 0u; j < nfuns(); ++j)
36  rootp[i] *= states[i][j] ? par_root[j] : (1.0 - par_root[j]);
37 
38  }
39 
40  // Preparing the random number generator
41  std::uniform_real_distribution<> urand(0, 1);
42  double r = urand(*rengine);
43  size_t idx = 0u;
44  double cumprob = rootp[idx];
45  while ((idx < rootp.size()) && (cumprob < r))
46  {
47  cumprob += rootp[++idx];
48  }
49 
50  #ifdef BARRY_DEBUG
51 
52  // auto totprob = std::accumulate(rootp.begin(), rootp.end(), 0.0);
53  // if (totprob < 0.9999999999999999 || totprob > 1.0000000000000001)
54  // throw std::runtime_error("Root probabilities do not sum to 1!"
55  // " (totprob = " + std::to_string(totprob) + ")");
56 
57  #endif
58 
59  // We now know the state of the root
60  res[nodes[preorder[0u]].ord] =
61  vector_caster< size_t, bool>(states[idx]);
62 
63  // Going in the opposite direction
64  for (auto& i : preorder)
65  {
66 
67  if (nodes[i].is_leaf())
68  continue;
69 
70  const Node & n = nodes[i];
71 
72  // Getting the id of the state
73  size_t lth_state = map_to_state_id[res[n.ord]];
74 
75  // Given the state of the current node, sample the state of the
76  // offspring, all based on the current state
77  // auto z = n.narray;
78  auto tmp = model->sample(n.narray[lth_state], par0);
79 
80  // Iterating through the offspring to assign the state
81  for (size_t j = 0u; j < n.offspring.size(); ++j)
82  res[n.offspring[j]->ord] = tmp.get_col_vec(j, false);
83 
84  }
85 
86  return res;
87 
88 }
89 
90 #endif
std::vector< size_t > sequence
size_t nfunctions
std::vector< std::vector< size_t > > simulate(const std::vector< double > &par)
std::map< size_t, Node > nodes
barry::MapVec_type< size_t > map_to_state_id
size_t nfuns() const noexcept
Number of functions analyzed.
Definition: geese-meat.hpp:430
void set_seed(const size_t &s)
A single node for the model.
std::vector< Node * > offspring
Offspring nodes.
std::vector< size_t > narray
ID of the array in the model.
size_t ord
Order in which the node was created.
return res
size_t size_t j
size_t i
#define INITIALIZED()
Definition: geese-bones.hpp:22