]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Estimating the Required Sample Sizes for a Chi-Square Test for the Standard Deviation</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="../cs_eg.html" title="Chi Squared Distribution Examples"> | |
9 | <link rel="prev" href="chi_sq_test.html" title="Chi-Square Test for the Standard Deviation"> | |
10 | <link rel="next" href="../f_eg.html" title="F Distribution Examples"> | |
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="chi_sq_test.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cs_eg.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="../f_eg.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h5 class="title"> | |
27 | <a name="math_toolkit.stat_tut.weg.cs_eg.chi_sq_size"></a><a class="link" href="chi_sq_size.html" title="Estimating the Required Sample Sizes for a Chi-Square Test for the Standard Deviation">Estimating | |
28 | the Required Sample Sizes for a Chi-Square Test for the Standard Deviation</a> | |
29 | </h5></div></div></div> | |
30 | <p> | |
31 | Suppose we conduct a Chi Squared test for standard deviation and the | |
32 | result is borderline, a legitimate question to ask is "How large | |
33 | would the sample size have to be in order to produce a definitive result?" | |
34 | </p> | |
35 | <p> | |
36 | The class template <a class="link" href="../../../dist_ref/dists/chi_squared_dist.html" title="Chi Squared Distribution">chi_squared_distribution</a> | |
37 | has a static method <code class="computeroutput"><span class="identifier">find_degrees_of_freedom</span></code> | |
38 | that will calculate this value for some acceptable risk of type I failure | |
39 | <span class="emphasis"><em>alpha</em></span>, type II failure <span class="emphasis"><em>beta</em></span>, | |
40 | and difference from the standard deviation <span class="emphasis"><em>diff</em></span>. | |
41 | Please note that the method used works on variance, and not standard | |
42 | deviation as is usual for the Chi Squared Test. | |
43 | </p> | |
44 | <p> | |
45 | The code for this example is located in <a href="../../../../../../example/chi_square_std_dev_test.cpp" target="_top">chi_square_std_dev_test.cpp</a>. | |
46 | </p> | |
47 | <p> | |
48 | We begin by defining a procedure to print out the sample sizes required | |
49 | for various risk levels: | |
50 | </p> | |
51 | <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">chi_squared_sample_sized</span><span class="special">(</span> | |
52 | <span class="keyword">double</span> <span class="identifier">diff</span><span class="special">,</span> <span class="comment">// difference from variance to detect</span> | |
53 | <span class="keyword">double</span> <span class="identifier">variance</span><span class="special">)</span> <span class="comment">// true variance</span> | |
54 | <span class="special">{</span> | |
55 | </pre> | |
56 | <p> | |
57 | The procedure begins by printing out the input data: | |
58 | </p> | |
59 | <pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span> | |
60 | <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">;</span> | |
61 | ||
62 | <span class="comment">// Print out general info:</span> | |
63 | <span class="identifier">cout</span> <span class="special"><<</span> | |
64 | <span class="string">"_____________________________________________________________\n"</span> | |
65 | <span class="string">"Estimated sample sizes required for various confidence levels\n"</span> | |
66 | <span class="string">"_____________________________________________________________\n\n"</span><span class="special">;</span> | |
67 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">5</span><span class="special">);</span> | |
68 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">40</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">left</span> <span class="special"><<</span> <span class="string">"True Variance"</span> <span class="special"><<</span> <span class="string">"= "</span> <span class="special"><<</span> <span class="identifier">variance</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> | |
69 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">40</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">left</span> <span class="special"><<</span> <span class="string">"Difference to detect"</span> <span class="special"><<</span> <span class="string">"= "</span> <span class="special"><<</span> <span class="identifier">diff</span> <span class="special"><<</span> <span class="string">"\n"</span><span class="special">;</span> | |
70 | </pre> | |
71 | <p> | |
72 | And defines a table of significance levels for which we'll calculate | |
73 | sample sizes: | |
74 | </p> | |
75 | <pre class="programlisting"><span class="keyword">double</span> <span class="identifier">alpha</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> <span class="number">0.5</span><span class="special">,</span> <span class="number">0.25</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.05</span><span class="special">,</span> <span class="number">0.01</span><span class="special">,</span> <span class="number">0.001</span><span class="special">,</span> <span class="number">0.0001</span><span class="special">,</span> <span class="number">0.00001</span> <span class="special">};</span> | |
76 | </pre> | |
77 | <p> | |
78 | For each value of alpha we can calculate two sample sizes: one where | |
79 | the sample variance is less than the true value by <span class="emphasis"><em>diff</em></span> | |
80 | and one where it is greater than the true value by <span class="emphasis"><em>diff</em></span>. | |
81 | Thanks to the asymmetric nature of the Chi Squared distribution these | |
82 | two values will not be the same, the difference in their calculation | |
83 | differs only in the sign of <span class="emphasis"><em>diff</em></span> that's passed to | |
84 | <code class="computeroutput"><span class="identifier">find_degrees_of_freedom</span></code>. | |
85 | Finally in this example we'll simply things, and let risk level <span class="emphasis"><em>beta</em></span> | |
86 | be the same as <span class="emphasis"><em>alpha</em></span>: | |
87 | </p> | |
88 | <pre class="programlisting"><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"\n\n"</span> | |
89 | <span class="string">"_______________________________________________________________\n"</span> | |
90 | <span class="string">"Confidence Estimated Estimated\n"</span> | |
91 | <span class="string">" Value (%) Sample Size Sample Size\n"</span> | |
92 | <span class="string">" (lower one (upper one\n"</span> | |
93 | <span class="string">" sided test) sided test)\n"</span> | |
94 | <span class="string">"_______________________________________________________________\n"</span><span class="special">;</span> | |
95 | <span class="comment">//</span> | |
96 | <span class="comment">// Now print out the data for the table rows.</span> | |
97 | <span class="comment">//</span> | |
98 | <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">alpha</span><span class="special">)/</span><span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">alpha</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> | |
99 | <span class="special">{</span> | |
100 | <span class="comment">// Confidence value:</span> | |
101 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">fixed</span> <span class="special"><<</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">10</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> <span class="special"><<</span> <span class="number">100</span> <span class="special">*</span> <span class="special">(</span><span class="number">1</span><span class="special">-</span><span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">]);</span> | |
102 | <span class="comment">// calculate df for a lower single sided test:</span> | |
103 | <span class="keyword">double</span> <span class="identifier">df</span> <span class="special">=</span> <span class="identifier">chi_squared</span><span class="special">::</span><span class="identifier">find_degrees_of_freedom</span><span class="special">(</span> | |
104 | <span class="special">-</span><span class="identifier">diff</span><span class="special">,</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">],</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">],</span> <span class="identifier">variance</span><span class="special">);</span> | |
105 | <span class="comment">// convert to sample size:</span> | |
106 | <span class="keyword">double</span> <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">ceil</span><span class="special">(</span><span class="identifier">df</span><span class="special">)</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span> | |
107 | <span class="comment">// Print size:</span> | |
108 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">fixed</span> <span class="special"><<</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">16</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> <span class="special"><<</span> <span class="identifier">size</span><span class="special">;</span> | |
109 | <span class="comment">// calculate df for an upper single sided test:</span> | |
110 | <span class="identifier">df</span> <span class="special">=</span> <span class="identifier">chi_squared</span><span class="special">::</span><span class="identifier">find_degrees_of_freedom</span><span class="special">(</span> | |
111 | <span class="identifier">diff</span><span class="special">,</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">],</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">],</span> <span class="identifier">variance</span><span class="special">);</span> | |
112 | <span class="comment">// convert to sample size:</span> | |
113 | <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">ceil</span><span class="special">(</span><span class="identifier">df</span><span class="special">)</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span> | |
114 | <span class="comment">// Print size:</span> | |
115 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">fixed</span> <span class="special"><<</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">16</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> <span class="special"><<</span> <span class="identifier">size</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> | |
116 | <span class="special">}</span> | |
117 | <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> | |
118 | </pre> | |
119 | <p> | |
120 | For some example output, consider the <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc23.htm" target="_top">silicon | |
121 | wafer data</a> from the <a href="http://www.itl.nist.gov/div898/handbook/" target="_top">NIST/SEMATECH | |
122 | e-Handbook of Statistical Methods.</a>. In this scenario a supplier | |
123 | of 100 ohm.cm silicon wafers claims that his fabrication process can | |
124 | produce wafers with sufficient consistency so that the standard deviation | |
125 | of resistivity for the lot does not exceed 10 ohm.cm. A sample of N = | |
126 | 10 wafers taken from the lot has a standard deviation of 13.97 ohm.cm, | |
127 | and the question we ask ourselves is "How large would our sample | |
128 | have to be to reliably detect this difference?". | |
129 | </p> | |
130 | <p> | |
131 | To use our procedure above, we have to convert the standard deviations | |
132 | to variance (square them), after which the program output looks like | |
133 | this: | |
134 | </p> | |
135 | <pre class="programlisting">_____________________________________________________________ | |
136 | Estimated sample sizes required for various confidence levels | |
137 | _____________________________________________________________ | |
138 | ||
139 | True Variance = 100.00000 | |
140 | Difference to detect = 95.16090 | |
141 | ||
142 | ||
143 | _______________________________________________________________ | |
144 | Confidence Estimated Estimated | |
145 | Value (%) Sample Size Sample Size | |
146 | (lower one (upper one | |
147 | sided test) sided test) | |
148 | _______________________________________________________________ | |
149 | 50.000 2 2 | |
150 | 75.000 2 10 | |
151 | 90.000 4 32 | |
152 | 95.000 5 51 | |
153 | 99.000 7 99 | |
154 | 99.900 11 174 | |
155 | 99.990 15 251 | |
156 | 99.999 20 330 | |
157 | </pre> | |
158 | <p> | |
159 | In this case we are interested in a upper single sided test. So for example, | |
160 | if the maximum acceptable risk of falsely rejecting the null-hypothesis | |
161 | is 0.05 (Type I error), and the maximum acceptable risk of failing to | |
162 | reject the null-hypothesis is also 0.05 (Type II error), we estimate | |
163 | that we would need a sample size of 51. | |
164 | </p> | |
165 | </div> | |
166 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
167 | <td align="left"></td> | |
168 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
169 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
170 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
171 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
172 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
173 | 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>) | |
174 | </p> | |
175 | </div></td> | |
176 | </tr></table> | |
177 | <hr> | |
178 | <div class="spirit-nav"> | |
179 | <a accesskey="p" href="chi_sq_test.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cs_eg.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="../f_eg.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> | |
180 | </div> | |
181 | </body> | |
182 | </html> |