barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
model-bones.hpp
Go to the documentation of this file.
1 #ifndef BARRY_MODEL_BONES_HPP
2 #define BARRY_MODEL_BONES_HPP 1
3 
28 template<
29  typename Array_Type = BArray<>,
30  typename Data_Counter_Type = bool,
31  typename Data_Rule_Type = bool,
32  typename Data_Rule_Dyn_Type = bool
33  >
34 class Model {
35 
36 protected:
42  std::mt19937 * rengine = nullptr;
43  bool delete_rengine = false;
44 
62  std::vector< double > stats_support;
63  std::vector< size_t > stats_support_sizes;
64  std::vector< size_t > stats_support_sizes_acc;
65  std::vector< size_t > stats_support_n_arrays;
66  std::vector< std::vector< double > > stats_target;
67  std::vector< double > stats_likelihood;
68  std::vector< size_t > arrays2support;
70 
76 
83  bool with_pset = false;
84  std::vector< std::vector< Array_Type > > pset_arrays;
85  std::vector< double > pset_stats;
86  std::vector< double > pset_probs;
87  std::vector< size_t > pset_sizes;
88  std::vector< size_t > pset_locations;
90 
102 
104  std::vector< std::vector<double> > params_last;
105  std::vector< double > normalizing_constants;
106  std::vector< bool > first_calc_done;
107 
108  bool delete_counters = false;
109  bool delete_rules = false;
110  bool delete_rules_dyn = false;
111 
127  std::function<std::vector<double>(double *, size_t k)>
129 
130  std::vector< std::string > transform_model_term_names;
131 
132 public:
133 
141  const std::vector< double > & params,
142  BARRY_NCORES_ARG(=1),
143  int i = -1
144  );
145 
146  void update_likelihoods(
147  const std::vector< double > & params,
148  BARRY_NCORES_ARG(=1)
149  );
150 
151  void update_pset_probs(
152  const std::vector< double > & params,
153  BARRY_NCORES_ARG(=1),
154  int i = -1
155  );
156 
157  void set_rengine(std::mt19937 * rengine_, bool delete_ = false) {
158 
159  if (delete_rengine)
160  delete rengine;
161 
162  rengine = rengine_;
163  delete_rengine = delete_;
164 
165  };
166 
167  void set_seed(size_t s) {
168 
169  if (rengine == nullptr)
170  {
171  rengine = new std::mt19937;
172  delete_rengine = true;
173  }
174 
175  rengine->seed(s);
176 
177  };
179 
180  Model();
181  Model(size_t size_);
185  );
186 
187  virtual ~Model() {
188  if (delete_counters)
189  delete counters;
190 
191  if (delete_rules)
192  delete rules;
193 
194  if (delete_rules_dyn)
195  delete rules_dyn;
196 
197  if (delete_rengine)
198  delete rengine;
199  };
200 
201  void store_psets() noexcept;
202  std::vector< double > gen_key(const Array_Type & Array_);
203 
210  void add_counter(Counter<Array_Type, Data_Counter_Type> & counter);
211  void add_counter(
212  Counter_fun_type<Array_Type,Data_Counter_Type> count_fun_,
213  Counter_fun_type<Array_Type,Data_Counter_Type> init_fun_ = nullptr,
214  Data_Counter_Type data_ = nullptr
215  );
216  void set_counters(Counters<Array_Type,Data_Counter_Type> * counters_);
217  void add_hasher(Hasher_fun_type<Array_Type,Data_Counter_Type> fun_);
219 
226  void add_rule(Rule<Array_Type, Data_Rule_Type> & rule);
227  void add_rule(
228  Rule_fun_type<Array_Type, Data_Rule_Type> count_fun_,
229  Data_Rule_Type data_
230  );
231 
232  void set_rules(Rules<Array_Type,Data_Rule_Type> * rules_);
233 
234  void add_rule_dyn(Rule<Array_Type, Data_Rule_Dyn_Type> & rule);
235  void add_rule_dyn(
236  Rule_fun_type<Array_Type, Data_Rule_Dyn_Type> count_fun_,
237  Data_Rule_Dyn_Type data_
238  );
239 
240  void set_rules_dyn(Rules<Array_Type,Data_Rule_Dyn_Type> * rules_);
242 
243 
253  size_t add_array(const Array_Type & Array_, bool force_new = false);
254 
255 
268  double likelihood(
269  const std::vector<double> & params,
270  const size_t & i,
271  bool as_log = false,
272  bool no_update_normconst = false
273  );
274 
275  double likelihood(
276  const std::vector<double> & params,
277  const Array_Type & Array_,
278  int i = -1,
279  bool as_log = false,
280  bool no_update_normconst = false
281  );
282 
283  double likelihood(
284  const std::vector<double> & params,
285  const std::vector<double> & target_,
286  const size_t & i,
287  bool as_log = false,
288  bool no_update_normconst = false
289  );
290 
291  double likelihood(
292  const std::vector<double> & params,
293  const double * target_,
294  const size_t & i,
295  bool as_log = false,
296  bool no_update_normconst = false
297  );
298 
299  double likelihood_total(
300  const std::vector<double> & params,
301  bool as_log = false,
302  BARRY_NCORES_ARG(=2),
303  bool no_update_normconst = false
304  );
306 
316  const std::vector< double > & get_normalizing_constants() const;
317  const std::vector< double > & get_likelihoods() const;
318 
319  const std::vector< Array_Type > * get_pset(
320  const size_t & i
321  );
322 
323  const double * get_pset_stats(
324  const size_t & i
325  );
327 
328  void print_stats(size_t i) const;
329 
333  virtual void print() const;
334 
335  Array_Type sample(const Array_Type & Array_, const std::vector<double> & params = {});
336  Array_Type sample(const size_t & i, const std::vector<double> & params);
337 
351  double conditional_prob(
352  const Array_Type & Array_,
353  const std::vector< double > & params,
354  size_t i,
355  size_t j
356  );
357 
371  size_t size() const noexcept;
372  size_t size_unique() const noexcept;
373  size_t nterms() const noexcept;
374  size_t nrules() const noexcept;
375  size_t nrules_dyn() const noexcept;
376  size_t support_size() const noexcept;
377  std::vector< std::string > colnames() const;
379 
380  const std::mt19937 * get_rengine() const;
381 
382  Counters<Array_Type,Data_Counter_Type> * get_counters();
383  Rules<Array_Type,Data_Rule_Type> * get_rules();
384  Rules<Array_Type,Data_Rule_Dyn_Type> * get_rules_dyn();
385  Support<Array_Type,Data_Counter_Type,Data_Rule_Type,Data_Rule_Dyn_Type> * get_support_fun();
386 
398  std::vector< std::vector< double > > * get_stats_target();
399  std::vector< double > * get_stats_support();
400  std::vector< size_t > * get_stats_support_sizes();
401  std::vector< size_t > * get_stats_support_sizes_acc();
402  std::vector< size_t > * get_arrays2support();
403  std::vector< std::vector< Array_Type > > * get_pset_arrays();
404  std::vector< double > * get_pset_stats();
405  std::vector< double > * get_pset_probs();
406  std::vector< size_t > * get_pset_sizes();
407  std::vector< size_t > * get_pset_locations();
409 
420  void set_transform_model(
421  std::function<std::vector<double>(double*,size_t)> fun,
422  std::vector< std::string > names
423  );
424  std::vector<double> transform_model(
425  double * data,
426  size_t k
427  );
429 
430 };
431 
432 
433 #endif
#define BARRY_NCORES_ARG(default)
Baseline class for binary arrays.
A counter function based on change statistics.
Vector of counters.
General framework for discrete exponential models. This class allows generating discrete exponential ...
Definition: model-bones.hpp:34
size_t size_unique() const noexcept
std::vector< std::string > transform_model_term_names
const std::mt19937 * get_rengine() const
bool delete_rules
void update_normalizing_constants(const std::vector< double > &params, BARRY_NCORES_ARG(=1), int i=-1)
Computes the normalizing constant for a given set of parameters.
Definition: model-meat.hpp:136
std::vector< size_t > * get_stats_support_sizes_acc()
Accumulated number of vectors included in the support.
void update_pset_probs(const std::vector< double > &params, BARRY_NCORES_ARG(=1), int i=-1)
Definition: model-meat.hpp:225
Rules< Array_Type, Data_Rule_Dyn_Type > * rules_dyn
Definition: model-bones.hpp:98
std::vector< size_t > pset_sizes
Number of vectors included in the support.
Definition: model-bones.hpp:87
Rules< Array_Type, Data_Rule_Type > * rules
Definition: model-bones.hpp:97
std::vector< double > * get_pset_stats()
Statistics of the support(s)
StatsCounter< Array_Type, Data_Counter_Type > counter_fun
size_t size() const noexcept
void add_counter(Counter< Array_Type, Data_Counter_Type > &counter)
Definition: model-meat.hpp:508
Counters< Array_Type, Data_Counter_Type > * get_counters()
std::vector< std::vector< double > > stats_target
Target statistics of the model.
Definition: model-bones.hpp:66
std::vector< double > pset_probs
Probabilities of the support(s)
Definition: model-bones.hpp:86
Array_Type sample(const Array_Type &Array_, const std::vector< double > &params={})
void add_rule_dyn(Rule< Array_Type, Data_Rule_Dyn_Type > &rule)
Definition: model-meat.hpp:591
std::vector< double > stats_likelihood
Definition: model-bones.hpp:67
std::vector< double > stats_support
Sufficient statistics of the model (support)
Definition: model-bones.hpp:62
void store_psets() noexcept
Definition: model-meat.hpp:495
double conditional_prob(const Array_Type &Array_, const std::vector< double > &params, size_t i, size_t j)
Conditional probability ("Gibbs sampler")
Model< Array_Type, Data_Counter_Type, Data_Rule_Type, Data_Rule_Dyn_Type > & operator=(const Model< Array_Type, Data_Counter_Type, Data_Rule_Type, Data_Rule_Dyn_Type > &Model_)
Definition: model-meat.hpp:439
std::vector< std::vector< double > > * get_stats_target()
Raw pointers to the support and target statistics.
bool delete_rengine
Definition: model-bones.hpp:43
std::vector< std::vector< Array_Type > > * get_pset_arrays()
virtual ~Model()
Support< Array_Type, Data_Counter_Type, Data_Rule_Type, Data_Rule_Dyn_Type > * get_support_fun()
std::vector< double > gen_key(const Array_Type &Array_)
Definition: model-meat.hpp:501
std::vector< size_t > arrays2support
Definition: model-bones.hpp:68
size_t add_array(const Array_Type &Array_, bool force_new=false)
Adds an array to the support of not already included.
Definition: model-meat.hpp:633
void add_hasher(Hasher_fun_type< Array_Type, Data_Counter_Type > fun_)
Definition: model-meat.hpp:552
std::vector< std::string > colnames() const
double likelihood(const std::vector< double > &params, const size_t &i, bool as_log=false, bool no_update_normconst=false)
Definition: model-meat.hpp:810
std::vector< size_t > stats_support_sizes_acc
Accumulated number of vectors included in the support.
Definition: model-bones.hpp:64
std::vector< double > * get_stats_support()
Sufficient statistics of the support(s)
void set_rules(Rules< Array_Type, Data_Rule_Type > *rules_)
Definition: model-meat.hpp:573
size_t nrules() const noexcept
std::vector< size_t > * get_pset_locations()
const std::vector< double > & get_normalizing_constants() const
std::vector< size_t > stats_support_n_arrays
Number of arrays included per support.
Definition: model-bones.hpp:65
void print_stats(size_t i) const
size_t nterms() const noexcept
double likelihood_total(const std::vector< double > &params, bool as_log=false, BARRY_NCORES_ARG(=2), bool no_update_normconst=false)
bool with_pset
Definition: model-bones.hpp:83
Rules< Array_Type, Data_Rule_Type > * get_rules()
bool delete_rules_dyn
bool delete_counters
void set_counters(Counters< Array_Type, Data_Counter_Type > *counters_)
Definition: model-meat.hpp:534
size_t support_size() const noexcept
size_t nrules_dyn() const noexcept
std::vector< std::vector< double > > params_last
Vector of the previously used parameters.
virtual void print() const
Prints information about the model.
std::vector< double > transform_model(double *data, size_t k)
std::mt19937 * rengine
Definition: model-bones.hpp:42
Support< Array_Type, Data_Counter_Type, Data_Rule_Type, Data_Rule_Dyn_Type > support_fun
Definition: model-bones.hpp:99
void set_rules_dyn(Rules< Array_Type, Data_Rule_Dyn_Type > *rules_)
Definition: model-meat.hpp:615
std::vector< std::vector< Array_Type > > pset_arrays
Arrays of the support(s)
Definition: model-bones.hpp:84
std::function< std::vector< double >double *, size_t k)> transform_model_fun
Transformation of the model.
std::vector< size_t > * get_arrays2support()
std::vector< double > * get_pset_probs()
std::vector< size_t > * get_pset_sizes()
MapVec_type< double, size_t > keys2support
Map of types of arrays to support sets.
Definition: model-bones.hpp:75
std::vector< size_t > stats_support_sizes
Number of vectors included in the support.
Definition: model-bones.hpp:63
std::vector< double > normalizing_constants
std::vector< size_t > * get_stats_support_sizes()
Number of vectors included in the support.
std::vector< double > pset_stats
Statistics of the support(s)
Definition: model-bones.hpp:85
std::vector< size_t > pset_locations
Accumulated number of vectors included in the support.
Definition: model-bones.hpp:88
std::vector< bool > first_calc_done
const std::vector< double > & get_likelihoods() const
void update_likelihoods(const std::vector< double > &params,)
Definition: model-meat.hpp:186
void set_transform_model(std::function< std::vector< double >(double *, size_t)> fun, std::vector< std::string > names)
Set the transform_model_fun object.
Counters< Array_Type, Data_Counter_Type > * counters
Definition: model-bones.hpp:96
const std::vector< Array_Type > * get_pset(const size_t &i)
void set_rengine(std::mt19937 *rengine_, bool delete_=false)
void add_rule(Rule< Array_Type, Data_Rule_Type > &rule)
Definition: model-meat.hpp:563
void set_seed(size_t s)
Rules< Array_Type, Data_Rule_Dyn_Type > * get_rules_dyn()
Rule for determining if a cell should be included in a sequence.
Definition: rules-bones.hpp:20
Vector of objects of class Rule.
Definition: rules-bones.hpp:71
Count stats for a single Array.
Compute the support of sufficient statistics.
Data_Type fun
Data_Type count_fun_
Data_Type &&counter_ data(std::move(counter_.data))
Data_Type Counter_fun_type< Array_Type, Data_Type > init_fun_
Data_Type fun_
size_t size_t j
Data_Type counter
size_t i
Data_Type &&counter_ noexcept
Data_Type Counter_fun_type< Array_Type, Data_Type > Hasher_fun_type< Array_Type, Data_Type > Data_Type data_
Data_Type * counters_
std::unordered_map< std::vector< Ta >, Tb, vecHasher< Ta > > MapVec_type
Definition: typedefs.hpp:128
std::function< bool(const Array_Type &, size_t, size_t, Data_Type &)> Rule_fun_type
Definition: typedefs.hpp:190
std::function< std::vector< double >(const Array_Type &, Data_Type *)> Hasher_fun_type
Hasher function used by the counter.
Definition: typedefs.hpp:200
std::function< double(const Array_Type &, size_t, size_t, Data_Type &)> Counter_fun_type
Counter and rule functions.
Definition: typedefs.hpp:187