bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
beta_dist.h
Go to the documentation of this file.
1#ifndef BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_BETA_DIST_H
2#define BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_BETA_DIST_H
3
6
7#include <boost/random/beta_distribution.hpp>
8#include <cmath>
9#include <type_traits>
10#include <vector>
11
12namespace bitsim
13{
14namespace maths::stats
15{
16
22template <typename RealType = real_t> class BetaDist
23{
24 public:
25 static_assert(std::is_floating_point<RealType>::value, "Not a floating point type");
26
30 using result_type = RealType;
31
35 explicit BetaDist(result_type alpha = 1.0, result_type std = 1.0);
36
41
45 result_type sample() const;
46
50 result_type sample(uint_t seed) const;
51
55 std::vector<result_type> sample_many(uint_t size) const;
56
60 std::vector<result_type> sample_many(uint_t size, uint_t seed) const;
61
66 result_type mean() const { return dist_.alpha() / (dist_.alpha() + dist_.beta()); }
67
72 result_type variance() const;
73
77 result_type alpha() const { return dist_.alpha(); }
78
82 result_type beta() const { return dist_.beta(); }
83
87 void reset() { dist_.reset(); }
88
93 void reset(result_type a, result_type b);
94
95 private:
101 mutable boost::random::beta_distribution<RealType> dist_;
102};
103
104template <typename RealType>
105BetaDist<RealType>::BetaDist(RealType alpha, RealType beta) : dist_(alpha, beta)
106{
107}
108
109template <typename RealType> void BetaDist<RealType>::reset(result_type a, result_type b)
110{
111
112 dist_ = boost::random::beta_distribution<RealType>(a, b);
113}
114
115template <typename RealType> RealType BetaDist<RealType>::variance() const
116{
117 auto a = dist_.alpha();
118 auto b = dist_.beta();
119 return a * b / (maths::sqr(a + b) * (a + b + 1));
120}
121
122template <typename RealType> RealType BetaDist<RealType>::sample() const
123{
124
125 std::random_device rd{};
126 std::mt19937 gen{rd()};
127 return dist_(gen);
128}
129
130template <typename RealType> RealType BetaDist<RealType>::sample(uint_t seed) const
131{
132
133 std::mt19937 gen{seed};
134 return dist_(gen);
135}
136
137template <typename RealType>
138std::vector<RealType> BetaDist<RealType>::sample_many(uint_t size) const
139{
140
141 std::vector<RealType> samples(size);
142 std::random_device rd{};
143 std::mt19937 gen{rd()};
144
145 for (uint_t i = 0; i < size; ++i)
146 {
147 samples[i] = dist_(gen);
148 }
149
150 return samples;
151}
152
153template <typename RealType>
154std::vector<RealType> BetaDist<RealType>::sample_many(uint_t size, uint_t seed) const
155{
156
157 std::vector<RealType> samples(size);
158 std::mt19937 gen(seed);
159
160 for (uint_t i = 0; i < size; ++i)
161 {
162 samples[i] = dist_(gen);
163 }
164
165 return samples;
166}
167
168template <typename RealType> RealType BetaDist<RealType>::pdf(RealType x) const
169{
170
171 auto alpha = dist_.alpha();
172 auto beta = dist_.beta();
173 auto beta_func = std::beta(alpha, beta);
174 return std::pow(x, alpha - 1) * std::pow(1.0 - x, beta - 1.0) / beta_func;
175}
176
177} // namespace maths::stats
178} // namespace bitsim
179
180#endif // BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_BETA_DIST_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
result_type beta() const
Returns the beta parameter of the distribution.
Definition beta_dist.h:82
BetaDist(result_type alpha=1.0, result_type std=1.0)
Constructor.
Definition beta_dist.h:105
std::vector< result_type > sample_many(uint_t size) const
sample from the distribution
Definition beta_dist.h:138
result_type sample() const
Sample from the distribution.
Definition beta_dist.h:122
RealType result_type
The return type every time we call pdf, sample.
Definition beta_dist.h:30
result_type mean() const
The mean value of the distribution see https://en.wikipedia.org/wiki/Beta_distribution.
Definition beta_dist.h:66
result_type pdf(result_type x) const
compute the value of the PDF at the given point
Definition beta_dist.h:168
result_type variance() const
The variance of the distribution. see https://en.wikipedia.org/wiki/Beta_distribution.
Definition beta_dist.h:115
result_type alpha() const
Returns the alpha parameter of the distribution.
Definition beta_dist.h:77
void reset()
Reset the underlying distribution.
Definition beta_dist.h:87
Definition bernoulli_dist.cpp:6
T sqr(const T &v)
Definition math_utils.h:60
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40