Quantitative Methods · Methods
3Itô 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 . The “prices” were a pure random walk, with nothing to find. The first backtest was not an Itô 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, 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 representation) that underlie the pricing of One Quant Book 5.
3.1 The Itô integral
A trading strategy holds units of an asset whose price follows a Brownian motion; its gains over should be . The paths of have infinite total variation (Theorem 2.12), 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 evaluates at the left point, which is the only choice a trader can make.
Definition 3.1 (Simple process, Itô integral)
A simple process is with and each bounded and -measurable. Its Itô integral is . For an adapted with , the Itô integral is the limit of the integrals of simple processes with .
Theorem 3.2 (Itô isometry)
For adapted with , is a continuous square-integrable martingale with and
Proof. For a simple process, with : the cross terms , , vanish because is -measurable and ; the square terms give . The same computation on gives the martingale property. The integral is therefore an isometry from simple processes into , extends to their closure, and the martingale property passes to limits (Doob’s inequality, Proposition 1.9, gives a continuous version). ∎
Example 3.3 (The integral of against itself)
On a partition of , . The first sum telescopes to and the second tends to by Theorem 2.12:
The right-point sums tend to , and the midpoint sums to , the ordinary calculus answer (Figure 3.1).
Definition 3.4 (Stratonovich integral)
The Stratonovich integral is the limit of the sums . For a continuous semimartingale it equals .
The Stratonovich integral 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 with positions and prices computes gains : the left-point sum, an Itô integral, when uses only information available at . A backtest that credits the position computed at with the move into computes the right-point sum instead, and
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, with the 20-day moving average and chosen so that has unit variance, puts weight on the last increment: Figure 3.2 shows the timing and Figure 3.3 the result.
Proposition 3.5 (The look-ahead Sharpe ratio on a random walk)
Let be a random walk with daily increments , independent standard normal, and a unit-variance linear function of past increments with correlation with the latest one. Then the same-bar gains have mean and standard deviation , a daily Sharpe ratio of , while the next-bar gains have mean zero. For the moving-average rule, .
Proof. Write with standard normal and independent of . Then and , so the variance is (in units of ). For the rule, , whose variance is and whose coefficient on is . Next-bar gains have zero mean because is independent of . ∎
For , and the annualised same-bar Sharpe ratio is ; 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.
3.3 Itô’s formula in one and several dimensions
Definition 3.6 (Itô process, quadratic covariation)
An Itô process is , written , with adapted , such that and are finite almost surely. The quadratic covariation of two continuous processes is the limit in probability ; for Itô processes driven by with , .
The bookkeeping rules are , , : only products of two Brownian increments survive at first order in .
Theorem 3.7 (Itô’s formula)
If is an Itô process and , then
For and , ; in particular .
Partial proof. For with bounded derivatives and simple, Taylor’s formula on a partition gives . The first sum converges to . In the second, may be replaced by at a cost bounded by the modulus of continuity of times , which tends to zero, and tends to zero in as in Theorem 2.12. General , , follow by localisation and approximation (Karatzas and Shreve, 1991, §3.3). ∎
Example 3.8 (The logarithm of a geometric Brownian motion)
For , with : . The expected return is but the expected log return, the rate at which the median grows, is . With and , the mean of is and its median (Figure 3.4); the volatility decay of leveraged funds (One Quant Book 1, chapter 14) is the same correction applied twice.
3.4 Local martingales and the stochastic exponential
An Itô integral with only almost surely need not be integrable, let alone a martingale; it is one up to a sequence of stopping times.
Definition 3.9 (Local martingale)
An adapted process is a local martingale if there are stopping times such that each stopped process is a martingale.
Proposition 3.10 (Positive local martingales are supermartingales)
A local martingale bounded below, in particular a positive one, is a supermartingale. A local martingale with for every is a martingale.
Proof. If , Fatou’s lemma gives . Under the domination, dominated convergence replaces the inequality by an equality. ∎
The inequality can be strict: for a three-dimensional Brownian motion started away from the origin is a positive local martingale whose expectation decreases, a strict local martingale. Such processes model price bubbles and are why chapter 5 checks that a candidate density process is a true martingale before using it to change the measure.
Definition 3.11 (Stochastic exponential)
The stochastic exponential of a continuous semimartingale with is , the unique solution of , .
By Itô’s formula, . With it is the positive local martingale , the density process of every change of measure in chapter 5.
3.5 The representation theorem
Theorem 3.12 (Lévy’s characterisation)
A continuous local martingale with and is a Brownian motion.
Proof. Admitted here. ∎
Theorem 3.13 (Martingale representation)
Let be the filtration generated by a Brownian motion . Every square-integrable -martingale has the form for a unique adapted with .
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 , every payoff’s conditional expectation is a stochastic integral against , and 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 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, 3.3 and 3.4 and the Sharpe ratios 5.4 and .
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 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.
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 - Run
sums_table(),ito_check()(on a GBM path with steps, against from Itô’s formula and without the term),backtest()andfig_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 -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; the identity holds to ; the three Riemann sums of converge to their limits.
Stretch. Timestamps with latencies (a position becomes live some microseconds after its signal); transaction costs as a function of .
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 with the isometry and check it with Example 3.3.
Exercise 3.2 ★
Write with Itô’s formula, and deduce and a martingale built from .
Exercise 3.3 ★
A stock follows a geometric Brownian motion with and . What are the mean and the median of , and at what volatility would the median not grow at all?
Solution
Solution of Exercise 3.3.
Mean , median . The median is flat when , .
Exercise 3.4 ★★
Show that is a martingale and compute .
Solution
Solution of Exercise 3.4.
It is : , and , so the Itô integral is a true martingale. (the moment generating function of at 2).
Exercise 3.5 ★★
Use the product rule on to write as a stochastic integral, and compute its variance.
Solution
Solution of Exercise 3.5.
, so , with variance by the isometry.
Exercise 3.6 ★★
and with independent. Compute , and the instantaneous correlation of and .
Solution
Solution of Exercise 3.6.
, , correlation .
Exercise 3.7 ★★★
Coding. Repeat the backtest with a 60-day moving average: compute the look-ahead Sharpe ratio from Proposition 3.5 and its average over 100 simulated ten-year walks.
Solution
Solution of Exercise 3.7.
gives 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
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 , with making of unit variance; the flawed backtest credits with , the honest one with .
Part I — The rule.
- What is ?
- What is the correlation of with the day’s increment?
- What are the mean and standard deviation of the same-bar gain per day?
- What is the annualised same-bar Sharpe ratio?
- What does the honest backtest earn on average, and why?
Part II — The simulation.
- What Sharpe ratios does the seeded ten-year history give for the two backtests?
- Over 400 histories, what are the mean and standard deviation of each?
- Check the identity same-bar honest covariation on the seeded history.
- What -statistic does
lookahead_testreport for the seeded history? - Why is the honest Sharpe ratio’s standard deviation about ?
Part III — The Itô view.
- Write the same-bar P&L as an Itô integral plus a covariation.
- Why is the expectation of the Itô part zero?
- What are the look-ahead Sharpe ratios for windows of 5, 10 and 60 days?
- 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?
- Would trading at the next day’s open instead of the close remove the bias?
Part IV — Judgement.
- How would you detect this error in someone else’s backtest?
- Is look-ahead bias always positive?
- On real prices with a bid–ask bounce, what else can produce a covariation between position and price?
- State the named result: the look-ahead Sharpe ratio of the same-bar trend rule on a random walk.
- In one sentence: what makes a backtest an Itô integral?
Solution
Solution of Problem 3.1.
1. . 2. . 3. Mean and standard deviation per day, per unit of position. 4. . 5. Zero: is independent of . 6. 5.43 same-bar, next-bar. 7. Same-bar ; next-bar . 8. Both equal 9.653 (965% of notional); the difference is . 9. 34.9. 10. With no edge the annualised Sharpe ratio estimated over years has a standard error of about (chapter 11): for ten years. 11. : an Itô sum plus the covariation . 12. Its integrand is adapted, so it is a martingale started at zero. 13. 9.36, 7.47 and 3.45. 14. The daily Sharpe ratio per bar is unchanged, and annualising over bars multiplies it by : 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 ; and read the timestamps of the signal’s inputs. 17. No: its sign is the sign of ; 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 to the latest increment has a daily Sharpe ratio ; for the 20-day trend rule, 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 . Why is it not ?
Solution
Solution of Interview question 3.1.
. The left-point sums differ from by , which tends to : the quadratic variation does not vanish.
What the interviewer is looking for: the telescoping identity and the quadratic variation.
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
Solution of Interview question 3.2.
a year. The mean of is but the median is , 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 a martingale? Find the process you must subtract, and the variance of .
Solution
Solution of Interview question 3.3.
No: , so is the martingale. .
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
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 can fill only against prices after 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 that is not a martingale, and why should a pricing quant care?
Solution
Solution of Interview question 3.5.
A process that is a martingale only up to a sequence of stopping times; a positive one is a supermartingale and may lose expectation, like for a three-dimensional Brownian motion. A candidate density for a change of measure, or a deflated price, that is a strict local martingale 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
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 under the right measure. Physics often uses Stratonovich, the limit of smooth noise, because it keeps the ordinary chain rule. They differ by .
What the interviewer is looking for: non-anticipation versus smooth-noise limits.