]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Distributions are Objects</title> | |
5 | <link rel="stylesheet" href="../../../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../../index.html" title="Math Toolkit 2.5.1"> | |
8 | <link rel="up" href="../overview.html" title="Overview of Distributions"> | |
9 | <link rel="prev" href="headers.html" title="Headers and Namespaces"> | |
10 | <link rel="next" href="generic.html" title="Generic operations common to all distributions are non-member functions"> | |
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="headers.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="generic.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.overview.objects"></a><a class="link" href="objects.html" title="Distributions are Objects">Distributions | |
28 | are Objects</a> | |
29 | </h4></div></div></div> | |
30 | <p> | |
31 | Each kind of distribution in this library is a class type - an object. | |
32 | </p> | |
33 | <p> | |
34 | <a class="link" href="../../../policy.html" title="Chapter 15. Policies: Controlling Precision, Error Handling etc">Policies</a> provide fine-grained control of | |
35 | the behaviour of these classes, allowing the user to customise behaviour | |
36 | such as how errors are handled, or how the quantiles of discrete distribtions | |
37 | behave. | |
38 | </p> | |
39 | <div class="tip"><table border="0" summary="Tip"> | |
40 | <tr> | |
41 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../doc/src/images/tip.png"></td> | |
42 | <th align="left">Tip</th> | |
43 | </tr> | |
44 | <tr><td align="left" valign="top"><p> | |
45 | If you are familiar with statistics libraries using functions, and 'Distributions | |
46 | as Objects' seem alien, see <a class="link" href="../weg/nag_library.html" title="Comparison with C, R, FORTRAN-style Free Functions">the | |
47 | comparison to other statistics libraries.</a> | |
48 | </p></td></tr> | |
49 | </table></div> | |
50 | <p> | |
51 | Making distributions class types does two things: | |
52 | </p> | |
53 | <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> | |
54 | <li class="listitem"> | |
55 | It encapsulates the kind of distribution in the C++ type system; so, | |
56 | for example, Students-t distributions are always a different C++ type | |
57 | from Chi-Squared distributions. | |
58 | </li> | |
59 | <li class="listitem"> | |
60 | The distribution objects store any parameters associated with the distribution: | |
61 | for example, the Students-t distribution has a <span class="emphasis"><em>degrees of | |
62 | freedom</em></span> parameter that controls the shape of the distribution. | |
63 | This <span class="emphasis"><em>degrees of freedom</em></span> parameter has to be provided | |
64 | to the Students-t object when it is constructed. | |
65 | </li> | |
66 | </ul></div> | |
67 | <p> | |
68 | Although the distribution classes in this library are templates, there | |
69 | are typedefs on type <span class="emphasis"><em>double</em></span> that mostly take the usual | |
70 | name of the distribution (except where there is a clash with a function | |
71 | of the same name: beta and gamma, in which case using the default template | |
72 | arguments - <code class="computeroutput"><span class="identifier">RealType</span> <span class="special">=</span> | |
73 | <span class="keyword">double</span></code> - is nearly as convenient). | |
74 | Probably 95% of uses are covered by these typedefs: | |
75 | </p> | |
76 | <pre class="programlisting"><span class="comment">// using namespace boost::math; // Avoid potential ambiguity with names in std <random></span> | |
77 | <span class="comment">// Safer to declare specific functions with using statement(s):</span> | |
78 | ||
79 | <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">beta_distribution</span><span class="special">;</span> | |
80 | <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">binomial_distribution</span><span class="special">;</span> | |
81 | <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">students_t</span><span class="special">;</span> | |
82 | ||
83 | <span class="comment">// Construct a students_t distribution with 4 degrees of freedom:</span> | |
84 | <span class="identifier">students_t</span> <span class="identifier">d1</span><span class="special">(</span><span class="number">4</span><span class="special">);</span> | |
85 | ||
86 | <span class="comment">// Construct a double-precision beta distribution</span> | |
87 | <span class="comment">// with parameters a = 10, b = 20</span> | |
88 | <span class="identifier">beta_distribution</span><span class="special"><></span> <span class="identifier">d2</span><span class="special">(</span><span class="number">10</span><span class="special">,</span> <span class="number">20</span><span class="special">);</span> <span class="comment">// Note: _distribution<> suffix !</span> | |
89 | </pre> | |
90 | <p> | |
91 | If you need to use the distributions with a type other than <code class="computeroutput"><span class="keyword">double</span></code>, then you can instantiate the template | |
92 | directly: the names of the templates are the same as the <code class="computeroutput"><span class="keyword">double</span></code> typedef but with <code class="computeroutput"><span class="identifier">_distribution</span></code> | |
93 | appended, for example: <a class="link" href="../../dist_ref/dists/students_t_dist.html" title="Students t Distribution">Students | |
94 | t Distribution</a> or <a class="link" href="../../dist_ref/dists/binomial_dist.html" title="Binomial Distribution">Binomial | |
95 | Distribution</a>: | |
96 | </p> | |
97 | <pre class="programlisting"><span class="comment">// Construct a students_t distribution, of float type,</span> | |
98 | <span class="comment">// with 4 degrees of freedom:</span> | |
99 | <span class="identifier">students_t_distribution</span><span class="special"><</span><span class="keyword">float</span><span class="special">></span> <span class="identifier">d3</span><span class="special">(</span><span class="number">4</span><span class="special">);</span> | |
100 | ||
101 | <span class="comment">// Construct a binomial distribution, of long double type,</span> | |
102 | <span class="comment">// with probability of success 0.3</span> | |
103 | <span class="comment">// and 20 trials in total:</span> | |
104 | <span class="identifier">binomial_distribution</span><span class="special"><</span><span class="keyword">long</span> <span class="keyword">double</span><span class="special">></span> <span class="identifier">d4</span><span class="special">(</span><span class="number">20</span><span class="special">,</span> <span class="number">0.3</span><span class="special">);</span> | |
105 | </pre> | |
106 | <p> | |
107 | The parameters passed to the distributions can be accessed via getter member | |
108 | functions: | |
109 | </p> | |
110 | <pre class="programlisting"><span class="identifier">d1</span><span class="special">.</span><span class="identifier">degrees_of_freedom</span><span class="special">();</span> <span class="comment">// returns 4.0</span> | |
111 | </pre> | |
112 | <p> | |
113 | This is all well and good, but not very useful so far. What we often want | |
114 | is to be able to calculate the <span class="emphasis"><em>cumulative distribution functions</em></span> | |
115 | and <span class="emphasis"><em>quantiles</em></span> etc for these distributions. | |
116 | </p> | |
117 | </div> | |
118 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
119 | <td align="left"></td> | |
120 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
121 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
122 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
123 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
124 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
125 | 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>) | |
126 | </p> | |
127 | </div></td> | |
128 | </tr></table> | |
129 | <hr> | |
130 | <div class="spirit-nav"> | |
131 | <a accesskey="p" href="headers.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="generic.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> | |
132 | </div> | |
133 | </body> | |
134 | </html> |