Skip to content

Bayes' Theorem and Conditional Probability

Bayes' theorem lets you update your beliefs when new evidence arrives. It is the foundation of Bayesian statistics and many machine learning algorithms.

1 of 5 ~5 min

Recap: Conditional Probability

Before diving into Bayes’ theorem, let us revisit the conditional probability formula from the first article:

P(A∣B)=P(A∩B)P(B)P(A|B) = \frac{P(A \cap B)}{P(B)}

This reads as “the probability of AA given BB equals the joint probability of AA and BB, divided by the probability of BB.” The intuition: restricting our attention to the world where BB has happened, what fraction of those outcomes also have AA?

By rearranging this definition, we get the multiplication rule:

P(A∩B)=P(A∣B)⋅P(B)=P(B∣A)⋅P(A)P(A \cap B) = P(A|B) \cdot P(B) = P(B|A) \cdot P(A)

Both versions are equivalent. Which one you start from depends on what you know and what you are trying to find.

Bayes’ Theorem

Bayes’ theorem is simply a rearrangement of the multiplication rule, but its implications are enormous:

P(A∣B)=P(B∣A)⋅P(A)P(B)P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}

In words:

P(posterior)=P(likelihood)⋅P(prior)P(evidence)P(\text{posterior}) = \frac{P(\text{likelihood}) \cdot P(\text{prior})}{P(\text{evidence})}
  • Prior P(A)P(A): Your initial belief about AA before seeing any evidence.
  • Likelihood P(B∣A)P(B|A): How likely would this evidence BB be, if AA were true?
  • Posterior P(A∣B)P(A|B): Your updated belief about AA after seeing evidence BB.
  • Normalizing constant P(B)P(B): Ensures the posterior is a valid probability (sums to 1 over all possible hypotheses).

The normalizing constant is calculated as:

P(B)=P(B∣A)⋅P(A)+P(B∣Ac)⋅P(Ac)P(B) = P(B|A) \cdot P(A) + P(B|A^c) \cdot P(A^c)

This is just the law of total probability: the total probability of observing evidence BB is the weighted average of the likelihood under each possible hypothesis.

A Concrete Example: Earnings Surprise

Suppose you are analyzing a stock. Let UU be the event “the company reports positive earnings surprise” and let RR be the event “the stock goes up the next day.”

You have historical data telling you that:

  • P(U)=0.4P(U) = 0.4 (40% of quarters have a positive surprise)
  • P(R)=0.5P(R) = 0.5 (the stock goes up 50% of trading days)
  • P(R∣U)=0.75P(R|U) = 0.75 (given a positive surprise, the stock goes up 75% of the time)

What is P(U∣R)P(U|R) the probability of a positive earnings surprise given that the stock went up?

P(U∣R)=P(R∣U)⋅P(U)P(R)=0.75×0.40.5=0.300.50=0.60P(U|R) = \frac{P(R|U) \cdot P(U)}{P(R)} = \frac{0.75 \times 0.4}{0.5} = \frac{0.30}{0.50} = 0.60

So given that the stock went up, there is a 60% chance there was a positive earnings surprise, higher than the base rate of 40%. The evidence has updated our belief upward.

Using a 2×2 Table for Intuition

Stock Up (RR)Stock Down (RcR^c)Total
Surprise (UU)301040
No Surprise (UcU^c)204060
Total5050100

The table shows what happens if we observe 100 days. From the marginals, 40 days have a surprise and 50 days have a stock up day. The joint count of “surprise AND stock up” is 0.75×40=300.75 \times 40 = 30. The conditional P(U∣R)=30/50=0.60P(U|R) = 30/50 = 0.60, exactly as Bayes’ theorem computed.

Conjugate Priors

A powerful practical simplification arises with conjugate priors. If your prior and posterior are from the same family of distributions, then updating is computationally trivial.

The Beta-Binomial conjugate is a classic example. Suppose you observe kk successes in nn Bernoulli trials (like kk up-days in nn trading days). If your prior on the success probability θ\theta is Beta(α,β\alpha, \beta), then the posterior is also Beta:

P(θ∣k)=Beta(α+k,β+n−k)P(\theta | k) = \text{Beta}(\alpha + k, \beta + n - k)

You simply add the observed counts to the prior hyperparameters. No complicated integration required.

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats


alpha_prior, beta_prior = 2, 2

# Observed data: 6 up days out of 10 trading days
k = 6
n = 10

# Posterior: Beta(alpha + k, beta + n - k)
alpha_post = alpha_prior + k
beta_post = beta_prior + n - k

# Plot prior and posterior
theta = np.linspace(0, 1, 1000)
prior_pdf = stats.beta.pdf(theta, alpha_prior, beta_prior)
post_pdf = stats.beta.pdf(theta, alpha_post, beta_post)

plt.figure(figsize=(10, 5))
plt.plot(theta, prior_pdf, 'b--', lw=2, label=f'Prior Beta({alpha_prior},{beta_prior})')
plt.plot(theta, post_pdf, 'r-', lw=2, label=f'Posterior Beta({alpha_post},{beta_post})')
plt.axvline(x=0.6, color='gray', linestyle=':', label='MPE = 0.6')
plt.xlabel('θ (probability of up day)')
plt.ylabel('Density')
plt.title('Bayesian Update: Beta-Binomial Conjugate')
plt.legend()
plt.grid(alpha=0.3)
plt.show()

# Posterior mean (Bayesian estimate of theta)
posterior_mean = alpha_post / (alpha_post + beta_post)
print(f"Prior mean:   {alpha_prior / (alpha_prior + beta_prior):.4f}")
print(f"Posterior mean: {posterior_mean:.4f}")
print(f"MLE (k/n):    {k / n:.4f}")

PROMPT

Use numpy, matplotlib, and scipy.stats to do a Bayesian update of a Beta prior with binomial data, plot the prior and posterior, and print the means.

The posterior mean (0.57) is a compromise between the prior mean (0.5) and the maximum likelihood estimate (0.6). The more data you observe, the more the posterior shifts toward the data.

Applications in Quantitative Finance

Bayesian methods appear throughout quant finance. In signal processing, you might use a Kalman filter to update your estimate of an asset’s true price as new trades occur, each new price observation is evidence that updates your belief via Bayes’ theorem. In machine learning, naive Bayes classifiers use Bayes’ theorem to classify documents or market regimes based on observed features. In risk management, Bayesian updating allows you to incorporate new information (a credit downgrade, a macro shock) into your prior estimates of default probability.

The Base Rate Fallacy

Bayes’ theorem also explains a famous cognitive error: the base-rate fallacy. People tend to anchor on the accuracy of new evidence (a test, a signal, a diagnosis) while ignoring how rare the underlying event actually is. The result is a posterior that is wildly different from what intuition expects.

Consider a screening test for a rare disease affecting 1 in 1,000 people in the general population. The test is 99% accurate in both directions: it correctly identifies 99% of people who have the disease (sensitivity) and correctly clears 99% of healthy people (specificity). Let DD denote “has the disease” and ++ denote “tests positive.” Translating the givens:

  • P(D)=0.001P(D) = 0.001 base rate (prior)
  • P(+∣D)=0.99P(+|D) = 0.99 sensitivity (true positive rate)
  • P(+∣¬D)=0.01P(+|\neg D) = 0.01 false positive rate

We want P(D∣+)P(D|+) the probability a person actually has the disease given a positive test. First, by the law of total probability:

P(+)=P(+∣D)⋅P(D)+P(+∣¬D)⋅P(¬D)=(0.99)(0.001)+(0.01)(0.999)=0.01098P(+) = P(+|D) \cdot P(D) + P(+|\neg D) \cdot P(\neg D) = (0.99)(0.001) + (0.01)(0.999) = 0.01098

Then by Bayes’ theorem:

P(D∣+)=(0.99)(0.001)0.01098≈0.090P(D|+) = \frac{(0.99)(0.001)}{0.01098} \approx 0.090

So even with a 99%-accurate test, only about 9% of people who test positive actually have the disease. The naive intuition “99% accurate test → 99% chance of having the disease” ignores that the disease is so rare that false positives vastly outnumber true positives. This is the base-rate fallacy in action, and it shows up everywhere, medical testing, fraud detection, hiring screens, and trading signals, wherever the prior probability is small.

References

Gelman, A. et al. Bayesian Data Analysis, Chapter 1. CRC Press. Ross, S. M. A First Course in Probability, Chapter 3. Pearson.