• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright John Maddock 2006.
2 // Copyright Paul A. Bristow 2007, 2010
3 
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 #ifdef _MSC_VER
10 #  pragma warning(disable: 4512) // assignment operator could not be generated.
11 #  pragma warning(disable: 4510) // default constructor could not be generated.
12 #  pragma warning(disable: 4610) // can never be instantiated - user defined constructor required.
13 #endif
14 
15 #include <iostream>
16 using std::cout; using std::endl;
17 using std::left; using std::fixed; using std::right; using std::scientific;
18 #include <iomanip>
19 using std::setw;
20 using std::setprecision;
21 
22 #include <boost/math/distributions/students_t.hpp>
23    using boost::math::students_t;
24 
25 
two_samples_t_test_equal_sd(double Sm1,double Sd1,unsigned Sn1,double Sm2,double Sd2,unsigned Sn2,double alpha)26 void two_samples_t_test_equal_sd(
27         double Sm1, // Sm1 = Sample Mean 1.
28         double Sd1,   // Sd1 = Sample Standard Deviation 1.
29         unsigned Sn1,   // Sn1 = Sample Size 1.
30         double Sm2,   // Sm2 = Sample Mean 2.
31         double Sd2,   // Sd2 = Sample Standard Deviation 2.
32         unsigned Sn2,   // Sn2 = Sample Size 2.
33         double alpha)   // alpha = Significance Level.
34 {
35    // A Students t test applied to two sets of data.
36    // We are testing the null hypothesis that the two
37    // samples have the same mean and that any difference
38    // if due to chance.
39    // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm
40    //
41    using namespace std;
42    // using namespace boost::math;
43 
44    using boost::math::students_t;
45 
46    // Print header:
47    cout <<
48       "_______________________________________________\n"
49       "Student t test for two samples (equal variances)\n"
50       "_______________________________________________\n\n";
51    cout << setprecision(5);
52    cout << setw(55) << left << "Number of Observations (Sample 1)" << "=  " << Sn1 << "\n";
53    cout << setw(55) << left << "Sample 1 Mean" << "=  " << Sm1 << "\n";
54    cout << setw(55) << left << "Sample 1 Standard Deviation" << "=  " << Sd1 << "\n";
55    cout << setw(55) << left << "Number of Observations (Sample 2)" << "=  " << Sn2 << "\n";
56    cout << setw(55) << left << "Sample 2 Mean" << "=  " << Sm2 << "\n";
57    cout << setw(55) << left << "Sample 2 Standard Deviation" << "=  " << Sd2 << "\n";
58    //
59    // Now we can calculate and output some stats:
60    //
61    // Degrees of freedom:
62    double v = Sn1 + Sn2 - 2;
63    cout << setw(55) << left << "Degrees of Freedom" << "=  " << v << "\n";
64    // Pooled variance and hence standard deviation:
65    double sp = sqrt(((Sn1-1) * Sd1 * Sd1 + (Sn2-1) * Sd2 * Sd2) / v);
66    cout << setw(55) << left << "Pooled Standard Deviation" << "=  " << sp << "\n";
67    // t-statistic:
68    double t_stat = (Sm1 - Sm2) / (sp * sqrt(1.0 / Sn1 + 1.0 / Sn2));
69    cout << setw(55) << left << "T Statistic" << "=  " << t_stat << "\n";
70    //
71    // Define our distribution, and get the probability:
72    //
73    students_t dist(v);
74    double q = cdf(complement(dist, fabs(t_stat)));
75    cout << setw(55) << left << "Probability that difference is due to chance" << "=  "
76       << setprecision(3) << scientific << 2 * q << "\n\n";
77    //
78    // Finally print out results of alternative hypothesis:
79    //
80    cout << setw(55) << left <<
81       "Results for Alternative Hypothesis and alpha" << "=  "
82       << setprecision(4) << fixed << alpha << "\n\n";
83    cout << "Alternative Hypothesis              Conclusion\n";
84    cout << "Sample 1 Mean != Sample 2 Mean       " ;
85    if(q < alpha / 2)
86       cout << "NOT REJECTED\n";
87    else
88       cout << "REJECTED\n";
89    cout << "Sample 1 Mean <  Sample 2 Mean       ";
90    if(cdf(dist, t_stat) < alpha)
91       cout << "NOT REJECTED\n";
92    else
93       cout << "REJECTED\n";
94    cout << "Sample 1 Mean >  Sample 2 Mean       ";
95    if(cdf(complement(dist, t_stat)) < alpha)
96       cout << "NOT REJECTED\n";
97    else
98       cout << "REJECTED\n";
99    cout << endl << endl;
100 }
101 
two_samples_t_test_unequal_sd(double Sm1,double Sd1,unsigned Sn1,double Sm2,double Sd2,unsigned Sn2,double alpha)102 void two_samples_t_test_unequal_sd(
103         double Sm1,   // Sm1 = Sample Mean 1.
104         double Sd1,   // Sd1 = Sample Standard Deviation 1.
105         unsigned Sn1,   // Sn1 = Sample Size 1.
106         double Sm2,   // Sm2 = Sample Mean 2.
107         double Sd2,   // Sd2 = Sample Standard Deviation 2.
108         unsigned Sn2,   // Sn2 = Sample Size 2.
109         double alpha)   // alpha = Significance Level.
110 {
111    // A Students t test applied to two sets of data.
112    // We are testing the null hypothesis that the two
113    // samples have the same mean and
114    // that any difference is due to chance.
115    // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm
116    //
117    using namespace std;
118    using boost::math::students_t;
119 
120    // Print header:
121    cout <<
122       "_________________________________________________\n"
123       "Student t test for two samples (unequal variances)\n"
124       "_________________________________________________\n\n";
125    cout << setprecision(5);
126    cout << setw(55) << left << "Number of Observations (Sample 1)" << "=  " << Sn1 << "\n";
127    cout << setw(55) << left << "Sample 1 Mean" << "=  " << Sm1 << "\n";
128    cout << setw(55) << left << "Sample 1 Standard Deviation" << "=  " << Sd1 << "\n";
129    cout << setw(55) << left << "Number of Observations (Sample 2)" << "=  " << Sn2 << "\n";
130    cout << setw(55) << left << "Sample 2 Mean" << "=  " << Sm2 << "\n";
131    cout << setw(55) << left << "Sample 2 Standard Deviation" << "=  " << Sd2 << "\n";
132    //
133    // Now we can calculate and output some stats:
134    //
135    // Degrees of freedom:
136    double v = Sd1 * Sd1 / Sn1 + Sd2 * Sd2 / Sn2;
137    v *= v;
138    double t1 = Sd1 * Sd1 / Sn1;
139    t1 *= t1;
140    t1 /=  (Sn1 - 1);
141    double t2 = Sd2 * Sd2 / Sn2;
142    t2 *= t2;
143    t2 /= (Sn2 - 1);
144    v /= (t1 + t2);
145    cout << setw(55) << left << "Degrees of Freedom" << "=  " << v << "\n";
146    // t-statistic:
147    double t_stat = (Sm1 - Sm2) / sqrt(Sd1 * Sd1 / Sn1 + Sd2 * Sd2 / Sn2);
148    cout << setw(55) << left << "T Statistic" << "=  " << t_stat << "\n";
149    //
150    // Define our distribution, and get the probability:
151    //
152    students_t dist(v);
153    double q = cdf(complement(dist, fabs(t_stat)));
154    cout << setw(55) << left << "Probability that difference is due to chance" << "=  "
155       << setprecision(3) << scientific << 2 * q << "\n\n";
156    //
157    // Finally print out results of alternative hypothesis:
158    //
159    cout << setw(55) << left <<
160       "Results for Alternative Hypothesis and alpha" << "=  "
161       << setprecision(4) << fixed << alpha << "\n\n";
162    cout << "Alternative Hypothesis              Conclusion\n";
163    cout << "Sample 1 Mean != Sample 2 Mean       " ;
164    if(q < alpha / 2)
165       cout << "NOT REJECTED\n";
166    else
167       cout << "REJECTED\n";
168    cout << "Sample 1 Mean <  Sample 2 Mean       ";
169    if(cdf(dist, t_stat) < alpha)
170       cout << "NOT REJECTED\n";
171    else
172       cout << "REJECTED\n";
173    cout << "Sample 1 Mean >  Sample 2 Mean       ";
174    if(cdf(complement(dist, t_stat)) < alpha)
175       cout << "NOT REJECTED\n";
176    else
177       cout << "REJECTED\n";
178    cout << endl << endl;
179 }
180 
main()181 int main()
182 {
183    //
184    // Run tests for Car Mileage sample data
185    // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3531.htm
186    // from the NIST website http://www.itl.nist.gov.  The data compares
187    // miles per gallon of US cars with miles per gallon of Japanese cars.
188    //
189    two_samples_t_test_equal_sd(20.14458, 6.414700, 249, 30.48101, 6.107710, 79, 0.05);
190    two_samples_t_test_unequal_sd(20.14458, 6.414700, 249, 30.48101, 6.107710, 79, 0.05);
191 
192    return 0;
193 } // int main()
194 
195 /*
196 Output is:
197 
198   _______________________________________________
199   Student t test for two samples (equal variances)
200   _______________________________________________
201 
202   Number of Observations (Sample 1)                      =  249
203   Sample 1 Mean                                          =  20.145
204   Sample 1 Standard Deviation                            =  6.4147
205   Number of Observations (Sample 2)                      =  79
206   Sample 2 Mean                                          =  30.481
207   Sample 2 Standard Deviation                            =  6.1077
208   Degrees of Freedom                                     =  326
209   Pooled Standard Deviation                              =  6.3426
210   T Statistic                                            =  -12.621
211   Probability that difference is due to chance           =  5.273e-030
212 
213   Results for Alternative Hypothesis and alpha           =  0.0500
214 
215   Alternative Hypothesis              Conclusion
216   Sample 1 Mean != Sample 2 Mean       NOT REJECTED
217   Sample 1 Mean <  Sample 2 Mean       NOT REJECTED
218   Sample 1 Mean >  Sample 2 Mean       REJECTED
219 
220 
221   _________________________________________________
222   Student t test for two samples (unequal variances)
223   _________________________________________________
224 
225   Number of Observations (Sample 1)                      =  249
226   Sample 1 Mean                                          =  20.14458
227   Sample 1 Standard Deviation                            =  6.41470
228   Number of Observations (Sample 2)                      =  79
229   Sample 2 Mean                                          =  30.48101
230   Sample 2 Standard Deviation                            =  6.10771
231   Degrees of Freedom                                     =  136.87499
232   T Statistic                                            =  -12.94627
233   Probability that difference is due to chance           =  1.571e-025
234 
235   Results for Alternative Hypothesis and alpha           =  0.0500
236 
237   Alternative Hypothesis              Conclusion
238   Sample 1 Mean != Sample 2 Mean       NOT REJECTED
239   Sample 1 Mean <  Sample 2 Mean       NOT REJECTED
240   Sample 1 Mean >  Sample 2 Mean       REJECTED
241 
242 
243 
244 */
245 
246