Honeycomb  0.1
Component-Model Framework
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
honey::ChiSqr_< Real > Class Template Reference

Generate a random variate from a noncentral chi-square $\chi^2$ distribution. More...

#include <ChiSqr.h>

Inheritance diagram for honey::ChiSqr_< Real >:
Inheritance graph
[legend]
Collaboration diagram for honey::ChiSqr_< Real >:
Collaboration graph
[legend]

Public Types

typedef Matrix< matrix::dynamic, matrix::dynamic, RealMatrixN
 
typedef Vec< matrix::dynamic, RealVecN
 
typedef Vec< 2, RealVec2
 

Public Member Functions

 ChiSqr_ (optional< RandomGen & > gen, Real nu, Real lambda=0)
 
 ChiSqr_ (Real nu, Real lambda=0)
 
virtual Real next () const
 Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) More...
 
virtual Real pdf (Real x) const
 Probability Density Function. More...
 
virtual Real cdf (Real x) const
 Cumulative Distribution Function. More...
 
virtual Real cdfInv (Real P) const
 Inverse of the CDF. More...
 
virtual Real mean () const
 Calc mean. More...
 
virtual Real variance () const
 Calc variance. More...
 
Vec2 stdDevCi (Real stdDev, Real alpha) const
 Calculate the 100*(1-alpha)% confidence interval of the standard deviation. More...
 
- Public Member Functions inherited from honey::RandomDist< Real >
 RandomDist (optional< RandomGen & > gen=optnull)
 Construct with a random generator to use for next() More...
 
virtual ~RandomDist ()
 
virtual Real cdfComp (Real x) const
 Complement of the CDF. More...
 
Real stdDev () const
 Calc standard deviation. More...
 
void setGen (RandomGen &gen)
 Set random generator to use for next() More...
 
RandomGengetGen () const
 Get random generator. More...
 

Static Public Member Functions

static Real test (const VecN &observed, const VecN &expected)
 Calculate the p-value for a list of observed and expected frequencies. More...
 
static Real test (const MatrixN &mat)
 Calculate the p-value for a 'contingency table', a matrix of frequencies for different categories. More...
 

Public Attributes

Real nu
 
Real lambda
 

Friends

template<class >
class ChiSqr_
 

Additional Inherited Members

- Protected Types inherited from honey::RandomDist< Real >
typedef Numeral< Real >::Real_ Real_
 
typedef Real_::DoubleType Double_
 
typedef Double_::Real Double
 
typedef Alge_< RealAlge
 
typedef Alge_< DoubleAlge_d
 
typedef Uniform_< DoubleUniform
 
- Protected Member Functions inherited from honey::RandomDist< Real >
Real cdfInvFind (Real P, Real min, Real max, bool discrete=false) const
 Generic binary search algorithm to find Cdf. More...
 

Detailed Description

template<class Real>
class honey::ChiSqr_< Real >

Generate a random variate from a noncentral chi-square $\chi^2$ distribution.

The chi-square distribution can be used to test how likely it is that an observed distribution is due to chance. It can test the goodness of fit of observations to a model by testing a 'null hypothesis' that the observations are tied to some expected values. It can also be used to test if categorical tallies (eg. male or female for each race) within a table are related or independent.

Example


Use Pearson's chi-squared test to determine if a 6-sided die has been tampered with.
Take samples by rolling the die and observing the frequency of the 6 independent categorical variables (ie. number of times 1,2...6 appears face up)
Here the null hypothesis is that all the sides will appear face up the same number of times.

$\displaystyle \chi^2 = \sum_{i=1}^6 \frac{observedFreq_i - expectedFreq_i^2}{expectedFreq_i} $

This result, X^2, asymptotically approaches a chi-square distribution.
Let's say that the result is 12.
Next, use the Cumulative Distribution Function (CDF). The CDF is a curve that increases from 0 to 1, it tells us the probability that a sample will be <= X^2.
The number of degrees of freedom are the number of categorical variables (6) minus one.

$ p = 1 - cdf(\chi^2, 6-1) $

p will be about 0.03, or 3%. This tells us that the probability of the die samples being tied to their expected values (ie. pure chance) is 3%. Less than 5% strongly suggests that the categorical variables are correlated, so the die sides are not independent, they have been tampered with.


Probability density function when $\lambda = 0$, central chi-square distribution, same as $\mathrm{Gamma}(\nu/2, 2)$:

$\displaystyle p(x,\nu,\lambda) = \frac{1}{2^{\nu/2} \Gamma(\nu/2)} x^{\nu/2 - 1} e^{-x/2} $ where $\Gamma$ is the gamma function.

Probability density function when $\lambda \not= 0$, poisson weighted mix of central chi-square distributions:

$\displaystyle p(x,\nu,\lambda) = \sum_{i=0}^\infty \frac{e^{-\lambda/2} (\lambda/2)^i}{i!} \chi^2(x, \nu+2i) $

Parameters
nu$\nu$ Number of degrees of freedom. Range > 0
lambda$\lambda$ Non-centrality parameter. Range >= 0
Return values
xRandom variate. Range [0,inf]

Member Typedef Documentation

template<class Real >
typedef Matrix<matrix::dynamic, matrix::dynamic, Real> honey::ChiSqr_< Real >::MatrixN
template<class Real >
typedef Vec<2,Real> honey::ChiSqr_< Real >::Vec2
template<class Real >
typedef Vec<matrix::dynamic, Real> honey::ChiSqr_< Real >::VecN

Constructor & Destructor Documentation

template<class Real >
honey::ChiSqr_< Real >::ChiSqr_ ( optional< RandomGen & >  gen,
Real  nu,
Real  lambda = 0 
)
inline
template<class Real >
honey::ChiSqr_< Real >::ChiSqr_ ( Real  nu,
Real  lambda = 0 
)
inline

Member Function Documentation

template<class Real >
Real honey::ChiSqr_< Real >::cdf ( Real  x) const
virtual

Cumulative Distribution Function.

The integral of the PDF from -inf to x.

Parameters
xvalue, range depends on distribution.
Return values
Pa probability in range [0,1] that a random variate will be <= x.

Reimplemented from honey::RandomDist< Real >.

template<class Real >
Real honey::ChiSqr_< Real >::cdfInv ( Real  P) const
virtual

Inverse of the CDF.

Parameters
Pcumulative probability
Return values
xA value that has probability P of being >= a random variate X. Also, x satisfies: cdf(x) = P.

Reimplemented from honey::RandomDist< Real >.

template<class Real >
virtual Real honey::ChiSqr_< Real >::mean ( ) const
inlinevirtual

Calc mean.

Reimplemented from honey::RandomDist< Real >.

template<class Real >
Real honey::ChiSqr_< Real >::next ( ) const
virtual

Get next randomly distributed variate. Requires a random generator (see ctor or setGen())

Reimplemented from honey::RandomDist< Real >.

template<class Real >
Real honey::ChiSqr_< Real >::pdf ( Real  x) const
virtual

Probability Density Function.

The PDF integrates to 1 over the entire range of possible values of x.

Parameters
xvalue, range depends on distribution.
Return values
pa relative likelihood in range [0,inf] that a random variate X will equal x.

Reimplemented from honey::RandomDist< Real >.

template<class Real >
Vec2 honey::ChiSqr_< Real >::stdDevCi ( Real  stdDev,
Real  alpha 
) const
inline

Calculate the 100*(1-alpha)% confidence interval of the standard deviation.

template<class Real >
Real honey::ChiSqr_< Real >::test ( const VecN observed,
const VecN expected 
)
static

Calculate the p-value for a list of observed and expected frequencies.

template<class Real >
Real honey::ChiSqr_< Real >::test ( const MatrixN mat)
static

Calculate the p-value for a 'contingency table', a matrix of frequencies for different categories.

A p-value below 5% is significant, meaning that deviations between the categories are probably not due to random chance. All frequencies in the matrix should be at least 5 for reliable results. The degrees of freedom in the matrix is assumed to be (rows-1)*(cols-1), that is, it is not possible to populate any part of the matrix knowing other parts.

template<class Real >
virtual Real honey::ChiSqr_< Real >::variance ( ) const
inlinevirtual

Calc variance.

Reimplemented from honey::RandomDist< Real >.

Friends And Related Function Documentation

template<class Real >
template<class >
friend class ChiSqr_
friend

Member Data Documentation

template<class Real >
Real honey::ChiSqr_< Real >::lambda
template<class Real >
Real honey::ChiSqr_< Real >::nu

The documentation for this class was generated from the following files: