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-predict_sim.hpp
Go to the documentation of this file.
1 // #include "geese-bones.hpp"
2 
3 #ifndef GEESE_MEAT_PREDICT_SIM_HPP
4 #define GEESE_MEAT_PREDICT_SIM_HPP 1
5 
6 inline std::vector< std::vector<double> > Geese::predict_sim(
7  const std::vector< double > & par,
8  bool use_reduced_sequence,
9  size_t nsims
10 )
11 {
12 
13  INITIALIZED()
14 
15  // Preparing
16  std::vector< std::vector< size_t > > tmp;
17 
18  std::vector< double > zerovec(nfuns(), 0.0);
19  std::vector< std::vector< double > > res_vec(nnodes(), zerovec);
20  std::vector< int > counts(nnodes(), 0);
21 
22  // We will iterate through this list everytime we need to check
23  // whether we have all the annotations for the conditional prob.
24  auto annotated = this->get_annotated_nodes();
25 
26  for (size_t i = 0u; i < nsims; ++i)
27  {
28 
29  // Generating a sample
30  tmp = this->simulate(par);
31 
32  for (auto j = nodes.begin(); j != nodes.end(); ++j)
33  {
34  // Retrieving node
35  const Node & n = j->second;
36 
37  // Checking we have all matching
38  bool includeit = true;
39  for (auto & id : annotated)
40  {
41 
42  // Same node need not to match (since we are not conditionin
43  // each node on itself!)
44  if (n.id == id)
45  continue;
46 
47  const auto & ord = nodes[id].ord;
48  const auto & n_w_ann = nodes[id].annotations;
49  for (size_t f = 0u; f < nfuns(); ++f)
50  {
51  // No checking missings
52  if (n_w_ann[f] == 9u)
53  continue;
54 
55  // If this is not matching, then we cannot use it!
56  if (n_w_ann[f] != tmp[ord][f])
57  {
58  includeit = false;
59  break;
60  }
61 
62  }
63 
64  if (!includeit)
65  break;
66  }
67 
68  // If it passed the test, then we can use it for counting stuff
69  if (!includeit)
70  continue;
71 
72  for (size_t f = 0u; f < nfuns(); ++f)
73  if (tmp[n.ord][f] == 1u)
74  res_vec[n.ord][f] += 1.0;
75 
76  ++counts[n.ord];
77 
78  }
79 
80  }
81 
82  // Once the simulations have finalized, we can then approximate
83  // probabilities
84  for (size_t i = 0u; i < nnodes(); ++i)
85  {
86 
87  // if no counts, then continue
88  if (counts[i] == 0u)
89  continue;
90 
91  #ifdef BARRY_DEBUG
92  printf_barry("We used %i counts for node %i.\n", counts[i], i);
93  #endif
94  for (size_t f = 0u; f < nfuns(); ++f)
95  res_vec[i][f] /= (static_cast< double >(counts[i]) + 1e-10);
96  }
97 
98  return res_vec;
99 
100 }
101 
102 
103 #endif
#define printf_barry
size_t nnodes() const noexcept
Number of nodes (interior + leaf)
Definition: geese-meat.hpp:437
std::vector< std::vector< double > > predict_sim(const std::vector< double > &par, bool only_annotated=false, size_t nsims=10000u)
std::vector< size_t > get_annotated_nodes() const
Returns the ids of the nodes with at least one annotation.
Definition: geese-meat.hpp:771
std::vector< std::vector< size_t > > simulate(const std::vector< double > &par)
std::map< size_t, Node > nodes
size_t nfuns() const noexcept
Number of functions analyzed.
Definition: geese-meat.hpp:430
A single node for the model.
size_t id
Id of the node (as specified in the input)
size_t ord
Order in which the node was created.
size_t size_t j
size_t i
#define INITIALIZED()
Definition: geese-bones.hpp:22