Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
DiscreteDistribution.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 __DISCRETE_DISTRIBUTION_H_INCLUDED__
10#define __DISCRETE_DISTRIBUTION_H_INCLUDED__
11
12#include <assert.h>
13#include <random>
14#include <vector>
15
17{
28template <typename State> class DiscreteDistribution
29{
30 private:
32 class Param
33 {
34 private:
35 std::vector<State> m_support;
36 std::vector<double> m_weights;
37 using distribution_type = DiscreteDistribution<State>;
38
39 public:
41 Param() : m_support(std::vector<State>(1)), m_weights(std::vector<double>(1, 1.0))
42 {
43 }
45 Param(Param const &other) = default;
47 Param(std::vector<State> const &support, std::vector<double> const &weights)
48 : m_support(support), m_weights(weights)
49 {
50 }
52 Param(std::vector<State> &&support, std::vector<double> &&weights)
53 : m_support(std::move(support)), m_weights(std::move(weights))
54 {
55 }
57 Param &operator=(Param const &other) = default;
59 bool operator==(Param const &other)
60 {
61 return (m_support == other.m_support && m_weights == other.m_weights);
62 }
64 std::vector<State> const &support() const
65 {
66 return m_support;
67 }
69 std::vector<double> const &weights() const
70 {
71 return m_weights;
72 }
73 }; // end inner class Param
74 Param m_param;
75
76 public:
78 using result_type = State;
82 using param_type = Param;
93 explicit DiscreteDistribution(const param_type &p) : m_param(p)
94 {
95 }
107 DiscreteDistribution(std::vector<result_type> const &support, std::vector<double> const &weights)
108 : m_param(support, weights)
109 {
110 }
114 DiscreteDistribution(std::vector<result_type> &&support, std::vector<double> &&weights) noexcept
115 : m_param(std::move(support), std::move(weights))
116 {
117 }
131 template <typename Generator> result_type operator()(Generator &g) const
132 {
133 std::discrete_distribution<int> d(m_param.weights().cbegin(), m_param.weights().cend());
134 return m_param.support()[d(g)];
135 }
143 template <typename Generator> result_type operator()(Generator &g, const param_type &params) const
144 {
145 std::discrete_distribution<int> d(params.weights().cbegin(), params.weights().cend());
146 return params.support()[d(g)];
147 }
148}; // end DiscreteDistribution
149} // namespace quetzal::utils::random
150
151#endif
Sampling (non) arithmetic values in discrete probability distribution.
Definition DiscreteDistribution.hpp:29
DiscreteDistribution(DiscreteDistribution< State > &&other)=default
Move constructor.
DiscreteDistribution< State > & operator=(DiscreteDistribution< State > const &other)=default
Copy assignment operator.
DiscreteDistribution(const param_type &p)
Constructor.
Definition DiscreteDistribution.hpp:93
DiscreteDistribution(std::vector< result_type > &&support, std::vector< double > &&weights) noexcept
Constructor.
Definition DiscreteDistribution.hpp:114
DiscreteDistribution< State > & operator=(DiscreteDistribution< State > &&other)=default
Move assignment operator.
result_type operator()(Generator &g, const param_type &params) const
Generates random numbers that are distributed according to params The entropy is acquired by calling ...
Definition DiscreteDistribution.hpp:143
DiscreteDistribution()=default
Default constructor.
DiscreteDistribution(std::vector< result_type > const &support, std::vector< double > const &weights)
Constructor.
Definition DiscreteDistribution.hpp:107
DiscreteDistribution(DiscreteDistribution< State > const &other)=default
Copy constructor.
result_type operator()(Generator &g) const
Generates random objects that are distributed according to the associated parameter set....
Definition DiscreteDistribution.hpp:131
Random sampling processes.
Definition utils.hpp:26