xo-umbrella2/xo-distribution/include/xo/distribution/Normal.hpp
Roland Conybeare ecd019e8bb Add 'xo-distribution/' from commit '036ca5d817'
git-subtree-dir: xo-distribution
git-subtree-mainline: 4e022df686
git-subtree-split: 036ca5d817
2025-05-11 15:52:36 -05:00

54 lines
1.5 KiB
C++

/* @file Normal.hpp */
#pragma once
#include "Distribution.hpp"
#include <cmath>
namespace xo {
namespace distribution {
/* the guassian distribution, with mean 0 and variance 1
*/
class Normal : public Distribution<double> {
public:
Normal() = default;
/* N(0,1): mean 0, sdev 1 */
static rp<Normal> unit() { return new Normal(); }
/* normal probability density:
*
* x^2
* -(1/2) 1/2
* p(x) = e / (2.pi)
*/
static double density(double x) {
static double c_sqrt_2pi = ::sqrt(2 * M_PI);
return ::exp(-0.5 * x * x) / c_sqrt_2pi;
} /*density*/
/* cumulative distribution function for N(0,1):
*
* / x
* |
* | p(x).dx
* |
* / -oo
*
* where p(x) is the normal density function p(x) = e^[-x^2/2]
*/
static double cdf_impl(double x) {
return 0.5 * std::erfc(-M_SQRT1_2 * x);
} /*cdf_impl*/
// ----- inherited from Distribution<double> -----
virtual double cdf(double const & x) const override {
return cdf_impl(x);
} /*cdf*/
}; /*Normal*/
} /*namespace distribution*/
} /*namespace xo*/
/* end Normal.hpp */