• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Paul A. 2007, 2010
2 // Copyright John Maddock 2007
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 // Simple example of computing probabilities for a binomial random variable.
9 // Replication of source nag_binomial_dist (g01bjc).
10 
11 // Shows how to replace NAG C library calls by Boost Math Toolkit C++ calls.
12 // Note that the default policy does not replicate the way that NAG
13 // library calls handle 'bad' arguments, but you can define policies that do,
14 // as well as other policies that may suit your application even better.
15 // See the examples of changing default policies for details.
16 
17 #include <boost/math/distributions/binomial.hpp>
18 
19 #include <iostream>
20   using std::cout; using std::endl; using std::ios; using std::showpoint;
21 #include <iomanip>
22   using std::fixed; using std::setw;
23 
main()24 int main()
25 {
26   cout << "Using the binomial distribution to replicate a NAG library call." << endl;
27   using boost::math::binomial_distribution;
28 
29   // This replicates the computation of the examples of using nag-binomial_dist
30   // using g01bjc in section g01 Simple Calculations on Statistical Data.
31   // http://www.nag.co.uk/numeric/cl/manual/pdf/G01/g01bjc.pdf
32   // Program results section 8.3 page 3.g01bjc.3
33     //8.2. Program Data
34     //g01bjc Example Program Data
35     //4 0.50 2 : n, p, k
36     //19 0.44 13
37     //100 0.75 67
38     //2000 0.33 700
39     //8.3. Program Results
40     //g01bjc Example Program Results
41     //n p k plek pgtk peqk
42     //4 0.500 2 0.68750 0.31250 0.37500
43     //19 0.440 13 0.99138 0.00862 0.01939
44     //100 0.750 67 0.04460 0.95540 0.01700
45     //2000 0.330 700 0.97251 0.02749 0.00312
46 
47   cout.setf(ios::showpoint); // Trailing zeros to show significant decimal digits.
48   cout.precision(5); // Might calculate this from trials in distribution?
49   cout << fixed;
50   //  Binomial distribution.
51 
52   // Note  that  cdf(dist, k) is equivalent to NAG library plek probability of <= k
53   // cdf(complement(dist, k)) is equivalent to NAG library pgtk probability of > k
54   //             pdf(dist, k) is equivalent to NAG library peqk probability of == k
55 
56   cout << " n        p     k     plek     pgtk     peqk " << endl;
57   binomial_distribution<>my_dist(4, 0.5);
58   cout << setw(4) << (int)my_dist.trials() << "  " << my_dist.success_fraction()
59   << "   " << 2 << "  " << cdf(my_dist, 2) << "  "
60   << cdf(complement(my_dist, 2)) << "  " << pdf(my_dist, 2) << endl;
61 
62   binomial_distribution<>two(19, 0.440);
63   cout << setw(4) << (int)two.trials() <<  "  "  << two.success_fraction()
64     << "  " << 13 << "  " << cdf(two, 13) << "  "
65     << cdf(complement(two, 13)) << "  " << pdf(two, 13) << endl;
66 
67   binomial_distribution<>three(100, 0.750);
68   cout << setw(4) << (int)three.trials() << "  " << three.success_fraction()
69     << "  " << 67 << "  " << cdf(three, 67) << "  " << cdf(complement(three, 67))
70     << "  " << pdf(three, 67) << endl;
71   binomial_distribution<>four(2000, 0.330);
72   cout << setw(4) << (int)four.trials() <<  "  "  << four.success_fraction()
73   << " " << 700 << "  "
74     << cdf(four, 700) << "  " << cdf(complement(four, 700))
75     << "  " << pdf(four, 700) << endl;
76 
77   return 0;
78 } // int main()
79 
80 /*
81 
82 Example of using the binomial distribution to replicate a NAG library call.
83  n        p     k     plek     pgtk     peqk
84    4  0.50000   2  0.68750  0.31250  0.37500
85   19  0.44000  13  0.99138  0.00862  0.01939
86  100  0.75000  67  0.04460  0.95540  0.01700
87 2000  0.33000 700  0.97251  0.02749  0.00312
88 
89 
90  */
91 
92