Honeycomb  0.1
Component-Model Framework
Gaussian.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 
25 template<class Real>
26 class Gaussian_ : public RandomDist<Real>
27 {
28  typedef RandomDist<Real> Super;
29  RandomDist_imports();
30  typedef Trig_<Double> Trig_d;
31 
32 public:
33  Gaussian_(optional<RandomGen&> gen, Real mu = 0, Real sigma = 1) : Super(gen), mu(mu), sigma(sigma) { assert(sigma > 0); }
35 
36  virtual Real next() const;
37  virtual Real pdf(Real x) const;
38  virtual Real cdf(Real x) const;
39  virtual Real cdfInv(Real P) const;
40  virtual Real mean() const { return mu; }
41  virtual Real variance() const { return Alge::sqr(sigma); }
42 
44  static Real erf(Real x) { return 2*Gaussian_().cdf(Double(x)*Double_::sqrtTwo) - 1; }
46  static Real erfComp(Real x) { return 2*Gaussian_().cdf(-Double(x)*Double_::sqrtTwo); }
48  static Real erfInv(Real y) { return Gaussian_().cdfInv(Double(y+1)/2) / Double_::sqrtTwo; }
50  static Real erfCompInv(Real y) { return -Gaussian_().cdfInv(Double(y)/2) / Double_::sqrtTwo; }
51 
54 };
55 
59 
60 extern template class Gaussian_<Float>;
61 extern template class Gaussian_<Double>;
62 
63 }
Gaussian_< Real > Gaussian
Definition: Gaussian.h:56
Gaussian_< Float > Gaussian_f
Definition: Gaussian.h:57
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: Gaussian.cpp:12
virtual Real cdf(Real x) const
Cumulative Distribution Function.
Definition: Gaussian.cpp:40
virtual Real pdf(Real x) const
Probability Density Function.
Definition: Gaussian.cpp:32
Gaussian_(optional< RandomGen & > gen, Real mu=0, Real sigma=1)
Definition: Gaussian.h:33
static const double sqrtTwo
Definition: Double.h:42
static Real erfComp(Real x)
Complement of Error Function.
Definition: Gaussian.h:46
Real sigma
Definition: Gaussian.h:53
virtual Real variance() const
Calc variance.
Definition: Gaussian.h:41
static Real erfCompInv(Real y)
Inverse of complement Error Function.
Definition: Gaussian.h:50
Double_::Real Double
Definition: Dist.h:20
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
virtual Real cdfInv(Real P) const
Inverse of the CDF.
Definition: Gaussian.cpp:259
static Real erf(Real x)
Gauss Error Function, related to Cdf. Input: [-inf, inf] -> Output: [-1, 1].
Definition: Gaussian.h:44
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
Gaussian_< Double > Gaussian_d
Definition: Gaussian.h:58
static Num sqr(Num x)
Square.
Definition: Alge.h:60
Real mu
Definition: Gaussian.h:52
Gaussian_(Real mu=0, Real sigma=1)
Definition: Gaussian.h:34
Generate a normally (Gaussian) distributed random variate.
Definition: Gaussian.h:26
static Real erfInv(Real y)
Inverse of Error Function, related to CdfInv. Input: [-1, 1] -> Output: [-inf, inf].
Definition: Gaussian.h:48
Global Honeycomb namespace.
virtual Real mean() const
Calc mean.
Definition: Gaussian.h:40
Trigonometry.
Definition: Trig.h:52
Base class for all random distributions.
Definition: Dist.h:15