← Articles

Coins!

Coins as statistical machines: basic probability, combinatorics, independence, the binomial formula, and Bayes' theorem — with interactive visualizations.


Coins are our first model of a statistical machine — a simple device that randomly produces one of a finite number of results with some probability. For our purposes, a coin is any object or mechanism that, when flipped, produces one of two outcomes: heads (HH) or tails (TT). The Romans called this practice navia aut caput (“ship or head”), after the galley and portrait that appeared on either side of their currency — one of the earliest ways humans took chance into their own hands.

The Basics

A coin is characterized by a single parameter p(0,1)p \in (0, 1):

P(H)=p,P(T)=1p.\mathbb{P}(H) = p, \qquad \mathbb{P}(T) = 1 - p.

A fair coin has p=12p = \tfrac{1}{2}, meaning heads and tails appear with equal likelihood. We assume the coin must land on one side or the other.

Counting probability. When all outcomes are equally likely, probability is:

P(event)=number of desired outcomesnumber of total outcomes\mathbb{P}(\text{event}) = \frac{\text{number of desired outcomes}}{\text{number of total outcomes}}

⚠️ Common mistake. The formula above only applies when every outcome is equally likely. A classic confusion: “there are two outcomes — winning or losing the lottery — so the probability is 12\tfrac{1}{2}.” The error lies in treating distinct outcomes as equally likely ones. If 10,000 lottery tickets exist and any could win with the same probability, the correct denominator is 10,000, not 2.

Question 1. You flip a fair coin 10 times and get tails every time. What is the probability the next flip is heads?

Answer

12\tfrac{1}{2}. Each flip is independent — past results have no influence on future flips.

Question 2. What is the probability of flipping HHHHHH with a fair coin?

Answer

We can list all 8 outcomes for 3 flips:

Flip 1Flip 2Flip 3HHHHHH?
HHH
HHT
HTH
HTT
THH
THT
TTH
TTT

One desired outcome out of eight total:

P(HHH)=18\mathbb{P}(HHH) = \frac{1}{8}

Question 3. What is the probability of flipping exactly one head in three flips?

Answer

Three sequences have exactly one head — HTTHTT, THTTHT, TTHTTH — out of eight total:

Flip 1Flip 2Flip 3Exactly one HH?
HHH
HHT
HTH
HTT
THH
THT
TTH
TTT

P(exactly one H)=38\mathbb{P}(\text{exactly one } H) = \frac{3}{8}

Question 4. What is the probability of flipping exactly 5 heads in 10 flips of a fair coin?

Answer

Ten flips produce too many outcomes to list — there are 210=10242^{10} = 1024 of them. Let’s split the problem.

Number of desired outcomes

We need to choose which 5 of the 10 positions are heads. We have 10 choices for the first head, 9 for the second, and so on, giving

109876=10!5!10 \cdot 9 \cdot 8 \cdot 7 \cdot 6 = \frac{10!}{5!}

ordered placements. But this overcounts: labeling the five heads H1,H2,H3,H4,H5H_1, H_2, H_3, H_4, H_5 treats them as distinguishable when they are not.

Illustration showing labeled heads causing overcounting in the choose function

Each of the 5!=1205! = 120 orderings of those labels maps to the same physical sequence of heads and tails — so we divide by 5!5!:

10!5!5!=(105)=252\frac{10!}{5! \cdot 5!} = \binom{10}{5} = 252

This is the choose function, also written 10C5{}_{10}C_5 and read “10 choose 5.”

Number of total outcomes

Each flip has 2 outcomes, so 10 flips give 210=10242^{10} = 1024 equally likely sequences.

Combining both:

P(5 heads in 10 flips)=(105)210=2521024=632560.246\mathbb{P}(\text{5 heads in 10 flips}) = \frac{\binom{10}{5}}{2^{10}} = \frac{252}{1024} = \frac{63}{256} \approx 0.246

The choose function is just the entrance to combinatorics — a rich field dedicated to the art of counting. We will use it repeatedly going forward.

Question 5. What is the probability of flipping at least one head in 10 flips?

Answer

Summing over all non-zero head counts is tedious. Instead, use the complement: only one sequence has no heads at all (TTTTTTTTTTTTTTTTTTTT), so

P(at least one H)=1P(no heads)=1(12)10=111024=102310240.999.\mathbb{P}(\text{at least one } H) = 1 - \mathbb{P}(\text{no heads}) = 1 - \left(\frac{1}{2}\right)^{10} = 1 - \frac{1}{1024} = \frac{1023}{1024} \approx 0.999.

Question 6. A weighted coin has p=34p = \tfrac{3}{4}. What is the probability of flipping HHHHHH?

Answer

Counting probability does not apply here because HHHHHH and TTTTTT are not equally likely. We need a different strategy.

Notice that each flip is independent: the outcome of one does not affect any other. This means we can multiply:

P(HHH)=P(H)P(H)P(H)=(34)3=2764\mathbb{P}(HHH) = \mathbb{P}(H) \cdot \mathbb{P}(H) \cdot \mathbb{P}(H) = \left(\frac{3}{4}\right)^3 = \frac{27}{64}

Implementing a Coin

import random

class Coin:
    """A coin that lands heads with probability p."""

    def __init__(self, p=0.5):
        self.p = p

    def flip(self):
        return 'H' if random.random() < self.p else 'T'

Set Notation

Now that we can compute basic probabilities, the next step is combining them. Set theory gives us a precise language.

Define the sample space Ω\Omega as the set of all possible outcomes. An event is any subset AΩA \subseteq \Omega.

For three coin flips: Ω={HHH,HHT,HTH,HTT,THH,THT,TTH,TTT}\Omega = \{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT\}. The event “exactly one head” is {HTT,THT,TTH}Ω\{HTT, THT, TTH\} \subset \Omega.

Given an event AA, we write its probability as P(A)\mathbb{P}(A). Set operations produce new events:

OperationNotationProbabilistic meaning
ComplementP(Ac)\mathbb{P}(A^c)Probability that AA does not occur
IntersectionP(AB)\mathbb{P}(A \cap B)Probability that both AA and BB occur
UnionP(AB)\mathbb{P}(A \cup B)Probability that AA or BB (or both) occur

Two facts follow immediately:

  1. Independence. Events AA and BB are independent if and only if P(AB)=P(A)P(B)\mathbb{P}(A \cap B) = \mathbb{P}(A) \cdot \mathbb{P}(B).
  2. Inclusion-Exclusion (PIE). P(AB)=P(A)+P(B)P(AB).\mathbb{P}(A \cup B) = \mathbb{P}(A) + \mathbb{P}(B) - \mathbb{P}(A \cap B).

Question 6 (revisited with set notation). A weighted coin has p=34p = \tfrac{3}{4}. What is P(HHH)\mathbb{P}(HHH)?

Answer

Let AA, BB, CC be the events “first flip is HH”, “second flip is HH”, and “third flip is HH.” Since flips are independent, P(AB)=P(A)P(B)\mathbb{P}(A \cap B) = \mathbb{P}(A) \cdot \mathbb{P}(B), and the same holds for any pair. Applying this twice:

P(ABC)=P(AB)P(C)=P(A)P(B)P(C)=343434=2764.\begin{align*} \mathbb{P}(A \cap B \cap C) &= \mathbb{P}(A \cap B) \cdot \mathbb{P}(C) \\ &= \mathbb{P}(A) \cdot \mathbb{P}(B) \cdot \mathbb{P}(C) \\ &= \frac{3}{4} \cdot \frac{3}{4} \cdot \frac{3}{4} = \frac{27}{64}. \end{align*}

Question 7. Given a coin with P(H)=p\mathbb{P}(H) = p, derive the probability of getting exactly hh heads in nn flips.

Answer

From Question 4, there are (nh)\binom{n}{h} sequences of nn flips containing exactly hh heads. By independence, any specific sequence of hh heads and nhn-h tails has probability ph(1p)nhp^h \cdot (1-p)^{n-h}. Since there are (nh)\binom{n}{h} such sequences:

P(exactly h heads in n flips)=(nh)ph(1p)nh\boxed{\mathbb{P}(\text{exactly } h \text{ heads in } n \text{ flips}) = \binom{n}{h} p^h (1-p)^{n-h}}

Question 8. For p(0,1)p \in (0, 1), prove:

h=0n(nh)ph(1p)nh=1\sum_{h=0}^{n} \binom{n}{h} p^h (1-p)^{n-h} = 1

Answer

Apply the Binomial Theorem: (x+y)n=h=0n(nh)xhynh(x + y)^n = \sum_{h=0}^{n} \binom{n}{h} x^h y^{n-h}.

Set x=px = p and y=1py = 1-p:

h=0n(nh)ph(1p)nh=(p+(1p))n=1n=1.\sum_{h=0}^{n} \binom{n}{h} p^h (1-p)^{n-h} = \bigl(p + (1-p)\bigr)^n = 1^n = 1. \qquad \square

Simulation

The formula from Question 7 makes a testable prediction: if we flip a weighted coin nn times and repeat the experiment many times, the proportion of trials with each head count hh should approach (nh)ph(1p)nh\binom{n}{h} p^h (1-p)^{n-h} as the number of trials grows. Let’s verify this.

import math

NUM_TRIALS  = 10_000
NUM_FLIPS   = 8
COIN_WEIGHT = 3/4
GOAL_HEADS  = 5

coin = Coin(p=COIN_WEIGHT)

probability_of_goal = (
    math.comb(NUM_FLIPS, GOAL_HEADS)
    * COIN_WEIGHT ** GOAL_HEADS
    * (1 - COIN_WEIGHT) ** (NUM_FLIPS - GOAL_HEADS)
)

num_heads = []
for _ in range(NUM_TRIALS):
    flips = [coin.flip() for _ in range(NUM_FLIPS)]
    num_heads.append(flips.count('H'))

Histogram of 10,000 simulated trials of flipping a weighted coin (p=3/4) 8 times. Bars show the proportion of trials with each head count; the dotted red line marks the theoretical P(5 heads) ≈ 0.208.

After 10,000 trials the simulated distribution closely matches the theoretical binomial — for n=8n = 8 and p=34p = \tfrac{3}{4}, the most likely outcome is 6 heads, with P(5 heads)0.208P(\text{5 heads}) \approx 0.208 as predicted.

The controls below mirror the four parameters in the code. Press Run Simulation to generate your own trials, then try changing the goal, adjusting the coin weight, or cranking up the trial count to see how quickly the empirical distribution converges to theory.

120
0.050.95
Simulated Theoretical P(goal)

Bayes’ Rule

The final concept we introduce here is conditional probability — the probability of an event given information about another event.

Given events AA and BB with P(B)>0\mathbb{P}(B) > 0, the conditional probability of AA given BB is:

P(AB)=P(AB)P(B)\mathbb{P}(A \mid B) = \frac{\mathbb{P}(A \cap B)}{\mathbb{P}(B)}

From a set-theory perspective, this restricts the sample space from all of Ω\Omega down to BB: we are asking “among the outcomes in BB, what fraction also belong to AA?” We will see a geometric picture of this shortly.

Question 9. Events AA and BB are independent, with P(A)=12\mathbb{P}(A) = \tfrac{1}{2} and P(B)=13\mathbb{P}(B) = \tfrac{1}{3}. What are P(AB)\mathbb{P}(A \mid B) and P(BA)\mathbb{P}(B \mid A)?

P(AB)\mathbb{P}(A \mid B)

By independence, P(AB)=P(A)P(B)\mathbb{P}(A \cap B) = \mathbb{P}(A) \cdot \mathbb{P}(B), so:

P(AB)=P(AB)P(B)=P(A)P(B)P(B)=P(A)=12\mathbb{P}(A \mid B) = \frac{\mathbb{P}(A \cap B)}{\mathbb{P}(B)} = \frac{\mathbb{P}(A)\,\mathbb{P}(B)}{\mathbb{P}(B)} = \mathbb{P}(A) = \frac{1}{2}

When AA and BB are independent, conditioning on BB does not affect the probability of AA. The converse is also true: if conditioning leaves the probability unchanged, the events are independent.

P(BA)\mathbb{P}(B \mid A)

By the same reasoning:

P(BA)=P(BA)P(A)=P(B)P(A)P(A)=P(B)=13\mathbb{P}(B \mid A) = \frac{\mathbb{P}(B \cap A)}{\mathbb{P}(A)} = \frac{\mathbb{P}(B)\,\mathbb{P}(A)}{\mathbb{P}(A)} = \mathbb{P}(B) = \frac{1}{3}

Note: P(AB)=P(BA)\mathbb{P}(A \cap B) = \mathbb{P}(B \cap A) (intersection is commutative), but conditioning is notP(AB)P(BA)\mathbb{P}(A \mid B) \neq \mathbb{P}(B \mid A) in general.

Question 10. You flip a fair coin 3 times. Given that the first flip is heads, what is the probability that all three flips are heads?

Answer

Left to the reader. Hint: let A={HHH}A = \{HHH\} and B=B = “first flip is HH.” How many outcomes are in BB? Does ABA \subseteq B?

Question 11. You have two coins: a fair coin (p=12p = \tfrac{1}{2}) and a weighted coin (p=23p = \tfrac{2}{3}). You pick one at random and flip it, and it comes up heads. What is the probability that the next flip is also heads?

Answer

Left to the reader. Hint: use the first flip to update the probability you are holding the weighted coin (Bayes’ theorem), then compute the expected probability of heads on the second flip as a weighted average over both coins.

Geometric Interpretations

Visualize Bayes’ theorem as regions of a sample space. The full grid represents Ω\Omega. Paint event AA (blue) and event BB (orange) and watch the stats panel compute P(A)\mathbb{P}(A), P(B)\mathbb{P}(B), and P(AB)=P(AB)/P(B)\mathbb{P}(A \mid B) = \mathbb{P}(A \cap B) / \mathbb{P}(B) in real time.

Ω
Draw a closed shape — releasing connects the ends and fills
P(A)
of Ω
P(B)
of Ω
P(A∩B)
of Ω
P(A|B)
B is empty
A∩B within B
P(B|A)
A is empty
A∩B within A
Challenges
P(A|B) = 0 Draw A and B with no overlap
P(A|B) = 1 Draw B entirely inside A
Ω — 100 outcomes
P(A) 0 / 100 = 0.00
P(B) 0 / 100 = 0.00
P(A∩B) 0 / 100 = 0.00
P(A|B)
P(A∩B) / P(B)
P(B|A)
P(A∩B) / P(A)
Challenges
P(A) = 0.25 Assign exactly 25 squares to A
P(A|B) = 0.5 Let exactly half of B's squares overlap with A
P(A∩B) = 0.1 Put exactly 10 squares in both A and B
P(A|B) = P(B|A) Make A and B have the same number of squares

Penney’s Game

Coming soon.

Player A selects a sequence of heads and tails (length 3 or longer) and shows it to Player B. Player B then selects a different sequence of the same length. A fair coin is tossed until one player’s sequence appears as a consecutive run. The player whose sequence appears first wins.

Despite appearances, this game is not fair — for any sequence Player A picks, Player B can always choose a sequence that is strictly more likely to appear first. We will analyze specific matchups and derive a general strategy.

Additional Problems

Coming soon.