Skip to main content
v2026.11,858 entries · CC-BY 4.0

Probability Mass Function: Formula, Worked Example and Code

The probability mass function formula p(x) = P(X = x), explained with worked examples, a reading guide, APA reporting and Excel, R and Python code.

Written and maintained by CASRAI Editorial Board

Last updated

A probability mass function (PMF) gives the probability that a discrete random variable equals each exact value it can take. Its formula is p(x) = P(X = x), and for a valid PMF every probability is between 0 and 1 and all the probabilities add to 1. This guide shows the formula, a worked example with the arithmetic, how to read and report the result, and one-line code in Excel, R and Python.

What is a probability mass function?

A probability mass function is the rule that assigns a probability to each possible value of a discrete random variable, meaning a variable that takes countable values such as 0, 1, 2, 3. If X is the number of heads in three coin tosses, the PMF lists P(X = 0), P(X = 1), P(X = 2) and P(X = 3). The word “mass” reflects that the probability sits on individual points, unlike a continuous variable, where probability is spread over an interval and a density is used instead.

Terminology varies by source. The NIST/SEMATECH e-Handbook of Statistical Methods writes the discrete case as f(x) = Pr[X = x] and calls it the probability density function for a discrete variable; OpenStax calls it a probability distribution function (PDF) for a discrete random variable. If a textbook says “discrete pdf”, it means the PMF.

Probability mass function formula

The general definition is:

p(x) = P(X = x)

  • X: the random variable (the quantity being counted or observed).
  • x: one specific value that X can take.
  • p(x): the probability that X equals exactly that value.

A valid PMF has two properties, stated in OpenStax Introductory Statistics 2e: each probability is between zero and one inclusive, and the probabilities sum to one.

Binomial probability mass function formula

For a count of successes in a fixed number of independent trials, NIST gives the binomial PMF as:

P(x; p, n) = C(n, x) · px · (1 − p)n − x, for x = 0, 1, …, n

  • n: the number of trials.
  • x: the number of successes.
  • p: the probability of success on each trial, the same on every trial.
  • C(n, x) = n! / (x! (n − x)!): the number of ways to choose which x trials are the successes.

The related cumulative function adds PMF values: for a discrete variable, F(x) is the sum of f(i) for all values i up to x (NIST). See the cumulative distribution function for that side of the picture.

Probability mass function examples

Example 1: a PMF given as a table, with a validity check

OpenStax Introductory Statistics 2e, Example 4.1, records how many times per week a newborn’s crying woke each of 50 mothers after midnight. Dividing each count by 50 gives the PMF:

x (wakings) Mothers P(X = x)
0 2 2/50 = 0.04
1 11 11/50 = 0.22
2 23 23/50 = 0.46
3 9 9/50 = 0.18
4 4 4/50 = 0.08
5 1 1/50 = 0.02

Check 1, range: every value lies between 0 and 1.

Check 2, total: 2 + 11 + 23 + 9 + 4 + 1 = 50, so the probabilities sum to 50/50 = 1. Both conditions hold, so this is a valid PMF.

A probability from the PMF: P(X ≥ 3) = P(3) + P(4) + P(5) = 0.18 + 0.08 + 0.02 = 0.28.

The expected value: E(X) = Σ x · P(x) = 0(0.04) + 1(0.22) + 2(0.46) + 3(0.18) + 4(0.08) + 5(0.02) = 0 + 0.22 + 0.92 + 0.54 + 0.32 + 0.10 = 2.10 wakings per week. Check: (0×2 + 1×11 + 2×23 + 3×9 + 4×4 + 5×1) / 50 = 105/50 = 2.10. More on this calculation in the expected value formula guide.

Example 2: a binomial PMF

Let X be the number of successes in n = 5 independent trials with p = 0.3. Then P(X = 2) = C(5, 2) · 0.32 · 0.73.

  • C(5, 2) = 5! / (2! · 3!) = 120 / (2 × 6) = 10
  • 0.32 = 0.09 and 0.73 = 0.343
  • 10 × 0.09 × 0.343 = 0.3087

The full PMF for x = 0 to 5 is 0.16807, 0.36015, 0.30870, 0.13230, 0.02835 and 0.00243, which sum to 1. For more on this family see the binomial probability calculator guide and binomial distribution expected value.

How to interpret a PMF value

What you see How to read it
p(x) = 0.46 at x = 2 Under the model, X equals exactly 2 with probability 0.46. A PMF value is itself a probability.
Largest p(x) in the table The most likely single value (the mode). In Example 1 it is x = 2.
Sum of p(x) over a set of values The probability that X falls in that set, as in P(X ≥ 3) = 0.28.
Running total of p(x) up to x The cumulative probability F(x) = P(X ≤ x).
p(x) = 0 X cannot take that value under the model.

Assumptions and common mistakes

  • The variable must be discrete. For a continuous variable, P(X = x) is not the quantity to tabulate; a density function is used instead.
  • Probabilities must total 1. If a table of counts divided by n does not sum to 1, a category is missing or a count is wrong.
  • Binomial use needs a fixed n, a constant p and independent trials. These are the conditions in NIST’s definition of x successes in n trials with a fixed success probability.
  • Do not confuse P(X = x) with P(X ≤ x). The first is the PMF and the second is the CDF; in Example 1, P(X = 2) = 0.46 but P(X ≤ 2) = 0.04 + 0.22 + 0.46 = 0.72.
  • Mind the name. Some sources say “pdf” for a discrete variable; the object is still a PMF.

How to report a PMF result (APA example)

Report the model, the value and the probability. For example: “The number of night wakings per week among 50 mothers was modelled with an empirical probability mass function (Table 1). The probability of three or more wakings was .28, and the expected number was 2.10.” APA style omits the leading zero for statistics that cannot exceed 1, such as probabilities. Put the full PMF in a table and state n.

How to calculate a PMF in Excel, R or Python

These compute the binomial case from Example 2 (n = 5, p = 0.3, x = 2); each returns 0.3087.

Excel

=BINOM.DIST(2, 5, 0.3, FALSE)

The last argument, FALSE, asks for the mass at exactly 2; TRUE would return the cumulative probability, 0.83692.

R

dbinom(2, size = 5, prob = 0.3)   # 0.3087
dbinom(0:5, 5, 0.3)               # whole PMF

Python

from scipy.stats import binom
binom.pmf(2, 5, 0.3)              # 0.3087
binom.pmf(range(6), 5, 0.3)       # whole PMF

For an empirical table like Example 1, divide each count by the total: [c/50 for c in counts] in Python, or counts/sum(counts) in R.

Frequently asked questions

What is a probability mass function?

It is the function that gives P(X = x) for each value x of a discrete random variable. Its values are between 0 and 1 and add up to 1.

What is the probability mass function formula?

The general form is p(x) = P(X = x). For the binomial distribution it is C(n, x) px (1 − p)n − x.

What is the difference between a PMF and a CDF?

The PMF gives the probability at one value; the CDF adds the PMF values up to that point, so F(x) = P(X ≤ x).

Can a PMF value be greater than 1?

No. Each PMF value is a probability, so it lies between 0 and 1 inclusive.

Sources

Related reading: negative binomial distribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Probability Mass Function: Formula, Worked Example and Code

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.