The binomial distribution

Introduction to Statistics in Python

Maggie Matsui

Content Developer, DataCamp

Coin flipping

Hand flipping coin with one side H with 50% chance, other side T with 50% chance

Introduction to Statistics in Python

Binary outcomes

H and T, 1 and 0, Success and Failure, Win and Loss

Introduction to Statistics in Python

A single flip

binom.rvs(# of coins, probability of heads/success, size=# of trials)

1 = head, 0 = tails

from scipy.stats import binom

binom.rvs(1, 0.5, size=1)
array([1])
Introduction to Statistics in Python

One flip many times

binom.rvs(1, 0.5, size=8)
array([0, 1, 1, 0, 1, 0, 1, 1])

 

binom.rvs(1, 0.5, 8) with 1 in red, 0.5 in yellow, 8 in blue. Text: Flip 1 (in red) coin with 50% (in yellow) chance of success 8 (in blue) times

Introduction to Statistics in Python

Many flips one time

binom.rvs(8, 0.5, size=1)
array([5])

 

binom.rvs(8, 0.5, size=1) with 8 in red, 0.5 in yellow, 1 in blue. Text: Flip 8 (in red) coins with 50% (in yellow) chance of success 1 (in blue) time

Introduction to Statistics in Python

Many flips many times

binom.rvs(3, 0.5, size=10)
array([0, 3, 2, 1, 3, 0, 2, 2, 0, 0])

 

binom.rvs(3, 0.5, size=10) with 3 in red, 0.5 in yellow, 10 in blue. Text: Flip 3 (in red) coins with 50% (in yellow) chance of success 10 (in blue) times

Introduction to Statistics in Python

Other probabilities

binom.rvs(3, 0.25, size=10)
array([1, 1, 1, 1, 0, 0, 2, 0, 1, 0])

 

Picture of heads with 25% probability and tails with 75% probability

Introduction to Statistics in Python

Binomial distribution

Probability distribution of the number of successes in a sequence of independent trials

E.g. Number of heads in a sequence of coin flips

Described by $n$ and $p$

  • $n$: total number of trials
  • $p$: probability of success
binom.rvs(n=10, p=0.5, size=20)

Plot of binomial distribution with n=10, p = 0.5

Introduction to Statistics in Python

What's the probability of 7 heads?

$P(\text{heads} = 7)$

# binom.pmf(num heads, num trials, prob of heads)
binom.pmf(7, 10, 0.5)
0.1171875
Introduction to Statistics in Python

What's the probability of 7 or fewer heads?

$P(\text{heads} \le 7)$

binom.cdf(7, 10, 0.5)
0.9453125
Introduction to Statistics in Python

What's the probability of more than 7 heads?

$P(\text{heads} > 7)$

1 - binom.cdf(7, 10, 0.5)
0.0546875
Introduction to Statistics in Python

Expected value

$\text{Expected value} = n \times p$

Expected number of heads out of 10 flips $= 10 \times 0.5 = 5$

Introduction to Statistics in Python

Independence

The binomial distribution is a probability distribution of the number of successes in a sequence of independent trials

Box of tickets with 3 zeros and 3 ones. 50% chance of 0, 50% chance of 1

Introduction to Statistics in Python

Independence

The binomial distribution is a probability distribution of the number of successes in a sequence of independent trials

 

Probabilities of second trial are altered due to outcome of the first

 

If trials are not independent, the binomial distribution does not apply!

Box of tickets with 3 zeros and 2 ones. 60% chance of 0, 40% chance of 1

Introduction to Statistics in Python

Let's practice!

Introduction to Statistics in Python

Preparing Video For Download...