Home Libraries People FAQ More

##### 使用學生t分佈計算均值的置信區間(Calculating confidence intervals on the mean with the Students-t distribution)

注意 α 的值是錯誤地否決虛假設的最大可接受風險值。α的值越小，測試的強度就越大。 測試的置信水平定義為 1 - α，且通常表示為百分數。例如顯著性水平為0.05等價於 95% 的置信水平。參考在NIST/SEMATECH e-Handbook of Statistical Methods. 中的 "置信區間是什麼?" 瞭解更多信息。
注意 通常的假設獨立地且一致地分佈(independent and identically distributed) (i.i.d.) 的變量 以及 正態分佈(normal distribution) 當然也應用於這裡，就像它們應用在其它例子中那樣。

• 隨著樣本大小的增大，置信區間寬度變小。
• 隨著標準差增大，置信區間的寬度增大。
• 隨著置信水平增大，置信區間的寬度增大 (0.5 到 0.99999 - stronger).
• 隨著顯著性水平減小，置信區間的寬度增大 (0.5 towards 0.00000...01 - stronger).

```// 需要的頭文件:
#include <boost/math/distributions/students_t.hpp>
#include <iostream>
#include <iomanip>
// 為了方便使用，將所有的名字引入全局作用域:
using namespace boost::math;
using namespace std;

void confidence_limits_on_mean(
double Sm,           // Sm = Sample Mean.
double Sd,           // Sd = Sample Standard Deviation.
unsigned Sn)         // Sn = Sample Size.
{
using namespace std;
using namespace boost::math;

// Print out general info:
cout <<
"__________________________________\n"
"2-Sided Confidence Limits For Mean\n"
"__________________________________\n\n";
cout << setprecision(7);
cout << setw(40) << left << "Number of Observations" << "=  " << Sn << "\n";
cout << setw(40) << left << "Mean" << "=  " << Sm << "\n";
cout << setw(40) << left << "Standard Deviation" << "=  " << Sd << "\n";
```

```double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
```

```students_t dist(Sn - 1);
```

```double T = quantile(complement(dist, alpha[i] / 2));
```

```double w = T * Sd / sqrt(double(Sn));
```

```   __________________________________
2-Sided Confidence Limits For Mean
__________________________________

Number of Observations                  =  195
Mean                                    =  9.26146
Standard Deviation                      =  0.02278881

___________________________________________________________________
Confidence       T           Interval          Lower          Upper
Value (%)     Value          Width            Limit          Limit
___________________________________________________________________
50.000     0.676       1.103e-003        9.26036        9.26256
75.000     1.154       1.883e-003        9.25958        9.26334
90.000     1.653       2.697e-003        9.25876        9.26416
95.000     1.972       3.219e-003        9.25824        9.26468
99.000     2.601       4.245e-003        9.25721        9.26571
99.900     3.341       5.453e-003        9.25601        9.26691
99.990     3.973       6.484e-003        9.25498        9.26794
99.999     4.537       7.404e-003        9.25406        9.26886
```

```   __________________________________
2-Sided Confidence Limits For Mean
__________________________________

Number of Observations                  =  3
Mean                                    =  37.8000000
Standard Deviation                      =  0.9643650

___________________________________________________________________
Confidence       T           Interval          Lower          Upper
Value (%)     Value          Width            Limit          Limit
___________________________________________________________________
50.000     0.816            0.455       37.34539       38.25461
75.000     1.604            0.893       36.90717       38.69283
90.000     2.920            1.626       36.17422       39.42578
95.000     4.303            2.396       35.40438       40.19562
99.000     9.925            5.526       32.27408       43.32592
99.900    31.599           17.594       20.20639       55.39361
99.990    99.992           55.673      -17.87346       93.47346
99.999   316.225          176.067     -138.26683      213.86683
```