Quant Memo
Advanced

Stochastic Control and the HJB Equation

How to make the best decision at every instant when the future is random: the Hamilton-Jacobi-Bellman equation turns a lifetime of choices into one equation about right now.

Prerequisites: The Bellman Equations, Itô's Lemma, Brownian Motion

Suppose you manage a portfolio and can rebalance continuously between a risky asset and cash. How much should you hold in the risky asset, at every instant, for the rest of your life, to maximise how happy you'll be with your final wealth? You can't just pick one number — the right allocation depends on how much wealth you currently have, how much time is left, and the fact that tomorrow's price is random. Answering "what should I do right now" when the future keeps branching randomly is what stochastic control is for, and the Hamilton-Jacobi-Bellman (HJB) equation is the single equation that answers it.

The analogy: a canoeist crossing a river with a current that shifts by the minute

Picture paddling across a river to reach a dock, but the current's strength and direction drift randomly as you go. You can't plan your whole route in advance — a plan made now will be wrong five minutes from now, when the current has changed. The only sane strategy is a rule: at every point in the river, given how the current feels right now, choose the paddling direction that gives you the best expected outcome from here onward. That rule is a function of your position and the current, not a fixed list of moves. The HJB equation is the mathematical machine that produces exactly this kind of rule for any random system: instead of planning the whole journey, it tells you the best action at every possible state, at every moment, accounting for all the randomness still to come.

Writing it down

Let XtX_t be the state of the system at time tt — in the canoe story, your position; in finance, your wealth. You pick a control utu_t at each instant — your paddling direction, or the fraction of wealth in stocks — and the state evolves as a stochastic differential equation:

dXt=b(Xt,ut)dt+σ(Xt,ut)dWt.dX_t = b(X_t, u_t)\,dt + \sigma(X_t, u_t)\,dW_t .

In words: the state drifts by an amount bb that depends on where you are and what you do, plus a random wiggle of size σ\sigma driven by Brownian motion WtW_t, whose size also depends on your state and your choice.

You want to choose utu_t at every instant to maximise the expected value of a reward — some running payoff ff collected along the way plus a payoff gg at the final time TT:

V(t,x)=maxu  E[tTf(Xs,us)ds+g(XT)  |  Xt=x].V(t,x) = \max_{u} \; \mathbb{E}\left[\int_t^T f(X_s,u_s)\,ds + g(X_T) \;\middle|\; X_t = x\right].

In words: V(t,x)V(t,x), the value function, is the best expected total payoff you can achieve if you're in state xx at time tt and play optimally from there on. It is the entire point of the exercise — everything else builds toward computing VV.

The HJB equation is what VV must satisfy. It comes from a one-line argument: the best thing to do over a tiny interval dtdt is to pick the uu that maximises the instant's reward plus the expected change in the future's best value, and by Itô's lemma that expected change is tV+bxV+12σ2xxV\partial_t V + b\,\partial_x V + \tfrac12\sigma^2\partial_{xx}V. Setting the best achievable rate of improvement to zero (since VV is already optimal) gives:

tV(t,x)+maxu[f(x,u)+b(x,u)xV(t,x)+12σ(x,u)2xxV(t,x)]=0.\partial_t V(t,x) + \max_{u}\left[f(x,u) + b(x,u)\,\partial_x V(t,x) + \tfrac{1}{2}\sigma(x,u)^2\,\partial_{xx} V(t,x)\right] = 0 .

In words: at every instant, the rate the value function is falling due to time passing (tV\partial_t V) is exactly offset by the best you can currently extract — the running reward ff, plus how a small nudge in the state moves value (xV\partial_x V, the marginal value of the state), plus a correction for the randomness itself (xxV\partial_{xx}V, curvature — this is the same Itô correction that shows up everywhere randomness meets calculus). The maximisation inside the brackets is solved pointwise, for each (t,x)(t,x) separately — that is what turns an infinite-dimensional planning problem into something you can actually compute.

Path explorer
13055time →
end (bold path) 100.38spread of ends 58.966 independent paths, same settings

Drag the drift and volatility here and watch how the paths fan out. HJB is the tool that decides, at every point along a fan like this, what action keeps the expected best outcome highest — not just for one path, but averaged over every path that could still happen.

Worked example 1: the merton problem, one step at a time

Take the classic setup: wealth XtX_t, invest fraction uu in a risky asset with expected return μ\mu and volatility σ\sigma, rest in cash earning zero, no consumption, maximise E[logXT]\mathbb{E}[\log X_T]. Wealth evolves as dX=uμXdt+uσXdWdX = u\mu X\,dt + u\sigma X\,dW. Guess V(t,x)=logx+h(t)V(t,x) = \log x + h(t) for some function hh (log utility has this clean separable form). Then xV=1/x\partial_x V = 1/x, xxV=1/x2\partial_{xx}V = -1/x^2, and the HJB equation becomes

h(t)+maxu[uμ12u2σ2]=0.h'(t) + \max_u \left[u\mu - \tfrac12 u^2\sigma^2\right] = 0 .

The bracket is a plain quadratic in uu — maximise it by calculus: differentiate with respect to uu, set to zero: μuσ2=0\mu - u\sigma^2 = 0, so u=μ/σ2u^\star = \mu/\sigma^2. Plug numbers in: μ=8%\mu = 8\%, σ=20%\sigma = 20\%, so u=0.08/0.04=2.0u^\star = 0.08 / 0.04 = 2.0. This says invest 200% of wealth in the risky asset — borrowing 100% of wealth to lever up — because log utility with this risk-reward ratio wants that much exposure. Note the answer uu^\star didn't depend on wealth xx or time tt at all; that constant-fraction result is the celebrated Merton finding, and it dropped straight out of solving the bracket.

now state A: V = 9.487 state B: V = 10.488 already solved already solved action 1 action 2
Backward induction: the value at each future state is computed first, then "now" simply compares the payoffs of each available action plus its already-known future value. The HJB equation is this same sweep, done continuously instead of at discrete nodes.

Worked example 2: a two-state grid, done by hand

Take a toy two-period, two-state version to see the "solve backward" logic without calculus. Wealth is either $90 or $110 at time 1 (equally likely), and at time 1 you choose to hold cash (locks in current wealth) or a coin-flip bet that pays $20 gain or $20 loss with equal probability. Utility is wealth\sqrt{\text{wealth}}.

At $90: cash gives 90=9.487\sqrt{90}=9.487. The bet gives 0.5110+0.570=0.5(10.488)+0.5(8.367)=9.4270.5\sqrt{110}+0.5\sqrt{70}=0.5(10.488)+0.5(8.367)=9.427. Cash wins, so V(90)=9.487V(90)=9.487.

At $110: cash gives 110=10.488\sqrt{110}=10.488. The bet gives 0.5130+0.590=0.5(11.402)+0.5(9.487)=10.4440.5\sqrt{130}+0.5\sqrt{90}=0.5(11.402)+0.5(9.487)=10.444. Cash wins again, V(110)=10.488V(110)=10.488.

Working backward from these values to time 0 is exactly the HJB logic in discrete form: at each state, compare the payoff of each action plus the already-computed future value, and keep the best. The continuous-time HJB equation is the same backward sweep, shrunk to instants and solved with calculus instead of a lookup table.

What this means in practice

  • Portfolio choice, market making, and execution algorithms all reduce to an HJB equation. Optimal execution (how fast to sell a large block) and market-maker quoting (Avellaneda-Stoikov) are both HJB problems with different state variables.
  • Solving HJB by hand only works for a few tidy cases (log or power utility, linear dynamics). Real trading desks solve it numerically — on a grid, or via reinforcement learning, which is secretly approximating the same value function.
  • The verification theorem matters. A function that satisfies the HJB equation and the right boundary conditions is provably the optimal value — this is what justifies trusting a numerically computed policy in production.

The HJB equation turns "find the best plan over a random future" into "at every state, right now, pick the action that maximises immediate reward plus the expected rate of change of the best-possible future value." Solving it backward from the endpoint mirrors ordinary dynamic programming; the only new ingredient is the Itô correction term for randomness in the state.

The classic confusion is treating the HJB equation as something you solve forward, the way you'd simulate a path. You cannot — the value function at time tt depends on optimal decisions all the way to the terminal time TT, so it must be computed backward from TT to 00, exactly like discrete-time dynamic programming. A second, related trap: forgetting that the maximisation over uu happens inside the equation, separately at every (t,x)(t,x). Students often solve for a single "optimal uu" as if it were one number for the whole problem, when in general it is a full function of time and state — it only collapsed to a constant in the Merton example because of the special structure of log utility.

Related concepts

Practice in interviews

Further reading

  • Fleming & Soner, Controlled Markov Processes and Viscosity Solutions (ch. 2)
  • Merton, Optimum Consumption and Portfolio Rules in a Continuous-Time Model (1971)
  • Pham, Continuous-time Stochastic Control and Optimization with Financial Applications (ch. 3)
ShareTwitterLinkedIn