1[section:bernoulli_dist Bernoulli Distribution] 2 3``#include <boost/math/distributions/bernoulli.hpp>`` 4 5 namespace boost{ namespace math{ 6 template <class RealType = double, 7 class ``__Policy`` = ``__policy_class`` > 8 class bernoulli_distribution; 9 10 typedef bernoulli_distribution<> bernoulli; 11 12 template <class RealType, class ``__Policy``> 13 class bernoulli_distribution 14 { 15 public: 16 typedef RealType value_type; 17 typedef Policy policy_type; 18 19 bernoulli_distribution(RealType p); // Constructor. 20 // Accessor function. 21 RealType success_fraction() const 22 // Probability of success (as a fraction). 23 }; 24 }} // namespaces 25 26The Bernoulli distribution is a discrete distribution of the outcome 27of a single trial with only two results, 0 (failure) or 1 (success), 28with a probability of success p. 29 30The Bernoulli distribution is the simplest building block 31on which other discrete distributions of 32sequences of independent Bernoulli trials can be based. 33 34The Bernoulli is the binomial distribution (k = 1, p) with only one trial. 35 36[@http://en.wikipedia.org/wiki/Probability_density_function probability density function pdf] 37[expression f(0) = 1 - p, f(1) = p] 38 39[@http://en.wikipedia.org/wiki/Cumulative_Distribution_Function Cumulative distribution function] 40[expression D(k) = if (k == 0) 1 - p else 1] 41 42The following graph illustrates how the 43[@http://en.wikipedia.org/wiki/Probability_density_function probability density function pdf] 44varies with the outcome of the single trial: 45 46[graph bernoulli_pdf] 47 48and the [@http://en.wikipedia.org/wiki/Cumulative_Distribution_Function Cumulative distribution function] 49 50[graph bernoulli_cdf] 51 52[h4 Member Functions] 53 54 bernoulli_distribution(RealType p); 55 56Constructs a [@http://en.wikipedia.org/wiki/bernoulli_distribution 57bernoulli distribution] with success_fraction /p/. 58 59 RealType success_fraction() const 60 61Returns the /success_fraction/ parameter of this distribution. 62 63[h4 Non-member Accessors] 64 65All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions] 66that are generic to all distributions are supported: __usual_accessors. 67 68The domain of the random variable is 0 and 1, 69and the useful supported range is only 0 or 1. 70 71Outside this range, functions are undefined, or may throw domain_error exception 72and make an error message available. 73 74[h4 Accuracy] 75 76The Bernoulli distribution is implemented with simple arithmetic operators 77and so should have errors within an epsilon or two. 78 79[h4 Implementation] 80 81In the following table /p/ is the probability of success and /q = 1-p/. 82/k/ is the random variate, either 0 or 1. 83 84[note The Bernoulli distribution is implemented here as a /strict discrete/ distribution. 85If a generalised version, allowing k to be any real, is required then 86the binomial distribution with a single trial should be used, for example: 87 88`binomial_distribution(1, 0.25)` 89] 90 91[table 92[[Function][Implementation Notes]] 93[[Supported range][{0, 1}]] 94[[pdf][Using the relation: pdf = 1 - p for k = 0, else p ]] 95[[cdf][Using the relation: cdf = 1 - p for k = 0, else 1]] 96[[cdf complement][q = 1 - p]] 97[[quantile][if x <= (1-p) 0 else 1]] 98[[quantile from the complement][if x <= (1-p) 1 else 0]] 99[[mean][p]] 100[[variance][p * (1 - p)]] 101[[mode][if (p < 0.5) 0 else 1]] 102[[skewness][(1 - 2 * p) / sqrt(p * q)]] 103[[kurtosis][6 * p * p - 6 * p +1/ p * q]] 104[[kurtosis excess][kurtosis -3]] 105] 106 107[h4 References] 108* [@http://en.wikipedia.org/wiki/Bernoulli_distribution Wikipedia Bernoulli distribution] 109* [@http://mathworld.wolfram.com/BernoulliDistribution.html Weisstein, Eric W. "Bernoulli Distribution." From MathWorld--A Wolfram Web Resource.] 110 111[endsect] [/section:bernoulli_dist bernoulli] 112 113[/ 114 Copyright 2006 John Maddock and Paul A. Bristow. 115 Distributed under the Boost Software License, Version 1.0. 116 (See accompanying file LICENSE_1_0.txt or copy at 117 http://www.boost.org/LICENSE_1_0.txt). 118] 119 120