Home Libraries People FAQ More

計算二項分佈發生頻率( Frequency of Occurrence for)的置信界限( Confidence Limits)

```#include <iostream>
#include <iomanip>
#include <boost/math/distributions/binomial.hpp>

void confidence_limits_on_frequency(unsigned trials, unsigned successes)
{
//
// trials = 總的試驗次數.
// successes = 總的成功事件的次數.
//
// 計算一個遵循二項分佈的事件出現
// 頻率的置信界限
//
using namespace std;
using namespace boost::math;

// 打印基本信息:
cout <<
"___________________________________________\n"
"2-Sided Confidence Limits For Success Ratio\n"
"___________________________________________\n\n";
cout << setprecision(7);
cout << setw(40) << left << "Number of Observations" << "=  " << trials << "\n";
cout << setw(40) << left << "Number of successes" << "=  " << successes << "\n";
cout << setw(40) << left << "Sample frequency of occurrence" << "=  " << double(successes) / trials << "\n";
```

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

```cout << "\n\n"
"_______________________________________________________________________\n"
"Confidence        Lower CP       Upper CP       Lower JP       Upper JP\n"
" Value (%)        Limit          Limit          Limit          Limit\n"
"_______________________________________________________________________\n";
```

• 真值(true value)比上邊界( upper bound)大的風險(risk)也為α。

```   for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
{
// Confidence value:
cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
// 計算 Clopper Pearson 界限:
double l = binomial_distribution<>::find_lower_bound_on_p(
trials, successes, alpha[i]/2);
double u = binomial_distribution<>::find_upper_bound_on_p(
trials, successes, alpha[i]/2);
// 打印 Clopper Pearson 界限:
cout << fixed << setprecision(5) << setw(15) << right << l;
cout << fixed << setprecision(5) << setw(15) << right << u;
// 計算 Jeffreys Prior 界限:
l = binomial_distribution<>::find_lower_bound_on_p(
trials, successes, alpha[i]/2,
binomial_distribution<>::jeffreys_prior_interval);
u = binomial_distribution<>::find_upper_bound_on_p(
trials, successes, alpha[i]/2,
binomial_distribution<>::jeffreys_prior_interval);
// 打印 Jeffreys Prior 界限:
cout << fixed << setprecision(5) << setw(15) << right << l;
cout << fixed << setprecision(5) << setw(15) << right << u << std::endl;
}
cout << endl;
}
```

```___________________________________________
2-Sided Confidence Limits For Success Ratio
___________________________________________

Number of Observations                  =  20
Number of successes                     =  4
Sample frequency of occurrence          =  0.2

_______________________________________________________________________
Confidence        Lower CP       Upper CP       Lower JP       Upper JP
Value (%)        Limit          Limit          Limit          Limit
_______________________________________________________________________
50.000        0.12840        0.29588        0.14974        0.26916
75.000        0.09775        0.34633        0.11653        0.31861
90.000        0.07135        0.40103        0.08734        0.37274
95.000        0.05733        0.43661        0.07152        0.40823
99.000        0.03576        0.50661        0.04655        0.47859
99.900        0.01905        0.58632        0.02634        0.55960
99.990        0.01042        0.64997        0.01530        0.62495
99.999        0.00577        0.70216        0.00901        0.67897
```

```___________________________________________
2-Sided Confidence Limits For Success Ratio
___________________________________________

Number of Observations                  =  2000
Number of successes                     =  400
Sample frequency of occurrence          =  0.2000000

_______________________________________________________________________
Confidence        Lower CP       Upper CP       Lower JP       Upper JP
Value (%)        Limit          Limit          Limit          Limit
_______________________________________________________________________
50.000        0.19382        0.20638        0.19406        0.20613
75.000        0.18965        0.21072        0.18990        0.21047
90.000        0.18537        0.21528        0.18561        0.21503
95.000        0.18267        0.21821        0.18291        0.21796
99.000        0.17745        0.22400        0.17769        0.22374
99.900        0.17150        0.23079        0.17173        0.23053
99.990        0.16658        0.23657        0.16681        0.23631
99.999        0.16233        0.24169        0.16256        0.24143
```