started removing KLib related code:
- assertions - distributions new helper methods worked on stairs worked on grid-walkers worked on navigation
This commit is contained in:
57
math/distribution/Normal.h
Normal file
57
math/distribution/Normal.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef NORMAL_H
|
||||
#define NORMAL_H
|
||||
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
|
||||
namespace Distribution {
|
||||
|
||||
/** normal distribution */
|
||||
template <typename T> class Normal {
|
||||
|
||||
private:
|
||||
|
||||
const T mu;
|
||||
const T sigma;
|
||||
const T _a;
|
||||
|
||||
std::minstd_rand gen;
|
||||
std::normal_distribution<T> dist;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
Normal(const T mu, const T sigma) :
|
||||
mu(mu), sigma(sigma), _a(1.0 / (sigma * std::sqrt(2.0 * M_PI))), gen(1234), dist(mu,sigma) {
|
||||
|
||||
}
|
||||
|
||||
/** get probability for the given value */
|
||||
T getProbability(const T val) const {
|
||||
const T b = -0.5 * ((val-mu)/sigma) * ((val-mu)/sigma);
|
||||
return _a * std::exp(b);
|
||||
}
|
||||
|
||||
/** get a normally distributed random number */
|
||||
T draw() {
|
||||
return dist(gen);
|
||||
}
|
||||
|
||||
/** set the seed to use */
|
||||
void setSeed(const long seed) {
|
||||
gen.seed(seed);
|
||||
}
|
||||
|
||||
|
||||
/** get the probability for the given value */
|
||||
static T getProbability(const T mu, const T sigma, const T val) {
|
||||
const T a = 1.0 / (sigma * std::sqrt(2.0 * M_PI));
|
||||
const T b = -0.5 * ((val-mu)/sigma) * ((val-mu)/sigma);
|
||||
return a * std::exp(b);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // NORMAL_H
|
||||
Reference in New Issue
Block a user