Quant Sandbox
DerivativesPortfolio & RiskFixed IncomeCorporate FinanceStochastic ModelsGuide
Guide
Derivatives pricing
01Forwards, calls and puts02Combining positions03Arbitrage and the forward price04Put–call parity05The one-step binomial model06Risk-neutral probabilities07Multi-step trees and backward induction08American options and early exercise09Delta and hedging10Geometric Brownian motion11The Black–Scholes formula12The Greeks: gamma, vega, theta and rho13Implied volatility and the volatility smile14Stochastic volatility and the Heston model15Static replication: Breeden–Litzenberger and Carr–Madan16Exotic options: barriers, Asians and digitals
Portfolio & risk
01Returns and volatility02Diversification and correlation03The efficient frontier and the capital market line04Risk aversion and the optimal complete portfolio05CAPM, beta and the security market line06Performance measures: Sharpe, Treynor, Jensen and information ratio07Factor models and systematic risk08Value at Risk and expected shortfall
Fixed income
01Bond prices and yields02Duration and convexity03Immunization and duration matching04Spot rates, forward rates and the yield curve05Bond futures and the cheapest-to-deliver06Interest rate swaps07Callable bonds and negative convexity08Credit risk: default probabilities and credit spreads
Corporate finance
01Interest, compounding and discounting02Annuities, perpetuities and loan amortization03Net present value and internal rate of return04The weighted average cost of capital05Capital structure and the Modigliani–Miller theorems06Discounted cash flow valuation07Relative valuation with multiples08Real options: the value of flexibility
Stochastic models
01Random walks and Brownian motion02Probability spaces, σ-algebras and random variables03Filtrations, adapted processes and stopping times04Conditional expectation and martingales05Properties of Brownian motion06The reflection principle and first passage times07The Brownian bridge08The Itô integral09Itô's lemma10Kolmogorov equations and the Feynman–Kac formula11Change of measure and Girsanov's theorem12Monte Carlo option pricing13Simulating SDEs: the Euler–Maruyama scheme14Mean reversion: Ornstein–Uhlenbeck and Vasicek15Jumps and the Merton jump-diffusion model
Knowledge graph

© 2026 Quant Sandbox · A study aid for quantitative finance

Privacy & dataNo cookies

This site does not use cookies and asks for no personal data. It keeps a few small settings in your browser's local storage — your chosen theme and language, the sidebar layout, and your progress and answers in the Guides — so it remembers them on your next visit. This data never leaves your device; clearing your browser storage removes it. Inputs you change can also be captured in the page URL, so you can share or bookmark a specific setup. The site uses Vercel Web Analytics, a privacy-friendly, cookieless service that records only anonymous, aggregated usage (such as page views) without identifying you or tracking you across other sites.

Guide/Stochastic models

Conditional expectation and martingales

Lesson 4 of 15 · 1 min read

The lesson "Probability spaces, σ-algebras and random variables" set up the probability space (Ω,F,P)(\Omega, \mathcal{F}, \mathbb{P})(Ω,F,P), and "Filtrations, adapted processes and stopping times" described how information grows over time through a filtration F=(Fn)\mathbb{F} = (\mathcal{F}_n)F=(Fn​). This lesson uses both to define conditional expectation given a σ-algebra, states its calculation rules, defines a martingale (the mathematical model of a fair game), and proves why no betting strategy turns a fair game into a profitable one. The coin-flip walk WnW_nWn​ and Brownian motion WtW_tWt​ from "Random walks and Brownian motion" serve as the running examples.

Conditional expectation given a σ-algebra

Let XXX be an integrable random variable, E∣X∣<∞\mathbb{E}|X| < \inftyE∣X∣<∞, and let G⊆F\mathcal{G} \subseteq \mathcal{F}G⊆F be a sub-σ-algebra: the events whose occurrence is known when the information G\mathcal{G}G is available. The conditional expectation E[X∣G]\mathbb{E}[X \mid \mathcal{G}]E[X∣G] is a random variable YYY with two properties:

  • YYY is G\mathcal{G}G-measurable: its value can be computed from the information in G\mathcal{G}G.
  • E[X 1A]=E[Y 1A]\mathbb{E}[X\,\mathbf{1}_A] = \mathbb{E}[Y\,\mathbf{1}_A]E[X1A​]=E[Y1A​] for every event A∈GA \in \mathcal{G}A∈G: on every event that G\mathcal{G}G can see, YYY has the same average as XXX.

Such a YYY exists (by the Radon–Nikodym theorem) and is unique up to a set of probability zero. When XXX has a finite variance, E[X∣G]\mathbb{E}[X \mid \mathcal{G}]E[X∣G] is the G\mathcal{G}G-measurable random variable closest to XXX in mean square, which is why it is called the best forecast of XXX given G\mathcal{G}G. It is not a number fixed in advance but a random variable, because the forecast depends on what has been observed.

Example: toss a fair coin twice, let YYY be the number of heads and G=σ(X1)\mathcal{G} = \sigma(X_1)G=σ(X1​) the information from the first toss. G\mathcal{G}G contains only the events "first toss heads" and "first toss tails" (and ∅\emptyset∅, Ω\OmegaΩ), so E[Y∣G]\mathbb{E}[Y \mid \mathcal{G}]E[Y∣G] must be constant on each: 1+12=1.51 + \tfrac12 = 1.51+21​=1.5 after heads and 0+12=0.50 + \tfrac12 = 0.50+21​=0.5 after tails. The defining property holds on A={heads first}A = \{\text{heads first}\}A={heads first}: E[Y 1A]=12⋅1.5=0.75\mathbb{E}[Y\,\mathbf{1}_A] = \tfrac12 \cdot 1.5 = 0.75E[Y1A​]=21​⋅1.5=0.75 on both sides.

Rules for conditional expectation

All of these follow from the definition, and they are what one actually computes with. Here XXX and ZZZ are integrable and H⊆G\mathcal{H} \subseteq \mathcal{G}H⊆G are sub-σ-algebras.

  • Linearity: E[aX+bZ∣G]=a E[X∣G]+b E[Z∣G]\mathbb{E}[aX + bZ \mid \mathcal{G}] = a\,\mathbb{E}[X \mid \mathcal{G}] + b\,\mathbb{E}[Z \mid \mathcal{G}]E[aX+bZ∣G]=aE[X∣G]+bE[Z∣G].
  • Tower property: E[E[X∣G]∣H]=E[X∣H]\mathbb{E}\big[\mathbb{E}[X \mid \mathcal{G}] \mid \mathcal{H}\big] = \mathbb{E}[X \mid \mathcal{H}]E[E[X∣G]∣H]=E[X∣H]. Forecasting with less information what a better-informed forecast will be gives the less-informed forecast. With H={∅,Ω}\mathcal{H} = \{\emptyset, \Omega\}H={∅,Ω} this is E[E[X∣G]]=E[X]\mathbb{E}\big[\mathbb{E}[X \mid \mathcal{G}]\big] = \mathbb{E}[X]E[E[X∣G]]=E[X].
  • Taking out what is known: if ZZZ is G\mathcal{G}G-measurable and ZXZXZX is integrable, then E[ZX∣G]=Z E[X∣G]\mathbb{E}[ZX \mid \mathcal{G}] = Z\,\mathbb{E}[X \mid \mathcal{G}]E[ZX∣G]=ZE[X∣G].
  • Independence: if XXX is independent of G\mathcal{G}G, then E[X∣G]=E[X]\mathbb{E}[X \mid \mathcal{G}] = \mathbb{E}[X]E[X∣G]=E[X]. Information unrelated to XXX does not change its forecast.

In the coin example the tower property reads 12⋅1.5+12⋅0.5=1=E[Y]\tfrac12 \cdot 1.5 + \tfrac12 \cdot 0.5 = 1 = \mathbb{E}[Y]21​⋅1.5+21​⋅0.5=1=E[Y].

Martingales

Fix a filtration F=(Fn)n≥0\mathbb{F} = (\mathcal{F}_n)_{n \ge 0}F=(Fn​)n≥0​. A process X=(Xn)X = (X_n)X=(Xn​) is a martingale with respect to F\mathbb{F}F if

  • it is adapted: XnX_nXn​ is Fn\mathcal{F}_nFn​-measurable for every nnn;
  • it is integrable: E∣Xn∣<∞\mathbb{E}|X_n| < \inftyE∣Xn​∣<∞ for every nnn;
  • the best forecast of the next value is the current value: E[Xn+1∣Fn]=Xn\mathbb{E}[X_{n+1} \mid \mathcal{F}_n] = X_nE[Xn+1​∣Fn​]=Xn​.

In continuous time, with a filtration (Ft)t≥0(\mathcal{F}_t)_{t \ge 0}(Ft​)t≥0​, the third condition becomes E[Xt∣Fs]=Xs\mathbb{E}[X_t \mid \mathcal{F}_s] = X_sE[Xt​∣Fs​]=Xs​ for all s≤ts \le ts≤t. If E[Xn+1∣Fn]≥Xn\mathbb{E}[X_{n+1} \mid \mathcal{F}_n] \ge X_nE[Xn+1​∣Fn​]≥Xn​ instead, the process is a submartingale (a game in your favour); with ≤\le≤ it is a supermartingale (a game against you, like a casino with a house edge).

A martingale is a fair game: given everything observed so far, the expected change over the next step is zero. By the tower property the same holds over any number of steps, E[Xm∣Fn]=Xn\mathbb{E}[X_m \mid \mathcal{F}_n] = X_nE[Xm​∣Fn​]=Xn​ for m≥nm \ge nm≥n, and in particular E[Xn]=E[X0]\mathbb{E}[X_n] = \mathbb{E}[X_0]E[Xn​]=E[X0​].

Examples from Brownian motion

Use the natural filtration, the history of the path. Each example is one line with the rules above.

  • The symmetric random walk is a martingale: E[Wn+1∣Fn]=Wn+E[Xn+1∣Fn]=Wn+E[Xn+1]=Wn\mathbb{E}[W_{n+1} \mid \mathcal{F}_n] = W_n + \mathbb{E}[X_{n+1} \mid \mathcal{F}_n] = W_n + \mathbb{E}[X_{n+1}] = W_nE[Wn+1​∣Fn​]=Wn​+E[Xn+1​∣Fn​]=Wn​+E[Xn+1​]=Wn​, by taking out what is known and then independence.
  • Brownian motion is a martingale in continuous time: for s<ts < ts<t, Wt=Ws+(Wt−Ws)W_t = W_s + (W_t - W_s)Wt​=Ws​+(Wt​−Ws​), and the increment is independent of Fs\mathcal{F}_sFs​ with mean zero, so E[Wt∣Fs]=Ws\mathbb{E}[W_t \mid \mathcal{F}_s] = W_sE[Wt​∣Fs​]=Ws​.
  • Wt2W_t^2Wt2​ is not a martingale. Writing Wt2=Ws2+2Ws(Wt−Ws)+(Wt−Ws)2W_t^2 = W_s^2 + 2W_s(W_t - W_s) + (W_t - W_s)^2Wt2​=Ws2​+2Ws​(Wt​−Ws​)+(Wt​−Ws​)2 and taking the forecast term by term gives E[Wt2∣Fs]=Ws2+0+(t−s)\mathbb{E}[W_t^2 \mid \mathcal{F}_s] = W_s^2 + 0 + (t - s)E[Wt2​∣Fs​]=Ws2​+0+(t−s): it drifts upward, so it is a submartingale.
  • Wt2−tW_t^2 - tWt2​−t is a martingale: subtracting the drift ttt removes exactly the upward tendency.

Example: suppose W1=1W_1 = 1W1​=1. The forecast of W4W_4W4​ is still 1, but the forecast of W42W_4^2W42​ is 1+3=41 + 3 = 41+3=4, not 1. The spread of the path grows even though its centre does not move.

Betting strategies

Bet on a fair coin round by round. Before round kkk you choose a stake HkH_kHk​ using only the history Fk−1\mathcal{F}_{k-1}Fk−1​ (the stake is predictable), and win or lose it: your wealth changes by HkXkH_k X_kHk​Xk​ with Xk=±1X_k = \pm 1Xk​=±1. After nnn rounds

Vn=V0+∑k=1nHkXk,E[Vn+1∣Fn]=Vn+Hn+1 E[Xn+1∣Fn]=Vn.V_n = V_0 + \sum_{k=1}^{n} H_k X_k, \qquad \mathbb{E}[V_{n+1} \mid \mathcal{F}_n] = V_n + H_{n+1}\,\mathbb{E}[X_{n+1} \mid \mathcal{F}_n] = V_n.Vn​=V0​+k=1∑n​Hk​Xk​,E[Vn+1​∣Fn​]=Vn​+Hn+1​E[Xn+1​∣Fn​]=Vn​.

The stake is taken out of the forecast because it is known at time nnn, and the coin has conditional mean zero. So the wealth of every such strategy is again a martingale (a martingale transform), and its expected value stays at V0V_0V0​ at every fixed time. A strategy can change the shape of the outcome distribution, but not its mean.

The famous doubling strategy stakes 1, and after every loss doubles the stake, so that the first win recovers all earlier losses plus 1. Limit it to at most 5 rounds. A win in any of the first five rounds gives a net gain of 1. Losing all five rounds has probability (12)5=132(\tfrac12)^5 = \tfrac1{32}(21​)5=321​ and costs 1+2+4+8+16=311 + 2 + 4 + 8 + 16 = 311+2+4+8+16=31. The expected gain is

3132⋅1+132⋅(−31)=0.\tfrac{31}{32} \cdot 1 + \tfrac{1}{32} \cdot (-31) = 0.3231​⋅1+321​⋅(−31)=0.

The strategy wins a little with probability 96.9%96.9\%96.9% and loses a lot with probability 3.1%3.1\%3.1%. Without a round limit it needs unlimited credit: after 10 losses in a row the next stake is 512 and the losses so far are 1023.

In the figure you play this game yourself, starting from a wealth of 100. Choose a strategy: a constant stake of 1, doubling after a loss (stakes up to 64, then back to 1), or a stake of 5% of current wealth; no stake can exceed what you have. "Flip once" plays one round and "Play 100 rounds" plays a hundred, drawing your wealth as a step plot. "Simulate 1000 players" lets 1000 players play 100 rounds each with the chosen strategy and draws a histogram of their final wealth, with the average marked; "Reset" starts again with fresh coins. Compare the three strategies. The average final wealth stays near 100 in every case (the readout shows it with its 95% sampling band), but with a constant stake about 46% of players end ahead and nobody falls below about 60, with doubling about two thirds end ahead and some lose everything, and with 5% of wealth only about 38% end ahead while a few players finish with several times their starting wealth.

Betting strategies in a fair coin gameOpen in Brownian Motion →
round 0
One player's wealth, round by round
Final wealth of 1000 players after 100 rounds

Press "Simulate 1000 players" to see how final wealth is spread out.

Wealth now
100.00
Next stake
1.00
Average final wealth
–
Ended ahead
–
Worst outcome
–

The same calculation with a longer limit shows that more rounds only move the risk into a rarer, larger loss.

Worked example

The doubling strategy limited to six rounds

  1. Probability of losing all six rounds, in percent.

    (12)6\displaystyle \left(\tfrac12\right)^6(21​)6=1.5625%
  2. Total loss when all six rounds are lost.

    1+2+4+8+16+32\displaystyle 1 + 2 + 4 + 8 + 16 + 321+2+4+8+16+32=63
  3. Probability of a net gain of 1, in percent.

    1−(12)6\displaystyle 1 - \left(\tfrac12\right)^61−(21​)6=98.4375%
  4. Expected gain of the strategy.

    (1−164)⋅1+164⋅(−63)\displaystyle \left(1 - \tfrac{1}{64}\right) \cdot 1 + \tfrac{1}{64} \cdot (-63)(1−641​)⋅1+641​⋅(−63)=0

Stopping and the optional stopping theorem

A gambler rarely plays a fixed number of rounds; they stop when something happens. A stopping time τ\tauτ is a random time for which the decision to stop by time nnn uses only the information up to nnn: {τ≤n}∈Fn\{\tau \le n\} \in \mathcal{F}_n{τ≤n}∈Fn​ for every nnn. "Stop at the first win" is a stopping time; "stop just before the biggest loss" is not, because it needs the future.

Stopping a martingale at a stopping time keeps it a martingale: the stopped process Xτ∧nX_{\tau \wedge n}Xτ∧n​ is again a martingale, so E[Xτ∧n]=E[X0]\mathbb{E}[X_{\tau \wedge n}] = \mathbb{E}[X_0]E[Xτ∧n​]=E[X0​] for every fixed nnn. Passing to n→∞n \to \inftyn→∞ needs a condition. The optional stopping theorem states: if XXX is a martingale and τ\tauτ a stopping time, then

E[Xτ]=E[X0]\mathbb{E}[X_\tau] = \mathbb{E}[X_0]E[Xτ​]=E[X0​]

holds under any one of these hypotheses:

  • τ\tauτ is bounded: τ≤N\tau \le Nτ≤N for a fixed number NNN;
  • τ<∞\tau < \inftyτ<∞ almost surely and the stopped process is dominated: ∣Xτ∧n∣≤K|X_{\tau \wedge n}| \le K∣Xτ∧n​∣≤K for all nnn, for a fixed KKK (more generally, the family Xτ∧nX_{\tau \wedge n}Xτ∧n​ is uniformly integrable);
  • E[τ]<∞\mathbb{E}[\tau] < \inftyE[τ]<∞ and the increments are bounded: ∣Xn+1−Xn∣≤c|X_{n+1} - X_n| \le c∣Xn+1​−Xn​∣≤c.

Example (gambler's ruin): a symmetric walk starts at 0 and stops at τ\tauτ, the first time it hits +2+2+2 or −3-3−3. The stopped walk stays between −3-3−3 and +2+2+2, so the second hypothesis applies, and 0=E[Wτ]=2π+(−3)(1−π)0 = \mathbb{E}[W_\tau] = 2\pi + (-3)(1 - \pi)0=E[Wτ​]=2π+(−3)(1−π), where π\piπ is the probability of reaching +2+2+2 first. Hence π=3/5=0.6\pi = 3/5 = 0.6π=3/5=0.6, without any combinatorics.

The doubling strategy without a round limit shows why the hypotheses matter. Let τ\tauτ be the first win. It is a stopping time and finite with probability 1, and Vτ=V0+1V_\tau = V_0 + 1Vτ​=V0​+1, so E[Vτ]=V0+1≠V0\mathbb{E}[V_\tau] = V_0 + 1 \ne V_0E[Vτ​]=V0​+1=V0​. The theorem is not contradicted: τ\tauτ is unbounded, and before the win the wealth V0−(2k−1)V_0 - (2^k - 1)V0​−(2k−1) is not bounded below, so no hypothesis holds. With the cap of five rounds, τ∧5\tau \wedge 5τ∧5 is bounded and the expected gain is 0, as computed above.

Doob's maximal inequality

A martingale also controls how far its path can stray before time nnn, not only where it ends. For a martingale XXX and any λ>0\lambda > 0λ>0, Doob's maximal inequality gives

P(max⁡k≤n∣Xk∣≥λ)≤E∣Xn∣λ,E[max⁡k≤nXk2]≤4 E[Xn2].\mathbb{P}\Big(\max_{k \le n} |X_k| \ge \lambda\Big) \le \frac{\mathbb{E}|X_n|}{\lambda}, \qquad \mathbb{E}\Big[\max_{k \le n} X_k^2\Big] \le 4\,\mathbb{E}[X_n^2].P(k≤nmax​∣Xk​∣≥λ)≤λE∣Xn​∣​,E[k≤nmax​Xk2​]≤4E[Xn2​].

The second form is the L2L^2L2 version. Both hold with the maximum replaced by the supremum over [0,t][0, t][0,t] for a martingale with continuous paths such as Brownian motion. Example: for the coin-flip walk with n=100n = 100n=100, E∣W100∣≤E[W1002]=10\mathbb{E}|W_{100}| \le \sqrt{\mathbb{E}[W_{100}^2]} = 10E∣W100​∣≤E[W1002​]​=10, so the probability that the walk ever reaches distance 30 from the start within 100 steps is at most 10/30=1/310/30 = 1/310/30=1/3, and E[max⁡k≤100Wk2]≤400\mathbb{E}[\max_{k \le 100} W_k^2] \le 400E[maxk≤100​Wk2​]≤400. Inequalities like these are what make limits of martingales, and later the Itô integral, well behaved.

Prices as martingales

Martingales are the language of arbitrage-free pricing. In "Risk-neutral probabilities" the stock was expected to earn the risk-free rate under Q\mathbb{Q}Q, which says exactly that the discounted price e−rtSte^{-rt}S_te−rtSt​ is a martingale under Q\mathbb{Q}Q. A later lesson in this track, on changing the measure, shows how to move from the real-world measure P\mathbb{P}P, under which this fails, to Q\mathbb{Q}Q, under which it holds. A price is then simply the conditional expectation of the discounted payoff.

Summary

  • E[X∣G]\mathbb{E}[X \mid \mathcal{G}]E[X∣G] is the G\mathcal{G}G-measurable random variable with the same average as XXX on every event in G\mathcal{G}G; it obeys linearity, the tower property, taking out what is known, and the independence rule.
  • A martingale with respect to F\mathbb{F}F is adapted, integrable and satisfies E[Xn+1∣Fn]=Xn\mathbb{E}[X_{n+1} \mid \mathcal{F}_n] = X_nE[Xn+1​∣Fn​]=Xn​ (in continuous time E[Xt∣Fs]=Xs\mathbb{E}[X_t \mid \mathcal{F}_s] = X_sE[Xt​∣Fs​]=Xs​). WtW_tWt​ and Wt2−tW_t^2 - tWt2​−t are martingales.
  • Betting with predictable stakes on a fair game gives a martingale transform: the expected wealth never changes at a fixed time.
  • Optional stopping, E[Xτ]=E[X0]\mathbb{E}[X_\tau] = \mathbb{E}[X_0]E[Xτ​]=E[X0​], needs a bounded stopping time, a dominated stopped process, or E[τ]<∞\mathbb{E}[\tau] < \inftyE[τ]<∞ with bounded increments; unlimited doubling violates all three.
  • Doob's maximal inequality bounds the whole path: P(max⁡k≤n∣Xk∣≥λ)≤E∣Xn∣/λ\mathbb{P}(\max_{k \le n}|X_k| \ge \lambda) \le \mathbb{E}|X_n|/\lambdaP(maxk≤n​∣Xk​∣≥λ)≤E∣Xn​∣/λ.

The next lesson, "Properties of Brownian motion", studies the paths of WtW_tWt​ in detail, using the martingales WtW_tWt​ and Wt2−tW_t^2 - tWt2​−t found here.

Try itSimulate Brownian paths and watch their spread grow in the Brownian motion lab →
3 questions on this lesson at the end of the series →
← PreviousFiltrations, adapted processes and stopping timesNext →Properties of Brownian motion
Connections
Conditional expectation and martingales
Open the full graph →
Labs for this lesson
Brownian Motion