Trading Dev AcademyFree quant education

Free lesson · Statistics

Total expectation and total variance: within-regime and between-regime risk

Open interactive lessonPractice calculationsExplore labs

Start with the idea

  • Returns can vary inside each state, and the states can have different average returns. Total risk includes both sources.
Symbols, units & horizon
  • X: one-day return in basis points
  • Z: regime label
  • μ: unconditional expected one-day return in basis points
  • E: expectation under the model probabilities
  • Var: variance in squared basis points
  • conditioning on Z restricts the distribution to that regime

When and why to use this

Use these identities to reconcile a mixture model, stress scenarios or regime-conditioned forecast with unconditional portfolio risk.

Start here · the same idea in everyday words

Technical termEveryday meaningConcrete example
ConditionalRestricted to a stated event or state.The average return on quiet days considers only quiet days.
RegimeA named market state in a model.Quiet and stressed are example labels; a live signal must use a label available before its trade.
UnconditionalAveraged over all states in the model.Combine quiet-day and stressed-day outcomes using their probabilities.

Unpack a technical sentence

Technical wording: Unconditional variance combines within-regime and between-regime variation.

Read it as: Returns can vary inside each state, and the states can have different average returns. Total risk includes both sources.

A slower worked example · one step at a time

  1. Imagine equally likely states A and B. The one-day return is always +2 bp in A and always −2 bp in B.
  2. Inside either state there is no spread at all: the conditional variance is zero.
  3. Across both states the mean is .5 times 2 plus .5 times −2, or 0 bp.
  4. Each state mean is 2 bp away from the overall mean. Square that distance to get 4 bp² and take the weighted average: .5 times 4 plus .5 times 4 equals 4 bp².
  5. Total variance is zero within-state variance plus 4 bp² between-state variance. Zero uncertainty inside each state therefore does not imply zero uncertainty before the state is known.
Self-assessed: How can two states each have zero variance while their mixture has positive variance?

Their fixed outcomes differ. Before knowing the state, you still do not know which outcome you will receive.

Study loop: predict the result, work through the arithmetic, run the Python, then explain which assumption would make the result unreliable.

  • A regime can be a state observed before trading, or a latent model state. Do not assign it retrospectively using the future return when constructing a signal.
  • The law of total expectation averages each conditional mean using its regime probability. The law of total variance adds average conditional variance and variance of conditional means.
  • In this example, the quiet state has probability .75, conditional mean 1 bp and variance 4 bp². The stressed state has probability .25, mean −3 bp and variance 16 bp². These are synthetic fixed one-day distributions, not empirical estimates.
Before reading the working, evaluate 0.75*4+0.25*16+0.75*1+0.25*9. Give the result in squared basis points.

The unconditional mean is 0 bp and variance 10 bp². Averaging the conditional variances alone would omit 3 bp² of regime risk.

μ=𝔼[𝔼[X|Z]],Var⁡(X)=𝔼[Var⁡(X|Z)]+Var⁡(𝔼[X|Z])
Probability identities for finite moments; model assumptions for regimes

Total expectation and total variance: within-regime and between-regime risk

  1. Weighted mean: .75×1+.25×(−3)=0 bp.
  2. Within-regime variance: .75×4+.25×16=7 bp².
  3. Between-regime variance: .75×(1−0)²+.25×(−3−0)²=3 bp². Total variance is 7+3=10 bp².
Work it by hand

The unconditional mean is 0 bp and variance 10 bp². Averaging the conditional variances alone would omit 3 bp² of regime risk.

An analogy to remember

Two rooms can each have stable temperatures yet differ substantially from one another. Total variation includes both within-room and between-room differences.

How this becomes a building block

A regime forecast can produce a mean and variance that reconcile exactly with its component probabilities and conditional moments.

Python · run and reuse

Architecture pseudocode

# Python 3.10+; standard library only unless NumPy is imported.
# Synthetic teaching inputs; no historical performance claim.
from math import isclose

def mixture_moments(probabilities, means, variances):
    # Means in bp, variances in bp², all for one daily horizon.
    p,m,v = list(probabilities),list(means),list(variances)
    if not p or not len(p)==len(m)==len(v) or any(x<0 for x in p+v) or not isclose(sum(p),1):
        raise ValueError("Matching lengths, nonnegative probabilities/variances and probabilities summing to one required")
    mean = sum(a*b for a,b in zip(p,m))
    within = sum(a*b for a,b in zip(p,v))
    between = sum(a*(b-mean)**2 for a,b in zip(p,m))
    return mean, within, between, within+between

print(mixture_moments([.75,.25],[1,-3],[4,16]))

# Replay the slower, synthetic example above. Units are stated in its steps.
def plain_language_example():
    from math import exp, e  # Standard library; no extra dependency.
    return {
        "mean": .5*2+.5*(-2),
        "within": 0,
        "between": .5*(2-0)**2+.5*(-2-0)**2,
        "sd": 4**.5,
    }

print(plain_language_example())
Self-assessed: explain the calculation, its output units, and one condition under which it fails.

Use these identities to reconcile a mixture model, stress scenarios or regime-conditioned forecast with unconditional portfolio risk. Estimated regime probabilities and conditional moments can change. A state labelled using future information cannot support a causal live signal.

Python implementation

Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.

# Python 3.10+; standard library only unless NumPy is imported.
# Synthetic teaching inputs; no historical performance claim.
from math import isclose

def mixture_moments(probabilities, means, variances):
    # Means in bp, variances in bp², all for one daily horizon.
    p,m,v = list(probabilities),list(means),list(variances)
    if not p or not len(p)==len(m)==len(v) or any(x<0 for x in p+v) or not isclose(sum(p),1):
        raise ValueError("Matching lengths, nonnegative probabilities/variances and probabilities summing to one required")
    mean = sum(a*b for a,b in zip(p,m))
    within = sum(a*b for a,b in zip(p,v))
    between = sum(a*(b-mean)**2 for a,b in zip(p,m))
    return mean, within, between, within+between

print(mixture_moments([.75,.25],[1,-3],[4,16]))

# Replay the slower, synthetic example above. Units are stated in its steps.
def plain_language_example():
    from math import exp, e  # Standard library; no extra dependency.
    return {
        "mean": .5*2+.5*(-2),
        "within": 0,
        "between": .5*(2-0)**2+.5*(-2-0)**2,
        "sd": 4**.5,
    }

print(plain_language_example())

Continue learning

Statistics & Probability — all lessons
  1. Start with counts, probabilities and averages
  2. Random outcomes, sample averages and the limits of the bell curve
  3. Conditional probability: update a belief with evidence
  4. Prediction markets: probability, price and net expected value
  5. Expected value: measure the payoff before choosing the risk
  6. Correlation: how many strategies do you really have?
  7. Conditional probability and Bayes: where the edge actually lives
  8. The central limit theorem: sampling means under explicit assumptions
  9. Law of large numbers: what averaging can and cannot establish
  10. Central limit theorem, standard errors and confidence intervals
  11. Total expectation and total variance: within-regime and between-regime risk
  12. Markov, Chebyshev and Jensen: bounds without a full distribution
  13. Gauss–Markov theorem: what a regression hedge ratio guarantees
  14. Delta method: propagate estimator uncertainty through a smooth function
  15. Multiple testing and the union bound: account for the whole search
  16. Estimation uncertainty and Bayesian updating

Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations