File: binomial_example_nag.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (91 lines) | stat: -rw-r--r-- 3,583 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Paul A. 2007, 2010
// Copyright John Maddock 2007
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

// Simple example of computing probabilities for a binomial random variable.
// Replication of source nag_binomial_dist (g01bjc).

// Shows how to replace NAG C library calls by Boost Math Toolkit C++ calls.
// Note that the default policy does not replicate the way that NAG
// library calls handle 'bad' arguments, but you can define policies that do,
// as well as other policies that may suit your application even better.
// See the examples of changing default policies for details.

#include <boost/math/distributions/binomial.hpp>

#include <iostream>
  using std::cout; using std::endl; using std::ios; using std::showpoint;
#include <iomanip>
  using std::fixed; using std::setw;

int main()
{
  cout << "Using the binomial distribution to replicate a NAG library call." << endl;
  using boost::math::binomial_distribution;

  // This replicates the computation of the examples of using nag-binomial_dist
  // using g01bjc in section g01 Simple Calculations on Statistical Data.
  // http://www.nag.co.uk/numeric/cl/manual/pdf/G01/g01bjc.pdf
  // Program results section 8.3 page 3.g01bjc.3
    //8.2. Program Data
    //g01bjc Example Program Data
    //4 0.50 2 : n, p, k
    //19 0.44 13
    //100 0.75 67
    //2000 0.33 700
    //8.3. Program Results
    //g01bjc Example Program Results
    //n p k plek pgtk peqk
    //4 0.500 2 0.68750 0.31250 0.37500
    //19 0.440 13 0.99138 0.00862 0.01939
    //100 0.750 67 0.04460 0.95540 0.01700
    //2000 0.330 700 0.97251 0.02749 0.00312

  cout.setf(ios::showpoint); // Trailing zeros to show significant decimal digits.
  cout.precision(5); // Might calculate this from trials in distribution?
  cout << fixed;
  //  Binomial distribution.

  // Note  that  cdf(dist, k) is equivalent to NAG library plek probability of <= k
  // cdf(complement(dist, k)) is equivalent to NAG library pgtk probability of > k
  //             pdf(dist, k) is equivalent to NAG library peqk probability of == k

  cout << " n        p     k     plek     pgtk     peqk " << endl;
  binomial_distribution<>my_dist(4, 0.5);
  cout << setw(4) << (int)my_dist.trials() << "  " << my_dist.success_fraction()
  << "   " << 2 << "  " << cdf(my_dist, 2) << "  "
  << cdf(complement(my_dist, 2)) << "  " << pdf(my_dist, 2) << endl;

  binomial_distribution<>two(19, 0.440);
  cout << setw(4) << (int)two.trials() <<  "  "  << two.success_fraction()
    << "  " << 13 << "  " << cdf(two, 13) << "  "
    << cdf(complement(two, 13)) << "  " << pdf(two, 13) << endl;

  binomial_distribution<>three(100, 0.750);
  cout << setw(4) << (int)three.trials() << "  " << three.success_fraction()
    << "  " << 67 << "  " << cdf(three, 67) << "  " << cdf(complement(three, 67))
    << "  " << pdf(three, 67) << endl;
  binomial_distribution<>four(2000, 0.330);
  cout << setw(4) << (int)four.trials() <<  "  "  << four.success_fraction()
  << " " << 700 << "  "
    << cdf(four, 700) << "  " << cdf(complement(four, 700))
    << "  " << pdf(four, 700) << endl;

  return 0;
} // int main()

/*

Example of using the binomial distribution to replicate a NAG library call.
 n        p     k     plek     pgtk     peqk
   4  0.50000   2  0.68750  0.31250  0.37500
  19  0.44000  13  0.99138  0.00862  0.01939
 100  0.75000  67  0.04460  0.95540  0.01700
2000  0.33000 700  0.97251  0.02749  0.00312


 */