Honeycomb  0.1
Component-Model Framework
Dist.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 
4 #include "Honey/Math/Alge/Alge.h"
5 #include "Honey/Math/Alge/Trig.h"
7 
8 namespace honey
9 {
10 
11 template<class Real> class Uniform_;
12 
14 template<class Real>
16 {
17 protected:
18  typedef typename Numeral<Real>::Real_ Real_;
19  typedef typename Real_::DoubleType Double_;
20  typedef typename Double_::Real Double;
21  typedef Alge_<Real> Alge;
24 
25  #define RandomDist_imports() \
26  using typename Super::Real_; \
27  using typename Super::Double_; \
28  using typename Super::Double; \
29  using typename Super::Alge; \
30  using typename Super::Alge_d; \
31  using typename Super::Uniform; \
32  using Super::getGen; \
33 
34 public:
36  RandomDist(optional<RandomGen&> gen = optnull) : _gen(gen.ptr()) {}
37 
38  virtual ~RandomDist() {}
39 
41  virtual Real next() const { return 0; }
42 
44 
50  virtual Real pdf(Real x) const { mt_unused(x); return 0; }
51 
53 
59  virtual Real cdf(Real x) const { mt_unused(x); return 0; }
60 
62 
68  virtual Real cdfComp(Real x) const { return 1 - cdf(x); }
69 
71 
75  virtual Real cdfInv(Real P) const { mt_unused(P); return 0; }
77  virtual Real mean() const { return 0; }
79  virtual Real variance() const { return 0; }
81  Real stdDev() const { return Alge::sqrt(variance()); }
82 
84  void setGen(RandomGen& gen) { _gen = &gen; }
86  RandomGen& getGen() const { assert(_gen, "Must set a random generator"); return *_gen; }
87 
88 protected:
90  Real cdfInvFind(Real P, Real min, Real max, bool discrete = false) const;
91 
92 private:
93  RandomGen* _gen;
94 };
95 
96 extern template class RandomDist<Float>;
97 extern template class RandomDist<Double>;
98 
99 }
Real cdfInvFind(Real P, Real min, Real max, bool discrete=false) const
Generic binary search algorithm to find Cdf.
Definition: Dist.cpp:10
Alge_< Real > Alge
Definition: Dist.h:21
Algebra.
Definition: Alge.h:13
virtual Real variance() const
Calc variance.
Definition: Dist.h:79
Random number generator interface.
Definition: Gen.h:11
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
#define mt_unused(Param)
Remove the unused parameter warning.
Definition: Meta.h:20
Numeral< Real >::Real_ Real_
Definition: Dist.h:18
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: Dist.h:41
static Real sqrt(Real x)
Square Root.
Definition: Alge.h:63
virtual Real cdf(Real x) const
Cumulative Distribution Function.
Definition: Dist.h:59
RandomGen & getGen() const
Get random generator.
Definition: Dist.h:86
Real_::DoubleType Double_
Definition: Dist.h:19
Double_::Real Double
Definition: Dist.h:20
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
virtual Real mean() const
Calc mean.
Definition: Dist.h:77
virtual Real cdfComp(Real x) const
Complement of the CDF.
Definition: Dist.h:68
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
virtual ~RandomDist()
Definition: Dist.h:38
double Real
Definition: Real.h:14
virtual Real pdf(Real x) const
Probability Density Function.
Definition: Dist.h:50
virtual Real cdfInv(Real P) const
Inverse of the CDF.
Definition: Dist.h:75
Alge_< Double > Alge_d
Definition: Dist.h:22
Real stdDev() const
Calc standard deviation.
Definition: Dist.h:81
RandomDist(optional< RandomGen & > gen=optnull)
Construct with a random generator to use for next()
Definition: Dist.h:36
Generate a random variate between min and max non-inclusive with uniform (flat) distribution.
Definition: Dist.h:11
void setGen(RandomGen &gen)
Set random generator to use for next()
Definition: Dist.h:84
Global Honeycomb namespace.
Uniform_< Double > Uniform
Definition: Dist.h:23
Base class for all random distributions.
Definition: Dist.h:15