Home Libraries People FAQ More

##### 計算負二項分佈的出現頻率的置信界限(Calculating Confidence Limits on the Frequency of Occurrence for the Negative Binomial Distribution)

`negative_binomial_distribution<>::find_lower_bound_on_p` and `negative_binomial_distribution<>::find_upper_bound_on_p`

```#include <boost/math/distributions/negative_binomial.hpp>
using boost::math::negative_binomial;

#include <iostream>
using std::cout; using std::endl;
#include <iomanip>
using std::setprecision;
using std::setw; using std::left; using std::fixed; using std::right;```

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

```void confidence_limits_on_frequency(unsigned trials, unsigned successes)
{
// trials = 總的試驗次數.
// successes = 總的成功觀測次數.
// failures = trials - successes.
// success_fraction = successes /trials.
// 打印基本信息:
cout <<
"______________________________________________\n"
"2-Sided Confidence Limits For Success Fraction\n"
"______________________________________________\n\n";
cout << setprecision(7);
cout << setw(40) << left << "Number of trials" << " =  " << trials << "\n";
cout << setw(40) << left << "Number of successes" << " =  " << successes << "\n";
cout << setw(40) << left << "Number of failures" << " =  " << trials - successes << "\n";
cout << setw(40) << left << "Observed frequency of occurrence" << " =  " << double(successes) / trials << "\n";

// 打印表頭:
cout << "\n\n"
"___________________________________________\n"
"Confidence        Lower          Upper\n"
" Value (%)        Limit          Limit\n"
"___________________________________________\n";```

```   // 現在為alpha值表打印上界和下界(upper and lower limit).
for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
{
// Confidence value:
cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
// 計算範圍:
double lower = negative_binomial::find_lower_bound_on_p(trials, successes, alpha[i]/2);
double upper = negative_binomial::find_upper_bound_on_p(trials, successes, alpha[i]/2);
// 打印界限值:
cout << fixed << setprecision(5) << setw(15) << right << lower;
cout << fixed << setprecision(5) << setw(15) << right << upper << endl;
}
cout << endl;
} // void confidence_limits_on_frequency(unsigned trials, unsigned successes)```

```int main()
{
confidence_limits_on_frequency(20, 2); // 20 次試驗， 2 次成功， 2 in 20, = 1 in 10 = 0.1 success fraction.
confidence_limits_on_frequency(200, 20); // 更多的試驗次數，但是成功分數(success fraction)仍為 0.1 .
confidence_limits_on_frequency(2000, 200); // 更多的試驗次數，但是成功分數(success fraction)仍為 0.1.

return 0;
} // int main()

```

```______________________________________________
2-Sided Confidence Limits For Success Fraction
______________________________________________
Number of trials                         =  20
Number of successes                      =  2
Number of failures                       =  18
Observed frequency of occurrence         =  0.1
___________________________________________
Confidence        Lower          Upper
Value (%)        Limit          Limit
___________________________________________
50.000        0.04812        0.13554
75.000        0.03078        0.17727
90.000        0.01807        0.22637
95.000        0.01235        0.26028
99.000        0.00530        0.33111
99.900        0.00164        0.41802
99.990        0.00051        0.49202
99.999        0.00016        0.55574
```

```______________________________________________
2-Sided Confidence Limits For Success Fraction
______________________________________________
Number of trials                         =  2000
Number of successes                      =  200
Number of failures                       =  1800
Observed frequency of occurrence         =  0.1
___________________________________________
Confidence        Lower          Upper
Value (%)        Limit          Limit
___________________________________________
50.000        0.09536        0.10445
75.000        0.09228        0.10776
90.000        0.08916        0.11125
95.000        0.08720        0.11352
99.000        0.08344        0.11802
99.900        0.07921        0.12336
99.990        0.07577        0.12795
99.999        0.07282        0.13206
```