f_test.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright John Maddock 2006
  2. // Copyright Paul A. Bristow 2007, 2008, 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. #ifdef _MSC_VER
  8. # pragma warning(disable: 4512) // assignment operator could not be generated.
  9. # pragma warning(disable: 4510) // default constructor could not be generated.
  10. # pragma warning(disable: 4610) // can never be instantiated - user defined constructor required.
  11. # pragma warning(disable: 4180) // qualifier has no effect (in Fusion).
  12. #endif
  13. #include <iostream>
  14. using std::cout; using std::endl;
  15. using std::left; using std::fixed; using std::right; using std::scientific;
  16. #include <iomanip>
  17. using std::setw;
  18. using std::setprecision;
  19. #include <boost/math/distributions/fisher_f.hpp>
  20. void f_test(
  21. double sd1, // Sample 1 std deviation
  22. double sd2, // Sample 2 std deviation
  23. double N1, // Sample 1 size
  24. double N2, // Sample 2 size
  25. double alpha) // Significance level
  26. {
  27. //
  28. // An F test applied to two sets of data.
  29. // We are testing the null hypothesis that the
  30. // standard deviation of the samples is equal, and
  31. // that any variation is down to chance. We can
  32. // also test the alternative hypothesis that any
  33. // difference is not down to chance.
  34. // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda359.htm
  35. //
  36. // Avoid "using namespace boost::math;" because of potential name ambiguity.
  37. using boost::math::fisher_f;
  38. // Print header:
  39. cout <<
  40. "____________________________________\n"
  41. "F test for equal standard deviations\n"
  42. "____________________________________\n\n";
  43. cout << setprecision(5);
  44. cout << "Sample 1:\n";
  45. cout << setw(55) << left << "Number of Observations" << "= " << N1 << "\n";
  46. cout << setw(55) << left << "Sample Standard Deviation" << "= " << sd1 << "\n\n";
  47. cout << "Sample 2:\n";
  48. cout << setw(55) << left << "Number of Observations" << "= " << N2 << "\n";
  49. cout << setw(55) << left << "Sample Standard Deviation" << "= " << sd2 << "\n\n";
  50. //
  51. // Now we can calculate and output some stats:
  52. //
  53. // F-statistic:
  54. double F = (sd1 / sd2);
  55. F *= F;
  56. cout << setw(55) << left << "Test Statistic" << "= " << F << "\n\n";
  57. //
  58. // Finally define our distribution, and get the probability:
  59. //
  60. fisher_f dist(N1 - 1, N2 - 1);
  61. double p = cdf(dist, F);
  62. cout << setw(55) << left << "CDF of test statistic: " << "= "
  63. << setprecision(3) << scientific << p << "\n";
  64. double ucv = quantile(complement(dist, alpha));
  65. double ucv2 = quantile(complement(dist, alpha / 2));
  66. double lcv = quantile(dist, alpha);
  67. double lcv2 = quantile(dist, alpha / 2);
  68. cout << setw(55) << left << "Upper Critical Value at alpha: " << "= "
  69. << setprecision(3) << scientific << ucv << "\n";
  70. cout << setw(55) << left << "Upper Critical Value at alpha/2: " << "= "
  71. << setprecision(3) << scientific << ucv2 << "\n";
  72. cout << setw(55) << left << "Lower Critical Value at alpha: " << "= "
  73. << setprecision(3) << scientific << lcv << "\n";
  74. cout << setw(55) << left << "Lower Critical Value at alpha/2: " << "= "
  75. << setprecision(3) << scientific << lcv2 << "\n\n";
  76. //
  77. // Finally print out results of null and alternative hypothesis:
  78. //
  79. cout << setw(55) << left <<
  80. "Results for Alternative Hypothesis and alpha" << "= "
  81. << setprecision(4) << fixed << alpha << "\n\n";
  82. cout << "Alternative Hypothesis Conclusion\n";
  83. cout << "Standard deviations are unequal (two sided test) ";
  84. if((ucv2 < F) || (lcv2 > F))
  85. cout << "NOT REJECTED\n";
  86. else
  87. cout << "REJECTED\n";
  88. cout << "Standard deviation 1 is less than standard deviation 2 ";
  89. if(lcv > F)
  90. cout << "NOT REJECTED\n";
  91. else
  92. cout << "REJECTED\n";
  93. cout << "Standard deviation 1 is greater than standard deviation 2 ";
  94. if(ucv < F)
  95. cout << "NOT REJECTED\n";
  96. else
  97. cout << "REJECTED\n";
  98. cout << endl << endl;
  99. }
  100. int main()
  101. {
  102. //
  103. // Run tests for ceramic strength data:
  104. // see http://www.itl.nist.gov/div898/handbook/eda/section4/eda42a1.htm
  105. // The data for this case study were collected by Said Jahanmir of the
  106. // NIST Ceramics Division in 1996 in connection with a NIST/industry
  107. // ceramics consortium for strength optimization of ceramic strength.
  108. //
  109. f_test(65.54909, 61.85425, 240, 240, 0.05);
  110. //
  111. // And again for the process change comparison:
  112. // see http://www.itl.nist.gov/div898/handbook/prc/section3/prc32.htm
  113. // A new procedure to assemble a device is introduced and tested for
  114. // possible improvement in time of assembly. The question being addressed
  115. // is whether the standard deviation of the new assembly process (sample 2) is
  116. // better (i.e., smaller) than the standard deviation for the old assembly
  117. // process (sample 1).
  118. //
  119. f_test(4.9082, 2.5874, 11, 9, 0.05);
  120. return 0;
  121. }
  122. /*
  123. Output:
  124. f_test.cpp
  125. F-test_example1.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Debug\F_test_example1.exe
  126. ____________________________________
  127. F test for equal standard deviations
  128. ____________________________________
  129. Sample 1:
  130. Number of Observations = 240
  131. Sample Standard Deviation = 65.549
  132. Sample 2:
  133. Number of Observations = 240
  134. Sample Standard Deviation = 61.854
  135. Test Statistic = 1.123
  136. CDF of test statistic: = 8.148e-001
  137. Upper Critical Value at alpha: = 1.238e+000
  138. Upper Critical Value at alpha/2: = 1.289e+000
  139. Lower Critical Value at alpha: = 8.080e-001
  140. Lower Critical Value at alpha/2: = 7.756e-001
  141. Results for Alternative Hypothesis and alpha = 0.0500
  142. Alternative Hypothesis Conclusion
  143. Standard deviations are unequal (two sided test) REJECTED
  144. Standard deviation 1 is less than standard deviation 2 REJECTED
  145. Standard deviation 1 is greater than standard deviation 2 REJECTED
  146. ____________________________________
  147. F test for equal standard deviations
  148. ____________________________________
  149. Sample 1:
  150. Number of Observations = 11.00000
  151. Sample Standard Deviation = 4.90820
  152. Sample 2:
  153. Number of Observations = 9.00000
  154. Sample Standard Deviation = 2.58740
  155. Test Statistic = 3.59847
  156. CDF of test statistic: = 9.589e-001
  157. Upper Critical Value at alpha: = 3.347e+000
  158. Upper Critical Value at alpha/2: = 4.295e+000
  159. Lower Critical Value at alpha: = 3.256e-001
  160. Lower Critical Value at alpha/2: = 2.594e-001
  161. Results for Alternative Hypothesis and alpha = 0.0500
  162. Alternative Hypothesis Conclusion
  163. Standard deviations are unequal (two sided test) REJECTED
  164. Standard deviation 1 is less than standard deviation 2 REJECTED
  165. Standard deviation 1 is greater than standard deviation 2 NOT REJECTED
  166. ____________________________________
  167. F test for equal standard deviations
  168. ____________________________________
  169. Sample 1:
  170. Number of Observations = 240
  171. Sample Standard Deviation = 65.549
  172. Sample 2:
  173. Number of Observations = 240
  174. Sample Standard Deviation = 61.854
  175. Test Statistic = 1.123
  176. CDF of test statistic: = 8.148e-001
  177. Upper Critical Value at alpha: = 1.238e+000
  178. Upper Critical Value at alpha/2: = 1.289e+000
  179. Lower Critical Value at alpha: = 8.080e-001
  180. Lower Critical Value at alpha/2: = 7.756e-001
  181. Results for Alternative Hypothesis and alpha = 0.0500
  182. Alternative Hypothesis Conclusion
  183. Standard deviations are unequal (two sided test) REJECTED
  184. Standard deviation 1 is less than standard deviation 2 REJECTED
  185. Standard deviation 1 is greater than standard deviation 2 REJECTED
  186. ____________________________________
  187. F test for equal standard deviations
  188. ____________________________________
  189. Sample 1:
  190. Number of Observations = 11.00000
  191. Sample Standard Deviation = 4.90820
  192. Sample 2:
  193. Number of Observations = 9.00000
  194. Sample Standard Deviation = 2.58740
  195. Test Statistic = 3.59847
  196. CDF of test statistic: = 9.589e-001
  197. Upper Critical Value at alpha: = 3.347e+000
  198. Upper Critical Value at alpha/2: = 4.295e+000
  199. Lower Critical Value at alpha: = 3.256e-001
  200. Lower Critical Value at alpha/2: = 2.594e-001
  201. Results for Alternative Hypothesis and alpha = 0.0500
  202. Alternative Hypothesis Conclusion
  203. Standard deviations are unequal (two sided test) REJECTED
  204. Standard deviation 1 is less than standard deviation 2 REJECTED
  205. Standard deviation 1 is greater than standard deviation 2 NOT REJECTED
  206. */