Home Libraries People FAQ More

##### F分佈(F Distribution)實例

```void f_test(
double sd1,     // 樣本 1 標準差
double sd2,     // 樣本 1 標準差
double N1,      // 樣本 1 大小
double N2,      // 樣本 2 大小
double alpha)  // 顯著性水平(Significance level)
{
```

```using namespace std;
using namespace boost::math;

// 打印消息頭部:
cout <<
"____________________________________\n"
"F test for equal standard deviations\n"
"____________________________________\n\n";
cout << setprecision(5);
cout << "Sample 1:\n";
cout << setw(55) << left << "Number of Observations" << "=  " << N1 << "\n";
cout << setw(55) << left << "Sample Standard Deviation" << "=  " << sd1 << "\n\n";
cout << "Sample 2:\n";
cout << setw(55) << left << "Number of Observations" << "=  " << N2 << "\n";
cout << setw(55) << left << "Sample Standard Deviation" << "=  " << sd2 << "\n\n";
```

F測試(F-test)的統計檢驗量(test statistic) 只是這兩個標準差(standard deviations)的平方的比：

F = s12 / s22

```double F = (sd1 / sd2);
F *= F;
cout << setw(55) << left << "Test Statistic" << "=  " << F << "\n\n";
```

F(1-alpha; N1-1, N2-1) = ```quantile(fisher_f(N1-1, N2-1), alpha)```

F(alpha; N1-1, N2-1) = ```quantile(complement(fisher_f(N1-1, N2-1), alpha))```

```double ucv = quantile(complement(dist, alpha));
double ucv2 = quantile(complement(dist, alpha / 2));
double lcv = quantile(dist, alpha);
double lcv2 = quantile(dist, alpha / 2);
cout << setw(55) << left << "Upper Critical Value at alpha: " << "=  "
<< setprecision(3) << scientific << ucv << "\n";
cout << setw(55) << left << "Upper Critical Value at alpha/2: " << "=  "
<< setprecision(3) << scientific << ucv2 << "\n";
cout << setw(55) << left << "Lower Critical Value at alpha: " << "=  "
<< setprecision(3) << scientific << lcv << "\n";
cout << setw(55) << left << "Lower Critical Value at alpha/2: " << "=  "
<< setprecision(3) << scientific << lcv2 << "\n\n";
```

```cout << setw(55) << left <<
"Results for Alternative Hypothesis and alpha" << "=  "
<< setprecision(4) << fixed << alpha << "\n\n";
cout << "Alternative Hypothesis                                    Conclusion\n";

cout << "Standard deviations are unequal (two sided test)          ";
if((ucv2 < F) || (lcv2 > F))
cout << "ACCEPTED\n";
else
cout << "REJECTED\n";

cout << "Standard deviation 1 is less than standard deviation 2    ";
if(lcv > F)
cout << "ACCEPTED\n";
else
cout << "REJECTED\n";

cout << "Standard deviation 1 is greater than standard deviation 2 ";
if(ucv < F)
cout << "ACCEPTED\n";
else
cout << "REJECTED\n";
cout << endl << endl;
```

```F test for equal standard deviations
____________________________________

Sample 1:
Number of Observations                                 =  240
Sample Standard Deviation                              =  65.549

Sample 2:
Number of Observations                                 =  240
Sample Standard Deviation                              =  61.854

Test Statistic                                         =  1.123

CDF of test statistic:                                 =  8.148e-001
Upper Critical Value at alpha:                         =  1.238e+000
Upper Critical Value at alpha/2:                       =  1.289e+000
Lower Critical Value at alpha:                         =  8.080e-001
Lower Critical Value at alpha/2:                       =  7.756e-001

Results for Alternative Hypothesis and alpha           =  0.0500

Alternative Hypothesis                                    Conclusion
Standard deviations are unequal (two sided test)          REJECTED
Standard deviation 1 is less than standard deviation 2    REJECTED
Standard deviation 1 is greater than standard deviation 2 REJECTED
```

```____________________________________
F test for equal standard deviations
____________________________________

Sample 1:
Number of Observations                                 =  11.00000
Sample Standard Deviation                              =  4.90820

Sample 2:
Number of Observations                                 =  9.00000
Sample Standard Deviation                              =  2.58740

Test Statistic                                         =  3.59847

CDF of test statistic:                                 =  9.589e-001
Upper Critical Value at alpha:                         =  3.347e+000
Upper Critical Value at alpha/2:                       =  4.295e+000
Lower Critical Value at alpha:                         =  3.256e-001
Lower Critical Value at alpha/2:                       =  2.594e-001

Results for Alternative Hypothesis and alpha           =  0.0500

Alternative Hypothesis                                    Conclusion
Standard deviations are unequal (two sided test)          REJECTED
Standard deviation 1 is less than standard deviation 2    REJECTED
Standard deviation 1 is greater than standard deviation 2 ACCEPTED
```

 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)