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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Comparison with C, R, FORTRAN-style Free Functions</title>
<link rel="stylesheet" href="../../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../../../index.html" title="Math Toolkit 2.1.0">
<link rel="up" href="../weg.html" title="Worked Examples">
<link rel="prev" href="find_eg/find_mean_and_sd_eg.html" title="Find mean and standard deviation example">
<link rel="next" href="c_sharp.html" title="Using the Distributions from Within C#">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="find_eg/find_mean_and_sd_eg.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../weg.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="c_sharp.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="math_toolkit.stat_tut.weg.nag_library"></a><a class="link" href="nag_library.html" title="Comparison with C, R, FORTRAN-style Free Functions">Comparison with
C, R, FORTRAN-style Free Functions</a>
</h4></div></div></div>
<p>
You are probably familiar with a statistics library that has free functions,
for example the classic <a href="http://nag.com/numeric/CL/CLdescription.asp" target="_top">NAG
C library</a> and matching <a href="http://nag.com/numeric/FL/FLdescription.asp" target="_top">NAG
FORTRAN Library</a>, <a href="http://office.microsoft.com/en-us/excel/HP052090051033.aspx" target="_top">Microsoft
Excel BINOMDIST(number_s,trials,probability_s,cumulative)</a>, <a href="http://www.r-project.org/" target="_top">R</a>, <a href="http://www.ptc.com/products/mathcad/mathcad14/mathcad_func_chart.htm" target="_top">MathCAD
pbinom</a> and many others.
</p>
<p>
If so, you may find 'Distributions as Objects' unfamiliar, if not alien.
</p>
<p>
However, <span class="bold"><strong>do not panic</strong></span>, both definition
and usage are not really very different.
</p>
<p>
A very simple example of generating the same values as the <a href="http://nag.com/numeric/CL/CLdescription.asp" target="_top">NAG
C library</a> for the binomial distribution follows. (If you find slightly
different values, the Boost C++ version, using double or better, is very
likely to be the more accurate. Of course, accuracy is not usually a concern
for most applications of this function).
</p>
<p>
The <a href="http://www.nag.co.uk/numeric/cl/manual/pdf/G01/g01bjc.pdf" target="_top">NAG
function specification</a> is
</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">nag_binomial_dist</span><span class="special">(</span><span class="identifier">Integer</span> <span class="identifier">n</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">Integer</span> <span class="identifier">k</span><span class="special">,</span>
<span class="keyword">double</span> <span class="special">*</span><span class="identifier">plek</span><span class="special">,</span> <span class="keyword">double</span> <span class="special">*</span><span class="identifier">pgtk</span><span class="special">,</span> <span class="keyword">double</span> <span class="special">*</span><span class="identifier">peqk</span><span class="special">,</span> <span class="identifier">NagError</span> <span class="special">*</span><span class="identifier">fail</span><span class="special">)</span>
</pre>
<p>
and is called
</p>
<pre class="programlisting"><span class="identifier">g01bjc</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">k</span><span class="special">,</span> <span class="special">&</span><span class="identifier">plek</span><span class="special">,</span> <span class="special">&</span><span class="identifier">pgtk</span><span class="special">,</span> <span class="special">&</span><span class="identifier">peqk</span><span class="special">,</span> <span class="identifier">NAGERR_DEFAULT</span><span class="special">);</span>
</pre>
<p>
The equivalent using this Boost C++ library is:
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">;</span> <span class="comment">// Using declaration avoids very long names.</span>
<span class="identifier">binomial</span> <span class="identifier">my_dist</span><span class="special">(</span><span class="number">4</span><span class="special">,</span> <span class="number">0.5</span><span class="special">);</span> <span class="comment">// c.f. NAG n = 4, p = 0.5</span>
</pre>
<p>
and values can be output thus:
</p>
<pre class="programlisting"><span class="identifier">cout</span>
<span class="special"><<</span> <span class="identifier">my_dist</span><span class="special">.</span><span class="identifier">trials</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" "</span> <span class="comment">// Echo the NAG input n = 4 trials.</span>
<span class="special"><<</span> <span class="identifier">my_dist</span><span class="special">.</span><span class="identifier">success_fraction</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" "</span> <span class="comment">// Echo the NAG input p = 0.5</span>
<span class="special"><<</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">my_dist</span><span class="special">,</span> <span class="number">2</span><span class="special">)</span> <span class="special"><<</span> <span class="string">" "</span> <span class="comment">// NAG plek with k = 2</span>
<span class="special"><<</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">my_dist</span><span class="special">,</span> <span class="number">2</span><span class="special">))</span> <span class="special"><<</span> <span class="string">" "</span> <span class="comment">// NAG pgtk with k = 2</span>
<span class="special"><<</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">my_dist</span><span class="special">,</span> <span class="number">2</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> <span class="comment">// NAG peqk with k = 2</span>
</pre>
<p>
<code class="computeroutput"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">k</span><span class="special">)</span></code> is
equivalent to NAG library <code class="computeroutput"><span class="identifier">plek</span></code>,
lower tail probability of <= k
</p>
<p>
<code class="computeroutput"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">k</span><span class="special">))</span></code> is
equivalent to NAG library <code class="computeroutput"><span class="identifier">pgtk</span></code>,
upper tail probability of > k
</p>
<p>
<code class="computeroutput"><span class="identifier">pdf</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">k</span><span class="special">)</span></code> is
equivalent to NAG library <code class="computeroutput"><span class="identifier">peqk</span></code>,
point probability of == k
</p>
<p>
See <a href="../../../../../example/binomial_example_nag.cpp" target="_top">binomial_example_nag.cpp</a>
for details.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal,
Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
Holin, Bruno Lalande, John Maddock, Johan Råde, Gautam Sewani, Benjamin Sobotta,
Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="find_eg/find_mean_and_sd_eg.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../weg.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="c_sharp.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|