chi_squared_examples.qbk 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. [section:cs_eg Chi Squared Distribution Examples]
  2. [section:chi_sq_intervals Confidence Intervals on the Standard Deviation]
  3. Once you have calculated the standard deviation for your data, a legitimate
  4. question to ask is "How reliable is the calculated standard deviation?".
  5. For this situation the Chi Squared distribution can be used to calculate
  6. confidence intervals for the standard deviation.
  7. The full example code & sample output is in
  8. [@../../example/chi_square_std_dev_test.cpp chi_square_std_dev_test.cpp].
  9. We'll begin by defining the procedure that will calculate and print out the
  10. confidence intervals:
  11. void confidence_limits_on_std_deviation(
  12. double Sd, // Sample Standard Deviation
  13. unsigned N) // Sample size
  14. {
  15. We'll begin by printing out some general information:
  16. cout <<
  17. "________________________________________________\n"
  18. "2-Sided Confidence Limits For Standard Deviation\n"
  19. "________________________________________________\n\n";
  20. cout << setprecision(7);
  21. cout << setw(40) << left << "Number of Observations" << "= " << N << "\n";
  22. cout << setw(40) << left << "Standard Deviation" << "= " << Sd << "\n";
  23. and then define a table of significance levels for which we'll calculate
  24. intervals:
  25. double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
  26. The distribution we'll need to calculate the confidence intervals is a
  27. Chi Squared distribution, with N-1 degrees of freedom:
  28. chi_squared dist(N - 1);
  29. For each value of alpha, the formula for the confidence interval is given by:
  30. [equation chi_squ_tut1]
  31. Where [equation chi_squ_tut2] is the upper critical value, and
  32. [equation chi_squ_tut3] is the lower critical value of the
  33. Chi Squared distribution.
  34. In code we begin by printing out a table header:
  35. cout << "\n\n"
  36. "_____________________________________________\n"
  37. "Confidence Lower Upper\n"
  38. " Value (%) Limit Limit\n"
  39. "_____________________________________________\n";
  40. and then loop over the values of alpha and calculate the intervals
  41. for each: remember that the lower critical value is the same as the
  42. quantile, and the upper critical value is the same as the quantile
  43. from the complement of the probability:
  44. for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
  45. {
  46. // Confidence value:
  47. cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
  48. // Calculate limits:
  49. double lower_limit = sqrt((N - 1) * Sd * Sd / quantile(complement(dist, alpha[i] / 2)));
  50. double upper_limit = sqrt((N - 1) * Sd * Sd / quantile(dist, alpha[i] / 2));
  51. // Print Limits:
  52. cout << fixed << setprecision(5) << setw(15) << right << lower_limit;
  53. cout << fixed << setprecision(5) << setw(15) << right << upper_limit << endl;
  54. }
  55. cout << endl;
  56. To see some example output we'll use the
  57. [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda3581.htm
  58. gear data] from the __handbook.
  59. The data represents measurements of gear diameter from a manufacturing
  60. process.
  61. [pre'''
  62. ________________________________________________
  63. 2-Sided Confidence Limits For Standard Deviation
  64. ________________________________________________
  65. Number of Observations = 100
  66. Standard Deviation = 0.006278908
  67. _____________________________________________
  68. Confidence Lower Upper
  69. Value (%) Limit Limit
  70. _____________________________________________
  71. 50.000 0.00601 0.00662
  72. 75.000 0.00582 0.00685
  73. 90.000 0.00563 0.00712
  74. 95.000 0.00551 0.00729
  75. 99.000 0.00530 0.00766
  76. 99.900 0.00507 0.00812
  77. 99.990 0.00489 0.00855
  78. 99.999 0.00474 0.00895
  79. ''']
  80. So at the 95% confidence level we conclude that the standard deviation
  81. is between 0.00551 and 0.00729.
  82. [h4 Confidence intervals as a function of the number of observations]
  83. Similarly, we can also list the confidence intervals for the standard deviation
  84. for the common confidence levels 95%, for increasing numbers of observations.
  85. The standard deviation used to compute these values is unity,
  86. so the limits listed are *multipliers* for any particular standard deviation.
  87. For example, given a standard deviation of 0.0062789 as in the example
  88. above; for 100 observations the multiplier is 0.8780
  89. giving the lower confidence limit of 0.8780 * 0.006728 = 0.00551.
  90. [pre'''
  91. ____________________________________________________
  92. Confidence level (two-sided) = 0.0500000
  93. Standard Deviation = 1.0000000
  94. ________________________________________
  95. Observations Lower Upper
  96. Limit Limit
  97. ________________________________________
  98. 2 0.4461 31.9102
  99. 3 0.5207 6.2847
  100. 4 0.5665 3.7285
  101. 5 0.5991 2.8736
  102. 6 0.6242 2.4526
  103. 7 0.6444 2.2021
  104. 8 0.6612 2.0353
  105. 9 0.6755 1.9158
  106. 10 0.6878 1.8256
  107. 15 0.7321 1.5771
  108. 20 0.7605 1.4606
  109. 30 0.7964 1.3443
  110. 40 0.8192 1.2840
  111. 50 0.8353 1.2461
  112. 60 0.8476 1.2197
  113. 100 0.8780 1.1617
  114. 120 0.8875 1.1454
  115. 1000 0.9580 1.0459
  116. 10000 0.9863 1.0141
  117. 50000 0.9938 1.0062
  118. 100000 0.9956 1.0044
  119. 1000000 0.9986 1.0014
  120. ''']
  121. With just 2 observations the limits are from *0.445* up to to *31.9*,
  122. so the standard deviation might be about *half*
  123. the observed value up to [*30 times] the observed value!
  124. Estimating a standard deviation with just a handful of values leaves a very great uncertainty,
  125. especially the upper limit.
  126. Note especially how far the upper limit is skewed from the most likely standard deviation.
  127. Even for 10 observations, normally considered a reasonable number,
  128. the range is still from 0.69 to 1.8, about a range of 0.7 to 2,
  129. and is still highly skewed with an upper limit *twice* the median.
  130. When we have 1000 observations, the estimate of the standard deviation is starting to look convincing,
  131. with a range from 0.95 to 1.05 - now near symmetrical, but still about + or - 5%.
  132. Only when we have 10000 or more repeated observations can we start to be reasonably confident
  133. (provided we are sure that other factors like drift are not creeping in).
  134. For 10000 observations, the interval is 0.99 to 1.1 - finally a really convincing + or -1% confidence.
  135. [endsect] [/section:chi_sq_intervals Confidence Intervals on the Standard Deviation]
  136. [section:chi_sq_test Chi-Square Test for the Standard Deviation]
  137. We use this test to determine whether the standard deviation of a sample
  138. differs from a specified value. Typically this occurs in process change
  139. situations where we wish to compare the standard deviation of a new
  140. process to an established one.
  141. The code for this example is contained in
  142. [@../../example/chi_square_std_dev_test.cpp chi_square_std_dev_test.cpp], and
  143. we'll begin by defining the procedure that will print out the test
  144. statistics:
  145. void chi_squared_test(
  146. double Sd, // Sample std deviation
  147. double D, // True std deviation
  148. unsigned N, // Sample size
  149. double alpha) // Significance level
  150. {
  151. The procedure begins by printing a summary of the input data:
  152. using namespace std;
  153. using namespace boost::math;
  154. // Print header:
  155. cout <<
  156. "______________________________________________\n"
  157. "Chi Squared test for sample standard deviation\n"
  158. "______________________________________________\n\n";
  159. cout << setprecision(5);
  160. cout << setw(55) << left << "Number of Observations" << "= " << N << "\n";
  161. cout << setw(55) << left << "Sample Standard Deviation" << "= " << Sd << "\n";
  162. cout << setw(55) << left << "Expected True Standard Deviation" << "= " << D << "\n\n";
  163. The test statistic (T) is simply the ratio of the sample and "true" standard
  164. deviations squared, multiplied by the number of degrees of freedom (the
  165. sample size less one):
  166. double t_stat = (N - 1) * (Sd / D) * (Sd / D);
  167. cout << setw(55) << left << "Test Statistic" << "= " << t_stat << "\n";
  168. The distribution we need to use, is a Chi Squared distribution with N-1
  169. degrees of freedom:
  170. chi_squared dist(N - 1);
  171. The various hypothesis that can be tested are summarised in the following table:
  172. [table
  173. [[Hypothesis][Test]]
  174. [[The null-hypothesis: there is no difference in standard deviation from the specified value]
  175. [ Reject if T < [chi][super 2][sub (1-alpha/2; N-1)] or T > [chi][super 2][sub (alpha/2; N-1)] ]]
  176. [[The alternative hypothesis: there is a difference in standard deviation from the specified value]
  177. [ Reject if [chi][super 2][sub (1-alpha/2; N-1)] >= T >= [chi][super 2][sub (alpha/2; N-1)] ]]
  178. [[The alternative hypothesis: the standard deviation is less than the specified value]
  179. [ Reject if [chi][super 2][sub (1-alpha; N-1)] <= T ]]
  180. [[The alternative hypothesis: the standard deviation is greater than the specified value]
  181. [ Reject if [chi][super 2][sub (alpha; N-1)] >= T ]]
  182. ]
  183. Where [chi][super 2][sub (alpha; N-1)] is the upper critical value of the
  184. Chi Squared distribution, and [chi][super 2][sub (1-alpha; N-1)] is the
  185. lower critical value.
  186. Recall that the lower critical value is the same
  187. as the quantile, and the upper critical value is the same as the quantile
  188. from the complement of the probability, that gives us the following code
  189. to calculate the critical values:
  190. double ucv = quantile(complement(dist, alpha));
  191. double ucv2 = quantile(complement(dist, alpha / 2));
  192. double lcv = quantile(dist, alpha);
  193. double lcv2 = quantile(dist, alpha / 2);
  194. cout << setw(55) << left << "Upper Critical Value at alpha: " << "= "
  195. << setprecision(3) << scientific << ucv << "\n";
  196. cout << setw(55) << left << "Upper Critical Value at alpha/2: " << "= "
  197. << setprecision(3) << scientific << ucv2 << "\n";
  198. cout << setw(55) << left << "Lower Critical Value at alpha: " << "= "
  199. << setprecision(3) << scientific << lcv << "\n";
  200. cout << setw(55) << left << "Lower Critical Value at alpha/2: " << "= "
  201. << setprecision(3) << scientific << lcv2 << "\n\n";
  202. Now that we have the critical values, we can compare these to our test
  203. statistic, and print out the result of each hypothesis and test:
  204. cout << setw(55) << left <<
  205. "Results for Alternative Hypothesis and alpha" << "= "
  206. << setprecision(4) << fixed << alpha << "\n\n";
  207. cout << "Alternative Hypothesis Conclusion\n";
  208. cout << "Standard Deviation != " << setprecision(3) << fixed << D << " ";
  209. if((ucv2 < t_stat) || (lcv2 > t_stat))
  210. cout << "ACCEPTED\n";
  211. else
  212. cout << "REJECTED\n";
  213. cout << "Standard Deviation < " << setprecision(3) << fixed << D << " ";
  214. if(lcv > t_stat)
  215. cout << "ACCEPTED\n";
  216. else
  217. cout << "REJECTED\n";
  218. cout << "Standard Deviation > " << setprecision(3) << fixed << D << " ";
  219. if(ucv < t_stat)
  220. cout << "ACCEPTED\n";
  221. else
  222. cout << "REJECTED\n";
  223. cout << endl << endl;
  224. To see some example output we'll use the
  225. [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda3581.htm
  226. gear data] from the __handbook.
  227. The data represents measurements of gear diameter from a manufacturing
  228. process. The program output is deliberately designed to mirror
  229. the DATAPLOT output shown in the
  230. [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda358.htm
  231. NIST Handbook Example].
  232. [pre'''
  233. ______________________________________________
  234. Chi Squared test for sample standard deviation
  235. ______________________________________________
  236. Number of Observations = 100
  237. Sample Standard Deviation = 0.00628
  238. Expected True Standard Deviation = 0.10000
  239. Test Statistic = 0.39030
  240. CDF of test statistic: = 1.438e-099
  241. Upper Critical Value at alpha: = 1.232e+002
  242. Upper Critical Value at alpha/2: = 1.284e+002
  243. Lower Critical Value at alpha: = 7.705e+001
  244. Lower Critical Value at alpha/2: = 7.336e+001
  245. Results for Alternative Hypothesis and alpha = 0.0500
  246. Alternative Hypothesis Conclusion'''
  247. Standard Deviation != 0.100 ACCEPTED
  248. Standard Deviation < 0.100 ACCEPTED
  249. Standard Deviation > 0.100 REJECTED
  250. ]
  251. In this case we are testing whether the sample standard deviation is 0.1,
  252. and the null-hypothesis is rejected, so we conclude that the standard
  253. deviation ['is not] 0.1.
  254. For an alternative example, consider the
  255. [@http://www.itl.nist.gov/div898/handbook/prc/section2/prc23.htm
  256. silicon wafer data] again from the __handbook.
  257. In this scenario a supplier of 100 ohm.cm silicon wafers claims
  258. that his fabrication process can produce wafers with sufficient
  259. consistency so that the standard deviation of resistivity for
  260. the lot does not exceed 10 ohm.cm. A sample of N = 10 wafers taken
  261. from the lot has a standard deviation of 13.97 ohm.cm, and the question
  262. we ask ourselves is "Is the suppliers claim correct?".
  263. The program output now looks like this:
  264. [pre'''
  265. ______________________________________________
  266. Chi Squared test for sample standard deviation
  267. ______________________________________________
  268. Number of Observations = 10
  269. Sample Standard Deviation = 13.97000
  270. Expected True Standard Deviation = 10.00000
  271. Test Statistic = 17.56448
  272. CDF of test statistic: = 9.594e-001
  273. Upper Critical Value at alpha: = 1.692e+001
  274. Upper Critical Value at alpha/2: = 1.902e+001
  275. Lower Critical Value at alpha: = 3.325e+000
  276. Lower Critical Value at alpha/2: = 2.700e+000
  277. Results for Alternative Hypothesis and alpha = 0.0500
  278. Alternative Hypothesis Conclusion'''
  279. Standard Deviation != 10.000 REJECTED
  280. Standard Deviation < 10.000 REJECTED
  281. Standard Deviation > 10.000 ACCEPTED
  282. ]
  283. In this case, our null-hypothesis is that the standard deviation of
  284. the sample is less than 10: this hypothesis is rejected in the analysis
  285. above, and so we reject the manufacturers claim.
  286. [endsect] [/section:chi_sq_test Chi-Square Test for the Standard Deviation]
  287. [section:chi_sq_size Estimating the Required Sample Sizes for a Chi-Square Test for the Standard Deviation]
  288. Suppose we conduct a Chi Squared test for standard deviation and the result
  289. is borderline, a legitimate question to ask is "How large would the sample size
  290. have to be in order to produce a definitive result?"
  291. The class template [link math_toolkit.dist_ref.dists.chi_squared_dist
  292. chi_squared_distribution] has a static method
  293. `find_degrees_of_freedom` that will calculate this value for
  294. some acceptable risk of type I failure /alpha/, type II failure
  295. /beta/, and difference from the standard deviation /diff/. Please
  296. note that the method used works on variance, and not standard deviation
  297. as is usual for the Chi Squared Test.
  298. The code for this example is located in
  299. [@../../example/chi_square_std_dev_test.cpp chi_square_std_dev_test.cpp].
  300. We begin by defining a procedure to print out the sample sizes required
  301. for various risk levels:
  302. void chi_squared_sample_sized(
  303. double diff, // difference from variance to detect
  304. double variance) // true variance
  305. {
  306. The procedure begins by printing out the input data:
  307. using namespace std;
  308. using namespace boost::math;
  309. // Print out general info:
  310. cout <<
  311. "_____________________________________________________________\n"
  312. "Estimated sample sizes required for various confidence levels\n"
  313. "_____________________________________________________________\n\n";
  314. cout << setprecision(5);
  315. cout << setw(40) << left << "True Variance" << "= " << variance << "\n";
  316. cout << setw(40) << left << "Difference to detect" << "= " << diff << "\n";
  317. And defines a table of significance levels for which we'll calculate sample sizes:
  318. double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
  319. For each value of alpha we can calculate two sample sizes: one where the
  320. sample variance is less than the true value by /diff/ and one
  321. where it is greater than the true value by /diff/. Thanks to the
  322. asymmetric nature of the Chi Squared distribution these two values will
  323. not be the same, the difference in their calculation differs only in the
  324. sign of /diff/ that's passed to `find_degrees_of_freedom`. Finally
  325. in this example we'll simply things, and let risk level /beta/ be the
  326. same as /alpha/:
  327. cout << "\n\n"
  328. "_______________________________________________________________\n"
  329. "Confidence Estimated Estimated\n"
  330. " Value (%) Sample Size Sample Size\n"
  331. " (lower one (upper one\n"
  332. " sided test) sided test)\n"
  333. "_______________________________________________________________\n";
  334. //
  335. // Now print out the data for the table rows.
  336. //
  337. for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
  338. {
  339. // Confidence value:
  340. cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
  341. // calculate df for a lower single sided test:
  342. double df = chi_squared::find_degrees_of_freedom(
  343. -diff, alpha[i], alpha[i], variance);
  344. // convert to sample size:
  345. double size = ceil(df) + 1;
  346. // Print size:
  347. cout << fixed << setprecision(0) << setw(16) << right << size;
  348. // calculate df for an upper single sided test:
  349. df = chi_squared::find_degrees_of_freedom(
  350. diff, alpha[i], alpha[i], variance);
  351. // convert to sample size:
  352. size = ceil(df) + 1;
  353. // Print size:
  354. cout << fixed << setprecision(0) << setw(16) << right << size << endl;
  355. }
  356. cout << endl;
  357. For some example output, consider the
  358. [@http://www.itl.nist.gov/div898/handbook/prc/section2/prc23.htm
  359. silicon wafer data] from the __handbook.
  360. In this scenario a supplier of 100 ohm.cm silicon wafers claims
  361. that his fabrication process can produce wafers with sufficient
  362. consistency so that the standard deviation of resistivity for
  363. the lot does not exceed 10 ohm.cm. A sample of N = 10 wafers taken
  364. from the lot has a standard deviation of 13.97 ohm.cm, and the question
  365. we ask ourselves is "How large would our sample have to be to reliably
  366. detect this difference?".
  367. To use our procedure above, we have to convert the
  368. standard deviations to variance (square them),
  369. after which the program output looks like this:
  370. [pre'''
  371. _____________________________________________________________
  372. Estimated sample sizes required for various confidence levels
  373. _____________________________________________________________
  374. True Variance = 100.00000
  375. Difference to detect = 95.16090
  376. _______________________________________________________________
  377. Confidence Estimated Estimated
  378. Value (%) Sample Size Sample Size
  379. (lower one (upper one
  380. sided test) sided test)
  381. _______________________________________________________________
  382. 50.000 2 2
  383. 75.000 2 10
  384. 90.000 4 32
  385. 95.000 5 51
  386. 99.000 7 99
  387. 99.900 11 174
  388. 99.990 15 251
  389. 99.999 20 330'''
  390. ]
  391. In this case we are interested in a upper single sided test.
  392. So for example, if the maximum acceptable risk of falsely rejecting
  393. the null-hypothesis is 0.05 (Type I error), and the maximum acceptable
  394. risk of failing to reject the null-hypothesis is also 0.05
  395. (Type II error), we estimate that we would need a sample size of 51.
  396. [endsect] [/section:chi_sq_size Estimating the Required Sample Sizes for a Chi-Square Test for the Standard Deviation]
  397. [endsect] [/section:cs_eg Chi Squared Distribution]
  398. [/
  399. Copyright 2006, 2013 John Maddock and Paul A. Bristow.
  400. Distributed under the Boost Software License, Version 1.0.
  401. (See accompanying file LICENSE_1_0.txt or copy at
  402. http://www.boost.org/LICENSE_1_0.txt).
  403. ]