students_t_example1.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // students_t_example1.cpp
  2. // Copyright Paul A. Bristow 2006, 2007.
  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. // Example 1 of using Student's t
  8. // http://en.wikipedia.org/wiki/Student's_t-test says:
  9. // The t statistic was invented by William Sealy Gosset
  10. // for cheaply monitoring the quality of beer brews.
  11. // "Student" was his pen name.
  12. // WS Gosset was statistician for Guinness brewery in Dublin, Ireland,
  13. // hired due to Claude Guinness's innovative policy of recruiting the
  14. // best graduates from Oxford and Cambridge for applying biochemistry
  15. // and statistics to Guinness's industrial processes.
  16. // Gosset published the t test in Biometrika in 1908,
  17. // but was forced to use a pen name by his employer who regarded the fact
  18. // that they were using statistics as a trade secret.
  19. // In fact, Gosset's identity was unknown not only to fellow statisticians
  20. // but to his employer - the company insisted on the pseudonym
  21. // so that it could turn a blind eye to the breach of its rules.
  22. // Data for this example from:
  23. // P.K.Hou, O. W. Lau & M.C. Wong, Analyst (1983) vol. 108, p 64.
  24. // from Statistics for Analytical Chemistry, 3rd ed. (1994), pp 54-55
  25. // J. C. Miller and J. N. Miller, Ellis Horwood ISBN 0 13 0309907
  26. // Determination of mercury by cold-vapour atomic absorption,
  27. // the following values were obtained fusing a trusted
  28. // Standard Reference Material containing 38.9% mercury,
  29. // which we assume is correct or 'true'.
  30. double standard = 38.9;
  31. const int values = 3;
  32. double value[values] = {38.9, 37.4, 37.1};
  33. // Is there any evidence for systematic error?
  34. // The Students't distribution function is described at
  35. // http://en.wikipedia.org/wiki/Student%27s_t_distribution
  36. #include <boost/math/distributions/students_t.hpp>
  37. using boost::math::students_t; // Probability of students_t(df, t).
  38. #include <iostream>
  39. using std::cout; using std::endl;
  40. #include <iomanip>
  41. using std::setprecision;
  42. #include <cmath>
  43. using std::sqrt;
  44. int main()
  45. {
  46. cout << "Example 1 using Student's t function. " << endl;
  47. // Example/test using tabulated value
  48. // (deliberately coded as naively as possible).
  49. // Null hypothesis is that there is no difference (greater or less)
  50. // between measured and standard.
  51. double degrees_of_freedom = values-1; // 3-1 = 2
  52. cout << "Measurement 1 = " << value[0] << ", measurement 2 = " << value[1] << ", measurement 3 = " << value[2] << endl;
  53. double mean = (value[0] + value[1] + value[2]) / static_cast<double>(values);
  54. cout << "Standard = " << standard << ", mean = " << mean << ", (mean - standard) = " << mean - standard << endl;
  55. double sd = sqrt(((value[0] - mean) * (value[0] - mean) + (value[1] - mean) * (value[1] - mean) + (value[2] - mean) * (value[2] - mean))/ static_cast<double>(values-1));
  56. cout << "Standard deviation = " << sd << endl;
  57. if (sd == 0.)
  58. {
  59. cout << "Measured mean is identical to SRM value," << endl;
  60. cout << "so probability of no difference between measured and standard (the 'null hypothesis') is unity." << endl;
  61. return 0;
  62. }
  63. double t = (mean - standard) * std::sqrt(static_cast<double>(values)) / sd;
  64. cout << "Student's t = " << t << endl;
  65. cout.precision(2); // Useful accuracy is only a few decimal digits.
  66. cout << "Probability of Student's t is " << cdf(students_t(degrees_of_freedom), std::abs(t)) << endl;
  67. // 0.91, is 1 tailed.
  68. // So there is insufficient evidence of a difference to meet a 95% (1 in 20) criterion.
  69. return 0;
  70. } // int main()
  71. /*
  72. Output is:
  73. Example 1 using Student's t function.
  74. Measurement 1 = 38.9, measurement 2 = 37.4, measurement 3 = 37.1
  75. Standard = 38.9, mean = 37.8, (mean - standard) = -1.1
  76. Standard deviation = 0.964365
  77. Student's t = -1.97566
  78. Probability of Student's t is 0.91
  79. */