bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
beta_dist.h
Go to the documentation of this file.
1#ifndef BETA_DIST_H
2#define BETA_DIST_H
3
5#include "bitrl/bitrl_types.h"
7
8#include <boost/random/beta_distribution.hpp>
9#include <cmath>
10#include <type_traits>
11#include <vector>
12
13namespace bitrl
14{
15namespace utils::maths::stats
16{
17
23template <typename RealType = real_t> class BetaDist
24{
25public:
26 static_assert(std::is_floating_point<RealType>::value, "Not a floating point type");
27
32
36 explicit BetaDist(result_type alpha = 1.0, result_type std = 1.0);
37
42
46 result_type sample() const;
47
51 result_type sample(uint_t seed) const;
52
56 std::vector<result_type> sample_many(uint_t size) const;
57
61 std::vector<result_type> sample_many(uint_t size, uint_t seed) const;
62
67 result_type mean() const { return dist_.alpha() / (dist_.alpha() + dist_.beta()); }
68
73 result_type variance() const;
74
78 result_type alpha() const { return dist_.alpha(); }
79
83 result_type beta() const { return dist_.beta(); }
84
88 void reset() { dist_.reset(); }
89
95
96private:
102 mutable boost::random::beta_distribution<RealType> dist_;
103};
104
105template <typename RealType>
106BetaDist<RealType>::BetaDist(RealType alpha, RealType beta) : dist_(alpha, beta)
107{
108}
109
110template <typename RealType> void BetaDist<RealType>::reset(result_type a, result_type b)
111{
112
113 dist_ = boost::random::beta_distribution<RealType>(a, b);
114}
115
116template <typename RealType> RealType BetaDist<RealType>::variance() const
117{
118 auto a = dist_.alpha();
119 auto b = dist_.beta();
120 return a * b / (utils::maths::sqr(a + b) * (a + b + 1));
121}
122
123template <typename RealType> RealType BetaDist<RealType>::sample() const
124{
125
126 std::random_device rd{};
127 std::mt19937 gen{rd()};
128 return dist_(gen);
129}
130
131template <typename RealType> RealType BetaDist<RealType>::sample(uint_t seed) const
132{
133
134 std::mt19937 gen{seed};
135 return dist_(gen);
136}
137
138template <typename RealType>
139std::vector<RealType> BetaDist<RealType>::sample_many(uint_t size) const
140{
141
142 std::vector<RealType> samples(size);
143 std::random_device rd{};
144 std::mt19937 gen{rd()};
145
146 for (uint_t i = 0; i < size; ++i)
147 {
148 samples[i] = dist_(gen);
149 }
150
151 return samples;
152}
153
154template <typename RealType>
155std::vector<RealType> BetaDist<RealType>::sample_many(uint_t size, uint_t seed) const
156{
157
158 std::vector<RealType> samples(size);
159 std::mt19937 gen(seed);
160
161 for (uint_t i = 0; i < size; ++i)
162 {
163 samples[i] = dist_(gen);
164 }
165
166 return samples;
167}
168
169template <typename RealType> RealType BetaDist<RealType>::pdf(RealType x) const
170{
171
172 auto alpha = dist_.alpha();
173 auto beta = dist_.beta();
174 auto beta_func = std::beta(alpha, beta);
175 return std::pow(x, alpha - 1) * std::pow(1.0 - x, beta - 1.0) / beta_func;
176}
177
178}
179} // namespace rlenvscpp
180
181#endif
The beta distribution is a real-valued distribution which produces values in the range [0,...
Definition beta_dist.h:24
result_type alpha() const
Returns the alpha parameter of the distribution.
Definition beta_dist.h:78
result_type beta() const
Returns the beta parameter of the distribution.
Definition beta_dist.h:83
result_type variance() const
The variance of the distribution. see https://en.wikipedia.org/wiki/Beta_distribution.
Definition beta_dist.h:116
RealType result_type
Definition beta_dist.h:31
result_type sample() const
Sample from the distribution.
Definition beta_dist.h:123
void reset()
Reset the underlying distribution.
Definition beta_dist.h:88
result_type pdf(result_type x) const
compute the value of the PDF at the given point
Definition beta_dist.h:169
std::vector< result_type > sample_many(uint_t size) const
sample from the distribution
Definition beta_dist.h:139
BetaDist(result_type alpha=1.0, result_type std=1.0)
Constructor.
Definition beta_dist.h:106
result_type mean() const
The mean value of the distribution see https://en.wikipedia.org/wiki/Beta_distribution.
Definition beta_dist.h:67
T sqr(const T &v)
Definition math_utils.h:61
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43