• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Comparison with C, R, FORTRAN-style Free Functions</title>
5<link rel="stylesheet" href="../../../math.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Math Toolkit 2.12.0">
8<link rel="up" href="../weg.html" title="Worked Examples">
9<link rel="prev" href="find_eg/find_mean_and_sd_eg.html" title="Find mean and standard deviation example">
10<link rel="next" href="c_sharp.html" title="Using the Distributions from Within C#">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<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>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<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
28        C, R, FORTRAN-style Free Functions</a>
29</h4></div></div></div>
30<p>
31          You are probably familiar with a statistics library that has free functions,
32          for example the classic <a href="http://nag.com/numeric/CL/CLdescription.asp" target="_top">NAG
33          C library</a> and matching <a href="http://nag.com/numeric/FL/FLdescription.asp" target="_top">NAG
34          FORTRAN Library</a>, <a href="http://office.microsoft.com/en-us/excel/HP052090051033.aspx" target="_top">Microsoft
35          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
36          pbinom</a> and many others.
37        </p>
38<p>
39          If so, you may find 'Distributions as Objects' unfamiliar, if not alien.
40        </p>
41<p>
42          However, <span class="bold"><strong>do not panic</strong></span>, both definition
43          and usage are not really very different.
44        </p>
45<p>
46          A very simple example of generating the same values as the <a href="http://nag.com/numeric/CL/CLdescription.asp" target="_top">NAG
47          C library</a> for the binomial distribution follows. (If you find slightly
48          different values, the Boost C++ version, using double or better, is very
49          likely to be the more accurate. Of course, accuracy is not usually a concern
50          for most applications of this function).
51        </p>
52<p>
53          The <a href="http://www.nag.co.uk/numeric/cl/manual/pdf/G01/g01bjc.pdf" target="_top">NAG
54          function specification</a> is
55        </p>
56<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>
57<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>
58</pre>
59<p>
60          and is called
61        </p>
62<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">&amp;</span><span class="identifier">plek</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">pgtk</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">peqk</span><span class="special">,</span> <span class="identifier">NAGERR_DEFAULT</span><span class="special">);</span>
63</pre>
64<p>
65          The equivalent using this Boost C++ library is:
66        </p>
67<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>
68<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>
69</pre>
70<p>
71          and values can be output thus:
72        </p>
73<pre class="programlisting"><span class="identifier">cout</span>
74  <span class="special">&lt;&lt;</span> <span class="identifier">my_dist</span><span class="special">.</span><span class="identifier">trials</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span>             <span class="comment">// Echo the NAG input n = 4 trials.</span>
75  <span class="special">&lt;&lt;</span> <span class="identifier">my_dist</span><span class="special">.</span><span class="identifier">success_fraction</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span>   <span class="comment">// Echo the NAG input p = 0.5</span>
76  <span class="special">&lt;&lt;</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">&lt;&lt;</span> <span class="string">"  "</span>             <span class="comment">// NAG plek with k = 2</span>
77  <span class="special">&lt;&lt;</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">&lt;&lt;</span> <span class="string">"  "</span> <span class="comment">// NAG pgtk with k = 2</span>
78  <span class="special">&lt;&lt;</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">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>            <span class="comment">// NAG peqk with k = 2</span>
79</pre>
80<p>
81          <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
82          equivalent to NAG library <code class="computeroutput"><span class="identifier">plek</span></code>,
83          lower tail probability of &lt;= k
84        </p>
85<p>
86          <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
87          equivalent to NAG library <code class="computeroutput"><span class="identifier">pgtk</span></code>,
88          upper tail probability of &gt; k
89        </p>
90<p>
91          <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
92          equivalent to NAG library <code class="computeroutput"><span class="identifier">peqk</span></code>,
93          point probability of == k
94        </p>
95<p>
96          See <a href="../../../../../example/binomial_example_nag.cpp" target="_top">binomial_example_nag.cpp</a>
97          for details.
98        </p>
99</div>
100<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
101<td align="left"></td>
102<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
103      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
104      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
105      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
106      Daryle Walker and Xiaogang Zhang<p>
107        Distributed under the Boost Software License, Version 1.0. (See accompanying
108        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>)
109      </p>
110</div></td>
111</tr></table>
112<hr>
113<div class="spirit-nav">
114<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>
115</div>
116</body>
117</html>
118