3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Bernoulli Distribution
</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=
"../dists.html" title=
"Distributions">
9 <link rel=
"prev" href=
"arcine_dist.html" title=
"Arcsine Distribution">
10 <link rel=
"next" href=
"beta_dist.html" title=
"Beta Distribution">
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>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"arcine_dist.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../dists.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=
"beta_dist.html"><img src=
"../../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h4 class=
"title">
27 <a name=
"math_toolkit.dist_ref.dists.bernoulli_dist"></a><a class=
"link" href=
"bernoulli_dist.html" title=
"Bernoulli Distribution">Bernoulli
29 </h4></div></div></div>
30 <pre class=
"programlisting"><span class=
"preprocessor">#include
</span> <span class=
"special"><</span><span class=
"identifier">boost
</span><span class=
"special">/
</span><span class=
"identifier">math
</span><span class=
"special">/
</span><span class=
"identifier">distributions
</span><span class=
"special">/
</span><span class=
"identifier">bernoulli
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span></pre>
31 <pre class=
"programlisting"><span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">{
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">math
</span><span class=
"special">{
</span>
32 <span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"keyword">class
</span> <span class=
"identifier">RealType
</span> <span class=
"special">=
</span> <span class=
"keyword">double
</span><span class=
"special">,
</span>
33 <span class=
"keyword">class
</span> <a class=
"link" href=
"../../../policy.html" title=
"Chapter 15. Policies: Controlling Precision, Error Handling etc">Policy
</a> <span class=
"special">=
</span> <a class=
"link" href=
"../../pol_ref/pol_ref_ref.html" title=
"Policy Class Reference">policies::policy
<></a> <span class=
"special">></span>
34 <span class=
"keyword">class
</span> <span class=
"identifier">bernoulli_distribution
</span><span class=
"special">;
</span>
36 <span class=
"keyword">typedef
</span> <span class=
"identifier">bernoulli_distribution
</span><span class=
"special"><></span> <span class=
"identifier">bernoulli
</span><span class=
"special">;
</span>
38 <span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"keyword">class
</span> <span class=
"identifier">RealType
</span><span class=
"special">,
</span> <span class=
"keyword">class
</span> <a class=
"link" href=
"../../../policy.html" title=
"Chapter 15. Policies: Controlling Precision, Error Handling etc">Policy
</a><span class=
"special">></span>
39 <span class=
"keyword">class
</span> <span class=
"identifier">bernoulli_distribution
</span>
40 <span class=
"special">{
</span>
41 <span class=
"keyword">public
</span><span class=
"special">:
</span>
42 <span class=
"keyword">typedef
</span> <span class=
"identifier">RealType
</span> <span class=
"identifier">value_type
</span><span class=
"special">;
</span>
43 <span class=
"keyword">typedef
</span> <span class=
"identifier">Policy
</span> <span class=
"identifier">policy_type
</span><span class=
"special">;
</span>
45 <span class=
"identifier">bernoulli_distribution
</span><span class=
"special">(
</span><span class=
"identifier">RealType
</span> <span class=
"identifier">p
</span><span class=
"special">);
</span> <span class=
"comment">// Constructor.
</span>
46 <span class=
"comment">// Accessor function.
</span>
47 <span class=
"identifier">RealType
</span> <span class=
"identifier">success_fraction
</span><span class=
"special">()
</span> <span class=
"keyword">const
</span>
48 <span class=
"comment">// Probability of success (as a fraction).
</span>
49 <span class=
"special">};
</span>
50 <span class=
"special">}}
</span> <span class=
"comment">// namespaces
</span>
53 The Bernoulli distribution is a discrete distribution of the outcome of
54 a single trial with only two results,
0 (failure) or
1 (success), with
55 a probability of success p.
58 The Bernoulli distribution is the simplest building block on which other
59 discrete distributions of sequences of independent Bernoulli trials can
63 The Bernoulli is the binomial distribution (k =
1, p) with only one trial.
66 <a href=
"http://en.wikipedia.org/wiki/Probability_density_function" target=
"_top">probability
67 density function pdf
</a> f(
0) =
1 - p, f(
1) = p.
<a href=
"http://en.wikipedia.org/wiki/Cumulative_Distribution_Function" target=
"_top">Cumulative
68 distribution function
</a> D(k) = if (k ==
0)
1 - p else
1.
71 The following graph illustrates how the
<a href=
"http://en.wikipedia.org/wiki/Probability_density_function" target=
"_top">probability
72 density function pdf
</a> varies with the outcome of the single trial:
75 <span class=
"inlinemediaobject"><img src=
"../../../../graphs/bernoulli_pdf.svg" align=
"middle"></span>
78 and the
<a href=
"http://en.wikipedia.org/wiki/Cumulative_Distribution_Function" target=
"_top">Cumulative
79 distribution function
</a>
82 <span class=
"inlinemediaobject"><img src=
"../../../../graphs/bernoulli_cdf.svg" align=
"middle"></span>
85 <a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.h0"></a>
86 <span class=
"phrase"><a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.member_functions"></a></span><a class=
"link" href=
"bernoulli_dist.html#math_toolkit.dist_ref.dists.bernoulli_dist.member_functions">Member
89 <pre class=
"programlisting"><span class=
"identifier">bernoulli_distribution
</span><span class=
"special">(
</span><span class=
"identifier">RealType
</span> <span class=
"identifier">p
</span><span class=
"special">);
</span>
92 Constructs a
<a href=
"http://en.wikipedia.org/wiki/bernoulli_distribution" target=
"_top">bernoulli
93 distribution
</a> with success_fraction
<span class=
"emphasis"><em>p
</em></span>.
95 <pre class=
"programlisting"><span class=
"identifier">RealType
</span> <span class=
"identifier">success_fraction
</span><span class=
"special">()
</span> <span class=
"keyword">const
</span>
98 Returns the
<span class=
"emphasis"><em>success_fraction
</em></span> parameter of this distribution.
101 <a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.h1"></a>
102 <span class=
"phrase"><a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.non_member_accessors"></a></span><a class=
"link" href=
"bernoulli_dist.html#math_toolkit.dist_ref.dists.bernoulli_dist.non_member_accessors">Non-member
106 All the
<a class=
"link" href=
"../nmp.html" title=
"Non-Member Properties">usual non-member accessor
107 functions
</a> that are generic to all distributions are supported:
108 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.cdf">Cumulative Distribution Function
</a>,
109 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability Density Function
</a>,
110 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.quantile">Quantile
</a>,
<a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.hazard">Hazard Function
</a>,
<a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.chf">Cumulative Hazard Function
</a>,
111 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.mean">mean
</a>,
<a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.median">median
</a>,
112 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.mode">mode
</a>,
<a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.variance">variance
</a>,
113 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.sd">standard deviation
</a>,
114 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.skewness">skewness
</a>,
<a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.kurtosis">kurtosis
</a>,
<a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.kurtosis_excess">kurtosis_excess
</a>,
115 <a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.range">range
</a> and
<a class=
"link" href=
"../nmp.html#math_toolkit.dist_ref.nmp.support">support
</a>.
118 The domain of the random variable is
0 and
1, and the useful supported
119 range is only
0 or
1.
122 Outside this range, functions are undefined, or may throw domain_error
123 exception and make an error message available.
126 <a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.h2"></a>
127 <span class=
"phrase"><a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.accuracy"></a></span><a class=
"link" href=
"bernoulli_dist.html#math_toolkit.dist_ref.dists.bernoulli_dist.accuracy">Accuracy
</a>
130 The Bernoulli distribution is implemented with simple arithmetic operators
131 and so should have errors within an epsilon or two.
134 <a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.h3"></a>
135 <span class=
"phrase"><a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.implementation"></a></span><a class=
"link" href=
"bernoulli_dist.html#math_toolkit.dist_ref.dists.bernoulli_dist.implementation">Implementation
</a>
138 In the following table
<span class=
"emphasis"><em>p
</em></span> is the probability of success
139 and
<span class=
"emphasis"><em>q =
1-p
</em></span>.
<span class=
"emphasis"><em>k
</em></span> is the random
140 variate, either
0 or
1.
142 <div class=
"note"><table border=
"0" summary=
"Note">
144 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Note]" src=
"../../../../../../../doc/src/images/note.png"></td>
145 <th align=
"left">Note
</th>
147 <tr><td align=
"left" valign=
"top">
149 The Bernoulli distribution is implemented here as a
<span class=
"emphasis"><em>strict
150 discrete
</em></span> distribution. If a generalised version, allowing
151 k to be any real, is required then the binomial distribution with a single
152 trial should be used, for example:
155 <code class=
"computeroutput"><span class=
"identifier">binomial_distribution
</span><span class=
"special">(
</span><span class=
"number">1</span><span class=
"special">,
</span>
156 <span class=
"number">0.25</span><span class=
"special">)
</span></code>
160 <div class=
"informaltable"><table class=
"table">
198 Using the relation: pdf =
1 - p for k =
0, else p
210 Using the relation: cdf =
1 - p for k =
0, else
1
234 if x
<= (
1-p)
0 else
1
241 quantile from the complement
246 if x
<= (
1-p)
1 else
0
282 if (p
< 0.5)
0 else
1
294 (
1 -
2 * p) / sqrt(p * q)
306 6 * p * p -
6 * p +
1/ p * q
325 <a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.h4"></a>
326 <span class=
"phrase"><a name=
"math_toolkit.dist_ref.dists.bernoulli_dist.references"></a></span><a class=
"link" href=
"bernoulli_dist.html#math_toolkit.dist_ref.dists.bernoulli_dist.references">References
</a>
328 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
329 <li class=
"listitem">
330 <a href=
"http://en.wikipedia.org/wiki/Bernoulli_distribution" target=
"_top">Wikpedia
331 Bernoulli distribution
</a>
333 <li class=
"listitem">
334 <a href=
"http://mathworld.wolfram.com/BernoulliDistribution.html" target=
"_top">Weisstein,
335 Eric W.
"Bernoulli Distribution." From MathWorld--A Wolfram
340 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
341 <td align=
"left"></td>
342 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2006-
2010,
2012-
2014 Nikhar Agrawal,
343 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
344 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
345 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
346 Distributed under the Boost Software License, Version
1.0. (See accompanying
347 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>)
352 <div class=
"spirit-nav">
353 <a accesskey=
"p" href=
"arcine_dist.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../dists.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=
"beta_dist.html"><img src=
"../../../../../../../doc/src/images/next.png" alt=
"Next"></a>