chi_square_std_dev_test.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // Copyright John Maddock 2006, 2007
  2. // Copyright Paul A. Bristow 2010
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include <iostream>
  8. using std::cout; using std::endl;
  9. using std::left; using std::fixed; using std::right; using std::scientific;
  10. #include <iomanip>
  11. using std::setw;
  12. using std::setprecision;
  13. #include <boost/math/distributions/chi_squared.hpp>
  14. int error_result = 0;
  15. void confidence_limits_on_std_deviation(
  16. double Sd, // Sample Standard Deviation
  17. unsigned N) // Sample size
  18. {
  19. // Calculate confidence intervals for the standard deviation.
  20. // For example if we set the confidence limit to
  21. // 0.95, we know that if we repeat the sampling
  22. // 100 times, then we expect that the true standard deviation
  23. // will be between out limits on 95 occations.
  24. // Note: this is not the same as saying a 95%
  25. // confidence interval means that there is a 95%
  26. // probability that the interval contains the true standard deviation.
  27. // The interval computed from a given sample either
  28. // contains the true standard deviation or it does not.
  29. // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda358.htm
  30. // using namespace boost::math; // potential name ambiguity with std <random>
  31. using boost::math::chi_squared;
  32. using boost::math::quantile;
  33. using boost::math::complement;
  34. // Print out general info:
  35. cout <<
  36. "________________________________________________\n"
  37. "2-Sided Confidence Limits For Standard Deviation\n"
  38. "________________________________________________\n\n";
  39. cout << setprecision(7);
  40. cout << setw(40) << left << "Number of Observations" << "= " << N << "\n";
  41. cout << setw(40) << left << "Standard Deviation" << "= " << Sd << "\n";
  42. //
  43. // Define a table of significance/risk levels:
  44. double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
  45. //
  46. // Start by declaring the distribution we'll need:
  47. chi_squared dist(N - 1);
  48. //
  49. // Print table header:
  50. //
  51. cout << "\n\n"
  52. "_____________________________________________\n"
  53. "Confidence Lower Upper\n"
  54. " Value (%) Limit Limit\n"
  55. "_____________________________________________\n";
  56. //
  57. // Now print out the data for the table rows.
  58. for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
  59. {
  60. // Confidence value:
  61. cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
  62. // Calculate limits:
  63. double lower_limit = sqrt((N - 1) * Sd * Sd / quantile(complement(dist, alpha[i] / 2)));
  64. double upper_limit = sqrt((N - 1) * Sd * Sd / quantile(dist, alpha[i] / 2));
  65. // Print Limits:
  66. cout << fixed << setprecision(5) << setw(15) << right << lower_limit;
  67. cout << fixed << setprecision(5) << setw(15) << right << upper_limit << endl;
  68. }
  69. cout << endl;
  70. } // void confidence_limits_on_std_deviation
  71. void confidence_limits_on_std_deviation_alpha(
  72. double Sd, // Sample Standard Deviation
  73. double alpha // confidence
  74. )
  75. { // Calculate confidence intervals for the standard deviation.
  76. // for the alpha parameter, for a range number of observations,
  77. // from a mere 2 up to a million.
  78. // O. L. Davies, Statistical Methods in Research and Production, ISBN 0 05 002437 X,
  79. // 4.33 Page 68, Table H, pp 452 459.
  80. // using namespace std;
  81. // using namespace boost::math;
  82. using boost::math::chi_squared;
  83. using boost::math::quantile;
  84. using boost::math::complement;
  85. // Define a table of numbers of observations:
  86. unsigned int obs[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 40 , 50, 60, 100, 120, 1000, 10000, 50000, 100000, 1000000};
  87. cout << // Print out heading:
  88. "________________________________________________\n"
  89. "2-Sided Confidence Limits For Standard Deviation\n"
  90. "________________________________________________\n\n";
  91. cout << setprecision(7);
  92. cout << setw(40) << left << "Confidence level (two-sided) " << "= " << alpha << "\n";
  93. cout << setw(40) << left << "Standard Deviation" << "= " << Sd << "\n";
  94. cout << "\n\n" // Print table header:
  95. "_____________________________________________\n"
  96. "Observations Lower Upper\n"
  97. " Limit Limit\n"
  98. "_____________________________________________\n";
  99. for(unsigned i = 0; i < sizeof(obs)/sizeof(obs[0]); ++i)
  100. {
  101. unsigned int N = obs[i]; // Observations
  102. // Start by declaring the distribution with the appropriate :
  103. chi_squared dist(N - 1);
  104. // Now print out the data for the table row.
  105. cout << fixed << setprecision(3) << setw(10) << right << N;
  106. // Calculate limits: (alpha /2 because it is a two-sided (upper and lower limit) test.
  107. double lower_limit = sqrt((N - 1) * Sd * Sd / quantile(complement(dist, alpha / 2)));
  108. double upper_limit = sqrt((N - 1) * Sd * Sd / quantile(dist, alpha / 2));
  109. // Print Limits:
  110. cout << fixed << setprecision(4) << setw(15) << right << lower_limit;
  111. cout << fixed << setprecision(4) << setw(15) << right << upper_limit << endl;
  112. }
  113. cout << endl;
  114. }// void confidence_limits_on_std_deviation_alpha
  115. void chi_squared_test(
  116. double Sd, // Sample std deviation
  117. double D, // True std deviation
  118. unsigned N, // Sample size
  119. double alpha) // Significance level
  120. {
  121. //
  122. // A Chi Squared test applied to a single set of data.
  123. // We are testing the null hypothesis that the true
  124. // standard deviation of the sample is D, and that any variation is down
  125. // to chance. We can also test the alternative hypothesis
  126. // that any difference is not down to chance.
  127. // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda358.htm
  128. //
  129. // using namespace boost::math;
  130. using boost::math::chi_squared;
  131. using boost::math::quantile;
  132. using boost::math::complement;
  133. using boost::math::cdf;
  134. // Print header:
  135. cout <<
  136. "______________________________________________\n"
  137. "Chi Squared test for sample standard deviation\n"
  138. "______________________________________________\n\n";
  139. cout << setprecision(5);
  140. cout << setw(55) << left << "Number of Observations" << "= " << N << "\n";
  141. cout << setw(55) << left << "Sample Standard Deviation" << "= " << Sd << "\n";
  142. cout << setw(55) << left << "Expected True Standard Deviation" << "= " << D << "\n\n";
  143. //
  144. // Now we can calculate and output some stats:
  145. //
  146. // test-statistic:
  147. double t_stat = (N - 1) * (Sd / D) * (Sd / D);
  148. cout << setw(55) << left << "Test Statistic" << "= " << t_stat << "\n";
  149. //
  150. // Finally define our distribution, and get the probability:
  151. //
  152. chi_squared dist(N - 1);
  153. double p = cdf(dist, t_stat);
  154. cout << setw(55) << left << "CDF of test statistic: " << "= "
  155. << setprecision(3) << scientific << p << "\n";
  156. double ucv = quantile(complement(dist, alpha));
  157. double ucv2 = quantile(complement(dist, alpha / 2));
  158. double lcv = quantile(dist, alpha);
  159. double lcv2 = quantile(dist, alpha / 2);
  160. cout << setw(55) << left << "Upper Critical Value at alpha: " << "= "
  161. << setprecision(3) << scientific << ucv << "\n";
  162. cout << setw(55) << left << "Upper Critical Value at alpha/2: " << "= "
  163. << setprecision(3) << scientific << ucv2 << "\n";
  164. cout << setw(55) << left << "Lower Critical Value at alpha: " << "= "
  165. << setprecision(3) << scientific << lcv << "\n";
  166. cout << setw(55) << left << "Lower Critical Value at alpha/2: " << "= "
  167. << setprecision(3) << scientific << lcv2 << "\n\n";
  168. //
  169. // Finally print out results of alternative hypothesis:
  170. //
  171. cout << setw(55) << left <<
  172. "Results for Alternative Hypothesis and alpha" << "= "
  173. << setprecision(4) << fixed << alpha << "\n\n";
  174. cout << "Alternative Hypothesis Conclusion\n";
  175. cout << "Standard Deviation != " << setprecision(3) << fixed << D << " ";
  176. if((ucv2 < t_stat) || (lcv2 > t_stat))
  177. cout << "NOT REJECTED\n";
  178. else
  179. cout << "REJECTED\n";
  180. cout << "Standard Deviation < " << setprecision(3) << fixed << D << " ";
  181. if(lcv > t_stat)
  182. cout << "NOT REJECTED\n";
  183. else
  184. cout << "REJECTED\n";
  185. cout << "Standard Deviation > " << setprecision(3) << fixed << D << " ";
  186. if(ucv < t_stat)
  187. cout << "NOT REJECTED\n";
  188. else
  189. cout << "REJECTED\n";
  190. cout << endl << endl;
  191. } // void chi_squared_test
  192. void chi_squared_sample_sized(
  193. double diff, // difference from variance to detect
  194. double variance) // true variance
  195. {
  196. using namespace std;
  197. // using boost::math;
  198. using boost::math::chi_squared;
  199. using boost::math::quantile;
  200. using boost::math::complement;
  201. using boost::math::cdf;
  202. try
  203. {
  204. cout << // Print out general info:
  205. "_____________________________________________________________\n"
  206. "Estimated sample sizes required for various confidence levels\n"
  207. "_____________________________________________________________\n\n";
  208. cout << setprecision(5);
  209. cout << setw(40) << left << "True Variance" << "= " << variance << "\n";
  210. cout << setw(40) << left << "Difference to detect" << "= " << diff << "\n";
  211. //
  212. // Define a table of significance levels:
  213. //
  214. double alpha[] = { 0.5, 0.33333333333333333333333, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
  215. //
  216. // Print table header:
  217. //
  218. cout << "\n\n"
  219. "_______________________________________________________________\n"
  220. "Confidence Estimated Estimated\n"
  221. " Value (%) Sample Size Sample Size\n"
  222. " (lower one- (upper one-\n"
  223. " sided test) sided test)\n"
  224. "_______________________________________________________________\n";
  225. //
  226. // Now print out the data for the table rows.
  227. //
  228. for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
  229. {
  230. // Confidence value:
  231. cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
  232. // Calculate df for a lower single-sided test:
  233. double df = chi_squared::find_degrees_of_freedom(
  234. -diff, alpha[i], alpha[i], variance);
  235. // Convert to integral sample size (df is a floating point value in this implementation):
  236. double size = ceil(df) + 1;
  237. // Print size:
  238. cout << fixed << setprecision(0) << setw(16) << right << size;
  239. // Calculate df for an upper single-sided test:
  240. df = chi_squared::find_degrees_of_freedom(
  241. diff, alpha[i], alpha[i], variance);
  242. // Convert to integral sample size:
  243. size = ceil(df) + 1;
  244. // Print size:
  245. cout << fixed << setprecision(0) << setw(16) << right << size << endl;
  246. }
  247. cout << endl;
  248. }
  249. catch(const std::exception& e)
  250. { // Always useful to include try & catch blocks because default policies
  251. // are to throw exceptions on arguments that cause errors like underflow, overflow.
  252. // Lacking try & catch blocks, the program will abort without a message below,
  253. // which may give some helpful clues as to the cause of the exception.
  254. std::cout <<
  255. "\n""Message from thrown exception was:\n " << e.what() << std::endl;
  256. ++error_result;
  257. }
  258. } // chi_squared_sample_sized
  259. int main()
  260. {
  261. // Run tests for Gear data
  262. // see http://www.itl.nist.gov/div898/handbook/eda/section3/eda3581.htm
  263. // Tests measurements of gear diameter.
  264. //
  265. confidence_limits_on_std_deviation(0.6278908E-02, 100);
  266. chi_squared_test(0.6278908E-02, 0.1, 100, 0.05);
  267. chi_squared_sample_sized(0.1 - 0.6278908E-02, 0.1);
  268. //
  269. // Run tests for silicon wafer fabrication data.
  270. // see http://www.itl.nist.gov/div898/handbook/prc/section2/prc23.htm
  271. // A supplier of 100 ohm.cm silicon wafers claims that his fabrication
  272. // process can produce wafers with sufficient consistency so that the
  273. // standard deviation of resistivity for the lot does not exceed
  274. // 10 ohm.cm. A sample of N = 10 wafers taken from the lot has a
  275. // standard deviation of 13.97 ohm.cm
  276. //
  277. confidence_limits_on_std_deviation(13.97, 10);
  278. chi_squared_test(13.97, 10.0, 10, 0.05);
  279. chi_squared_sample_sized(13.97 * 13.97 - 100, 100);
  280. chi_squared_sample_sized(55, 100);
  281. chi_squared_sample_sized(1, 100);
  282. // List confidence interval multipliers for standard deviation
  283. // for a range of numbers of observations from 2 to a million,
  284. // and for a few alpha values, 0.1, 0.05, 0.01 for condfidences 90, 95, 99 %
  285. confidence_limits_on_std_deviation_alpha(1., 0.1);
  286. confidence_limits_on_std_deviation_alpha(1., 0.05);
  287. confidence_limits_on_std_deviation_alpha(1., 0.01);
  288. return error_result;
  289. }
  290. /*
  291. ________________________________________________
  292. 2-Sided Confidence Limits For Standard Deviation
  293. ________________________________________________
  294. Number of Observations = 100
  295. Standard Deviation = 0.006278908
  296. _____________________________________________
  297. Confidence Lower Upper
  298. Value (%) Limit Limit
  299. _____________________________________________
  300. 50.000 0.00601 0.00662
  301. 75.000 0.00582 0.00685
  302. 90.000 0.00563 0.00712
  303. 95.000 0.00551 0.00729
  304. 99.000 0.00530 0.00766
  305. 99.900 0.00507 0.00812
  306. 99.990 0.00489 0.00855
  307. 99.999 0.00474 0.00895
  308. ______________________________________________
  309. Chi Squared test for sample standard deviation
  310. ______________________________________________
  311. Number of Observations = 100
  312. Sample Standard Deviation = 0.00628
  313. Expected True Standard Deviation = 0.10000
  314. Test Statistic = 0.39030
  315. CDF of test statistic: = 1.438e-099
  316. Upper Critical Value at alpha: = 1.232e+002
  317. Upper Critical Value at alpha/2: = 1.284e+002
  318. Lower Critical Value at alpha: = 7.705e+001
  319. Lower Critical Value at alpha/2: = 7.336e+001
  320. Results for Alternative Hypothesis and alpha = 0.0500
  321. Alternative Hypothesis Conclusion
  322. Standard Deviation != 0.100 NOT REJECTED
  323. Standard Deviation < 0.100 NOT REJECTED
  324. Standard Deviation > 0.100 REJECTED
  325. _____________________________________________________________
  326. Estimated sample sizes required for various confidence levels
  327. _____________________________________________________________
  328. True Variance = 0.10000
  329. Difference to detect = 0.09372
  330. _______________________________________________________________
  331. Confidence Estimated Estimated
  332. Value (%) Sample Size Sample Size
  333. (lower one- (upper one-
  334. sided test) sided test)
  335. _______________________________________________________________
  336. 50.000 2 2
  337. 66.667 2 5
  338. 75.000 2 10
  339. 90.000 4 32
  340. 95.000 5 52
  341. 99.000 8 102
  342. 99.900 13 178
  343. 99.990 18 257
  344. 99.999 23 337
  345. ________________________________________________
  346. 2-Sided Confidence Limits For Standard Deviation
  347. ________________________________________________
  348. Number of Observations = 10
  349. Standard Deviation = 13.9700000
  350. _____________________________________________
  351. Confidence Lower Upper
  352. Value (%) Limit Limit
  353. _____________________________________________
  354. 50.000 12.41880 17.25579
  355. 75.000 11.23084 19.74131
  356. 90.000 10.18898 22.98341
  357. 95.000 9.60906 25.50377
  358. 99.000 8.62898 31.81825
  359. 99.900 7.69466 42.51593
  360. 99.990 7.04085 55.93352
  361. 99.999 6.54517 73.00132
  362. ______________________________________________
  363. Chi Squared test for sample standard deviation
  364. ______________________________________________
  365. Number of Observations = 10
  366. Sample Standard Deviation = 13.97000
  367. Expected True Standard Deviation = 10.00000
  368. Test Statistic = 17.56448
  369. CDF of test statistic: = 9.594e-001
  370. Upper Critical Value at alpha: = 1.692e+001
  371. Upper Critical Value at alpha/2: = 1.902e+001
  372. Lower Critical Value at alpha: = 3.325e+000
  373. Lower Critical Value at alpha/2: = 2.700e+000
  374. Results for Alternative Hypothesis and alpha = 0.0500
  375. Alternative Hypothesis Conclusion
  376. Standard Deviation != 10.000 REJECTED
  377. Standard Deviation < 10.000 REJECTED
  378. Standard Deviation > 10.000 NOT REJECTED
  379. _____________________________________________________________
  380. Estimated sample sizes required for various confidence levels
  381. _____________________________________________________________
  382. True Variance = 100.00000
  383. Difference to detect = 95.16090
  384. _______________________________________________________________
  385. Confidence Estimated Estimated
  386. Value (%) Sample Size Sample Size
  387. (lower one- (upper one-
  388. sided test) sided test)
  389. _______________________________________________________________
  390. 50.000 2 2
  391. 66.667 2 5
  392. 75.000 2 10
  393. 90.000 4 32
  394. 95.000 5 51
  395. 99.000 7 99
  396. 99.900 11 174
  397. 99.990 15 251
  398. 99.999 20 330
  399. _____________________________________________________________
  400. Estimated sample sizes required for various confidence levels
  401. _____________________________________________________________
  402. True Variance = 100.00000
  403. Difference to detect = 55.00000
  404. _______________________________________________________________
  405. Confidence Estimated Estimated
  406. Value (%) Sample Size Sample Size
  407. (lower one- (upper one-
  408. sided test) sided test)
  409. _______________________________________________________________
  410. 50.000 2 2
  411. 66.667 4 10
  412. 75.000 8 21
  413. 90.000 23 71
  414. 95.000 36 115
  415. 99.000 71 228
  416. 99.900 123 401
  417. 99.990 177 580
  418. 99.999 232 762
  419. _____________________________________________________________
  420. Estimated sample sizes required for various confidence levels
  421. _____________________________________________________________
  422. True Variance = 100.00000
  423. Difference to detect = 1.00000
  424. _______________________________________________________________
  425. Confidence Estimated Estimated
  426. Value (%) Sample Size Sample Size
  427. (lower one- (upper one-
  428. sided test) sided test)
  429. _______________________________________________________________
  430. 50.000 2 2
  431. 66.667 14696 14993
  432. 75.000 36033 36761
  433. 90.000 130079 132707
  434. 95.000 214283 218612
  435. 99.000 428628 437287
  436. 99.900 756333 771612
  437. 99.990 1095435 1117564
  438. 99.999 1440608 1469711
  439. ________________________________________________
  440. 2-Sided Confidence Limits For Standard Deviation
  441. ________________________________________________
  442. Confidence level (two-sided) = 0.1000000
  443. Standard Deviation = 1.0000000
  444. _____________________________________________
  445. Observations Lower Upper
  446. Limit Limit
  447. _____________________________________________
  448. 2 0.5102 15.9472
  449. 3 0.5778 4.4154
  450. 4 0.6196 2.9200
  451. 5 0.6493 2.3724
  452. 6 0.6720 2.0893
  453. 7 0.6903 1.9154
  454. 8 0.7054 1.7972
  455. 9 0.7183 1.7110
  456. 10 0.7293 1.6452
  457. 15 0.7688 1.4597
  458. 20 0.7939 1.3704
  459. 30 0.8255 1.2797
  460. 40 0.8454 1.2320
  461. 50 0.8594 1.2017
  462. 60 0.8701 1.1805
  463. 100 0.8963 1.1336
  464. 120 0.9045 1.1203
  465. 1000 0.9646 1.0383
  466. 10000 0.9885 1.0118
  467. 50000 0.9948 1.0052
  468. 100000 0.9963 1.0037
  469. 1000000 0.9988 1.0012
  470. ________________________________________________
  471. 2-Sided Confidence Limits For Standard Deviation
  472. ________________________________________________
  473. Confidence level (two-sided) = 0.0500000
  474. Standard Deviation = 1.0000000
  475. _____________________________________________
  476. Observations Lower Upper
  477. Limit Limit
  478. _____________________________________________
  479. 2 0.4461 31.9102
  480. 3 0.5207 6.2847
  481. 4 0.5665 3.7285
  482. 5 0.5991 2.8736
  483. 6 0.6242 2.4526
  484. 7 0.6444 2.2021
  485. 8 0.6612 2.0353
  486. 9 0.6755 1.9158
  487. 10 0.6878 1.8256
  488. 15 0.7321 1.5771
  489. 20 0.7605 1.4606
  490. 30 0.7964 1.3443
  491. 40 0.8192 1.2840
  492. 50 0.8353 1.2461
  493. 60 0.8476 1.2197
  494. 100 0.8780 1.1617
  495. 120 0.8875 1.1454
  496. 1000 0.9580 1.0459
  497. 10000 0.9863 1.0141
  498. 50000 0.9938 1.0062
  499. 100000 0.9956 1.0044
  500. 1000000 0.9986 1.0014
  501. ________________________________________________
  502. 2-Sided Confidence Limits For Standard Deviation
  503. ________________________________________________
  504. Confidence level (two-sided) = 0.0100000
  505. Standard Deviation = 1.0000000
  506. _____________________________________________
  507. Observations Lower Upper
  508. Limit Limit
  509. _____________________________________________
  510. 2 0.3562 159.5759
  511. 3 0.4344 14.1244
  512. 4 0.4834 6.4675
  513. 5 0.5188 4.3960
  514. 6 0.5464 3.4848
  515. 7 0.5688 2.9798
  516. 8 0.5875 2.6601
  517. 9 0.6036 2.4394
  518. 10 0.6177 2.2776
  519. 15 0.6686 1.8536
  520. 20 0.7018 1.6662
  521. 30 0.7444 1.4867
  522. 40 0.7718 1.3966
  523. 50 0.7914 1.3410
  524. 60 0.8065 1.3026
  525. 100 0.8440 1.2200
  526. 120 0.8558 1.1973
  527. 1000 0.9453 1.0609
  528. 10000 0.9821 1.0185
  529. 50000 0.9919 1.0082
  530. 100000 0.9943 1.0058
  531. 1000000 0.9982 1.0018
  532. */