Hamilton-Jacobi-Bellman Equation: Reinforcement Learning and Diffusion Models
TL;DR Highlight
Continuous-time Bellman gives the HJB equation, solved with neural policy iteration and linked to diffusion models.
Who Should Read
A developer building LLM services without an ML background who wants to understand the math behind RL and diffusion models.
Core Mechanics
- HJB is Bellman's equation in continuous time
- Discrete Bellman picks the action maximizing immediate reward plus next-state value.
- Shrinking the time step h to 0 turns the same logic into a PDE, the HJB equation.
- Bellman noticed in the 1950s that it has the same structure as the 19th-century Hamilton-Jacobi equation from physics.
- Noise adds a curvature term
- The state follows an SDE: dX = f dt + Σ dW.
- The HJB becomes ρV(x) = max_a { r(x,a) + L^a V(x) }.
- The generator L^a adds a ½Tr(ΣΣᵀ∇²V) term to the gradient term.
- Policy iteration with two neural nets
- The value V_θ and policy α_φ are each an MLP.
- Evaluation fits V by regression on Monte Carlo rollouts.
- Improvement does gradient ascent on Q(x, α_φ(x)), with ∇V and ∇²V from autograd and detached.
- Without a model, use continuous-time Q-learning
- Policy iteration is model-based and needs f and Σ.
- From sampled transitions (X_t, a_t, r_t, X_{t+Δt}), build the TD target y = rΔt + e^{-ρΔt}V̄(X_{t+Δt}).
- The critic fits this target with MSE and the actor ascends Q.
- Validated on two closed-form problems
- On stochastic LQR, learned V and policy closely match V*=-½Px²-c and a*=-Kx.
- On the Merton portfolio, the controls converge to π*=0.625 and k*≈0.0478.
- Both have analytic solutions, which makes them good solver checks.
- Diffusion models can be framed as control
- Reverse-time sampling is written as a diffusion with control u.
- Setting V(x,t) = -log p_{T-t}(x) makes it the value function of a finite-horizon HJB.
- The goal of the control is to match the terminal distribution to p_data.
Community Reactions
- Control theory still pays off
- A commenter who got bitten by the control bug in EE undergrad says direct applications made the hard math bearable.
- The math feels too hard
- A software engineer feels outclassed by math PhDs and worries about their career.
- An RL beginner finds the post beyond their level and asks for resources and step-by-step library examples.
- Doubt about continuous math on digital computers
- One commenter argues it is not obvious why equations over infinite-precision reals map onto finite bit-string algorithms.
- Formatting issues reported
- A commenter says the Bellman equation label overlaps the equation and quote marks leaked into the displayed HJB.
- They also report the 'Suggest changes' link returns 404.
How to Apply
- Compute the generator with autograd
- Following the post's compute_generator idea, take ∇V and ∇²V from a 1-D V_net and compute L^aV.
- Check it against V(x)=-½Px², whose answer you know.
- Reproduce neural PI on LQR
- Implement the 1-D stochastic LQR using the parameter table in the post.
- Check that the policy converges to a*=-Kx and the HJB residual shrinks.
- Log the HJB residual as a stopping signal
- On sampled points, log the norm of ρV - max_a{r + L^aV}.
- Stop training when it and the parameter changes plateau.
Try It
# Paste into Claude/ChatGPT (simplified version of the post's method: use the LLM as a tutor)
I'm a backend developer without an ML background. Explain the following step by step in PyTorch.
Goal: solve a 1-D stochastic LQR with neural policy iteration
- Dynamics: dX = (alpha*X + beta*a) dt + sigma dW
- Reward: r(x,a) = -0.5*(q*x^2 + r_a*a^2)
- Values: alpha={{alpha}}, beta=1.0, q=1.0, r_a=0.1, sigma=0.3, rho=0.1
- V_net and policy_net are each an MLP
- generator: L^a V = dV/dx * f + 0.5 * sigma^2 * d2V/dx2 (use autograd)
- Policy evaluation: Monte Carlo rollouts + regression
- Policy improvement: gradient ascent on Q=(r+L^aV)/rho (detach V's derivatives)
Requests:
1) A minimal PyTorch implementation under 30 lines
2) Code comparing against the analytic solution (Riccati equation for P)
3) Code that prints the HJB residualTerminology
Related Papers
Frostbite: A new CPU architecture discovered, designed and verified by AI
AI agents designed a CPU from scratch, claiming 4.59x perf/W over a traditional core
Restricting AI agents in mobile operating systems platforms from capturing data
Signal now blocks OS-level AI agents from reading its screen on iOS 27 and Android
Actions Speak Louder than Words: Stateful Privacy Sniffing of LLM Agents via Behavioral Covert Channels in Web Interactions
Agents' link choices alone let attackers rebuild user queries at F1 0.89; SOR cuts it to 0.39.
Tracking and Limiting Unsupported Claim Propagation in Multi-Agent LLM Systems: A Provenance-Aware Framework
Passing claims with source, evidence and history kept equivalence at 5 agent stages at 96% versus 31%.
LLM assisted reverse engineering
Pointing an agent at binaries with skills and MD notes made years-long hacks feasible
Metrics-in-the-Loop: Complexity Feedback Reshapes Agent-Written Code
Injecting per-turn complexity reports into coding agents cut code erosion without changing correctness
Why is Gemini 4 Carbon taking so long?
Carbon took nearly an hour on bug fixes that Astra and Opus finished in minutes, mostly from extra unrequested work.
Weight-Space Programming builds neural networks like a 1990s researcher
LLM hand-wrote MNIST weights to 91.3%, using 1990s-style techniques