|
Boost-Commit : |
From: pbristow_at_[hidden]
Date: 2007-08-05 10:06:59
Author: pbristow
Date: 2007-08-05 10:06:58 EDT (Sun, 05 Aug 2007)
New Revision: 38450
URL: http://svn.boost.org/trac/boost/changeset/38450
Log:
update to use Quickbook in C++ system
Text files modified:
sandbox/math_toolkit/libs/math/example/Neg_binomial_confidence_limits.cpp | 130 ++++++++++++++++++++++++++-------------
1 files changed, 85 insertions(+), 45 deletions(-)
Modified: sandbox/math_toolkit/libs/math/example/Neg_binomial_confidence_limits.cpp
==============================================================================
--- sandbox/math_toolkit/libs/math/example/Neg_binomial_confidence_limits.cpp (original)
+++ sandbox/math_toolkit/libs/math/example/Neg_binomial_confidence_limits.cpp 2007-08-05 10:06:58 EDT (Sun, 05 Aug 2007)
@@ -1,3 +1,5 @@
+// neg_binomial_confidence_limits.cpp
+
// Copyright John Maddock 2006
// Copyright Paul A. Bristow 2007
// Use, modification and distribution are subject to the
@@ -5,85 +7,125 @@
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
+// Caution: this file contains quickbook markup as well as code
+// and comments, don't change any of the special comment markups!
+
+//[neg_binomial_confidence_limits
+
+/*`
+
+First we need some includes to access the negative binomial distribution
+(and some basic std output of course).
+
+*/
+
#include <boost/math/distributions/negative_binomial.hpp>
+using boost::math::negative_binomial;
#include <iostream>
-using std::cout;
-using std::endl;
+using std::cout; using std::endl;
#include <iomanip>
+using std::setprecision;
+using std::setw; using std::left; using std::fixed; using std::right;
+
+/*`
+First define a table of significance levels: these are the
+probabilities that the true occurrence frequency lies outside the calculated
+interval:
+*/
+
+ double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
+
+/*`
+
+Confidence value as % is (1 - alpha) * 100, so alpha 0.05 == 95% confidence
+that the true occurence frequency lies *inside* the calculated interval.
+
+We need a function to calculate and print confidence limits
+for an observed frequency of occurrence
+that follows a negative binomial distribution.
+
+*/
void confidence_limits_on_frequency(unsigned trials, unsigned successes)
{
// trials = Total number of trials.
// successes = Total number of observed successes.
// failures = trials - successes.
- //
- // Calculate confidence limits for an observed
- // frequency of occurrence that follows a negative binomial distribution.
-
- using namespace std;
- using namespace boost::math;
-
+ // success_fraction = successes /trials.
// Print out general info:
cout <<
- "___________________________________________\n"
- "2-Sided Confidence Limits For Success Ratio\n"
- "___________________________________________\n\n";
+ "______________________________________________\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 probability of occurrence" << " = " << double(successes) / trials << "\n";
-
- // Define a table of significance levels:
- double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
+ cout << setw(40) << left << "Observed frequency of occurrence" << " = " << double(successes) / trials << "\n";
// Print table header:
-
cout << "\n\n"
"___________________________________________\n"
"Confidence Lower Upper\n"
" Value (%) Limit Limit\n"
"___________________________________________\n";
- // Now print out the data for the alpha table rows.
+
+/*`
+And now for the important part - the bounds themselves.
+For each value of /alpha/, we call `find_lower_bound_on_p` and
+`find_upper_bound_on_p` to obtain lower and upper bounds respectively.
+Note that since we are calculating a two-sided interval,
+we must divide the value of alpha in two. Had we been calculating a
+single-sided interval, for example: ['"Calculate a lower bound so that we are P%
+sure that the true occurrence frequency is greater than some value"]
+then we would *not* have divided by two.
+
+*/
+
+ // Now print out the upper and lower limits for the alpha table values.
for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
{
// Confidence value:
cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
- // calculate bounds:
- double l = negative_binomial::find_lower_bound_on_p(trials, successes, alpha[i]/2);
- double u = negative_binomial::find_upper_bound_on_p(trials, successes, alpha[i]/2);
- // Print Limits:
- cout << fixed << setprecision(5) << setw(15) << right << l;
- cout << fixed << setprecision(5) << setw(15) << right << u << endl;
+ // Calculate bounds:
+ 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);
+ // Print limits:
+ 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)
+/*`
+And then call confidence_limits_on_frequency with increasing numbers of trials,
+but always the same success fraction 0.1, or 1 in 10.
+
+*/
int main()
{
- confidence_limits_on_frequency(20, 2);
- confidence_limits_on_frequency(200, 20);
- confidence_limits_on_frequency(2000, 200);
+ confidence_limits_on_frequency(20, 2); // 20 trials, 2 successes, 2 in 20, = 1 in 10 = 0.1 success fraction.
+ confidence_limits_on_frequency(200, 20); // More trials, but same 0.1 success fraction.
+ confidence_limits_on_frequency(2000, 200); // Many more trials, but same 0.1 success fraction.
return 0;
} // int main()
-/*
+//] [/negative_binomial_confidence_limits_eg end of Quickbook in C++ markup]
-Output:
+/*
-Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\neg_binomial_confidence_levels.exe"
-___________________________________________
-2-Sided Confidence Limits For Success Ratio
-___________________________________________
+______________________________________________
+2-Sided Confidence Limits For Success Fraction
+______________________________________________
Number of trials = 20
Number of successes = 2
Number of failures = 18
-Observed probability of occurrence = 0.1
+Observed frequency of occurrence = 0.1
___________________________________________
Confidence Lower Upper
Value (%) Limit Limit
@@ -96,13 +138,13 @@
99.900 0.00164 0.41802
99.990 0.00051 0.49202
99.999 0.00016 0.55574
-___________________________________________
-2-Sided Confidence Limits For Success Ratio
-___________________________________________
+______________________________________________
+2-Sided Confidence Limits For Success Fraction
+______________________________________________
Number of trials = 200
Number of successes = 20
Number of failures = 180
-Observed probability of occurrence = 0.1000000
+Observed frequency of occurrence = 0.1000000
___________________________________________
Confidence Lower Upper
Value (%) Limit Limit
@@ -115,13 +157,13 @@
99.900 0.04343 0.18212
99.990 0.03641 0.20017
99.999 0.03095 0.21664
-___________________________________________
-2-Sided Confidence Limits For Success Ratio
-___________________________________________
+______________________________________________
+2-Sided Confidence Limits For Success Fraction
+______________________________________________
Number of trials = 2000
Number of successes = 200
Number of failures = 1800
-Observed probability of occurrence = 0.1000000
+Observed frequency of occurrence = 0.1000000
___________________________________________
Confidence Lower Upper
Value (%) Limit Limit
@@ -134,6 +176,4 @@
99.900 0.07921 0.12336
99.990 0.07577 0.12795
99.999 0.07282 0.13206
-*/
-
-
+*/
\ No newline at end of file
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk