Home Libraries People FAQ More

##### 測試樣本均值與真實均值的差(Testing a sample mean for difference from a "true" mean)

注意 非統計學家可能會說 '不否決'意味著 '接受' (一般對於虛假設( null-hypothesis)) ，錯誤地認為這沒有差別。但統計學家避開這一點來迴避錯誤地暗示這是一個沒有差別的積極證據。「不否決」在這裡關這裡沒有存在差別的證據，但是仍然可能可能存在差別。例如，參考argument from ignorance 以及 Absence of evidence does not constitute evidence of absence.
```// 需要的頭文件:
#include <boost/math/distributions/students_t.hpp>
#include <iostream>
#include <iomanip>
// Bring everything into global namespace for ease of use:
using namespace boost::math;
using namespace std;

void single_sample_t_test(double M, double Sm, double Sd, unsigned Sn, double alpha)
{
//
// M = true mean.
// Sm = Sample Mean.
// Sd = Sample Standard Deviation.
// Sn = Sample Size.
// alpha = Significance Level.
```

```// 均值的差:
double diff = Sm - M;
// 自由度:
unsigned v = Sn - 1;
// t-statistic:
double t_stat = diff * sqrt(double(Sn)) / Sd;
```

```students_t dist(v);
double q = cdf(complement(dist, fabs(t_stat)));
```

```cdf(complement(dist, fabs(t))) < alpha / 2``` 則否決

```cdf(complement(dist, fabs(t))) > alpha / 2```

```cdf(dist, t) > alpha``` 則否決

Reject if complement of CDF of 對於：t > 顯著性水平

```cdf(complement(dist, t)) > alpha``` 則否決

注意 對於雙側測試(two-sided test)是與```alpha / 2``` 比較，對於單側測試(single-sided test)是與`alpha` 比較

```   __________________________________
Student t test for a single sample
__________________________________

Number of Observations                                 =  195
Sample Mean                                            =  9.26146
Sample Standard Deviation                              =  0.02279
Expected True Mean                                     =  5.00000

Sample Mean - Expected Test Mean                       =  4.26146
Degrees of Freedom                                     =  194
T Statistic                                            =  2611.28380
Probability that difference is due to chance           =  0.000e+000

Results for Alternative Hypothesis and alpha           =  0.0500

Alternative Hypothesis     Conclusion
Mean != 5.000              NOT REJECTED
Mean  < 5.000              REJECTED
Mean  > 5.000              NOT REJECTED
```

```   __________________________________
Student t test for a single sample
__________________________________

Number of Observations                                 =  3
Sample Mean                                            =  37.80000
Sample Standard Deviation                              =  0.96437
Expected True Mean                                     =  38.90000

Sample Mean - Expected Test Mean                       =  -1.10000
Degrees of Freedom                                     =  2
T Statistic                                            =  -1.97566
Probability that difference is due to chance           =  1.869e-001

Results for Alternative Hypothesis and alpha           =  0.0500

Alternative Hypothesis     Conclusion
Mean != 38.900             REJECTED
Mean  < 38.900             REJECTED
Mean  > 38.900             REJECTED
```

```__________________________________
Student t test for a single sample
__________________________________

Number of Observations                                 =  3
Sample Mean                                            =  37.80000
Sample Standard Deviation                              =  0.96437
Expected True Mean                                     =  38.90000

Sample Mean - Expected Test Mean                       =  -1.10000
Degrees of Freedom                                     =  2
T Statistic                                            =  -1.97566
Probability that difference is due to chance           =  1.869e-001

Results for Alternative Hypothesis and alpha           =  0.1000

Alternative Hypothesis     Conclusion
Mean != 38.900            REJECTED
Mean  < 38.900            NOT REJECTED
Mean  > 38.900            REJECTED
```

 Copyright ?2006 , 2007, 2008 John Maddock, Paul A. Bristow, Hubert Holin, Xiaogang Zhang and Bruno Lalande Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)