Bernoulli: The Stats of One Biased Coin Flip

Probability and StatisticsEasy+10 XP+10

Bernoulli: The Stats of One Biased Coin Flip

A Bernoulli random variable is a single coin flip with bias p — it's 1 ("success") with probability p, and 0 otherwise. It's the building block every other discrete distribution in this section is made from.

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

Task: write bernoulli_stats(p, x) returning [pmf, mean, variance], each rounded to 4 decimal places.

  • The mean of a Bernoulli variable is just p itself.
  • The variance is p(1-p) — it's largest at p = 0.5, where the outcome is least predictable.

Examples

p = 0.3
x = 1
returns [0.3, 0.3, 0.21]
p = 0.3
x = 0
returns [0.7, 0.3, 0.21]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run