Search

Search papers and lessons

Bernoulli Distribution

Level 2 · 8 min read

The Bernoulli distribution describes a single yes-or-no trial: one number p gives the chance of a success, and the failure chance is 1 minus p.

On this page

  • Colour = topic mix
  • Probability
  • Sign in to see your progress

TL;DR

Many random experiments have only two outcomes: a coin lands heads or tails, a part passes or fails, a click happens or does not. The Bernoulli distribution is the model for one such trial. It has a single parameter pp, the chance of success, and the chance of failure is then 1−p1 - p.

Its real power comes from building blocks. Count how many successes happen across many independent trials and you get a binomial count. Use it as the output of a classifier and you get the standard yes-or-no loss.

Where you'll see it: in dropout, where each neuron is kept or switched off by a coin flip, and in binary classifiers, which predict the probability that a label is 1.

Intuition

Only two answers

Take a single item off a production line and ask whether it is defective. There are two outcomes, and nothing in between. You can describe the situation completely with one number: the defect rate. If one item in five is defective, the defect rate is 0.20.2, and every other fact about the single item follows from it.

That is the Bernoulli model. It is the simplest possible random variable, a random variable that takes only the values 0 and 1. Calling 1 a success and 0 a failure is just a labelling choice. The number pp is the whole story.

Almost every binary question in machine learning reduces to a Bernoulli trial. Whether a user clicks, whether an email is spam, whether a neuron survives dropout, each is one coin flip with a bias that the model has to estimate.

−0.200.20.40.60.811.200.20.40.60.81outcomeprobability
  • p = 0.2
  • p = 0.5
A Bernoulli distribution has just two bars: the failure at 0 and the success at 1. The bias p sets how tall the success bar is.

The math

Definition

Definition: Bernoulli distribution

A random variable XX is Bernoulli with parameter pp, where 0≤p≤10 \le p \le 1, when

P(X=1)=p,P(X=0)=1−p.P(X = 1) = p, \qquad P(X = 0) = 1 - p.

We write X∼Bernoulli(p)X \sim \mathrm{Bernoulli}(p).

A compact way to write the same probability mass function is

P(X=x)=px(1−p)1−x,x∈{0,1}.P(X = x) = p^{x}(1 - p)^{1 - x}, \qquad x \in \{0, 1\}.

Substitute x=1x = 1 and you get pp. Substitute x=0x = 0 and you get 1−p1 - p. The two rules from probability are satisfied: both values are between 0 and 1, and they sum to 1.

Mean and variance

The expected value is the probability-weighted average of the outcomes:

E[X]=0⋅(1−p)+1⋅p=p.\mathbb{E}[X] = 0 \cdot (1 - p) + 1 \cdot p = p.

So the long-run fraction of successes equals pp, which is why pp is called the success probability.

The variance is the expected squared distance from the mean. Since X2=XX^2 = X when XX is 0 or 1, we have E[X2]=p\mathbb{E}[X^2] = p, and therefore

Var(X)=E[X2]−(E[X])2=p−p2=p(1−p).\mathrm{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 = p - p^2 = p(1 - p).

The variance is largest at p=0.5p = 0.5, where the outcome is most uncertain, and it falls to zero at p=0p = 0 or p=1p = 1, where the outcome is certain. The standard deviation is the square root of the variance.

Sums of independent trials

If X1,…,XnX_1, \ldots, X_n are independent Bernoulli trials, each with the same pp, their sum S=X1+⋯+XnS = X_1 + \cdots + X_n counts the successes. Its distribution is the binomial distribution, with mean npnp and variance np(1−p)np(1 - p). The variances add because the trials are independent.

Worked examples

Example
A fair coin and a biased part

Part one: a fair coin. Let p=0.5p = 0.5.

  1. Mean: E[X]=0.5\mathbb{E}[X] = 0.5.
  2. Variance: p(1−p)=0.5×0.5=0.25p(1 - p) = 0.5 \times 0.5 = 0.25.
  3. Standard deviation: 0.25=0.5\sqrt{0.25} = 0.5.

Part two: a defective part. Let p=0.2p = 0.2, the chance an item is defective.

  1. Mean: E[X]=0.2\mathbb{E}[X] = 0.2, so one item in five is expected to be defective.
  2. Variance: p(1−p)=0.2×0.8=0.16p(1 - p) = 0.2 \times 0.8 = 0.16.
  3. Standard deviation: 0.16=0.4\sqrt{0.16} = 0.4.

Interpretation: the fair coin is the most uncertain case, with a standard deviation of 0.5 that is as large as a Bernoulli can get. The defective-part model is more predictable, because most items succeed and the variance is smaller.

Example
Ten items in a row

Using the defect rate p=0.2p = 0.2 from above, let each item be an independent trial, and ask for the chance that all 10 items are good, meaning every trial is a failure.

  1. The chance that one item is good is 1−0.2=0.81 - 0.2 = 0.8.
  2. Independence means the chances multiply: 0.8100.8^{10}.
  3. 0.82=0.640.8^{2} = 0.64, 0.84=0.40960.8^{4} = 0.4096, 0.88≈0.16780.8^{8} \approx 0.1678, and 0.810=0.88×0.82≈0.1678×0.64≈0.10740.8^{10} = 0.8^{8} \times 0.8^{2} \approx 0.1678 \times 0.64 \approx 0.1074.
  4. Expected number of defects in 10 items: 10×0.2=210 \times 0.2 = 2.

Interpretation: a clean run of 10 happens only about 11% of the time, even though each item is good 80% of the time. Independent small chances compound quickly, which is why a batch test that looks fine item by item can still fail often at batch scale.

Tradeoffs and failure modes

The Bernoulli model is the right choice when each trial has exactly two outcomes and the trials are independent with a fixed success chance. It fails in predictable ways when those conditions break.

SituationProblemBetter choice
More than two outcomes, such as a die rollOne success probability cannot describe several categoriesCategorical distribution
Trials that influence each other, such as draws without replacementIndependence is violated, so variances do not addHypergeometric or a model of the dependence
The success chance itself changes over timeA single fixed pp is wrongA model where pp varies, or a sequence of separate estimates
A count of successes over many trialsTracking each trial is unnecessaryBinomial distribution for the total
A continuous quantity such as a waiting timeBernoulli only models yes or noExponential or Gaussian, depending on the shape

One common mistake is to treat a rare-event rate as certain after seeing few trials. With 3 failures in 3 trials, a naive estimate of pp is 0, which says success is impossible. That is a symptom of too little data, not evidence.

History

The Bernoulli distribution is named after the Swiss mathematician Jacob Bernoulli. His book Ars Conjectandi, published in 1713 after his death, contains his law of large numbers for repeated binary trials. It shows that the observed fraction of successes approaches pp as the number of trials grows, which gave a mathematical basis for estimating a probability from frequencies.

The idea caught on because nearly every question about a yes-or-no outcome reduces to it. In the twentieth century it became the standard building block for statistical testing and for models of binary data. In modern machine learning, the Bernoulli likelihood underlies logistic regression and the binary cross-entropy loss used to train binary classifiers.

Check yourself

QuestionA coin has p=0.7p = 0.7 of landing heads. What are the mean and the variance of a single flip, where heads counts as 1?Show answer

The mean is E[X]=p=0.7\mathbb{E}[X] = p = 0.7. The variance is p(1−p)=0.7×0.3=0.21p(1 - p) = 0.7 \times 0.3 = 0.21.

QuestionFor a Bernoulli with p=0.5p = 0.5, the variance is 0.25. For p=0.9p = 0.9, what is the variance, and why is it smaller?Show answer

The variance is 0.9×0.1=0.090.9 \times 0.1 = 0.09. It is smaller because the outcome is more predictable: almost every trial is a success, so there is less spread around the mean.

QuestionTen independent trials each have success probability 0.3. What is the probability that all ten fail, and what is the expected number of successes?Show answer

Each trial fails with probability 0.7, so all ten fail with probability 0.710≈0.02820.7^{10} \approx 0.0282. The expected number of successes is 10×0.3=310 \times 0.3 = 3.

QuestionA model predicts that an email is spam with probability q=0.9q = 0.9. The true label is spam, so y=1y = 1. Compute the binary cross-entropy loss −[yln⁡q+(1−y)ln⁡(1−q)]-[y \ln q + (1 - y) \ln(1 - q)]. Then do the same if the true label is not spam, so y=0y = 0.Show answer

If y=1y = 1, the loss is −ln⁡0.9≈0.105-\ln 0.9 \approx 0.105. If y=0y = 0, the loss is −ln⁡(1−0.9)=−ln⁡0.1≈2.303-\ln(1 - 0.9) = -\ln 0.1 \approx 2.303. The loss is small when the prediction matches the label and large when the model is confidently wrong. This is the Bernoulli likelihood turned into a training loss, which is the reason it appears in classifier training.

QuestionIn dropout, each neuron is kept with probability 0.8 and dropped otherwise. Why can each neuron's mask be modelled as a Bernoulli trial, and what does the number of kept neurons follow?Show answer

Each neuron's keep-or-drop decision is a single yes-or-no outcome with a fixed chance of 0.8, applied independently. That is a Bernoulli trial. The number of kept neurons out of nn is a sum of independent Bernoulli trials, so it follows a binomial distribution with mean 0.8n0.8n.

Sources