1 // students_t_example2.cpp
2 
3 // Copyright Paul A. Bristow 2006.
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 // Example 2 of using Student's t
10 
11 // A general guide to Student's t is at
12 // http://en.wikipedia.org/wiki/Student's_t-test
13 // (and many other elementary and advanced statistics texts).
14 // It says:
15 // The t statistic was invented by William Sealy Gosset
16 // for cheaply monitoring the quality of beer brews.
17 // "Student" was his pen name.
18 // Gosset was statistician for Guinness brewery in Dublin, Ireland,
19 // hired due to Claude Guinness's innovative policy of recruiting the
20 // best graduates from Oxford and Cambridge for applying biochemistry
21 // and statistics to Guinness's industrial processes.
22 // Gosset published the t test in Biometrika in 1908,
23 // but was forced to use a pen name by his employer who regarded the fact
24 // that they were using statistics as a trade secret.
25 // In fact, Gosset's identity was unknown not only to fellow statisticians
26 // but to his employer - the company insisted on the pseudonym
27 // so that it could turn a blind eye to the breach of its rules.
28 
29 // The Students't distribution function is described at
30 // http://en.wikipedia.org/wiki/Student%27s_t_distribution
31 
32 #include <boost/math/distributions/students_t.hpp>
33    using boost::math::students_t;  // Probability of students_t(df, t).
34 
35 #include <iostream>
36    using std::cout;
37    using std::endl;
38 #include <iomanip>
39    using std::setprecision;
40    using std::setw;
41 #include <cmath>
42    using std::sqrt;
43 
44 // This example of a one-sided test is from:
45 //
46 // from Statistics for Analytical Chemistry, 3rd ed. (1994), pp 59-60
47 // J. C. Miller and J. N. Miller, Ellis Horwood ISBN 0 13 0309907.
48 
49 // An acid-base titrimetric method has a significant indicator error and
50 // thus tends to give results with a positive systematic error (+bias).
51 // To test this an exactly 0.1 M solution of acid is used to titrate
52 // 25.00 ml of exactly 0.1 M solution of alkali,
53 // with the following results (ml):
54 
55 double reference = 25.00; // 'True' result.
56 const int values = 6; // titrations.
57 double data [values] = {25.06, 25.18, 24.87, 25.51, 25.34, 25.41};
58 
main()59 int main()
60 {
61    cout << "Example2 using Student's t function. ";
62 #if defined(__FILE__) && defined(__TIMESTAMP__) && defined(_MSC_FULL_VER)
63    cout << "  " << __FILE__ << ' ' << __TIMESTAMP__ << ' '<< _MSC_FULL_VER;
64 #endif
65    cout << endl;
66 
67    double sum = 0.;
68    for (int value = 0; value < values; value++)
69    { // Echo data and calculate mean.
70       sum += data[value];
71       cout << setw(4) << value << ' ' << setw(14) << data[value] << endl;
72    }
73    double mean = sum /static_cast<double>(values);
74    cout << "Mean = " << mean << endl; // 25.2283
75 
76    double sd = 0.;
77    for (int value = 0; value < values; value++)
78    { // Calculate standard deviation.
79       sd +=(data[value] - mean) * (data[value] - mean);
80    }
81    int degrees_of_freedom = values - 1; // Use the n-1 formula.
82    sd /= degrees_of_freedom; // == variance.
83    sd= sqrt(sd);
84    cout << "Standard deviation = " << sd<< endl; // = 0.238279
85 
86    double t = (mean - reference) * sqrt(static_cast<double>(values))/ sd; //
87    cout << "Student's t = " << t << ", with " << degrees_of_freedom << " degrees of freedom." << endl; // = 2.34725
88 
89    cout << "Probability of positive bias is " << cdf(students_t(degrees_of_freedom), t) << "."<< endl; // =  0.967108.
90    // A 1-sided test because only testing for a positive bias.
91    // If > 0.95 then greater than 1 in 20 conventional (arbitrary) requirement.
92 
93    return 0;
94 }  // int main()
95 
96 /*
97 
98 Output is:
99 
100 ------ Build started: Project: students_t_example2, Configuration: Debug Win32 ------
101 Compiling...
102 students_t_example2.cpp
103 Linking...
104 Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\students_t_example2.exe"
105 Example2 using Student's t function.   ..\..\..\..\..\..\boost-sandbox\libs\math_functions\example\students_t_example2.cpp Sat Aug 12 16:55:59 2006 140050727
106    0          25.06
107    1          25.18
108    2          24.87
109    3          25.51
110    4          25.34
111    5          25.41
112 Mean = 25.2283
113 Standard deviation = 0.238279
114 Student's t = 2.34725, with 5 degrees of freedom.
115 Probability of positive bias is 0.967108.
116 Build Time 0:03
117 Build log was saved at "file://i:\boost-06-05-03-1300\libs\math\test\Math_test\students_t_example2\Debug\BuildLog.htm"
118 students_t_example2 - 0 error(s), 0 warning(s)
119 ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
120 
121 */
122 
123 
124 
125 
126 
127