Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
sampling_policy.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __SAMPLING_POLICY_H_INCLUDED__
10#define __SAMPLING_POLICY_H_INCLUDED__
11
12#include "editor_policy.hpp"
13#include "filter_policy.hpp"
14#include "memoize.hpp"
15
16#include <random>
17#include <utility> // std::forward, std::move
18#include <vector>
19
20namespace quetzal
21{
22namespace coalescence
23{
24namespace occupancy_spectrum
25{
26namespace sampling_policy
27{
34template <class FilterPolicy = filter_policy::return_always_true, class EditorPolicy = editor_policy::identity>
36{
46 template <typename Generator> static auto const &sample(int k, int N, Generator &g)
47 {
48 assert(k > 0);
49 assert(N > 0);
50 return memoize::memoize<FilterPolicy, EditorPolicy>(k, N)(g);
51 }
52}; // end class in_memoized_distribution
57{
67 template <typename Generator> static auto sample(int k, int N, Generator &g)
68 {
69 assert(k > 0);
70 assert(N > 0);
71 std::vector<int> M_j(k + 1);
72 std::vector<int> parents(N);
73 // Populate the urns (parents) by throwing balls (lineages) at them
74 std::uniform_int_distribution<int> distribution(0, N - 1);
75 for (int i = 1; i <= k; ++i)
76 {
77 parents[distribution(g)] += 1;
78 }
79 // Build the occupancy spectrum by computing the occupancy numbers
80 for (int it : parents)
81 {
82 M_j[it] += 1;
83 }
84 assert(M_j.size() == k + 1);
85 return OccupancySpectrum(M_j, k, N);
86 }
87};
88} // end namespace sampling_policy
89} // namespace occupancy_spectrum
90} // namespace coalescence
91} // namespace quetzal
92
93#endif
An occupancy spectrum as defined in Becheler & Knowles, 2020.
Definition OccupancySpectrum.hpp:42
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21
Sample occupancy spectrums from a pre-computed ProbabilityDistribution object.
Definition sampling_policy.hpp:36
static auto const & sample(int k, int N, Generator &g)
Sample an occupancy spectrum.
Definition sampling_policy.hpp:46
Sample occupancy spectrums by simulating a ball to urn assignment random experiment.
Definition sampling_policy.hpp:57
static auto sample(int k, int N, Generator &g)
Sample an occupancy spectrum.
Definition sampling_policy.hpp:67