Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / math / doc / sf_and_dist / html / math_toolkit / dist / dist_ref / dists / chi_squared_dist.html
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="../../../../../../../../../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">
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 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>
30 <p>
31 </p>
32 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span></pre>
33 <p>
34           </p>
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>
36
37 <span class="keyword">template</span> <span class="special">&lt;</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&lt;&gt;</a> <span class="special">&gt;</span>
39 <span class="keyword">class</span> <span class="identifier">chi_squared_distribution</span><span class="special">;</span>
40
41 <span class="keyword">typedef</span> <span class="identifier">chi_squared_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">chi_squared</span><span class="special">;</span>
42
43 <span class="keyword">template</span> <span class="special">&lt;</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">&gt;</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>
49
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>
52
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>
55
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>
64
65 <span class="special">}}</span> <span class="comment">// namespaces</span>
66 </pre>
67 <p>
68             The Chi-Squared distribution is one of the most widely used distributions
69             in statistical tests. If &#967;<sub>i</sub> &#160; are &#957; &#160; 
70 independent, normally distributed random
71             variables with means &#956;<sub>i</sub> &#160; and variances &#963;<sub>i</sub><sup>2</sup>, then the random variable:
72           </p>
73 <p>
74             <span class="inlinemediaobject"><img src="../../../../../equations/chi_squ_ref1.png"></span>
75           </p>
76 <p>
77             is distributed according to the Chi-Squared distribution.
78           </p>
79 <p>
80             The Chi-Squared distribution is a special case of the gamma distribution
81             and has a single parameter &#957; &#160; that specifies the number of degrees of freedom.
82             The following graph illustrates how the distribution changes for different
83             values of &#957;:
84           </p>
85 <p>
86             <span class="inlinemediaobject"><img src="../../../../../graphs/chi_squared_pdf.png" align="middle"></span>
87           </p>
88 <h5>
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
91             Functions</a>
92           </h5>
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>
94 </pre>
95 <p>
96             Constructs a Chi-Squared distribution with <span class="emphasis"><em>v</em></span> degrees
97             of freedom.
98           </p>
99 <p>
100             Requires v &gt; 0, otherwise calls <a class="link" href="../../../main_overview/error_handling.html#domain_error">domain_error</a>.
101           </p>
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>
103 </pre>
104 <p>
105             Returns the parameter <span class="emphasis"><em>v</em></span> from which this object was
106             constructed.
107           </p>
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>
114 </pre>
115 <p>
116             Estimates the sample size required to detect a difference from a nominal
117             variance in a Chi-Squared test for equal standard deviations.
118           </p>
119 <div class="variablelist">
120 <p class="title"><b></b></p>
121 <dl>
122 <dt><span class="term">difference_from_variance</span></dt>
123 <dd><p>
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.
126                 </p></dd>
127 <dt><span class="term">alpha</span></dt>
128 <dd><p>
129                   The maximum acceptable risk of rejecting the null hypothesis when
130                   it is in fact true.
131                 </p></dd>
132 <dt><span class="term">beta</span></dt>
133 <dd><p>
134                   The maximum acceptable risk of falsely failing to reject the null
135                   hypothesis.
136                 </p></dd>
137 <dt><span class="term">variance</span></dt>
138 <dd><p>
139                   The nominal variance being tested against.
140                 </p></dd>
141 <dt><span class="term">hint</span></dt>
142 <dd><p>
143                   An optional hint on where to start looking for a result: the current
144                   sample size would be a good choice.
145                 </p></dd>
146 </dl>
147 </div>
148 <p>
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>.
151           </p>
152 <p>
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">&gt;</span>
160             <span class="number">0</span></code>, since no sample can have a negative
161             variance!
162           </p>
163 <p>
164             This procedure uses the method in Diamond, W. J. (1989). Practical Experiment
165             Designs, Van-Nostrand Reinhold, New York.
166           </p>
167 <p>
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>.
170           </p>
171 <h5>
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
174             Accessors</a>
175           </h5>
176 <p>
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>.
187           </p>
188 <p>
189             (We have followed the usual restriction of the mode to degrees of freedom
190             &gt;= 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).
193           </p>
194 <p>
195             The domain of the random variable is [0, +&#8734;].
196           </p>
197 <h5>
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>
200           </h5>
201 <p>
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.
204           </p>
205 <h5>
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>
208           </h5>
209 <p>
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.
212           </p>
213 <h5>
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>
216           </h5>
217 <p>
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 =
221             1-p</em></span>.
222           </p>
223 <div class="informaltable"><table class="table">
224 <colgroup>
225 <col>
226 <col>
227 </colgroup>
228 <thead><tr>
229 <th>
230                     <p>
231                       Function
232                     </p>
233                   </th>
234 <th>
235                     <p>
236                       Implementation Notes
237                     </p>
238                   </th>
239 </tr></thead>
240 <tbody>
241 <tr>
242 <td>
243                     <p>
244                       pdf
245                     </p>
246                   </td>
247 <td>
248                     <p>
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
250                       / 2, x / 2) / 2
251                     </p>
252                   </td>
253 </tr>
254 <tr>
255 <td>
256                     <p>
257                       cdf
258                     </p>
259                   </td>
260 <td>
261                     <p>
262                       Using the relation: p = <a class="link" href="../../../special/sf_gamma/igamma.html" title="Incomplete Gamma Functions">gamma_p</a>(v
263                       / 2, x / 2)
264                     </p>
265                   </td>
266 </tr>
267 <tr>
268 <td>
269                     <p>
270                       cdf complement
271                     </p>
272                   </td>
273 <td>
274                     <p>
275                       Using the relation: q = <a class="link" href="../../../special/sf_gamma/igamma.html" title="Incomplete Gamma Functions">gamma_q</a>(v
276                       / 2, x / 2)
277                     </p>
278                   </td>
279 </tr>
280 <tr>
281 <td>
282                     <p>
283                       quantile
284                     </p>
285                   </td>
286 <td>
287                     <p>
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
289                       / 2, p)
290                     </p>
291                   </td>
292 </tr>
293 <tr>
294 <td>
295                     <p>
296                       quantile from the complement
297                     </p>
298                   </td>
299 <td>
300                     <p>
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
302                       / 2, p)
303                     </p>
304                   </td>
305 </tr>
306 <tr>
307 <td>
308                     <p>
309                       mean
310                     </p>
311                   </td>
312 <td>
313                     <p>
314                       v
315                     </p>
316                   </td>
317 </tr>
318 <tr>
319 <td>
320                     <p>
321                       variance
322                     </p>
323                   </td>
324 <td>
325                     <p>
326                       2v
327                     </p>
328                   </td>
329 </tr>
330 <tr>
331 <td>
332                     <p>
333                       mode
334                     </p>
335                   </td>
336 <td>
337                     <p>
338                       v - 2 (if v &gt;= 2)
339                     </p>
340                   </td>
341 </tr>
342 <tr>
343 <td>
344                     <p>
345                       skewness
346                     </p>
347                   </td>
348 <td>
349                     <p>
350                       2 * sqrt(2 / v) == sqrt(8 / v)
351                     </p>
352                   </td>
353 </tr>
354 <tr>
355 <td>
356                     <p>
357                       kurtosis
358                     </p>
359                   </td>
360 <td>
361                     <p>
362                       3 + 12 / v
363                     </p>
364                   </td>
365 </tr>
366 <tr>
367 <td>
368                     <p>
369                       kurtosis excess
370                     </p>
371                   </td>
372 <td>
373                     <p>
374                       12 / v
375                     </p>
376                   </td>
377 </tr>
378 </tbody>
379 </table></div>
380 <h5>
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>
383           </h5>
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>
388               </li>
389 <li class="listitem">
390                 <a href="http://en.wikipedia.org/wiki/Chi-square_distribution" target="_top">Chi-square
391                 distribution</a>
392               </li>
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
396                 Web Resource.</a>
397               </li>
398 </ul></div>
399 </div>
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 &#169; 2006-2010 John Maddock, Paul A. Bristow, Hubert Holin, Xiaogang Zhang, Bruno
403       Lalande, Johan R&#229;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>)
406       </p>
407 </div></td>
408 </tr></table>
409 <hr>
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>
412 </div>
413 </body>
414 </html>