]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/binomial_example_nag.cpp
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)
8 // Simple example of computing probabilities for a binomial random variable.
9 // Replication of source nag_binomial_dist (g01bjc).
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.
17 #include <boost/math/distributions/binomial.hpp>
20 using std::cout
; using std::endl
; using std::ios
; using std::showpoint
;
22 using std::fixed
; using std::setw
;
26 cout
<< "Using the binomial distribution to replicate a NAG library call." << endl
;
27 using boost::math::binomial_distribution
;
29 // This replicates the computation of the examples of using nag-binomial_dist
30 // using g01bjc in section g01 Somple 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
34 //g01bjc Example Program Data
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
47 cout
.setf(ios::showpoint
); // Trailing zeros to show significant decimal digits.
48 cout
.precision(5); // Might calculate this from trials in distribution?
50 // Binomial distribution.
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
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
;
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
;
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()
74 << cdf(four
, 700) << " " << cdf(complement(four
, 700))
75 << " " << pdf(four
, 700) << endl
;
82 Example of using the binomial distribution to replicate a NAG library call.
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