bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
normal_dist.h
Go to the documentation of this file.
1#ifndef BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_NORMAL_DIST_H
2#define BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_NORMAL_DIST_H
3
6
7#include <cmath>
8#include <random>
9#include <type_traits>
10#include <vector>
11
12namespace bitsim
13{
14namespace maths::stats
15{
16
22template <typename RealType = real_t> class NormalDist
23{
24
25 public:
26 static_assert(std::is_floating_point<RealType>::value, "Not a floating point type");
27
31 using result_type = RealType;
32
36 NormalDist();
37
41 explicit NormalDist(result_type mu, result_type std = 1.0);
42
47
51 result_type sample() const;
52
56 result_type sample(uint_t seed) const;
57
61 std::vector<result_type> sample_many(uint_t size) const;
62
66 std::vector<result_type> sample_many(uint_t size, uint_t seed) const;
67
71 result_type mean() const { return dist_.mean(); }
72
76 result_type std() const { return dist_.stddev(); }
77
78 private:
84 mutable std::normal_distribution<RealType> dist_;
85};
86
87template <typename RealType>
88NormalDist<RealType>::NormalDist(RealType mu, RealType std) : dist_(mu, std)
89{
90}
91
92template <typename RealType> NormalDist<RealType>::NormalDist() : NormalDist<RealType>(0.0, 1.0) {}
93
94template <typename RealType> RealType NormalDist<RealType>::sample() const
95{
96
97 std::random_device rd{};
98 std::mt19937 gen{rd()};
99 return dist_(gen);
100}
101
102template <typename RealType> RealType NormalDist<RealType>::sample(uint_t seed) const
103{
104
105 std::mt19937 gen{seed};
106 return dist_(gen);
107}
108
109template <typename RealType>
110std::vector<RealType> NormalDist<RealType>::sample_many(uint_t size) const
111{
112
113 std::vector<RealType> samples(size);
114 std::random_device rd{};
115 std::mt19937 gen{rd()};
116
117 for (uint_t i = 0; i < size; ++i)
118 {
119 samples[i] = dist_(gen);
120 }
121
122 return samples;
123}
124
125template <typename RealType>
126std::vector<RealType> NormalDist<RealType>::sample_many(uint_t size, uint_t seed) const
127{
128
129 std::vector<RealType> samples(size);
130 std::mt19937 gen(seed);
131
132 for (uint_t i = 0; i < size; ++i)
133 {
134 samples[i] = dist_(gen);
135 }
136
137 return samples;
138}
139
140template <typename RealType> RealType NormalDist<RealType>::pdf(RealType x) const
141{
142
143 auto mu = dist_.mean();
144 auto std = dist_.stddev();
146 auto factor = 1.0 / (std * std::sqrt(2.0 * pi));
147 auto exp = std::exp(-0.5 * std::pow((x - mu) / std, 2.0));
148 return factor * exp;
149}
150
151} // namespace maths::stats
152} // namespace bitsim
153
154#endif // BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_NORMAL_DIST_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
result_type std() const
The STD of the distribution.
Definition normal_dist.h:76
result_type pdf(result_type x) const
compute the value of the PDF at the given point
Definition normal_dist.h:140
std::vector< result_type > sample_many(uint_t size) const
sample from the distribution
Definition normal_dist.h:110
result_type mean() const
The mean value of the distribution.
Definition normal_dist.h:71
RealType result_type
The return type every time we call pdf, sample.
Definition normal_dist.h:31
NormalDist()
Constructor.
Definition normal_dist.h:92
result_type sample() const
Sample from the distribution.
Definition normal_dist.h:94
const real_t PI
The PI constant.
Definition bitsim_consts.h:63
Definition bernoulli_dist.cpp:6
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40