]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Chi Squared Distribution</title> | |
5 | <link rel="stylesheet" href="../../../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../../index.html" title="Math Toolkit 2.5.1"> | |
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"> | |
11 | </head> | |
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> | |
20 | </tr></table> | |
21 | <hr> | |
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> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h4 class="title"> | |
27 | <a name="math_toolkit.dist_ref.dists.chi_squared_dist"></a><a class="link" href="chi_squared_dist.html" title="Chi Squared Distribution">Chi Squared | |
28 | Distribution</a> | |
29 | </h4></div></div></div> | |
30 | <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> | |
31 | <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> | |
32 | ||
33 | <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> | |
34 | <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter 15. Policies: Controlling Precision, Error Handling etc">Policy</a> <span class="special">=</span> <a class="link" href="../../pol_ref/pol_ref_ref.html" title="Policy Class Reference">policies::policy<></a> <span class="special">></span> | |
35 | <span class="keyword">class</span> <span class="identifier">chi_squared_distribution</span><span class="special">;</span> | |
36 | ||
37 | <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> | |
38 | ||
39 | <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="Chapter 15. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span> | |
40 | <span class="keyword">class</span> <span class="identifier">chi_squared_distribution</span> | |
41 | <span class="special">{</span> | |
42 | <span class="keyword">public</span><span class="special">:</span> | |
43 | <span class="keyword">typedef</span> <span class="identifier">RealType</span> <span class="identifier">value_type</span><span class="special">;</span> | |
44 | <span class="keyword">typedef</span> <span class="identifier">Policy</span> <span class="identifier">policy_type</span><span class="special">;</span> | |
45 | ||
46 | <span class="comment">// Constructor:</span> | |
47 | <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> | |
48 | ||
49 | <span class="comment">// Accessor to parameter:</span> | |
50 | <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> | |
51 | ||
52 | <span class="comment">// Parameter estimation:</span> | |
53 | <span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_degrees_of_freedom</span><span class="special">(</span> | |
54 | <span class="identifier">RealType</span> <span class="identifier">difference_from_mean</span><span class="special">,</span> | |
55 | <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">,</span> | |
56 | <span class="identifier">RealType</span> <span class="identifier">beta</span><span class="special">,</span> | |
57 | <span class="identifier">RealType</span> <span class="identifier">sd</span><span class="special">,</span> | |
58 | <span class="identifier">RealType</span> <span class="identifier">hint</span> <span class="special">=</span> <span class="number">100</span><span class="special">);</span> | |
59 | <span class="special">};</span> | |
60 | ||
61 | <span class="special">}}</span> <span class="comment">// namespaces</span> | |
62 | </pre> | |
63 | <p> | |
64 | The Chi-Squared distribution is one of the most widely used distributions | |
65 | in statistical tests. If χ<sub>i</sub>   are ν   | |
66 | independent, normally distributed random | |
67 | variables with means μ<sub>i</sub>   and variances σ<sub>i</sub><sup>2</sup>, then the random variable: | |
68 | </p> | |
69 | <p> | |
70 | <span class="inlinemediaobject"><img src="../../../../equations/chi_squ_ref1.svg"></span> | |
71 | </p> | |
72 | <p> | |
73 | is distributed according to the Chi-Squared distribution. | |
74 | </p> | |
75 | <p> | |
76 | The Chi-Squared distribution is a special case of the gamma distribution | |
77 | and has a single parameter ν   that specifies the number of degrees of freedom. | |
78 | The following graph illustrates how the distribution changes for different | |
79 | values of ν: | |
80 | </p> | |
81 | <p> | |
82 | <span class="inlinemediaobject"><img src="../../../../graphs/chi_squared_pdf.svg" align="middle"></span> | |
83 | </p> | |
84 | <h5> | |
85 | <a name="math_toolkit.dist_ref.dists.chi_squared_dist.h0"></a> | |
86 | <span class="phrase"><a name="math_toolkit.dist_ref.dists.chi_squared_dist.member_functions"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist_ref.dists.chi_squared_dist.member_functions">Member | |
87 | Functions</a> | |
88 | </h5> | |
89 | <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> | |
90 | </pre> | |
91 | <p> | |
92 | Constructs a Chi-Squared distribution with <span class="emphasis"><em>v</em></span> degrees | |
93 | of freedom. | |
94 | </p> | |
95 | <p> | |
96 | Requires v > 0, otherwise calls <a class="link" href="../../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>. | |
97 | </p> | |
98 | <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> | |
99 | </pre> | |
100 | <p> | |
101 | Returns the parameter <span class="emphasis"><em>v</em></span> from which this object was | |
102 | constructed. | |
103 | </p> | |
104 | <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> | |
105 | <span class="identifier">RealType</span> <span class="identifier">difference_from_variance</span><span class="special">,</span> | |
106 | <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">,</span> | |
107 | <span class="identifier">RealType</span> <span class="identifier">beta</span><span class="special">,</span> | |
108 | <span class="identifier">RealType</span> <span class="identifier">variance</span><span class="special">,</span> | |
109 | <span class="identifier">RealType</span> <span class="identifier">hint</span> <span class="special">=</span> <span class="number">100</span><span class="special">);</span> | |
110 | </pre> | |
111 | <p> | |
112 | Estimates the sample size required to detect a difference from a nominal | |
113 | variance in a Chi-Squared test for equal standard deviations. | |
114 | </p> | |
115 | <div class="variablelist"> | |
116 | <p class="title"><b></b></p> | |
117 | <dl class="variablelist"> | |
118 | <dt><span class="term">difference_from_variance</span></dt> | |
119 | <dd><p> | |
120 | The difference from the assumed nominal variance that is to be detected: | |
121 | Note that the sign of this value is critical, see below. | |
122 | </p></dd> | |
123 | <dt><span class="term">alpha</span></dt> | |
124 | <dd><p> | |
125 | The maximum acceptable risk of rejecting the null hypothesis when | |
126 | it is in fact true. | |
127 | </p></dd> | |
128 | <dt><span class="term">beta</span></dt> | |
129 | <dd><p> | |
130 | The maximum acceptable risk of falsely failing to reject the null | |
131 | hypothesis. | |
132 | </p></dd> | |
133 | <dt><span class="term">variance</span></dt> | |
134 | <dd><p> | |
135 | The nominal variance being tested against. | |
136 | </p></dd> | |
137 | <dt><span class="term">hint</span></dt> | |
138 | <dd><p> | |
139 | An optional hint on where to start looking for a result: the current | |
140 | sample size would be a good choice. | |
141 | </p></dd> | |
142 | </dl> | |
143 | </div> | |
144 | <p> | |
145 | Note that this calculation works with <span class="emphasis"><em>variances</em></span> and | |
146 | not <span class="emphasis"><em>standard deviations</em></span>. | |
147 | </p> | |
148 | <p> | |
149 | The sign of the parameter <span class="emphasis"><em>difference_from_variance</em></span> | |
150 | is important: the Chi Squared distribution is asymmetric, and the caller | |
151 | must decide in advance whether they are testing for a variance greater | |
152 | than a nominal value (positive <span class="emphasis"><em>difference_from_variance</em></span>) | |
153 | or testing for a variance less than a nominal value (negative <span class="emphasis"><em>difference_from_variance</em></span>). | |
154 | If the latter, then obviously it is a requirement that <code class="computeroutput"><span class="identifier">variance</span> | |
155 | <span class="special">+</span> <span class="identifier">difference_from_variance</span> | |
156 | <span class="special">></span> <span class="number">0</span></code>, | |
157 | since no sample can have a negative variance! | |
158 | </p> | |
159 | <p> | |
160 | This procedure uses the method in Diamond, W. J. (1989). Practical Experiment | |
161 | Designs, Van-Nostrand Reinhold, New York. | |
162 | </p> | |
163 | <p> | |
164 | See also section on Sample sizes required in <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc232.htm" target="_top">the | |
165 | NIST Engineering Statistics Handbook, Section 7.2.3.2</a>. | |
166 | </p> | |
167 | <h5> | |
168 | <a name="math_toolkit.dist_ref.dists.chi_squared_dist.h1"></a> | |
169 | <span class="phrase"><a name="math_toolkit.dist_ref.dists.chi_squared_dist.non_member_accessors"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist_ref.dists.chi_squared_dist.non_member_accessors">Non-member | |
170 | Accessors</a> | |
171 | </h5> | |
172 | <p> | |
173 | All the <a class="link" href="../nmp.html" title="Non-Member Properties">usual non-member accessor | |
174 | functions</a> that are generic to all distributions are supported: | |
175 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.cdf">Cumulative Distribution Function</a>, | |
176 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability Density Function</a>, | |
177 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.quantile">Quantile</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.hazard">Hazard Function</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.chf">Cumulative Hazard Function</a>, | |
178 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mean">mean</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.median">median</a>, | |
179 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mode">mode</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.variance">variance</a>, | |
180 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.sd">standard deviation</a>, | |
181 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.skewness">skewness</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis">kurtosis</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis_excess">kurtosis_excess</a>, | |
182 | <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.range">range</a> and <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.support">support</a>. | |
183 | </p> | |
184 | <p> | |
185 | (We have followed the usual restriction of the mode to degrees of freedom | |
186 | >= 2, but note that the maximum of the pdf is actually zero for degrees | |
187 | of freedom from 2 down to 0, and provide an extended definition that would | |
188 | avoid a discontinuity in the mode as alternative code in a comment). | |
189 | </p> | |
190 | <p> | |
191 | The domain of the random variable is [0, +∞]. | |
192 | </p> | |
193 | <h5> | |
194 | <a name="math_toolkit.dist_ref.dists.chi_squared_dist.h2"></a> | |
195 | <span class="phrase"><a name="math_toolkit.dist_ref.dists.chi_squared_dist.examples"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist_ref.dists.chi_squared_dist.examples">Examples</a> | |
196 | </h5> | |
197 | <p> | |
198 | Various <a class="link" href="../../stat_tut/weg/cs_eg.html" title="Chi Squared Distribution Examples">worked examples</a> | |
199 | are available illustrating the use of the Chi Squared Distribution. | |
200 | </p> | |
201 | <h5> | |
202 | <a name="math_toolkit.dist_ref.dists.chi_squared_dist.h3"></a> | |
203 | <span class="phrase"><a name="math_toolkit.dist_ref.dists.chi_squared_dist.accuracy"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist_ref.dists.chi_squared_dist.accuracy">Accuracy</a> | |
204 | </h5> | |
205 | <p> | |
206 | The Chi-Squared distribution is implemented in terms of the <a class="link" href="../../sf_gamma/igamma.html" title="Incomplete Gamma Functions">incomplete | |
207 | gamma functions</a>: please refer to the accuracy data for those functions. | |
208 | </p> | |
209 | <h5> | |
210 | <a name="math_toolkit.dist_ref.dists.chi_squared_dist.h4"></a> | |
211 | <span class="phrase"><a name="math_toolkit.dist_ref.dists.chi_squared_dist.implementation"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist_ref.dists.chi_squared_dist.implementation">Implementation</a> | |
212 | </h5> | |
213 | <p> | |
214 | In the following table <span class="emphasis"><em>v</em></span> is the number of degrees | |
215 | of freedom of the distribution, <span class="emphasis"><em>x</em></span> is the random variate, | |
216 | <span class="emphasis"><em>p</em></span> is the probability, and <span class="emphasis"><em>q = 1-p</em></span>. | |
217 | </p> | |
218 | <div class="informaltable"><table class="table"> | |
219 | <colgroup> | |
220 | <col> | |
221 | <col> | |
222 | </colgroup> | |
223 | <thead><tr> | |
224 | <th> | |
225 | <p> | |
226 | Function | |
227 | </p> | |
228 | </th> | |
229 | <th> | |
230 | <p> | |
231 | Implementation Notes | |
232 | </p> | |
233 | </th> | |
234 | </tr></thead> | |
235 | <tbody> | |
236 | <tr> | |
237 | <td> | |
238 | <p> | |
239 | ||
240 | </p> | |
241 | </td> | |
242 | <td> | |
243 | <p> | |
244 | Using the relation: pdf = <a class="link" href="../../sf_gamma/gamma_derivatives.html" title="Derivative of the Incomplete Gamma Function">gamma_p_derivative</a>(v | |
245 | / 2, x / 2) / 2 | |
246 | </p> | |
247 | </td> | |
248 | </tr> | |
249 | <tr> | |
250 | <td> | |
251 | <p> | |
252 | cdf | |
253 | </p> | |
254 | </td> | |
255 | <td> | |
256 | <p> | |
257 | Using the relation: p = <a class="link" href="../../sf_gamma/igamma.html" title="Incomplete Gamma Functions">gamma_p</a>(v | |
258 | / 2, x / 2) | |
259 | </p> | |
260 | </td> | |
261 | </tr> | |
262 | <tr> | |
263 | <td> | |
264 | <p> | |
265 | cdf complement | |
266 | </p> | |
267 | </td> | |
268 | <td> | |
269 | <p> | |
270 | Using the relation: q = <a class="link" href="../../sf_gamma/igamma.html" title="Incomplete Gamma Functions">gamma_q</a>(v | |
271 | / 2, x / 2) | |
272 | </p> | |
273 | </td> | |
274 | </tr> | |
275 | <tr> | |
276 | <td> | |
277 | <p> | |
278 | quantile | |
279 | </p> | |
280 | </td> | |
281 | <td> | |
282 | <p> | |
283 | Using the relation: x = 2 * <a class="link" href="../../sf_gamma/igamma_inv.html" title="Incomplete Gamma Function Inverses">gamma_p_inv</a>(v | |
284 | / 2, p) | |
285 | </p> | |
286 | </td> | |
287 | </tr> | |
288 | <tr> | |
289 | <td> | |
290 | <p> | |
291 | quantile from the complement | |
292 | </p> | |
293 | </td> | |
294 | <td> | |
295 | <p> | |
296 | Using the relation: x = 2 * <a class="link" href="../../sf_gamma/igamma_inv.html" title="Incomplete Gamma Function Inverses">gamma_q_inv</a>(v | |
297 | / 2, p) | |
298 | </p> | |
299 | </td> | |
300 | </tr> | |
301 | <tr> | |
302 | <td> | |
303 | <p> | |
304 | mean | |
305 | </p> | |
306 | </td> | |
307 | <td> | |
308 | <p> | |
309 | v | |
310 | </p> | |
311 | </td> | |
312 | </tr> | |
313 | <tr> | |
314 | <td> | |
315 | <p> | |
316 | variance | |
317 | </p> | |
318 | </td> | |
319 | <td> | |
320 | <p> | |
321 | 2v | |
322 | </p> | |
323 | </td> | |
324 | </tr> | |
325 | <tr> | |
326 | <td> | |
327 | <p> | |
328 | mode | |
329 | </p> | |
330 | </td> | |
331 | <td> | |
332 | <p> | |
333 | v - 2 (if v >= 2) | |
334 | </p> | |
335 | </td> | |
336 | </tr> | |
337 | <tr> | |
338 | <td> | |
339 | <p> | |
340 | skewness | |
341 | </p> | |
342 | </td> | |
343 | <td> | |
344 | <p> | |
345 | 2 * sqrt(2 / v) == sqrt(8 / v) | |
346 | </p> | |
347 | </td> | |
348 | </tr> | |
349 | <tr> | |
350 | <td> | |
351 | <p> | |
352 | kurtosis | |
353 | </p> | |
354 | </td> | |
355 | <td> | |
356 | <p> | |
357 | 3 + 12 / v | |
358 | </p> | |
359 | </td> | |
360 | </tr> | |
361 | <tr> | |
362 | <td> | |
363 | <p> | |
364 | kurtosis excess | |
365 | </p> | |
366 | </td> | |
367 | <td> | |
368 | <p> | |
369 | 12 / v | |
370 | </p> | |
371 | </td> | |
372 | </tr> | |
373 | </tbody> | |
374 | </table></div> | |
375 | <h5> | |
376 | <a name="math_toolkit.dist_ref.dists.chi_squared_dist.h5"></a> | |
377 | <span class="phrase"><a name="math_toolkit.dist_ref.dists.chi_squared_dist.references"></a></span><a class="link" href="chi_squared_dist.html#math_toolkit.dist_ref.dists.chi_squared_dist.references">References</a> | |
378 | </h5> | |
379 | <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> | |
380 | <li class="listitem"> | |
381 | <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm" target="_top">NIST | |
382 | Exploratory Data Analysis</a> | |
383 | </li> | |
384 | <li class="listitem"> | |
385 | <a href="http://en.wikipedia.org/wiki/Chi-square_distribution" target="_top">Chi-square | |
386 | distribution</a> | |
387 | </li> | |
388 | <li class="listitem"> | |
389 | <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html" target="_top">Weisstein, | |
390 | Eric W. "Chi-Squared Distribution." From MathWorld--A Wolfram | |
391 | Web Resource.</a> | |
392 | </li> | |
393 | </ul></div> | |
394 | </div> | |
395 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
396 | <td align="left"></td> | |
397 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
398 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
399 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
400 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
401 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
402 | 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>) | |
403 | </p> | |
404 | </div></td> | |
405 | </tr></table> | |
406 | <hr> | |
407 | <div class="spirit-nav"> | |
408 | <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> | |
409 | </div> | |
410 | </body> | |
411 | </html> |