How to Apply Bayes’ Theorem to Multiple Random Variables
At a glance
- 1. Specify the unknowns
Treat the variables you want to infer as a joint vector, using their joint prior distribution.
- 2. Write the likelihood
Find the probability or density of the observed evidence given all unknown variables.
- 3. Normalize
Sum or integrate the likelihood times the joint prior over every possible value.
- 4. Marginalize if needed
Sum or integrate the joint posterior over variables you do not need.
Bayes’ theorem extends directly to several unknown random variables: use their joint prior and the likelihood of the evidence given all of them. To find the posterior for just one variable, sum or integrate the joint posterior over the variables you do not need. The variables do not have to be independent.
The joint form of Bayes’ theorem
Let be unknown variables and let denote observed evidence. For discrete variables,
The prior in the numerator is the joint probability of the variables taking those values. In general, it is not the product of their individual probabilities. The denominator is the probability of the evidence under all possible values of the unknowns:
The sum ranges over every possible combination of values. For continuous variables, replace the sum with an integral and use probability densities; if the evidence is continuous, use its density as well. For example, with two continuous unknowns and ,
Here is the joint prior density. The posterior density integrates to 1, just as the discrete posterior probabilities sum to 1.
Finding the posterior for only one variable
Often you want to infer but do not care about another unknown variable . First form the joint posterior, then marginalize over :
for discrete . For continuous , use an integral instead:
This step accounts for every possible value of the nuisance variable rather than pretending it is known. If there are several nuisance variables, sum or integrate over all of them.
Worked example with two binary variables
Suppose and are binary unknowns, and an observation has the following prior probabilities and likelihoods. The prior entries already describe the joint distribution, so no independence assumption is needed.
| Prior | Likelihood | Product | ||
|---|---|---|---|---|
| 0 | 0 | 0.50 | 0.10 | 0.050 |
| 0 | 1 | 0.20 | 0.20 | 0.040 |
| 1 | 0 | 0.20 | 0.60 | 0.120 |
| 1 | 1 | 0.10 | 0.90 | 0.090 |
The last column is the likelihood multiplied by the prior for each combination. Add those products to get the evidence probability:
Divide each product by to get the joint posterior:
| Posterior | ||
|---|---|---|
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
For the posterior probability that , add the rows with :
For , add the rows with :
These marginal results come from the joint posterior. The same method works with more variables, although the number of combinations can grow quickly.
When can the likelihood be split?
If the evidence consists of several observations , the likelihood is generally the joint probability . You may write it as a product of separate likelihoods only when the observations are conditionally independent given the unknown variables. Similarly, replacing a joint prior with a product of individual priors requires the unknown variables to be independent under that prior.
A model can have some conditional independence relationships and not others. Use only the independence assumptions justified by the model or the problem statement; Bayes’ theorem itself does not supply them.
Common mistakes
- Multiplying marginal priors automatically. In general, is not . Use the joint prior unless independence is established.
- Forgetting the denominator. The likelihood times the prior gives unnormalized posterior weights. Divide by the total evidence probability to normalize them.
- Ignoring nuisance variables. If a variable is unknown but not of interest, marginalize over it rather than fixing it to an arbitrary value.
- Confusing densities with point probabilities. For continuous variables, a density at a point is not the probability of that exact value. Bayes’ density formula gives a conditional density, and the evidence density must be positive.
For discrete variables the rule is a sum over possible values; for continuous variables it is an integral. In either case, the procedure is the same: combine the joint prior with the likelihood, normalize, and marginalize if you need a smaller posterior distribution.
References
- Introduction to Probability, 2nd ed., Joseph K. Blitzstein and Jessica Hwang
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.