Honeycomb  0.1
Component-Model Framework
Beta.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 
9 template<class Real> class BetaInc;
10 
12 
29 template<class Real>
30 class Beta_ : public RandomDist<Real>
31 {
32  typedef RandomDist<Real> Super;
33  RandomDist_imports();
35  typedef Gamma_<Double> Gamma;
37  typedef BetaInc<Double> BetaInc;
38 
39 public:
40  Beta_(optional<RandomGen&> gen, Real a, Real b) : RandomDist<Real>(gen), a(a), b(b) { assert(a > 0 && b > 0); }
41  Beta_(Real a, Real b) : Beta_(optnull, a, b) {}
42 
43  virtual Real next() const;
44  virtual Real pdf(Real x) const;
45  virtual Real cdf(Real x) const;
46  virtual Real cdfInv(Real P) const;
47  virtual Real mean() const { return a/(a+b); }
48  virtual Real variance() const { return (a*b) / (Alge::sqr(a+b)*(a+b+1)); }
49 
52 
55 };
56 
57 typedef Beta_<Real> Beta;
60 
61 extern template class Beta_<Float>;
62 extern template class Beta_<Double>;
63 
64 
66 template<class Real>
67 class BetaInc
68 {
69  typedef typename Numeral<Real>::Real_ Real_;
70  typedef Alge_<Real> Alge;
72  typedef Gaussian_<Real> Gaussian;
73 public:
74  static Real calc(Real x, Real a, Real b);
75  static Real calcInv(Real y0, Real a, Real b);
76 private:
77  static void values(int& n_data, Real& a, Real& b, Real& x, Real& fx);
78  static Real betaIn(Real x, Real p, Real q, Real beta, int& fault);
79 };
80 
81 extern template class BetaInc<Float>;
82 extern template class BetaInc<Double>;
83 
84 }
Beta_< Real > Beta
Definition: Beta.h:57
Generate a random variate from a beta distribution .
Definition: Beta.h:30
Real a
Definition: Beta.h:53
Algebra.
Definition: Alge.h:13
Evaluate the incomplete beta function.
Definition: Beta.h:9
Gaussian_< Real > Gaussian
Definition: Gaussian.h:56
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
Generate a random variate from a gamma distribution .
Definition: Gamma.h:29
Class to evaluate Gamma and related functions.
Definition: Gamma.h:9
static Real exp(Real x)
Euler's number e raised to exponent x (e^x)
Definition: Alge.h:68
virtual Real variance() const
Calc variance.
Definition: Beta.h:48
GammaFunc_< Real > GammaFunc
Definition: Gamma.h:100
Alge_< Real > Alge
Definition: Alge.h:130
Beta_< Double > Beta_d
Definition: Beta.h:59
static Real gammaLn(Real z)
ln(gamma) Useful in arithmetic because results of gamma can be large
Definition: Gamma.cpp:219
Beta_< Float > Beta_f
Definition: Beta.h:58
Double func() const
Evaluate the beta function.
Definition: Beta.h:51
virtual Real mean() const
Calc mean.
Definition: Beta.h:47
Double_::Real Double
Definition: Dist.h:20
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
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
auto values(Range &&range) -> Range_< TupleIter< mt_iterOf(range), 1 >, TupleIter< mt_iterOf(range), 1 >>
Create a range over the values of a map or map iterator range.
Definition: StdUtil.h:30
virtual Real cdfInv(Real P) const
Inverse of the CDF.
Definition: Beta.cpp:43
Numeral< Real >::Real_ Real_
Operations and constants for Real type. See Float_, Double_.
Definition: Real.h:25
Beta_(optional< RandomGen & > gen, Real a, Real b)
Definition: Beta.h:40
static Num sqr(Num x)
Square.
Definition: Alge.h:60
virtual Real cdf(Real x) const
Cumulative Distribution Function.
Definition: Beta.cpp:37
virtual Real pdf(Real x) const
Probability Density Function.
Definition: Beta.cpp:18
Generate a normally (Gaussian) distributed random variate.
Definition: Gaussian.h:26
Real b
Definition: Beta.h:54
Global Honeycomb namespace.
Beta_(Real a, Real b)
Definition: Beta.h:41
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: Beta.cpp:10
Base class for all random distributions.
Definition: Dist.h:15