3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Chi Squared Distribution</title>
5 <link rel="stylesheet" href="../../../../../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="../../../../index.html" title="Math Toolkit">
8 <link rel="up" href="../dists.html" title="Distributions">
9 <link rel="prev" href="cauchy_dist.html" title="Cauchy-Lorentz Distribution">
10 <link rel="next" href="exp_dist.html" title="Exponential Distribution">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../../boost.png"></td>
15 <td align="center"><a href="../../../../../../../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../../../../../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../../../../../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="cauchy_dist.html"><img src="../../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.html"><img src="../../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exp_dist.html"><img src="../../../../../../../../../doc/src/images/next.png" alt="Next"></a>
25 <div class="section math_toolkit_dist_dist_ref_dists_chi_squared_dist">
26 <div class="titlepage"><div><div><h5 class="title">
27 <a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist"></a><a class="link" href="chi_squared_dist.html" title="Chi Squared Distribution">Chi
28 Squared Distribution</a>
29 </h5></div></div></div>
32 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">/</span><span class="identifier">chi_squared</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></pre>
35 <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
37 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">RealType</span> <span class="special">=</span> <span class="keyword">double</span><span class="special">,</span>
38 <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Policies">Policy</a> <span class="special">=</span> <a class="link" href="../../../policy/pol_ref/pol_ref_ref.html" title="Policy Class Reference">policies::policy<></a> <span class="special">></span>
39 <span class="keyword">class</span> <span class="identifier">chi_squared_distribution</span><span class="special">;</span>
41 <span class="keyword">typedef</span> <span class="identifier">chi_squared_distribution</span><span class="special"><></span> <span class="identifier">chi_squared</span><span class="special">;</span>
43 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Policies">Policy</a><span class="special">></span>
44 <span class="keyword">class</span> <span class="identifier">chi_squared_distribution</span>
45 <span class="special">{</span>
46 <span class="keyword">public</span><span class="special">:</span>
47 <span class="keyword">typedef</span> <span class="identifier">RealType</span> <span class="identifier">value_type</span><span class="special">;</span>
48 <span class="keyword">typedef</span> <span class="identifier">Policy</span> <span class="identifier">policy_type</span><span class="special">;</span>
50 <span class="comment">// Constructor:</span>
51 <span class="identifier">chi_squared_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">i</span><span class="special">);</span>
53 <span class="comment">// Accessor to parameter:</span>
54 <span class="identifier">RealType</span> <span class="identifier">degrees_of_freedom</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
56 <span class="comment">// Parameter estimation:</span>
57 <span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_degrees_of_freedom</span><span class="special">(</span>
58 <span class="identifier">RealType</span> <span class="identifier">difference_from_mean</span><span class="special">,</span>
59 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">,</span>
60 <span class="identifier">RealType</span> <span class="identifier">beta</span><span class="special">,</span>
61 <span class="identifier">RealType</span> <span class="identifier">sd</span><span class="special">,</span>
62 <span class="identifier">RealType</span> <span class="identifier">hint</span> <span class="special">=</span> <span class="number">100</span><span class="special">);</span>
63 <span class="special">};</span>
65 <span class="special">}}</span> <span class="comment">// namespaces</span>
68 The Chi-Squared distribution is one of the most widely used distributions
69 in statistical tests. If χ<sub>i</sub>   are ν  
70 independent, normally distributed random
71 variables with means μ<sub>i</sub>   and variances σ<sub>i</sub><sup>2</sup>, then the random variable:
74 <span class="inlinemediaobject"><img src="../../../../../equations/chi_squ_ref1.png"></span>
77 is distributed according to the Chi-Squared distribution.
80 The Chi-Squared distribution is a special case of the gamma distribution
81 and has a single parameter ν   that specifies the number of degrees of freedom.
82 The following graph illustrates how the distribution changes for different
86 <span class="inlinemediaobject"><img src="../../../../../graphs/chi_squared_pdf.png" align="middle"></span>
89 <a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.h0"></a>
90 <span><a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.member_functions"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist.dist_ref.dists.chi_squared_dist.member_functions">Member
93 <pre class="programlisting"><span class="identifier">chi_squared_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">v</span><span class="special">);</span>
96 Constructs a Chi-Squared distribution with <span class="emphasis"><em>v</em></span> degrees
100 Requires v > 0, otherwise calls <a class="link" href="../../../main_overview/error_handling.html#domain_error">domain_error</a>.
102 <pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">degrees_of_freedom</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
105 Returns the parameter <span class="emphasis"><em>v</em></span> from which this object was
108 <pre class="programlisting"><span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_degrees_of_freedom</span><span class="special">(</span>
109 <span class="identifier">RealType</span> <span class="identifier">difference_from_variance</span><span class="special">,</span>
110 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">,</span>
111 <span class="identifier">RealType</span> <span class="identifier">beta</span><span class="special">,</span>
112 <span class="identifier">RealType</span> <span class="identifier">variance</span><span class="special">,</span>
113 <span class="identifier">RealType</span> <span class="identifier">hint</span> <span class="special">=</span> <span class="number">100</span><span class="special">);</span>
116 Estimates the sample size required to detect a difference from a nominal
117 variance in a Chi-Squared test for equal standard deviations.
119 <div class="variablelist">
120 <p class="title"><b></b></p>
122 <dt><span class="term">difference_from_variance</span></dt>
124 The difference from the assumed nominal variance that is to be
125 detected: Note that the sign of this value is critical, see below.
127 <dt><span class="term">alpha</span></dt>
129 The maximum acceptable risk of rejecting the null hypothesis when
132 <dt><span class="term">beta</span></dt>
134 The maximum acceptable risk of falsely failing to reject the null
137 <dt><span class="term">variance</span></dt>
139 The nominal variance being tested against.
141 <dt><span class="term">hint</span></dt>
143 An optional hint on where to start looking for a result: the current
144 sample size would be a good choice.
149 Note that this calculation works with <span class="emphasis"><em>variances</em></span>
150 and not <span class="emphasis"><em>standard deviations</em></span>.
153 The sign of the parameter <span class="emphasis"><em>difference_from_variance</em></span>
154 is important: the Chi Squared distribution is asymmetric, and the caller
155 must decide in advance whether they are testing for a variance greater
156 than a nominal value (positive <span class="emphasis"><em>difference_from_variance</em></span>)
157 or testing for a variance less than a nominal value (negative <span class="emphasis"><em>difference_from_variance</em></span>).
158 If the latter, then obviously it is a requirement that <code class="computeroutput"><span class="identifier">variance</span> <span class="special">+</span>
159 <span class="identifier">difference_from_variance</span> <span class="special">></span>
160 <span class="number">0</span></code>, since no sample can have a negative
164 This procedure uses the method in Diamond, W. J. (1989). Practical Experiment
165 Designs, Van-Nostrand Reinhold, New York.
168 See also section on Sample sizes required in <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc232.htm" target="_top">the
169 NIST Engineering Statistics Handbook, Section 7.2.3.2</a>.
172 <a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.h1"></a>
173 <span><a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.non_member_accessors"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist.dist_ref.dists.chi_squared_dist.non_member_accessors">Non-member
177 All the <a class="link" href="../nmp.html" title="Non-Member Properties">usual non-member
178 accessor functions</a> that are generic to all distributions are supported:
179 <a class="link" href="../nmp.html#math.dist.cdf">Cumulative Distribution Function</a>,
180 <a class="link" href="../nmp.html#math.dist.pdf">Probability Density Function</a>, <a class="link" href="../nmp.html#math.dist.quantile">Quantile</a>, <a class="link" href="../nmp.html#math.dist.hazard">Hazard
181 Function</a>, <a class="link" href="../nmp.html#math.dist.chf">Cumulative Hazard Function</a>,
182 <a class="link" href="../nmp.html#math.dist.mean">mean</a>, <a class="link" href="../nmp.html#math.dist.median">median</a>,
183 <a class="link" href="../nmp.html#math.dist.mode">mode</a>, <a class="link" href="../nmp.html#math.dist.variance">variance</a>,
184 <a class="link" href="../nmp.html#math.dist.sd">standard deviation</a>, <a class="link" href="../nmp.html#math.dist.skewness">skewness</a>,
185 <a class="link" href="../nmp.html#math.dist.kurtosis">kurtosis</a>, <a class="link" href="../nmp.html#math.dist.kurtosis_excess">kurtosis_excess</a>,
186 <a class="link" href="../nmp.html#math.dist.range">range</a> and <a class="link" href="../nmp.html#math.dist.support">support</a>.
189 (We have followed the usual restriction of the mode to degrees of freedom
190 >= 2, but note that the maximum of the pdf is actually zero for degrees
191 of freedom from 2 down to 0, and provide an extended definition that
192 would avoid a discontinuity in the mode as alternative code in a comment).
195 The domain of the random variable is [0, +∞].
198 <a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.h2"></a>
199 <span><a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.examples"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist.dist_ref.dists.chi_squared_dist.examples">Examples</a>
202 Various <a class="link" href="../../stat_tut/weg/cs_eg.html" title="Chi Squared Distribution Examples">worked examples</a>
203 are available illustrating the use of the Chi Squared Distribution.
206 <a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.h3"></a>
207 <span><a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.accuracy"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist.dist_ref.dists.chi_squared_dist.accuracy">Accuracy</a>
210 The Chi-Squared distribution is implemented in terms of the <a class="link" href="../../../special/sf_gamma/igamma.html" title="Incomplete Gamma Functions">incomplete
211 gamma functions</a>: please refer to the accuracy data for those functions.
214 <a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.h4"></a>
215 <span><a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.implementation"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist.dist_ref.dists.chi_squared_dist.implementation">Implementation</a>
218 In the following table <span class="emphasis"><em>v</em></span> is the number of degrees
219 of freedom of the distribution, <span class="emphasis"><em>x</em></span> is the random
220 variate, <span class="emphasis"><em>p</em></span> is the probability, and <span class="emphasis"><em>q =
223 <div class="informaltable"><table class="table">
249 Using the relation: pdf = <a class="link" href="../../../special/sf_gamma/gamma_derivatives.html" title="Derivative of the Incomplete Gamma Function">gamma_p_derivative</a>(v
262 Using the relation: p = <a class="link" href="../../../special/sf_gamma/igamma.html" title="Incomplete Gamma Functions">gamma_p</a>(v
275 Using the relation: q = <a class="link" href="../../../special/sf_gamma/igamma.html" title="Incomplete Gamma Functions">gamma_q</a>(v
288 Using the relation: x = 2 * <a class="link" href="../../../special/sf_gamma/igamma_inv.html" title="Incomplete Gamma Function Inverses">gamma_p_inv</a>(v
296 quantile from the complement
301 Using the relation: x = 2 * <a class="link" href="../../../special/sf_gamma/igamma_inv.html" title="Incomplete Gamma Function Inverses">gamma_q_inv</a>(v
350 2 * sqrt(2 / v) == sqrt(8 / v)
381 <a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.h5"></a>
382 <span><a name="math_toolkit.dist.dist_ref.dists.chi_squared_dist.references"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist.dist_ref.dists.chi_squared_dist.references">References</a>
384 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
385 <li class="listitem">
386 <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm" target="_top">NIST
387 Exploratory Data Analysis</a>
389 <li class="listitem">
390 <a href="http://en.wikipedia.org/wiki/Chi-square_distribution" target="_top">Chi-square
393 <li class="listitem">
394 <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html" target="_top">Weisstein,
395 Eric W. "Chi-Squared Distribution." From MathWorld--A Wolfram
400 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
401 <td align="left"></td>
402 <td align="right"><div class="copyright-footer">Copyright © 2006-2010 John Maddock, Paul A. Bristow, Hubert Holin, Xiaogang Zhang, Bruno
403 Lalande, Johan Råde, Gautam Sewani, Thijs van den Berg and Benjamin Sobotta<p>
404 Distributed under the Boost Software License, Version 1.0. (See accompanying
405 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
410 <div class="spirit-nav">
411 <a accesskey="p" href="cauchy_dist.html"><img src="../../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.html"><img src="../../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exp_dist.html"><img src="../../../../../../../../../doc/src/images/next.png" alt="Next"></a>