Skip to main content

The probability mass function

Gather the outcomes that give each value and you get the probability mass function, one bar per value. Build it for the sum of two dice, a Galton board, a squared variable and flips until the first head, then set it beside a simulation.
M06.5.2L1 Core25 minIn review

Before this lesson

M06.5.1 A random variable is a function of the outcome M06.2.3 Unordered samples and binomial coefficients

After this lesson you can

  • Build the PMF of a discrete random variable by gathering the outcomes that give each value.
  • State the two properties of a PMF, never negative and summing to 1, and find P(X in a set) by adding bars.
  • Find the PMF of g(X) from the PMF of X by pooling the values that g sends to the same number.
  • Work with a PMF on infinitely many values, using a geometric series to check that it sums to 1.
  • Compute a PMF from scratch with Counter and Fraction and compare it with simulated relative frequencies.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

The sum of two dice can be anything from 2 to 12, but 7 turns up far more often than 2. A ball on a Galton board can land in any bin, yet it piles up in the middle. How do we describe, in one picture, how likely each value of a random variable is?

From the last lesson

1. What is a random variable?

Show answer

A function from the sample space \(\Omega\) to the real numbers: it gives each outcome \(\omega\) a number \(X(\omega)\).

2. For two dice and \({X = \text{sum}}\), what kind of object is \({\{X = 7\}}\), and how big is it?

Show answer

An event: the set of outcomes whose sum is 7. It holds 6 of the 36 outcomes.

Intuition

A random variable sorts the outcomes into piles, one pile for each value. The probability of a value is the probability of its pile. Draw one bar per value, as tall as that probability, and you have the probability mass function (PMF): the whole distribution of \(X\) in one picture.

On the left, the 6 by 6 grid of outcomes of two dice, each cell showing its sum, titled 36 outcomes, each 1 over 36. The six cells of sum 7 on the diagonal are yellow. On the right, titled the PMF of X, a grey bar chart over the values 2 to 12 with heights 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 in 36ths, written above the bars; the bar at 7 is yellow and labelled p X of 7 equals 6 over 36. A line underneath reads: gather the outcomes for each value; the 6 yellow cells make the yellow bar.
M06.5.2-F1

Each diagonal of the grid has one sum. Gather its cells and the count becomes the height of one bar: the PMF of the sum of two dice rises from \(1/36\) to \(6/36\) and falls back.

The grid’s diagonals do the counting. Sum 2 has one cell, \({(1, 1)}\); sum 3 has two; and so on up to sum 7 with six, after which the diagonals shorten again. Nothing is lost in this picture except the outcomes themselves: the PMF keeps how likely each value is and forgets which outcome gave it.

Build it up

Step 1. A Galton board with 4 rows. Each of the \({2^4 = 16}\) paths is equally likely, \(1/16\) each, because every bounce is a fair left or right. Bin \(k\) collects the paths with \(k\) right bounces, and there are \(\binom{4}{k}\) of those: choose which \(k\) of the 4 bounces go right (lesson M06.2.3).

A Galton board with 4 rows of grey pegs, each peg labelled with the number of paths that reach it: 1; 1, 1; 1, 2, 1; 1, 3, 3, 1. Below are 5 bins numbered 0 to 4, each holding a grey bar of height 1, 4, 6, 4, 1, with the bar in bin 2 in yellow. Under the bins: 1/16, 4/16, 6/16, 4/16, 1/16. On the right: 4 rows, fair pegs; 16 paths, each 1/16; paths to bin k: 4 choose k; p X of k equals 4 choose k over 16; yellow: 6 paths reach bin 2, so p X of 2 equals 6/16; the bars add to 16/16 equals 1.
M06.5.2-F2

Pascal’s triangle on the pegs counts the paths. Each path has probability \(1/16\), so bin \(k\) gets \(\binom{4}{k}/16\).

So \({p_X(k) = \binom{4}{k}/16}\): the bars are 1, 4, 6, 4, 1 sixteenths. With \(n\) rows the same argument gives, for \({k = 0, 1, \dots, n}\),

\[ p_X(k) = \binom{n}{k} \frac{1}{2^n}. \]

For 10 rows the middle bin gets \({\binom{10}{5}/1024} = {252/1024}\), about 0.246.

Step 2. A PMF for \(W^2\). Take the winnings of lesson M06.5.1, \({W = 2X - 3}\) for three flips, which is \({-3}\), \({-1}\), 1 or 3 with chances \(1/8\), \(3/8\), \(3/8\) and \(1/8\). Square it. The values \(1\) and \({-1}\) both square to 1, and \(3\) and \({-3}\) both square to 9. The event \({\{W^2 = 1\}}\) is \({\{W = -1\} \cup \{W = 1\}}\), two disjoint events, so their bars add.

Top: the PMF of W, where W equals 2X minus 3, as grey bars over minus 3, minus 1, 1 and 3 with heights 1/8, 3/8, 3/8 and 1/8; the bars at minus 1 and 1 are yellow. Vermillion arrows lead down from minus 1 and 1 to one bar over the value 1 of a lower number line, and from minus 3 and 3 to one bar over the value 9. Bottom: the PMF of W squared, a yellow bar at 1 labelled 3/8 plus 3/8 equals 6/8, and a grey bar at 9 labelled 1/8 plus 1/8 equals 2/8. A title reads: square each value, then add the bars that land together.
M06.5.2-F3

Squaring sends two values of \(W\) to each value of \(W^2\), so the PMF of \(W^2\) adds two bars of \(W\) for each of its own.

\[ \begin{aligned} p_{W^2}(1) &= \frac{3}{8} + \frac{3}{8} = \frac{3}{4}, \\ p_{W^2}(9) &= \frac{1}{8} + \frac{1}{8} = \frac{1}{4}. \end{aligned} \]

Because \(W\) is symmetric about 0 (the bar at \(w\) matches the bar at \({-w}\)), each value of \(W^2\) gets exactly twice the bar of its positive root.

Step 3. Infinitely many values. Flip a fair coin until the first head and let \(N\) be the number of flips. \({N = k}\) means \({k - 1}\) tails and then a head, one particular string of \(k\) flips, so \({p_N(k) = (1/2)^k}\) for \({k = 1, 2, 3, \dots}\), with no largest value.

Left: a grey bar chart titled p N of k equals one half to the power k, over k equals 1 to 8 flips until the first head, with bars 1/2, 1/4, 1/8, 1/16 and smaller ones after, and the note: on for ever. Right: a single column of height 1 cut into pieces from the bottom: k equals 1: 1/2, k equals 2: 1/4, k equals 3: 1/8, then thinner slices, with a thin yellow slice at the top labelled the rest. Underneath: total 1.
M06.5.2-F5

Each bar is half the one before, and laid end to end they fill a column of height 1: every piece takes half of what is left.

The formal statement

NoteDefinition: probability mass function

The probability mass function of a discrete random variable \(X\) is

\[ p_X(x) = \Prob(X = x) \]

for every real number \(x\). It is 0 at every \(x\) that \(X\) never takes.

Two properties hold for every PMF, and both come from the fact that the events \({\{X = x\}}\) partition \(\Omega\) (lesson M06.5.1).

Never negative, never above 1. \({p_X(x) = \Prob(X = x)}\) is the probability of an event, so \({0 \leq p_X(x) \leq 1}\) by the axioms.

The bars add to 1. The events \({\{X = x\}}\), one for each value, are disjoint and together make up \(\Omega\). By additivity (over a list of events, if there are infinitely many values),

\[ \sum_{x} p_X(x) = \Prob(\Omega) = 1. \]

Probabilities of sets of values. For any set \(A\) of numbers, the event \({\{X \in A\}}\) is the disjoint union of the events \({\{X = x\}}\) for \(x\) in \(A\), so

\[ \Prob(X \in A) = \sum_{x \in A} p_X(x). \]

In the picture: shade the bars over \(A\) and add their heights.

The PMF of \(g(X)\). The event \({\{g(X) = y\}}\) is the union of the events \({\{X = x\}}\) over all \(x\) with \({g(x) = y}\). These are disjoint, so

\[ p_{g(X)}(y) = \sum_{x \,:\, g(x) = y} p_X(x). \]

When \(g\) never sends two values to the same place, each bar simply moves; when it does, the bars that land together are added, as for \(W^2\).

Checking the infinite PMF. The bars of \(N\) form a geometric series with first term \(1/2\) and ratio \(1/2\) (lesson M01.7.3):

\[ \begin{aligned} \sum_{k=1}^{\infty} \left(\tfrac{1}{2}\right)^k &= \frac{1/2}{1 - 1/2} \\ &= 1. \end{aligned} \]

So \(p_N\) is a genuine PMF, and sums of its bars give probabilities: for example \({\Prob(N \leq 3)} = {1/2 + 1/4 + 1/8} = {7/8}\).

Worked example

The sum of two dice. Find the PMF of \(X\), then \({\Prob(X \geq 10)}\) and \({\Prob(X \text{ is prime})}\).

The PMF. The cells on the diagonal for sum \(x\) number \({6 - \abs{x - 7}}\): 1 for \({x = 2}\), rising by one each step to 6 at \({x = 7}\), then falling back to 1 at 12. Each cell has probability \(1/36\), so for \({x = 2, 3, \dots, 12}\)

\[ p_X(x) = \frac{6 - \abs{x - 7}}{36}. \]

Check. The counts add to 36, so the bars add to 1:

\[ \begin{aligned} &1 + 2 + 3 + 4 + 5 + 6 \\ &\quad + 5 + 4 + 3 + 2 + 1 = 36. \end{aligned} \]

Two copies of the PMF of the sum of two dice, grey bars over 2 to 12 with heights 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 in 36ths. Top, titled P of X at least 10: the bars at 10, 11 and 12 are yellow and the note reads 3 plus 2 plus 1 equals 6 cells: 6/36 equals 1/6. Bottom, titled P of X is prime: the bars at 2, 3, 5, 7 and 11 are yellow and the note reads 1 plus 2 plus 4 plus 6 plus 2 equals 15: 15/36 equals 5/12.
M06.5.2-F4

A probability about \(X\) is a sum of bars. Top: the values 10 to 12. Bottom: the five prime values.

\({\Prob(X \geq 10)}\). Add the bars over 10, 11 and 12:

\[ \frac{3}{36} + \frac{2}{36} + \frac{1}{36} = \frac{6}{36} = \frac{1}{6}. \]

\({\Prob(X \text{ is prime})}\). The primes from 2 to 12 are 2, 3, 5, 7 and 11. Add their bars:

\[ \begin{aligned} &\frac{1 + 2 + 4 + 6 + 2}{36} \\ &\quad= \frac{15}{36} = \frac{5}{12}. \end{aligned} \]

Once the PMF is built, neither question needs the grid again.

Code

From scratch: apply the random variable to every outcome, count the values with collections.Counter, and turn counts into exact fractions.

import itertools
import math
from collections import Counter
from fractions import Fraction

omega = list(itertools.product(range(1, 7), repeat=2))
counts = Counter(a + b for a, b in omega)              # value -> number of outcomes
pmf = {x: Fraction(counts[x], len(omega)) for x in sorted(counts)}

for x, p in pmf.items():
    print(x, p)
print("total:", sum(pmf.values()))
print("P(X >= 10) =", sum(p for x, p in pmf.items() if x >= 10))
print("P(X prime) =", sum(pmf[x] for x in (2, 3, 5, 7, 11)))
2 1/36
3 1/18
4 1/12
5 1/9
6 5/36
7 1/6
8 5/36
9 1/9
10 1/12
11 1/18
12 1/36
total: 1
P(X >= 10) = 1/6
P(X prime) = 5/12

The PMF of a function of \(X\) pools bars. Here is \(W^2\) for three flips, and the Galton board with 4 and with 10 rows from math.comb.

flips = ["".join(s) for s in itertools.product("HT", repeat=3)]
W = Counter(2 * w.count("H") - 3 for w in flips)
pmf_W = {w: Fraction(c, 8) for w, c in sorted(W.items())}

pmf_W2 = Counter()
for w, p in pmf_W.items():
    pmf_W2[w * w] += p                   # values that square alike pool their bars
print("PMF of W:", pmf_W)
print("PMF of W squared:", dict(pmf_W2))

galton4 = [Fraction(math.comb(4, k), 2 ** 4) for k in range(5)]
print("Galton, 4 rows:", galton4)
print("Galton, 10 rows, bin 5:", math.comb(10, 5) / 2 ** 10)
PMF of W: {-3: Fraction(1, 8), -1: Fraction(3, 8), 1: Fraction(3, 8), 3: Fraction(1, 8)}
PMF of W squared: {9: Fraction(1, 4), 1: Fraction(3, 4)}
Galton, 4 rows: [Fraction(1, 16), Fraction(1, 4), Fraction(3, 8), Fraction(1, 4), Fraction(1, 16)]
Galton, 10 rows, bin 5: 0.24609375

Now simulate. Roll two dice 100,000 times, find the distinct sums and how often each appears with numpy.unique(..., return_counts=True), and divide by the number of rolls to get relative frequencies.

import numpy as np

rng = np.random.default_rng(7)
sums = rng.integers(1, 7, size=100_000) + rng.integers(1, 7, size=100_000)
values, freq = np.unique(sums, return_counts=True)
shares = freq / len(sums)
exact = np.array([float(pmf[x]) for x in values])
for x, s, e in zip(values, shares, exact):
    print(f"{x:2d}  simulated {s:.4f}   exact {e:.4f}")
print("largest gap:", np.max(np.abs(shares - exact)))
 2  simulated 0.0273   exact 0.0278
 3  simulated 0.0558   exact 0.0556
 4  simulated 0.0840   exact 0.0833
 5  simulated 0.1108   exact 0.1111
 6  simulated 0.1386   exact 0.1389
 7  simulated 0.1660   exact 0.1667
 8  simulated 0.1378   exact 0.1389
 9  simulated 0.1102   exact 0.1111
10  simulated 0.0850   exact 0.0833
11  simulated 0.0563   exact 0.0556
12  simulated 0.0282   exact 0.0278
largest gap: 0.0016466666666666713

The simulated shares sit within a hundredth of the exact bars, but they are not equal to them, and a different seed gives slightly different shares. The PMF is what those shares settle towards.

Explore

M06.5-X1

Galton board

Drop one ball and follow its path: that is the outcome, and the lit bin is the value. Then drop a hundred at a time and watch the blue shares settle onto the exact PMF bars; try 1 row, then 10.

A Galton board with 4 rows of pegs labelled with Pascal's triangle, and 5 bins holding bars of 1, 4, 6, 4 and 1 sixteenths: the PMF of the bin of a ball on a fair board, with the bar for bin 2 in yellow.

The middle bins fill fastest because more paths lead there, not because a ball prefers the middle: every path has the same probability. The tilt control, used again in M06.8.1, makes right bounces more or less likely than left ones.

Pitfalls

Two bar charts of a PMF for W squared over the values 1 and 9. Left, marked with a cross and titled copy one bar each: a bar of 3/8 at 1 and a bar of 1/8 at 9, with the note bars add to 1/2. Right, marked with a tick and titled pool the values: a yellow bar of 3/4 at 1 labelled 3/8 plus 3/8, and a bar of 1/4 at 9 labelled 1/8 plus 1/8, with the note bars add to 1.
M06.5.2-F6

Wrong beside right. Copying one bar of \(W\) for each value of \(W^2\) loses half the probability; the total of 1/2 gives the mistake away.

“The PMF of \(W^2\) is the PMF of \(W\) at the square root.”

Both \({-1}\) and 1 square to 1, so \({\{W^2 = 1\}}\) contains both events. Taking only \({p_W(1) = 3/8}\) gives bars that add to \(1/2\). Pooling gives \(3/4\) and \(1/4\), which add to 1. Checking that the bars add to 1 catches this mistake every time.

“The bar heights of a histogram are the PMF.”

A histogram of 100,000 simulated rolls shows relative frequencies, and they wobble: another seed gives other heights. The PMF is exact, the limit those frequencies settle towards. A histogram of raw counts is further off still, since its bars add to the number of rolls, not to 1.

“Every value of \(X\) is equally likely, because every outcome is.”

Equally likely outcomes do not make equally likely values: the sum 7 has six outcomes and the sum 2 has one. The 11 sums are not each \(1/11\) (lesson M06.2.1). Gather the outcomes first; the PMF inherits its shape from how many outcomes each value collects. A classifier’s predicted labels behave the same way: rare classes collect few examples.

Check yourself

Recall

1. Define the PMF of a discrete random variable \(X\).

Show answer

\({p_X(x) = \Prob(X = x)}\) for every real \(x\), the probability that \(X\) takes the value \(x\); it is 0 at values \(X\) never takes.

2. What two properties does every PMF have?

Show answer

Every bar is between 0 and 1, and the bars add up to 1.

3. How do you find \({\Prob(X \in A)}\) from the PMF?

Show answer

Add the bars over the values in \(A\): \({\Prob(X \in A)} = {\sum_{x \in A} p_X(x)}\).

4. What is the PMF of the bin of a fair Galton board with \(n\) rows?

Show answer

\({p_X(k) = \binom{n}{k}/2^n}\) for \({k = 0, \dots, n}\): \(\binom{n}{k}\) paths end in bin \(k\), each with probability \({1/2^n}\).

5. Why do the bars of \({p_N(k) = (1/2)^k}\), \({k = 1, 2, \dots}\) add to 1?

Show answer

They form a geometric series with first term \(1/2\) and ratio \(1/2\), whose sum is \({(1/2)/(1 - 1/2) = 1}\).

Exercises

1. ★ Let \(M\) be the larger of two dice. Show that \({p_M(m) = (2m - 1)/36}\) for \({m = 1, \dots, 6}\), check that the bars add to 1, and find \({\Prob(M \geq 5)}\).

Show solution

\({\{M = m\}}\) holds the outcomes where one die is \(m\) and the other is at most \(m\): the \(m\) cells \({(i, m)}\) with \({i \leq m}\) and the \(m\) cells \({(m, j)}\) with \({j \leq m}\), with the corner \({(m, m)}\) counted twice. That is \({m + m - 1 = 2m - 1}\) cells. The counts 1, 3, 5, 7, 9, 11 add to 36, so the bars add to 1. Then

\[ \begin{aligned} \Prob(M \geq 5) &= \frac{9 + 11}{36} \\ &= \frac{20}{36} = \frac{5}{9}. \end{aligned} \]

2. ★★ A fair Galton board has 5 rows. Write down its PMF and find the probability that a ball lands in bin 2 or bin 3.

Show solution

There are \({2^5 = 32}\) paths, and \(\binom{5}{k}\) end in bin \(k\): the counts are 1, 5, 10, 10, 5, 1, so \({p_X(k) = \binom{5}{k}/32}\). The bins are disjoint events, so

\[ \Prob(X \in \{2, 3\}) = \frac{10 + 10}{32} = \frac{5}{8}. \]

3. ★★★ A random variable takes the values \({k = 1, 2, 3, \dots}\) with \({p(k) = c\,(1/3)^k}\). Find the constant \(c\) that makes this a PMF, then find \({\Prob(X \geq 3)}\) in two ways.

Show solution

The bars must add to 1. The geometric series with first term \(1/3\) and ratio \(1/3\) gives

\[ \sum_{k=1}^{\infty} \left(\tfrac{1}{3}\right)^k = \frac{1/3}{1 - 1/3} = \frac{1}{2}, \]

so \({c \cdot 1/2 = 1}\) and \({c = 2}\). First way, by the complement:

\[ \begin{aligned} \Prob(X \geq 3) &= 1 - p(1) - p(2) \\ &= 1 - \frac{2}{3} - \frac{2}{9} = \frac{1}{9}. \end{aligned} \]

Second way, summing the tail as a geometric series with first term \({2 (1/3)^3 = 2/27}\) and ratio \(1/3\): \({(2/27)/(2/3) = 1/9}\). The two agree.

Summary card

The PMF gathers the outcomes for each value into one bar.

The anchor figure again: the 6 by 6 grid of outcomes of two dice with the six cells of sum 7 in yellow, beside the PMF of the sum, bars from 1/36 up to 6/36 at 7 and back down to 1/36 at 12.
M06.5.2-F1

Gather outcomes, then add bars.

  • Definition. \({p_X(x) = \Prob(X = x)}\).
  • Rules. \({p_X(x) \geq 0}\) and \({\sum_x p_X(x) = 1}\).
  • Sets of values. \({\Prob(X \in A)} = {\sum_{x \in A} p_X(x)}\).
  • Functions. \(p_{g(X)}(y)\) adds \(p_X(x)\) over all \(x\) with \({g(x) = y}\).
  • Galton board. \({p_X(k) = \binom{n}{k}/2^n}\).

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Hossein Pishro-Nik, Introduction to Probability, Statistics, and Random Processes, Section 3.1.3. The definition of the PMF as \({P_X(x_k)} = {P(X = x_k)}\) and its properties: every value lies between 0 and 1, the values add to 1, and \({P(X \in A)}\) is the sum over \(A\).
  • Jeremy Orloff and Jonathan Bloom, Reading 4a: Discrete Random Variables, MIT OpenCourseWare 18.05, Spring 2022. Section 2.4 defines the pmf as \({p(a) = P(X = a)}\), notes that it is 0 at values \(X\) never takes, and tabulates the PMF of the maximum of two dice, 1/36, 3/36, …, 11/36, which is exercise 1 here.
  • NumPy documentation, numpy.unique: return_counts=True also returns “the number of times each unique item appears”, and the unique values come back sorted.

Next. M06.5.3 The cumulative distribution function stacks the bars into a staircase.

This lesson unlocks

M06.5.3 The cumulative distribution function M06.7.1 Expectation: the long-run average M06.8.1 Bernoulli and binomial: counting successes

Your progress is saved in this browser only. No account, no tracking.
Back to top