Skip to content
LeetQuant
E02 Bayesian Statistics

E02 · Bayesian Statistics · Lesson 1 of 4

Conjugate Models

Why exponential families have conjugate priors, the Normal–Inverse-Gamma model for a mean and variance that are both unknown and its Student-t predictive, the Dirichlet–Multinomial for categorical data, and posterior predictive checks.

18 min

Objective

The Bayesian Estimation lesson listed three conjugate pairs (Beta–Binomial, Normal–Normal with known variance, Gamma–Poisson) and showed that the posterior mean is a shrinkage estimator. This lesson explains why those pairs exist, handles the case that matters most for returns (mean and variance both unknown), extends the Beta to many categories, and shows how to test whether a fitted Bayesian model actually reproduces the data. By the end you should be able to write down a conjugate update from the likelihood alone, compute a Student-t predictive interval by hand, and design a posterior predictive check.

Where conjugacy comes from

A family is an exponential family if its density can be written

p(x∣η)=h(x) exp⁡ ⁣(η⊤T(x)−A(η)),p(x \mid \eta) = h(x)\, \exp\!\big(\eta^\top T(x) - A(\eta)\big),

with natural parameter η\eta, sufficient statistic T(x)T(x) and log-normaliser A(η)A(\eta). For nn independent draws the likelihood is exp⁡(η⊤∑iT(xi)−nA(η))\exp\big(\eta^\top \sum_i T(x_i) - n A(\eta)\big) times a factor free of η\eta. Any prior of the same shape in η\eta,

π(η∣χ,ν)=exp⁡(η⊤χ−νA(η))Z(χ,ν),\pi(\eta \mid \chi, \nu) = \frac{\exp\big(\eta^\top \chi - \nu A(\eta)\big)}{Z(\chi, \nu)},

is conjugate, because multiplying the two just adds exponents:

χn=χ+∑i=1nT(xi),νn=ν+n.\chi_n = \chi + \sum_{i=1}^{n} T(x_i), \qquad \nu_n = \nu + n .

So the prior is literally pseudo-data: ν\nu prior observations whose sufficient statistics sum to χ\chi. Three consequences are worth knowing:

  1. The posterior mean of E[T(x)]\mathbb{E}[T(x)] is linear in the data: (χ+∑T(xi))/(ν+n)(\chi + \sum T(x_i))/(\nu + n) (Diaconis–Ylvisaker). Every shrinkage formula in the earlier lesson is a special case.
  2. Predictive and evidence are ratios of normalisers. The posterior predictive is p(xnew∣x1:n)=h(xnew) Z(χn+T(xnew),νn+1)/Z(χn,νn)p(x_{\text{new}} \mid x_{1:n}) = h(x_{\text{new}})\, Z(\chi_n + T(x_{\text{new}}), \nu_n + 1)/Z(\chi_n, \nu_n) and the marginal likelihood is ∏ih(xi)⋅Z(χn,νn)/Z(χ,ν)\prod_i h(x_i) \cdot Z(\chi_n, \nu_n)/Z(\chi, \nu). The second formula is what makes exact Bayes factors possible in the model-comparison lesson.
  3. Outside exponential families there is no fixed-size conjugate prior. By the Pitman–Koopman–Darmois theorem, only exponential families (under regularity) have sufficient statistics of fixed dimension. A Student-t likelihood for fat-tailed returns has none, which is one reason MCMC exists.

Check with the Bernoulli: px(1−p)1−x=exp⁡(xlog⁡p1−p+log⁡(1−p))p^x (1 - p)^{1-x} = \exp\big(x \log\tfrac{p}{1-p} + \log(1 - p)\big), so η=logit⁡p\eta = \operatorname{logit} p, T(x)=xT(x) = x, A(η)=log⁡(1+eη)A(\eta) = \log(1 + e^\eta). The conjugate prior exp⁡(ηχ−νA)\exp(\eta\chi - \nu A) equals pχ(1−p)ν−χp^{\chi}(1-p)^{\nu - \chi} in η\eta-space; converting to a density in pp with the Jacobian dη/dp=1/(p(1−p))d\eta/dp = 1/(p(1-p)) gives Beta(χ,ν−χ)\text{Beta}(\chi, \nu - \chi): χ\chi prior successes in ν\nu prior trials.

Normal data, mean and variance both unknown

Returns have an unknown mean and an unknown volatility, and with a short history the uncertainty in the volatility matters. The conjugate prior is the Normal–Inverse-Gamma (NIG):

σ2∼Inv-Gamma(a0,b0),μ∣σ2∼N ⁣(m0,σ2κ0).\sigma^2 \sim \text{Inv-Gamma}(a_0, b_0), \qquad \mu \mid \sigma^2 \sim N\!\left(m_0, \frac{\sigma^2}{\kappa_0}\right) .

Read κ0\kappa_0 as the number of prior observations behind m0m_0, and 2a02a_0 as the number behind the variance guess (the prior mean of σ2\sigma^2 is b0/(a0−1)b_0/(a_0 - 1)). With nn observations, mean xˉ\bar x and sum of squares S=∑i(xi−xˉ)2S = \sum_i (x_i - \bar x)^2, the posterior is NIG with

κn=κ0+n,mn=κ0m0+nxˉκn,an=a0+n2,bn=b0+S2+κ0n(xˉ−m0)22κn.\kappa_n = \kappa_0 + n, \quad m_n = \frac{\kappa_0 m_0 + n \bar x}{\kappa_n}, \quad a_n = a_0 + \frac n2, \quad b_n = b_0 + \frac S2 + \frac{\kappa_0 n (\bar x - m_0)^2}{2 \kappa_n} .

The three pieces of bnb_n are the prior's scale, the scatter within the sample, and a penalty for disagreement between the prior mean and the sample mean: a surprising xˉ\bar x makes you less sure of the volatility too.

Integrating out σ2\sigma^2 gives Student-t distributions. Integrating (σ2)−(a+3/2)exp⁡{−(b+κ(μ−m)2/2)/σ2}(\sigma^2)^{-(a + 3/2)} \exp\{-(b + \kappa(\mu - m)^2/2)/\sigma^2\} over σ2\sigma^2 leaves (b+κ(μ−m)2/2)−(a+1/2)(b + \kappa(\mu - m)^2/2)^{-(a + 1/2)}, which is a tt kernel with 2a2a degrees of freedom. So

μ∣x∼t2an ⁣(mn,  bnanκn),xnew∣x∼t2an ⁣(mn,  bn(κn+1)anκn),\mu \mid x \sim t_{2a_n}\!\left(m_n,\; \frac{b_n}{a_n \kappa_n}\right), \qquad x_{\text{new}} \mid x \sim t_{2a_n}\!\left(m_n,\; \frac{b_n (\kappa_n + 1)}{a_n \kappa_n}\right),

where the second argument is the squared scale. The predictive is wider than the plug-in N(xˉ,s2)N(\bar x, s^2) for two reasons: the κn+1\kappa_n + 1 factor adds uncertainty about μ\mu, and the tt tails add uncertainty about σ\sigma. With small samples the tail difference is large.

In the limit κ0→0\kappa_0 \to 0, a0→−12a_0 \to -\tfrac12, b0→0b_0 \to 0 (the prior p(μ,σ2)∝1/σ2p(\mu, \sigma^2) \propto 1/\sigma^2) you get an=(n−1)/2a_n = (n-1)/2 and μ∣x∼tn−1(xˉ,s2/n)\mu \mid x \sim t_{n-1}(\bar x, s^2/n): the classical tt interval, now with a probability statement attached.

Worked example

a new strategy with twelve months of returns

A strategy has 12 monthly returns with mean xˉ=1.0%\bar x = 1.0\% and sample standard deviation s=3.0%s = 3.0\%. Your prior, from similar strategies: m0=0m_0 = 0, κ0=4\kappa_0 = 4 (worth four months), a0=3a_0 = 3, b0=32b_0 = 32 (prior mean variance 32/2=1632/2 = 16, i.e. about 4% monthly vol). Find the posterior for μ\mu, P(μ>0)P(\mu > 0), and a 95% predictive interval for next month. Compare the chance of a month below −5%-5\% with the plug-in normal answer.

Show solution

Update. S=(n−1)s2=11×9=99S = (n - 1)s^2 = 11 \times 9 = 99.

  • κn=4+12=16\kappa_n = 4 + 12 = 16, mn=(0+12×1.0)/16=0.75m_n = (0 + 12 \times 1.0)/16 = 0.75.
  • an=3+6=9a_n = 3 + 6 = 9, so 2an=182a_n = 18 degrees of freedom.
  • bn=32+99/2+4×12×122×16=32+49.5+1.5=83b_n = 32 + 99/2 + \dfrac{4 \times 12 \times 1^2}{2 \times 16} = 32 + 49.5 + 1.5 = 83.

The mean. Squared scale bn/(anκn)=83/144=0.576b_n/(a_n\kappa_n) = 83/144 = 0.576, scale 0.7590.759. So μ∣x∼t18(0.75,0.7592)\mu \mid x \sim t_{18}(0.75, 0.759^2). With t18,0.975=2.101t_{18, 0.975} = 2.101 the 95% credible interval is 0.75±1.60=[−0.85%,2.35%]0.75 \pm 1.60 = [-0.85\%, 2.35\%], and P(μ>0)=Ft18(0.75/0.759)=Ft18(0.988)=0.832P(\mu > 0) = F_{t_{18}}(0.75/0.759) = F_{t_{18}}(0.988) = 0.832. The sample mean of 1% was pulled to 0.75% because the prior is worth 4 of the 16 effective observations.

Next month. Squared scale 83×17/144=9.8083 \times 17/144 = 9.80, scale 3.133.13. The 95% predictive interval is 0.75±2.101×3.13=[−5.83%,7.33%]0.75 \pm 2.101 \times 3.13 = [-5.83\%, 7.33\%].

Tail. Under the predictive, P(xnew<−5)=Ft18((−5−0.75)/3.13)=0.041P(x_{\text{new}} < -5) = F_{t_{18}}((-5 - 0.75)/3.13) = 0.041. The plug-in N(1,32)N(1, 3^2) gives Φ(−2)=0.023\Phi(-2) = 0.023. The Bayesian answer is almost twice as large: twelve months cannot pin down the volatility, and the plug-in pretends they can. Note also the posterior mean of σ2\sigma^2 is bn/(an−1)=83/8=10.4b_n/(a_n - 1) = 83/8 = 10.4 (about 3.2% vol), higher than s2=9s^2 = 9, because the prior expected more volatility.

Many categories: Dirichlet–Multinomial

For KK outcomes with probabilities p=(p1,…,pK)p = (p_1, \dots, p_K) and counts n=(n1,…,nK)n = (n_1, \dots, n_K) from NN trials, the conjugate prior is the Dirichlet, π(p)∝∏kpkαk−1\pi(p) \propto \prod_k p_k^{\alpha_k - 1}, and

p∣n∼Dir(α1+n1,…,αK+nK).p \mid n \sim \text{Dir}(\alpha_1 + n_1, \dots, \alpha_K + n_K) .

With α0=∑kαk\alpha_0 = \sum_k \alpha_k, each margin is pk∼Beta(αk,α0−αk)p_k \sim \text{Beta}(\alpha_k, \alpha_0 - \alpha_k), so

E[pk]=αkα0,Var⁡(pk)=αk(α0−αk)α02(α0+1),P(next=k∣n)=αk+nkα0+N.\mathbb{E}[p_k] = \frac{\alpha_k}{\alpha_0}, \qquad \operatorname{Var}(p_k) = \frac{\alpha_k(\alpha_0 - \alpha_k)}{\alpha_0^2(\alpha_0 + 1)}, \qquad P(\text{next} = k \mid n) = \frac{\alpha_k + n_k}{\alpha_0 + N} .

The components are negatively correlated, since they sum to one. Uses in quant work: the next tick direction (up, flat, down), the frequency of market regimes, each row of a Markov transition matrix (a credit-rating or regime-switching matrix estimated from few transitions), and order-type mixes. The prior's job is mostly to stop a category with zero observed counts from getting probability zero: with Dir(1,…,1)\text{Dir}(1, \dots, 1) the predictive is Laplace's rule of succession generalised to KK outcomes.

Worked example

next tick direction

You model the next mid-price move in an illiquid name as up, flat or down, with prior Dir(2,2,2)\text{Dir}(2, 2, 2). In 10 moves you see 7 up, 2 flat and 1 down. Find the posterior, the predictive probabilities for the next move, the posterior SD of pupp_{\text{up}}, P(pup>0.5)P(p_{\text{up}} > 0.5), and the probability that the next two moves are both up.

Show solution

Posterior. Dir(9,4,3)\text{Dir}(9, 4, 3) with α0=16\alpha_0 = 16.

Predictive. 9/16=0.56259/16 = 0.5625 up, 4/16=0.254/16 = 0.25 flat, 3/16=0.18753/16 = 0.1875 down. The raw frequencies 0.7,0.2,0.10.7, 0.2, 0.1 are pulled towards 13\tfrac13 each by six pseudo-observations.

Uncertainty. Var⁡(pup)=9×7162×17=0.01448\operatorname{Var}(p_{\text{up}}) = \dfrac{9 \times 7}{16^2 \times 17} = 0.01448, SD 0.1200.120. The margin is pup∼Beta(9,7)p_{\text{up}} \sim \text{Beta}(9, 7), and P(pup>0.5)=0.696P(p_{\text{up}} > 0.5) = 0.696: ten moves leave a lot of doubt about whether up-moves are even the majority.

Two ups in a row. The predictive for a pair is E[pup2]\mathbb{E}[p_{\text{up}}^2], not E[pup]2\mathbb{E}[p_{\text{up}}]^2:

E[pup2]=α1(α1+1)α0(α0+1)=9×1016×17=0.331,\mathbb{E}[p_{\text{up}}^2] = \frac{\alpha_1(\alpha_1 + 1)}{\alpha_0(\alpha_0 + 1)} = \frac{9 \times 10}{16 \times 17} = 0.331,

compared with 0.56252=0.3160.5625^2 = 0.316. Equivalently, update on the first up (to Dir(10,4,3)\text{Dir}(10, 4, 3)) and then predict 10/1710/17: 916×1017=0.331\tfrac{9}{16} \times \tfrac{10}{17} = 0.331. Future draws are positively correlated through the unknown pp; treating them as independent at the posterior mean understates the chance of runs.

Posterior predictive checks

A posterior is only as good as the model. A posterior predictive check asks whether data simulated from the fitted model look like the data you have:

  1. Draw θ(s)\theta^{(s)} from the posterior.
  2. Simulate a replicated data set yrep,(s)y^{\text{rep}, (s)} of the same size from p(y∣θ(s))p(y \mid \theta^{(s)}).
  3. Compute a test statistic TT on each replicate and on the real data, and report pB=P(T(yrep)≤T(y)∣y)p_B = P\big(T(y^{\text{rep}}) \le T(y) \mid y\big) (or the other tail).

A pBp_B near 0 or 1 means the model cannot reproduce that feature. Choose TT to target what you will use the model for: tails (minimum, kurtosis, count of 3-sigma days) for risk, autocorrelation for forecasting. Statistics the model fits directly, like the sample mean in a normal model, almost always pass, because the data were used to fit them; that pass tells you nothing.

The code below fits the NIG model to 60 months of returns that contain a few crash months, then checks the mean and the minimum.

import random, statistics as st

random.seed(7)
# "Observed" monthly returns (%): mostly calm, occasionally a crash month.
y = [random.gauss(1.0, 2.0) if random.random() > 0.08 else random.gauss(-8.0, 3.0)
     for _ in range(60)]
n, ybar = len(y), st.mean(y)
ss = sum((v - ybar) ** 2 for v in y)

# Weak Normal-Inverse-Gamma prior: m0 = 0, kappa0 = 1, a0 = 2, b0 = 4.
k0, m0, a0, b0 = 1.0, 0.0, 2.0, 4.0
kn = k0 + n
mn = (k0 * m0 + n * ybar) / kn
an, bn = a0 + n / 2, b0 + ss / 2 + k0 * n * (ybar - m0) ** 2 / (2 * kn)

def draw_posterior():
    sigma2 = 1 / random.gammavariate(an, 1 / bn)   # sigma^2 ~ Inv-Gamma(an, bn)
    mu = random.gauss(mn, (sigma2 / kn) ** 0.5)
    return mu, sigma2

stats = {"mean": st.mean, "min": min}
obs = {k: f(y) for k, f in stats.items()}
hits = {k: 0 for k in stats}
R = 4000
for _ in range(R):
    mu, s2 = draw_posterior()
    rep = [random.gauss(mu, s2 ** 0.5) for _ in range(n)]
    for k, f in stats.items():
        hits[k] += f(rep) <= obs[k]

for k in stats:
    print(f"T = {k:4s}  observed {obs[k]:6.2f}   P(T_rep <= T_obs) = {hits[k] / R:.3f}")

Output:

T = mean  observed   0.32   P(T_rep <= T_obs) = 0.503
T = min   observed -11.03   P(T_rep <= T_obs) = 0.011

The mean check is perfect and meaningless. The minimum check fails: only about 1% of replicated histories contain a month as bad as −11%-11\%, although the real history does. A normal model is fine for estimating the average return here but should not be trusted for drawdown or VaR; a Student-t or mixture likelihood is the next step, and it is no longer conjugate.

Knowledge check

With an NIG prior m0=0m_0 = 0, κ0=2\kappa_0 = 2, a0=2a_0 = 2 and b0=3b_0 = 3, you observe n=8n = 8 returns with mean xˉ=1.5\bar x = 1.5. What are the posterior mean of μ\mu and the degrees of freedom of its Student-t marginal?

Knowledge check

Next moves are up, flat or down with prior Dir(1,1,1)\text{Dir}(1, 1, 1). In 8 moves you see 5 up, 3 flat and 0 down. What is the posterior predictive probability that the next move is down?

Knowledge check

You fit a normal model to 500 daily returns and run posterior predictive checks. The posterior predictive p-value is 0.48 for the sample mean and 0.001 for the sample kurtosis. What should you conclude?

Knowledge check

With 12 months of data, why is the NIG posterior predictive for next month's return wider in the tails than a normal distribution with the sample mean and sample variance?

Key takeaways

  • Exponential families h(x)exp⁡(η⊤T(x)−A(η))h(x)\exp(\eta^\top T(x) - A(\eta)) have conjugate priors of the form exp⁡(η⊤χ−νA(η))\exp(\eta^\top\chi - \nu A(\eta)): the prior is ν\nu pseudo-observations with summed statistics χ\chi, and updating adds the data's sufficient statistics.
  • Predictives and marginal likelihoods in conjugate models are ratios of normalising constants, so they are available in closed form.
  • For a normal model with unknown mean and variance use the NIG prior; the posterior of μ\mu and the predictive are Student-t with 2an2a_n degrees of freedom. With short histories the predictive tails are much heavier than the plug-in normal's.
  • The flat-prior limit p(μ,σ2)∝1/σ2p(\mu, \sigma^2) \propto 1/\sigma^2 reproduces the classical tt interval.
  • Dirichlet–Multinomial: add counts to α\alpha; the predictive is (αk+nk)/(α0+N)(\alpha_k + n_k)/(\alpha_0 + N); joint predictions of several future draws are not products of single predictions.
  • Posterior predictive checks test whether the model reproduces features you care about. Use statistics the model was not fitted to, especially tails.

Practice problems

Finished reading?

Answer the 4 knowledge checks to complete this lesson (0 done).