---
title: "Itô Calculus"
book: "Quantitative Methods"
subject: quant
language: en
chapter: 3
exercises: 8
source: https://one-course.com/books/quant/4/en/chapter/3-ito-calculus
---

# Chapter 3 — Itô Calculus

A junior researcher backtests a trend-following rule on ten years of daily closes: hold a position proportional to the distance of the close above its 20-day moving average. The Sharpe ratio is 5.4. The rule reads the closing price and the backtest credits it with the return into that same close; shifted by one day, so that a position decided at a close earns the next day’s return, the Sharpe ratio is $-0.04$. The “prices” were a pure random walk, with nothing to find. The first backtest was not an [Itô integral](#def-qm-ito-calculus-integral): its integrand looked at the increment it was multiplied by, and collected the covariation of the position with the price, a quantity that exists on every random path. This chapter builds the [Itô integral](#def-qm-ito-calculus-integral), whose integrand is fixed before the increment arrives, proves Itô’s formula, the chain rule with a second-order term that the rest of the series uses on every page, and ends with the two structural theorems (Lévy’s characterisation and [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) representation) that underlie the pricing of One Quant Book 5.

## 3.1 The Itô integral

A trading strategy holds $H_t$ units of an asset whose price follows a [Brownian motion](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-bm); its gains over $[0, T]$ should be $\int_0^T H_t\,dW_t$. The paths of $W$ have infinite total variation ([Theorem 2.12](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#thm-qm-brownian-motion-qv)), so the integral cannot be defined path by path as a Stieltjes integral, and the choice of evaluation point in each interval matters in the limit. The [Itô integral](#def-qm-ito-calculus-integral) evaluates $H$ at the left point, which is the only choice a trader can make.

**Definition 3.1 (Simple process, Itô integral).**

A *simple process* is $H_t = \sum_{k=0}^{n-1}h_k\mathbf 1_{(t_k,
t_{k+1}]}(t)$ with $0 = t_0 < \dots < t_n = T$ and each $h_k$ bounded and $\mathcal
F_{t_k}$-measurable. Its *Itô integral* is $\int_0^t H_s\,dW_s = \sum_k
h_k(W_{t_{k+1}\wedge t} - W_{t_k\wedge t})$. For an adapted $H$ with $\E\int_0^T H_s^2\,ds <
\infty$, the Itô integral is the $L^2$ limit of the integrals of simple processes $H^{(n)}$ with $\E\int_0^T(H^{(n)}_s - H_s)^2\,ds \to 0$.

**Theorem 3.2 (Itô isometry).**

For adapted $H$ with $\E\int_0^T H_s^2\,ds < \infty$, $I_t = \int_0^t H_s\,dW_s$ is a continuous square-integrable [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) with $I_0 = 0$ and

$$
\E\Bigl[\Bigl(\int_0^T H_s\,dW_s\Bigr)^2\Bigr] = \E\int_0^T H_s^2\,ds, \qquad [I]_t = \int_0^t H_s^2\,ds.
$$

**Proof.** For a [simple process](#def-qm-ito-calculus-integral), with $\Delta_k = W_{t_{k+1}} - W_{t_k}$: the cross terms $\E[h_jh_k\Delta_j\Delta_k]$, $j < k$, vanish because $h_jh_k\Delta_j$ is $\mathcal F_{t_k}$-measurable and $\E_{t_k}[\Delta_k] = 0$; the square terms give $\E[h_k^2]\E[\Delta_k^2] = \E[h_k^2](t_{k+1} - t_k)$. The same computation on $[s, t]$ gives the [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) property. The integral is therefore an isometry from simple processes into $L^2$, extends to their closure, and the [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) property passes to $L^2$ limits (Doob’s inequality, [Proposition 1.9](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#prop-qm-probability-at-speed-doob), gives a continuous version). ∎

**Example 3.3 (The integral of WWW against itself).**

On a partition of $[0, T]$, $\sum_k W_{t_k}\Delta_k = \tfrac12\sum_k(W_{t_{k+1}}^2 - W_{t_k}^2) -
\tfrac12\sum_k\Delta_k^2$. The first sum telescopes to $\tfrac12 W_T^2$ and the second tends to $\tfrac12 T$ by [Theorem 2.12](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#thm-qm-brownian-motion-qv):

$$
\int_0^T W_t\,dW_t = \tfrac12\bigl(W_T^2 - T\bigr).
$$

The right-point sums tend to $\tfrac12(W_T^2 + T)$, and the midpoint sums to $\tfrac12W_T^2$, the ordinary calculus answer ([Figure 3.1](#fig-qm-ito-calculus-sums)).

![Sums approximating ∈t_01 W\,dW on one path (W_1 = 2.04) sampled at 2k points. The left-point sums converge to 1/2(W_12 - 1) = 1.58, the right-point sums to 2.58 (dashed lines); the midpoint sums equal 1/2W_12 = 2.08 exactly, whatever the grid. Data: the chapter’s tutorial, seeded.](https://one-course.com/images/onecourse/chapters/quant-4/qm-ito-calculus/fig-f84c191cdc11.svg)

***Figure 3.1.** Sums approximating $\int_0^1 W\,dW$ on one path ($W_1 = 2.04$) sampled at $2^k$ points. The left-point sums converge to $\tfrac12(W_1^2 - 1) = 1.58$, the right-point sums to $2.58$ (dashed lines); the midpoint sums equal $\tfrac12W_1^2 = 2.08$ exactly, whatever the grid. Data: the chapter’s tutorial, seeded.*

**Definition 3.4 (Stratonovich integral).**

The *Stratonovich integral* $\int_0^T X_t\circ dW_t$ is the limit of the sums $\sum_k\tfrac12(X_{t_k} + X_{t_{k+1}})\Delta_k$. For a continuous semimartingale $X$ it equals $\int_0^T X_t\,dW_t + \tfrac12[X, W]_T$.

The [Stratonovich integral](#def-qm-ito-calculus-strat) obeys the ordinary chain rule, which is why physicists modelling a smooth noise by a white one use it. It averages the integrand over the interval, which a trade cannot do: the position is set before the price move. Finance uses Itô.

## 3.2 The integral in a backtest

A backtest on a grid $t_0 < t_1 < \dots$ with positions $\theta_{t_i}$ and prices $S_{t_i}$ computes gains $\sum_i\theta_{t_i}(S_{t_{i+1}} - S_{t_i})$: the left-point sum, an [Itô integral](#def-qm-ito-calculus-integral), when $\theta_{t_i}$ uses only information available at $t_i$. A backtest that credits the position computed at $t_{i+1}$ with the move into $t_{i+1}$ computes the right-point sum instead, and

$$
\sum_i\theta_{t_{i+1}}\Delta S_i - \sum_i\theta_{t_i}\Delta S_i = \sum_i\Delta\theta_i\,\Delta S_i
\longrightarrow [\theta, S]_T:
$$

the look-ahead profit is the covariation of the position with the price, which is not zero on a random walk whenever the position responds to the latest price. The rule of the hook, $\theta_t = (S_t - \bar S^{(20)}_t)/c$ with $\bar S^{(20)}$ the 20-day moving average and $c$ chosen so that $\theta$ has unit variance, puts weight $(L - 1)/L = 0.95$ on the last increment: [Figure 3.2](#fig-qm-ito-calculus-timing) shows the timing and [Figure 3.3](#fig-qm-ito-calculus-backtest) the result.

![Which price move a position may earn. A position computed from the close at t earns the move from t to t + 1 (solid): the left-point sum. Crediting it with the move into t (dashed) multiplies the position by an increment it has already seen.](https://one-course.com/images/onecourse/chapters/quant-4/qm-ito-calculus/fig-565517ebbc22.svg)

***Figure 3.2.** Which price move a position may earn. A position computed from the close at $t$ earns the move from $t$ to $t + 1$ (solid): the left-point sum. Crediting it with the move into $t$ (dashed) multiplies the position by an increment it has already seen.*

**Proposition 3.5 (The look-ahead Sharpe ratio on a random walk).**

Let $S$ be a random walk with daily increments $\sigma Z_t$, $Z_t$ independent standard normal, and $\theta_t$ a unit-variance linear function of past increments with correlation $a$ with the latest one. Then the same-bar gains $\theta_t\,\Delta S_t$ have mean $a\sigma$ and standard deviation $\sigma\sqrt{1 + a^2}$, a daily Sharpe ratio of $a/\sqrt{1 + a^2}$, while the next-bar gains have mean zero. For the moving-average rule, $a = \frac{(L-1)/L}{(\sum_{m=1}^{L-1}(m/L)^2)^{1/2}}$.

**Proof.** Write $\theta_t = aZ_t + \sqrt{1 - a^2}\,Y$ with $Y$ standard normal and independent of $Z_t$. Then $\E[\theta_tZ_t] = a$ and $\E[\theta_t^2Z_t^2] = a^2\E[Z^4] + (1 - a^2) = 1 + 2a^2$, so the variance is $1 + a^2$ (in units of $\sigma^2$). For the rule, $S_t - \bar S_t = \sum_{i=0}^{L-2}\frac{L-1-i}{L}\Delta
S_{t-i}$, whose variance is $\sigma^2\sum_{m=1}^{L-1}(m/L)^2$ and whose coefficient on $\Delta S_t$ is $(L-1)/L$. Next-bar gains $\theta_{t-1}\Delta S_t$ have zero mean because $\theta_{t-1}$ is independent of $\Delta S_t$. ∎

For $L = 20$, $a = 0.382$ and the annualised same-bar Sharpe ratio is $\sqrt{252}\times 0.382/
\sqrt{1.146} = 5.67$; over 400 simulated ten-year random walks it averages 5.69, and the honest backtest 0.00 with a standard deviation of 0.32, the noise one expects from ten years of a strategy with no edge.

![The 20-day trend rule on one ten-year random walk with 1% daily volatility. Credited with the same bar it earns 958% of notional (Sharpe ratio 5.4), almost exactly the covariation of position and price (965%); credited with the next bar it earns -7\% (Sharpe ratio -0.04). Data: the chapter’s tutorial, seeded.](https://one-course.com/images/onecourse/chapters/quant-4/qm-ito-calculus/fig-40d1fb0b072b.svg)

***Figure 3.3.** The 20-day trend rule on one ten-year random walk with 1% daily volatility. Credited with the same bar it earns 958% of notional (Sharpe ratio 5.4), almost exactly the covariation of position and price (965%); credited with the next bar it earns $-7\%$ (Sharpe ratio $-0.04$). Data: the chapter’s tutorial, seeded.*

## 3.3 Itô’s formula in one and several dimensions

**Definition 3.6 (Itô process, quadratic covariation).**

An *Itô process* is $X_t = X_0 + \int_0^t\mu_s\,ds + \int_0^t\sigma_s\,dW_s$, written $dX_t = \mu_t\,dt + \sigma_t\,dW_t$, with adapted $\mu$, $\sigma$ such that $\int_0^T|\mu_s|\,ds$ and $\int_0^T\sigma_s^2\,ds$ are finite almost surely. The *quadratic covariation* of two continuous processes is the limit in probability $[X, Y]_t =
\lim\sum_k(X_{t_{k+1}} - X_{t_k})(Y_{t_{k+1}} - Y_{t_k})$; for Itô processes driven by $W^1, W^2$ with $d\langle W^1, W^2\rangle_t = \rho\,dt$, $d[X, Y]_t = \sigma^X_t\sigma^Y_t\rho\,dt$.

The bookkeeping rules are $dt\,dt = 0$, $dt\,dW = 0$, $dW^i\,dW^j = \rho_{ij}\,dt$: only products of two Brownian increments survive at first order in $dt$.

**Theorem 3.7 (Itô’s formula).**

If $X$ is an [Itô process](#def-qm-ito-calculus-process) and $f \in C^{1,2}([0,T]\times\R)$, then

$$
df(t, X_t) = \partial_tf\,dt + \partial_xf\,dX_t + \tfrac12\partial_{xx}f\,d[X]_t
= \bigl(\partial_tf + \mu_t\partial_xf + \tfrac12\sigma_t^2\partial_{xx}f\bigr)dt + \sigma_t\partial_xf\,dW_t.
$$

For $X = (X^1, \dots, X^d)$ and $f \in C^{1,2}$, $df = \partial_tf\,dt + \sum_i\partial_if\,dX^i +
\tfrac12\sum_{i,j}\partial_{ij}f\,d[X^i, X^j]$; in particular $d(XY) = X\,dY + Y\,dX + d[X, Y]$.

**Partial proof.** For $f(x)$ with bounded derivatives and $\mu, \sigma$ simple, Taylor’s formula on a partition gives $f(X_T) - f(X_0) = \sum f'(X_{t_k})\Delta X_k + \tfrac12\sum f^{\prime\prime}(\xi_k)(\Delta X_k)^2$. The first sum converges to $\int f'(X)\,dX$. In the second, $f^{\prime\prime}(\xi_k)$ may be replaced by $f^{\prime\prime}(X_{t_k})$ at a cost bounded by the modulus of continuity of $f^{\prime\prime}$ times $\sum(\Delta X_k)^2$, which tends to zero, and $\sum f^{\prime\prime}(X_{t_k})((\Delta X_k)^2 - \sigma_{t_k}^2\Delta t_k)$ tends to zero in $L^2$ as in [Theorem 2.12](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#thm-qm-brownian-motion-qv). General $f$, $\mu$, $\sigma$ follow by localisation and approximation (Karatzas and Shreve, 1991, §3.3). ∎

**Example 3.8 (The logarithm of a geometric Brownian motion).**

For $dS = \mu S\,dt + \sigma S\,dW$, with $f = \ln$: $d\ln S = dS/S - \tfrac12 d[S]/S^2 = (\mu -
\tfrac12\sigma^2)dt + \sigma\,dW$. The expected return is $\mu$ but the expected log return, the rate at which the median grows, is $\mu - \tfrac12\sigma^2$. With $\mu = 10\%$ and $\sigma = 40\%$, the mean of $S_{10}/S_0$ is $e^{1} = 2.72$ and its median $e^{0.2} = 1.22$ ([Figure 3.4](#fig-qm-ito-calculus-gbm)); the volatility decay of leveraged funds (One Quant Book 1, chapter 14) is the same correction applied twice.

![Geometric Brownian motion with = 10\% and = 40\%: the mean grows at 10% a year, the median at 2%, and after ten years the 10th percentile (shaded band: 10th to 90th) is 0.24. Data: 100 000 paths simulated exactly, seeded.](https://one-course.com/images/onecourse/chapters/quant-4/qm-ito-calculus/fig-30d87751282b.svg)

***Figure 3.4.** Geometric [Brownian motion](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-bm) with $\mu = 10\%$ and $\sigma = 40\%$: the mean grows at 10% a year, the median at 2%, and after ten years the 10th percentile (shaded band: 10th to 90th) is 0.24. Data: 100 000 paths simulated exactly, seeded.*

## 3.4 Local martingales and the stochastic exponential

An [Itô integral](#def-qm-ito-calculus-integral) $\int H\,dW$ with only $\int_0^TH_s^2\,ds < \infty$ almost surely need not be integrable, let alone a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale); it is one up to a sequence of [stopping times](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-stopping).

**Definition 3.9 (Local martingale).**

An [adapted process](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-filtration) $M$ is a *local martingale* if there are [stopping times](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-stopping) $\tau_n \uparrow \infty$ such that each stopped process $M_{t\wedge\tau_n}$ is a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale).

**Proposition 3.10 (Positive local martingales are supermartingales).**

A [local martingale](#def-qm-ito-calculus-local) bounded below, in particular a positive one, is a [supermartingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale). A [local martingale](#def-qm-ito-calculus-local) $M$ with $\E[\sup_{s\le t}|M_s|] < \infty$ for every $t$ is a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale).

**Proof.** If $M \ge 0$, Fatou’s lemma gives $\E_s[M_t] = \E_s[\lim_n M_{t\wedge\tau_n}] \le \liminf_n
M_{s\wedge\tau_n} = M_s$. Under the domination, dominated convergence replaces the inequality by an equality. ∎

The inequality can be strict: $1/|B_t|$ for a three-dimensional [Brownian motion](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-bm) started away from the origin is a positive [local martingale](#def-qm-ito-calculus-local) whose expectation decreases, a *strict* [local martingale](#def-qm-ito-calculus-local). Such processes model price bubbles and are why chapter 5 checks that a candidate [density process](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-change) is a true [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) before using it to change the measure.

**Definition 3.11 (Stochastic exponential).**

The *stochastic exponential* of a continuous semimartingale $X$ with $X_0 = 0$ is $\mathcal E(X)_t = \exp(X_t - \tfrac12[X]_t)$, the unique solution of $dZ = Z\,dX$, $Z_0 = 1$.

By Itô’s formula, $d\exp(X - \tfrac12[X]) = \exp(\cdot)(dX - \tfrac12 d[X] + \tfrac12 d[X]) = Z\,dX$. With $X = \int\gamma\,dW$ it is the positive [local martingale](#def-qm-ito-calculus-local) $\exp(\int\gamma\,dW - \tfrac12\int\gamma^2dt)$, the [density process](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-change) of every [change of measure](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-change) in chapter 5.

## 3.5 The representation theorem

**Theorem 3.12 (Lévy’s characterisation).**

A continuous [local martingale](#def-qm-ito-calculus-local) $M$ with $M_0 = 0$ and $[M]_t = t$ is a [Brownian motion](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-bm).

**Proof.** *Admitted here.* ∎

**Theorem 3.13 (Martingale representation).**

Let $\mathbb F$ be the [filtration](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-filtration) generated by a [Brownian motion](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-bm) $W$. Every square-integrable $\mathbb F$-martingale $M$ has the form $M_t = M_0 + \int_0^tH_s\,dW_s$ for a unique adapted $H$ with $\E\int_0^TH_s^2\,ds < \infty$.

**Proof.** *Admitted here.* ∎

Both are proved in Karatzas and Shreve (1991, §3.3 and §3.4). The representation theorem is the mathematics of replication: if the only randomness is $W$, every payoff’s [conditional expectation](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-condexp) is a stochastic integral against $W$, and $H$ is the position that reproduces it. One Quant Book 5, chapter 1, builds on it the notions of a replicating portfolio and a complete market; it fails as soon as there are jumps (chapter 6) or more sources of risk than traded assets.

## 3.6 Tutorial: Itô sums and an honest backtest

**Goal.** See the left point of the [Itô integral](#def-qm-ito-calculus-integral) in numbers, check Itô’s formula on one path, and measure the look-ahead profit of a same-bar backtest. **End state:** Figures [3.1](#fig-qm-ito-calculus-sums), [3.3](#fig-qm-ito-calculus-backtest) and [3.4](#fig-qm-ito-calculus-gbm) and the Sharpe ratios 5.4 and $-0.04$.

1. **The integrals.** The running project’s gains are left-point sums; the same-bar version and the covariation that separates them are one line each. `def gains (theta: np.ndarray, s: np.ndarray) -> np.ndarray: """Adapted (Ito) gains: position theta[i], chosen at t_i, earns s[i+1] - s[i]. theta and s have the same length n + 1; the result has length n + 1 and starts at zero.""" th, x = np.asarray(theta, dtype=float ), np.asarray(s, dtype=float ) if th.shape != x.shape: raise ValueError(" theta and s must be aligned on the same times " ) return np.concatenate([[0.0 ], np.cumsum(th[:-1 ] * np.diff(x))]) def same_bar_gains (theta: np.ndarray, s: np.ndarray) -> np.ndarray: """Anticipating gains: the position chosen at t_{i+1} is credited with the move into t_{i+1}.""" th, x = np.asarray(theta, dtype=float ), np.asarray(s, dtype=float ) return np.concatenate([[0.0 ], np.cumsum(th[1 :] * np.diff(x))]) def covariation (x: np.ndarray, y: np.ndarray) -> np.ndarray: """Realised covariation [x, y] accumulated along the grid (quadratic variation when x is y).""" return np.concatenate([[0.0 ], np.cumsum(np.diff(np.asarray(x, float )) * np.diff(np.asarray(y, float )))])` **Listing 3.1.** Adapted gains, same-bar gains and the covariation between them. code/firm/stochint/firm_stochint.py
2. **The rule**: the distance of the close from its moving average, scaled to unit variance under a random walk, and the look-ahead mean of [Proposition 3.5](#prop-qm-ito-calculus-lookahead). `def trend_signal (s: np.ndarray, L: int = LOOKBACK) -> np.ndarray: """theta_t = (S_t - MA_L(t)) / sd, scaled to unit variance under a random walk; zero while the window fills.""" c = np.cumsum(np.concatenate([[0.0 ], s])) ma = np.full(s.size, np.nan) ma[L - 1 :] = (c[L:] - c[:-L]) / L scale = SIG_D * math.sqrt(sum ((m / L) ** 2 for m in range (1 , L))) th = (s - ma) / scale th[: L - 1 ] = 0.0 return th def spurious_mean (L: int = LOOKBACK, sig: float = SIG_D) -> float : """E[theta_t dS_t] for the same-bar rule on a random walk: (L-1)/L sig / sqrt(sum (m/L)^2).""" return (L - 1 ) / L * sig / math.sqrt(sum ((m / L) ** 2 for m in range (1 , L)))` **Listing 3.2.** The trend signal and the expected look-ahead gain. code/methods/03-ito-calculus/python/qm_ito.py
3. **Run** `sums_table()` , `ito_check()` (on a GBM path with $2^{14}$ steps, $\ln S_1 = 0.70040$ against $0.70039$ from Itô’s formula and $0.78179$ without the $-\tfrac12\,d[S]/S^2$ term), `backtest()` and `fig_ito.py` .

**What to change next.** Replace the trend rule by a mean-reversion rule and predict the sign of the look-ahead bias; add a one-tick bid–ask bounce to the prices and see what the honest backtest of a mean-reversion rule then reports.

## 3.7 Build: discrete stochastic integrals

**Purpose.** Every backtest of the miniature firm computes its P&L through these functions, so that a position can earn only the price move after it is decided, and a look-ahead leaves a measurable trace.

**Interface.** `gains(theta, s)`; `same_bar_gains(theta, s)`; `covariation(x, y)`; `lookahead_test(theta, s)` returning the same-bar excess, the covariation it equals, a $t$-statistic and the identity error; `riemann_sums(h, x, point)`.

**Rules.** Positions and prices are aligned on the same timestamps and the function, not the caller, shifts them; the identity same-bar $-$ adapted $=$ covariation holds to rounding.

**Acceptance tests.** `code/firm/stochint/tests/`: on random walks the adapted gains of any rule have mean zero; the same-bar gains of the trend rule have the mean of [Proposition 3.5](#prop-qm-ito-calculus-lookahead); the identity holds to $10^{-12}$; the three Riemann sums of $\int W\,dW$ converge to their limits.

**Stretch.** Timestamps with latencies (a position becomes live some microseconds after its signal); transaction costs as a function of $|\Delta\theta|$.

Sources and further reading

- K. Itô, “Stochastic integral”, *Proceedings of the Imperial Academy, Tokyo* 20, 1944.
- K. Itô, “On a formula concerning stochastic differentials”, *Nagoya Mathematical Journal* 3, 1951.
- R. L. Stratonovich, “A new representation for stochastic integrals and equations”, *SIAM Journal on Control* 4, 1966.
- I. Karatzas and S. E. Shreve, *Brownian Motion and Stochastic Calculus* , Springer, 2nd ed., 1991.

## 3.8 Exercises

**Exercise 3.1 ★.**

Compute $\E[(\int_0^1W_t\,dW_t)^2]$ with the isometry and check it with [Example 3.3](#ex-qm-ito-calculus-wdw).

**Solution of Exercise 3.1.**

$\E\int_0^1W_t^2\,dt = \int_0^1t\,dt = \tfrac12$. With [Example 3.3](#ex-qm-ito-calculus-wdw), $\Var(\tfrac12(W_1^2 - 1)) = \tfrac14\Var(W_1^2) = \tfrac14 \times 2 = \tfrac12$, and the mean is zero.

**Exercise 3.2 ★.**

Write $d(W_t^3)$ with Itô’s formula, and deduce $\E[W_t^3]$ and a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) built from $W^3$.

**Solution of Exercise 3.2.**

$d(W^3) = 3W^2\,dW + 3W\,dt$. Taking expectations, $\E[W_t^3] = 3\int_0^t\E[W_s]\,ds = 0$, and $W_t^3 - 3\int_0^tW_s\,ds$ is a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale).

**Exercise 3.3 ★.**

A stock follows a geometric [Brownian motion](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-bm) with $\mu = 10\%$ and $\sigma = 40\%$. What are the mean and the median of $S_{10}/S_0$, and at what volatility would the median not grow at all?

**Solution of Exercise 3.3.**

Mean $e^{1} = 2.72$, median $e^{(0.10 - 0.08) \times 10} = 1.22$. The median is flat when $\sigma^2 = 2\mu$, $\sigma = \sqrt{0.2} = 44.7\%$.

**Exercise 3.4 ★★.**

Show that $e^{W_t - t/2}$ is a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) and compute $\E[e^{2W_1}]$.

**Solution of Exercise 3.4.**

It is $\mathcal E(W)_t$: $dZ = Z\,dW$, and $\E\int_0^tZ_s^2\,ds = \int_0^te^s\,ds < \infty$, so the [Itô integral](#def-qm-ito-calculus-integral) is a true [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale). $\E[e^{2W_1}] = e^{2}= 7.39$ (the moment generating function of $\mathcal
N(0,1)$ at 2).

**Exercise 3.5 ★★.**

Use the product rule on $tW_t$ to write $\int_0^1 W_t\,dt$ as a stochastic integral, and compute its variance.

**Solution of Exercise 3.5.**

$d(tW_t) = W_t\,dt + t\,dW_t$, so $\int_0^1W_t\,dt = W_1 - \int_0^1t\,dW_t = \int_0^1(1 - t)\,dW_t$, with variance $\int_0^1(1 - t)^2dt = \tfrac13$ by the isometry.

**Exercise 3.6 ★★.**

$X = W^1$ and $Y = 2W^1 + W^2$ with $W^1, W^2$ independent. Compute $[X, Y]_t$, $[Y]_t$ and the instantaneous correlation of $X$ and $Y$.

**Solution of Exercise 3.6.**

$[X, Y]_t = 2t$, $[Y]_t = 5t$, correlation $2/\sqrt5 = 0.894$.

**Exercise 3.7 ★★★.**

*Coding.* Repeat the backtest with a 60-day moving average: compute the look-ahead Sharpe ratio from [Proposition 3.5](#prop-qm-ito-calculus-lookahead) and its average over 100 simulated ten-year walks.

**Solution of Exercise 3.7.**

$L = 60$ gives $a = 0.223$ and a look-ahead Sharpe ratio of 3.45; the average over 100 seeded walks is 3.47. A slower average puts less weight on the last increment, so the bias is smaller, but it is still enormous.

**Exercise 3.8 ★★★.**

*Find the flaw.* “Our rule decides from the closing price and trades in the closing auction; the backtest credits it with the next day’s return, so there is no look-ahead.”

**Solution of Exercise 3.8.**

The signal uses the closing price, which is not known until the closing auction in which the rule claims to trade has printed: the decision is not adapted to the information available when the order must be sent. Compute the signal from the auction’s indicative price published before the order deadline (chapter 1), or trade at the next open.

## 3.9 Problem: The Look-Ahead in the Backtest

**Problem 3.1.**

Weekend problem — a Sharpe ratio of 5.4 on a random walk

The log price is a random walk with 1% daily volatility. The rule holds $\theta_t = (S_t -
\bar S^{(20)}_t)/c$, with $c$ making $\theta$ of unit variance; the flawed backtest credits $\theta_t$ with $S_t - S_{t-1}$, the honest one with $S_{t+1} - S_t$.

**Part I — The rule.**

1. What is $c$ ?
2. What is the correlation $a$ of $\theta_t$ with the day’s increment?
3. What are the mean and standard deviation of the same-bar gain per day?
4. What is the annualised same-bar Sharpe ratio?
5. What does the honest backtest earn on average, and why?

**Part II — The simulation.**

6. What Sharpe ratios does the seeded ten-year history give for the two backtests?
7. Over 400 histories, what are the mean and standard deviation of each?
8. Check the identity same-bar $-$ honest $=$ covariation on the seeded history.
9. What $t$ -statistic does `lookahead_test` report for the seeded history?
10. Why is the honest Sharpe ratio’s standard deviation about $1/\sqrt{10}$ ?

**Part III — The Itô view.**

11. Write the same-bar P&L as an [Itô integral](#def-qm-ito-calculus-integral) plus a covariation.
12. Why is the expectation of the Itô part zero?
13. What are the look-ahead Sharpe ratios for windows of 5, 10 and 60 days?
14. If the flawed backtest ran on five-minute bars, 78 a day, with the same rule in bars, what would its annualised Sharpe ratio be?
15. Would trading at the next day’s open instead of the close remove the bias?

**Part IV — Judgement.**

16. How would you detect this error in someone else’s backtest?
17. Is look-ahead bias always positive?
18. On real prices with a bid–ask bounce, what else can produce a covariation between position and price?
19. State the *named result* : the look-ahead Sharpe ratio of the same-bar trend rule on a random walk.
20. In one sentence: what makes a backtest an [Itô integral](#def-qm-ito-calculus-integral) ?

**Solution of Problem 3.1.**

**1.** $c = 0.01\,(\sum_{m=1}^{19}(m/20)^2)^{1/2} = 0.01 \times 2.485 = 0.0248$. **2.** $a = 0.95/2.485 = 0.382$. **3.** Mean $a\sigma = 0.0038$ and standard deviation $\sigma\sqrt{1 + a^2} = 0.0107$ per day, per unit of position. **4.** $\sqrt{252} \times 0.382/1.070 = 5.67$. **5.** Zero: $\theta_{t}$ is independent of $S_{t+1} - S_t$. **6.** 5.43 same-bar, $-0.04$ next-bar. **7.** Same-bar $5.69 \pm 0.13$; next-bar $0.00 \pm 0.32$. **8.** Both equal 9.653 (965% of notional); the difference is $2 \times 10^{-14}$. **9.** 34.9. **10.** With no edge the annualised Sharpe ratio estimated over $T$ years has a standard error of about $1/\sqrt T$ (chapter 11): $0.32$ for ten years. **11.** $\sum\theta_t\Delta S_t = \sum\theta_{t-1}\Delta S_t + \sum\Delta\theta_t\Delta S_t$: an Itô sum plus the covariation $[\theta, S]$. **12.** Its integrand is adapted, so it is a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) started at zero. **13.** 9.36, 7.47 and 3.45. **14.** The daily Sharpe ratio per bar is unchanged, and annualising over $252 \times 78$ bars multiplies it by $\sqrt{78}$: 50. **15.** Yes, if the signal uses only the close and the gains start at the next open; the position then misses the overnight move, which the honest backtest must also leave out. **16.** Shift the positions by one bar and see whether the performance collapses; run `lookahead_test`, or regress the daily P&L on $\Delta\theta_t\Delta S_t$; and read the timestamps of the signal’s inputs. **17.** No: its sign is the sign of $[\theta, S]$; a mean-reversion rule, which sells as the price rises, has a negative look-ahead bias. **18.** The bounce between bid and ask makes last-trade prices mean-revert; an honest mean-reversion rule on those prices “earns” the bounce, which costs the spread to capture (One Quant Book 10). **19.** Named result: *the look-ahead in the backtest*: on a random walk the same-bar backtest of a unit-variance rule with correlation $a$ to the latest increment has a daily Sharpe ratio $a/\sqrt{1 + a^2}$; for the 20-day trend rule, $a = 0.382$ and the annualised Sharpe ratio is 5.67 (5.69 in simulation), against zero for the Itô sum. **20.** Each position is fixed from information available before the price move it is multiplied by.

## 3.10 Interview questions

**Interview question 3.1 ★ researcher.**

Compute $\int_0^TW_t\,dW_t$. Why is it not $\tfrac12W_T^2$?

**Solution of Interview question 3.1.**

$\tfrac12(W_T^2 - T)$. The left-point sums differ from $\tfrac12\sum\Delta(W^2)$ by $\tfrac12\sum(\Delta W)^2$, which tends to $T$: the [quadratic variation](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-qv) does not vanish.

*What the interviewer is looking for: the telescoping identity and the [quadratic variation](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-qv).*

**Interview question 3.2 ★ trader, researcher.**

A stock has an expected return of 10% and a volatility of 40%. What is its expected log return, and what does a typical holder earn over ten years?

**Solution of Interview question 3.2.**

$\mu - \sigma^2/2 = 2\%$ a year. The mean of $S_{10}/S_0$ is $e^{1} = 2.72$ but the median is $1.22$, and a tenth of holders end below 0.24: the mean is carried by a few large outcomes.

*What the interviewer is looking for: Itô’s correction and mean versus median.*

**Interview question 3.3 ★★ researcher.**

Is $W_t^2$ a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale)? Find the process you must subtract, and the variance of $\int_0^TW\,dW$.

**Solution of Interview question 3.3.**

No: $d(W^2) = 2W\,dW + dt$, so $W_t^2 - t$ is the [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale). $\Var(\int_0^TW\,dW) = \int_0^Tt\,dt =
T^2/2$.

*What the interviewer is looking for: Itô’s formula and the isometry.*

**Interview question 3.4 ★★ developer, researcher.**

How would you design a backtesting engine that cannot commit look-ahead by construction?

**Solution of Interview question 3.4.**

Drive it by events in time order: every datum carries the time it became available, the strategy sees an as-of view, and an order it emits at $t$ can fill only against prices after $t$ plus a latency. The engine, not the strategy, applies positions to the next interval, and a test compares every run with a shifted run and flags covariation between positions and concurrent returns.

*What the interviewer is looking for: availability timestamps, as-of joins, and a structural shift.*

**Interview question 3.5 ★★ researcher, bank.**

What is a [local martingale](#def-qm-ito-calculus-local) that is not a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale), and why should a pricing quant care?

**Solution of Interview question 3.5.**

A process that is a [martingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) only up to a sequence of [stopping times](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-stopping); a positive one is a [supermartingale](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) and may lose expectation, like $1/|B_t|$ for a three-dimensional [Brownian motion](https://one-course.com/books/quant/4/en/chapter/2-brownian-motion#def-qm-brownian-motion-bm). A candidate density for a [change of measure](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-change), or a deflated price, that is a strict [local martingale](#def-qm-ito-calculus-local) gives prices that violate put–call parity or a measure that is not a probability.

*What the interviewer is looking for: localisation, Fatou, and the pricing consequence.*

**Interview question 3.6 ★★★ researcher, mle.**

Itô or Stratonovich: which does finance use, which does physics use, and why?

**Solution of Interview question 3.6.**

Finance uses Itô: a position must be set before the price move, which is the left point, and the resulting gains are [martingales](https://one-course.com/books/quant/4/en/chapter/1-probability-at-speed#def-qm-probability-at-speed-martingale) under the right measure. Physics often uses Stratonovich, the limit of smooth noise, because it keeps the ordinary chain rule. They differ by $\tfrac12[X,W]$.

*What the interviewer is looking for: non-anticipation versus smooth-noise limits.*
