Honeycomb  0.1
Component-Model Framework
ChiSqr.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 
51 template<class Real>
52 class ChiSqr_ : public RandomDist<Real>
53 {
54  typedef RandomDist<Real> Super;
55  RandomDist_imports();
56  template<class> friend class ChiSqr_;
58  typedef Gamma_<Double> Gamma;
60 
61 public:
64  typedef Vec<2,Real> Vec2;
65 
66  ChiSqr_(optional<RandomGen&> gen, Real nu, Real lambda = 0) : RandomDist<Real>(gen), nu(nu), lambda(lambda) { assert(nu > 0 && lambda >= 0); }
68 
69  virtual Real next() const;
70  virtual Real pdf(Real x) const;
71  virtual Real cdf(Real x) const;
72  virtual Real cdfInv(Real P) const;
73  virtual Real mean() const { return nu + lambda; }
74  virtual Real variance() const { return 2*nu + 4*lambda; }
75 
77  Vec2 stdDevCi(Real stdDev, Real alpha) const
78  {
79  return Vec2(stdDev * Alge::sqrt(nu / cdfInv(1-alpha/2)),
80  stdDev * Alge::sqrt(nu / cdfInv(alpha/2)));
81  }
82 
84  /*
85  * A p-value below 5% is significant, meaning the observations are probably not due to random chance.
86  * The degrees of freedom is assumed to be the number of observations - 1, that is,
87  * none of the observations can be predicted by knowing other observations in the set.
88  */
89  static Real test(const VecN& observed, const VecN& expected);
90 
92 
98  static Real test(const MatrixN& mat);
99 
102 };
103 
107 
108 extern template class ChiSqr_<Float>;
109 extern template class ChiSqr_<Double>;
110 
111 }
ChiSqr_(Real nu, Real lambda=0)
Definition: ChiSqr.h:67
Real nu
Definition: ChiSqr.h:100
Gamma_< Real > Gamma
Definition: Gamma.h:54
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
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: ChiSqr.cpp:11
Generate a random variate from a noncentral chi-square distribution.
Definition: ChiSqr.h:52
Generate a random variate from a gamma distribution .
Definition: Gamma.h:29
ChiSqr_< Real > ChiSqr
Definition: ChiSqr.h:104
virtual Real mean() const
Calc mean.
Definition: ChiSqr.h:73
ChiSqr_< Double > ChiSqr_d
Definition: ChiSqr.h:106
Class to evaluate Gamma and related functions.
Definition: Gamma.h:9
GammaFunc_< Real > GammaFunc
Definition: Gamma.h:100
static Real sqrt(Real x)
Square Root.
Definition: Alge.h:63
ChiSqr_< Float > ChiSqr_f
Definition: ChiSqr.h:105
virtual Real cdfInv(Real P) const
Inverse of the CDF.
Definition: ChiSqr.cpp:99
virtual Real variance() const
Calc variance.
Definition: ChiSqr.h:74
static Real test(const VecN &observed, const VecN &expected)
Calculate the p-value for a list of observed and expected frequencies.
Definition: ChiSqr.cpp:114
Matrix< matrix::dynamic, matrix::dynamic, Real > MatrixN
Definition: ChiSqr.h:62
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Vec2 stdDevCi(Real stdDev, Real alpha) const
Calculate the 100*(1-alpha)% confidence interval of the standard deviation.
Definition: ChiSqr.h:77
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
virtual Real cdf(Real x) const
Cumulative Distribution Function.
Definition: ChiSqr.cpp:67
Real lambda
Definition: ChiSqr.h:101
Vec< 2, Real > Vec2
Definition: ChiSqr.h:64
Vec< matrix::dynamic, Real > VecN
Definition: ChiSqr.h:63
Real stdDev() const
Calc standard deviation.
Definition: Dist.h:81
Generate a normally (Gaussian) distributed random variate.
Definition: Gaussian.h:26
Global Honeycomb namespace.
virtual Real pdf(Real x) const
Probability Density Function.
Definition: ChiSqr.cpp:20
ChiSqr_(optional< RandomGen & > gen, Real nu, Real lambda=0)
Definition: ChiSqr.h:66
Base class for all random distributions.
Definition: Dist.h:15