#ifndef NORMAL_H #define NORMAL_H #include #include #include "../random/RandomGenerator.h" #include "../../Assertions.h" namespace Distribution { /** normal distribution */ template class Normal { private: const T mu; const T sigma; const T _a; Random::RandomGenerator gen; std::normal_distribution 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(RANDOM_SEED), 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 mean value */ const T getMu() { return this->mu; } /** get the standard deviation */ const T getSigma() { return this->sigma; } /** get the probability for the given value */ static T getProbability(const T mu, const T sigma, const T val) { Assert::isTrue(sigma > 0, "sigma must be >= 0"); 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