Home Libraries People FAQ More

##### 比較學生t測試兩個樣本的均值(Comparing the means of two samples with the Students-t test)

// 需要的頭文件:
#include <boost/math/distributions/students_t.hpp>
#include <iostream>
#include <iomanip>
// 簡化使用:
using namespace boost::math;
using namespace std;

void two_samples_t_test_equal_sd(
double Sm1,       // Sm1 = Sample 1 Mean.
double Sd1,       // Sd1 = Sample 1 Standard Deviation.
unsigned Sn1,     // Sn1 = Sample 1 Size.
double Sm2,       // Sm2 = Sample 2 Mean.
double Sd2,       // Sd2 = Sample 2 Standard Deviation.
unsigned Sn2,     // Sn2 = Sample 2 Size.
double alpha)     // alpha = Significance Level.
{

// 自由度:
double v = Sn1 + Sn2 - 2;
cout << setw(55) << left << "Degrees of Freedom" << "=  " << v << "\n";
// 合併的方差:
double sp = sqrt(((Sn1-1) * Sd1 * Sd1 + (Sn2-1) * Sd2 * Sd2) / v);
cout << setw(55) << left << "Pooled Standard Deviation" << "=  " << v << "\n";
// t-statistic:
double t_stat = (Sm1 - Sm2) / (sp * sqrt(1.0 / Sn1 + 1.0 / Sn2));
cout << setw(55) << left << "T Statistic" << "=  " << t_stat << "\n";

students_t dist(v);
double q = cdf(complement(dist, fabs(t_stat)));
cout << setw(55) << left << "Probability that difference is due to chance" << "=  "
<< setprecision(3) << scientific << 2 * q << "\n\n";

注意 對於一個雙側測試( two-sided test)，我們必須比較 alpha / 2 而不是 alpha。

________________________________________________
Student t test for two samples (equal variances)
________________________________________________

Number of Observations (Sample 1)                      =  249
Sample 1 Mean                                          =  20.14458
Sample 1 Standard Deviation                            =  6.41470
Number of Observations (Sample 2)                      =  79
Sample 2 Mean                                          =  30.48101
Sample 2 Standard Deviation                            =  6.10771
Degrees of Freedom                                     =  326.00000
Pooled Standard Deviation                              =  326.00000
T Statistic                                            =  -12.62059
Probability that difference is due to chance           =  5.273e-030

Results for Alternative Hypothesis and alpha           =  0.0500

Alternative Hypothesis              Conclusion
Sample 1 Mean != Sample 2 Mean       NOT REJECTED
Sample 1 Mean <  Sample 2 Mean       NOT REJECTED
Sample 1 Mean >  Sample 2 Mean       REJECTED

注意 一些統計類庫將這個有效的自由度截斷為一個整數自由度：如果你依賴於查找表，這可能是必需的，但是因為我們的代碼完全支持非整數自由度，因此在這種情況下沒有必要將浮點數截斷。同樣也要注意當自由度小於 Welch-Satterthwaite 逼近值時，這可能是一個重要的錯誤源。

// 自由度:
double v = Sd1 * Sd1 / Sn1 + Sd2 * Sd2 / Sn2;
v *= v;
double t1 = Sd1 * Sd1 / Sn1;
t1 *= t1;
t1 /=  (Sn1 - 1);
double t2 = Sd2 * Sd2 / Sn2;
t2 *= t2;
t2 /= (Sn2 - 1);
v /= (t1 + t2);
cout << setw(55) << left << "Degrees of Freedom" << "=  " << v << "\n";
// t-statistic:
double t_stat = (Sm1 - Sm2) / sqrt(Sd1 * Sd1 / Sn1 + Sd2 * Sd2 / Sn2);
cout << setw(55) << left << "T Statistic" << "=  " << t_stat << "\n";

__________________________________________________
Student t test for two samples (unequal variances)
__________________________________________________

Number of Observations (Sample 1)                      =  249
Sample 1 Mean                                          =  20.145
Sample 1 Standard Deviation                            =  6.4147
Number of Observations (Sample 2)                      =  79
Sample 2 Mean                                          =  30.481
Sample 2 Standard Deviation                            =  6.1077
Degrees of Freedom                                     =  136.87
T Statistic                                            =  -12.946
Probability that difference is due to chance           =  1.571e-025

Results for Alternative Hypothesis and alpha           =  0.0500

Alternative Hypothesis              Conclusion
Sample 1 Mean != Sample 2 Mean       NOT REJECTED
Sample 1 Mean <  Sample 2 Mean       NOT REJECTED
Sample 1 Mean >  Sample 2 Mean       REJECTED