]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright John Maddock 2006 |
2 | // Copyright Paul A. Bristow 2010 | |
3 | ||
4 | // Use, modification and distribution are subject to the | |
5 | // Boost Software License, Version 1.0. | |
6 | // (See accompanying file LICENSE_1_0.txt | |
7 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | #ifdef _MSC_VER | |
10 | # pragma warning(disable: 4512) // assignment operator could not be generated. | |
11 | # pragma warning(disable: 4510) // default constructor could not be generated. | |
12 | # pragma warning(disable: 4610) // can never be instantiated - user defined constructor required. | |
13 | #endif | |
14 | ||
15 | #include <iostream> | |
16 | using std::cout; using std::endl; | |
17 | #include <iomanip> | |
18 | using std::fixed; using std::left; using std::right; using std::right; using std::setw; | |
19 | using std::setprecision; | |
20 | ||
21 | #include <boost/math/distributions/binomial.hpp> | |
22 | ||
23 | void confidence_limits_on_frequency(unsigned trials, unsigned successes) | |
24 | { | |
25 | // | |
26 | // trials = Total number of trials. | |
27 | // successes = Total number of observed successes. | |
28 | // | |
29 | // Calculate confidence limits for an observed | |
30 | // frequency of occurrence that follows a binomial distribution. | |
31 | // | |
32 | //using namespace std; // Avoid | |
33 | // using namespace boost::math; // potential name ambiguity with std <random> | |
34 | using boost::math::binomial_distribution; | |
35 | ||
36 | // Print out general info: | |
37 | cout << | |
38 | "___________________________________________\n" | |
39 | "2-Sided Confidence Limits For Success Ratio\n" | |
40 | "___________________________________________\n\n"; | |
41 | cout << setprecision(7); | |
42 | cout << setw(40) << left << "Number of Observations" << "= " << trials << "\n"; | |
43 | cout << setw(40) << left << "Number of successes" << "= " << successes << "\n"; | |
44 | cout << setw(40) << left << "Sample frequency of occurrence" << "= " << double(successes) / trials << "\n"; | |
45 | // | |
46 | // Define a table of significance levels: | |
47 | // | |
48 | double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 }; | |
49 | // | |
50 | // Print table header: | |
51 | // | |
52 | cout << "\n\n" | |
53 | "_______________________________________________________________________\n" | |
54 | "Confidence Lower CP Upper CP Lower JP Upper JP\n" | |
55 | " Value (%) Limit Limit Limit Limit\n" | |
56 | "_______________________________________________________________________\n"; | |
57 | // | |
58 | // Now print out the data for the table rows. | |
59 | // | |
60 | for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i) | |
61 | { | |
62 | // Confidence value: | |
63 | cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]); | |
64 | // Calculate Clopper Pearson bounds: | |
65 | double l = binomial_distribution<>::find_lower_bound_on_p(trials, successes, alpha[i]/2); | |
66 | double u = binomial_distribution<>::find_upper_bound_on_p(trials, successes, alpha[i]/2); | |
67 | // Print Clopper Pearson Limits: | |
68 | cout << fixed << setprecision(5) << setw(15) << right << l; | |
69 | cout << fixed << setprecision(5) << setw(15) << right << u; | |
70 | // Calculate Jeffreys Prior Bounds: | |
71 | l = binomial_distribution<>::find_lower_bound_on_p(trials, successes, alpha[i]/2, binomial_distribution<>::jeffreys_prior_interval); | |
72 | u = binomial_distribution<>::find_upper_bound_on_p(trials, successes, alpha[i]/2, binomial_distribution<>::jeffreys_prior_interval); | |
73 | // Print Jeffreys Prior Limits: | |
74 | cout << fixed << setprecision(5) << setw(15) << right << l; | |
75 | cout << fixed << setprecision(5) << setw(15) << right << u << std::endl; | |
76 | } | |
77 | cout << endl; | |
78 | } // void confidence_limits_on_frequency() | |
79 | ||
80 | int main() | |
81 | { | |
82 | confidence_limits_on_frequency(20, 4); | |
83 | confidence_limits_on_frequency(200, 40); | |
84 | confidence_limits_on_frequency(2000, 400); | |
85 | ||
86 | return 0; | |
87 | } // int main() | |
88 | ||
89 | /* | |
90 | ||
91 | ------ Build started: Project: binomial_confidence_limits, Configuration: Debug Win32 ------ | |
92 | Compiling... | |
93 | binomial_confidence_limits.cpp | |
94 | Linking... | |
95 | Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\binomial_confidence_limits.exe" | |
96 | ___________________________________________ | |
97 | 2-Sided Confidence Limits For Success Ratio | |
98 | ___________________________________________ | |
99 | ||
100 | Number of Observations = 20 | |
101 | Number of successes = 4 | |
102 | Sample frequency of occurrence = 0.2 | |
103 | ||
104 | ||
105 | _______________________________________________________________________ | |
106 | Confidence Lower CP Upper CP Lower JP Upper JP | |
107 | Value (%) Limit Limit Limit Limit | |
108 | _______________________________________________________________________ | |
109 | 50.000 0.12840 0.29588 0.14974 0.26916 | |
110 | 75.000 0.09775 0.34633 0.11653 0.31861 | |
111 | 90.000 0.07135 0.40103 0.08734 0.37274 | |
112 | 95.000 0.05733 0.43661 0.07152 0.40823 | |
113 | 99.000 0.03576 0.50661 0.04655 0.47859 | |
114 | 99.900 0.01905 0.58632 0.02634 0.55960 | |
115 | 99.990 0.01042 0.64997 0.01530 0.62495 | |
116 | 99.999 0.00577 0.70216 0.00901 0.67897 | |
117 | ||
118 | ___________________________________________ | |
119 | 2-Sided Confidence Limits For Success Ratio | |
120 | ___________________________________________ | |
121 | ||
122 | Number of Observations = 200 | |
123 | Number of successes = 40 | |
124 | Sample frequency of occurrence = 0.2000000 | |
125 | ||
126 | ||
127 | _______________________________________________________________________ | |
128 | Confidence Lower CP Upper CP Lower JP Upper JP | |
129 | Value (%) Limit Limit Limit Limit | |
130 | _______________________________________________________________________ | |
131 | 50.000 0.17949 0.22259 0.18190 0.22001 | |
132 | 75.000 0.16701 0.23693 0.16934 0.23429 | |
133 | 90.000 0.15455 0.25225 0.15681 0.24956 | |
134 | 95.000 0.14689 0.26223 0.14910 0.25951 | |
135 | 99.000 0.13257 0.28218 0.13468 0.27940 | |
136 | 99.900 0.11703 0.30601 0.11902 0.30318 | |
137 | 99.990 0.10489 0.32652 0.10677 0.32366 | |
138 | 99.999 0.09492 0.34485 0.09670 0.34197 | |
139 | ||
140 | ___________________________________________ | |
141 | 2-Sided Confidence Limits For Success Ratio | |
142 | ___________________________________________ | |
143 | ||
144 | Number of Observations = 2000 | |
145 | Number of successes = 400 | |
146 | Sample frequency of occurrence = 0.2000000 | |
147 | ||
148 | ||
149 | _______________________________________________________________________ | |
150 | Confidence Lower CP Upper CP Lower JP Upper JP | |
151 | Value (%) Limit Limit Limit Limit | |
152 | _______________________________________________________________________ | |
153 | 50.000 0.19382 0.20638 0.19406 0.20613 | |
154 | 75.000 0.18965 0.21072 0.18990 0.21047 | |
155 | 90.000 0.18537 0.21528 0.18561 0.21503 | |
156 | 95.000 0.18267 0.21821 0.18291 0.21796 | |
157 | 99.000 0.17745 0.22400 0.17769 0.22374 | |
158 | 99.900 0.17150 0.23079 0.17173 0.23053 | |
159 | 99.990 0.16658 0.23657 0.16681 0.23631 | |
160 | 99.999 0.16233 0.24169 0.16256 0.24143 | |
161 | ||
162 | */ | |
163 | ||
164 | ||
165 |