Hamiltonian Neural Networks: Does a Financial System Conserve Anything?
There are two questions you can ask about Hamiltonian Neural Networks, and only one of them is interesting.
The boring question is "are HNNs stable?" The answer is yes, and it is a theorem, not an experiment. Encode Hamilton's equations into the architecture, integrate with a symplectic scheme, and backward error analysis hands you bounded energy error over exponentially long horizons. Nothing about finance is involved. You do not need a backtest to believe it.
The interesting question is the one the stability result quietly presupposes: does a financial state vector admit a conserved quantity at all? An HNN does not discover whether exists. It assumes exists and finds the best one under that assumption. If markets have no conserved for your chosen coordinates, the network will still converge, still produce a smooth scalar, still integrate without blowing up — and the whole thing will be a very stable model of nothing.
This post builds the machinery, then spends the back half trying to break it. The claim under test is narrow and falsifiable: that a chosen pair — say log-price and EMA of returns — is canonically conjugate, meaning actually holds for some . That is an empirical claim, and the draft's own theory section gives no reason to believe it.
Hamiltonian Mechanics: What Is Actually New Here
Phase space, dynamical systems, trajectories and orbits are already vocabulary on this blog — see attractors in algotrading for the definitions and complex manifolds for phase-space reconstruction via Takens embedding. What follows assumes them.
The part that is new is the symplectic structure. Classical mechanics has two equivalent formulations. Newton gives . Hamilton gives a scalar function from which all dynamics follow:
Three consequences matter, and none of them are available to a generic dynamical model:
- is conserved along trajectories. The system is confined to a constant-energy surface — a lower-dimensional manifold inside phase space. This is a hard constraint on where the state can go, not a tendency.
- The symplectic 2-form is preserved. Phase-space volume is exactly conserved (Liouville's theorem). Trajectories cannot converge onto an attractor; the flow is incompressible.
- The dynamics are time-reversible. Run the flow backward and you recover the initial state exactly.
Point 2 is worth pausing on, because it is the sharpest tension with everything else on this blog. Liouville's theorem says a Hamiltonian system has no attractors. Volume-preserving flows cannot contract onto anything. If you believe markets have attracting structure — and the attractors article argues they do — then you are already committed to markets not being Hamiltonian in the strict sense. That is a real conflict, not a technicality, and it is the reason the dissipative extensions below are not an afterthought.
Why the Integrator Matters
Simulate a Hamiltonian system with a naive integrator (Euler, RK4) and the computed trajectory slowly drifts off the constant-energy surface. Local truncation error is small; the geometric error is systematic. Over long horizons the simulation becomes qualitatively wrong: orbits that should close start spiralling.
Standard neural networks have the same defect for a deeper reason. An MLP mapping state to derivatives has no mechanism to preserve anything. It learns local dynamics adequately and accumulates errors that compound.
The Conservation Claim, Briefly
This blog has already argued that markets obey conservation-like principles — Navier-Stokes for algotrading, part 2 makes the case at length, from liquidity-as-mass through to hydrodynamic risk management, and sources the econophysics framing properly. Mean-variance optimisation is likewise already a Lagrangian system on this blog: see Markowitz portfolio theory for the constrained quadratic form and Almgren-Chriss optimal execution for the risk-budget/ identification, where plays exactly the role of a conjugate momentum.
None of that is re-derived here, because the interesting move is different. Those articles use conservation as a metaphor that motivates a model. An HNN turns conservation into an architectural constraint that the model cannot violate even if the data begs it to. That difference — soft analogy versus hard structure — is the entire content of what follows, and it cuts both ways: a constraint the data does not satisfy is not a prior, it is a bug.
HNN Architecture: Structural vs. Penalised Conservation
The general physics-informed mechanism — take autograd derivatives of the network output with respect to its inputs, form a physics residual, minimise it — is already demonstrated in the Navier-Stokes PINN, whose physics_loss builds u_t, u_x, u_xx via autograd.grad and squares the residual. The HNN uses that same mechanism with one change, and the change is the whole point.
A PINN penalises violations of a physical law. The residual is a loss term with a weight; the optimiser trades it off against data fit. Turn the weight down, or hand it conflicting data, and the network will happily violate the law. Conservation is a preference.
An HNN makes conservation structural. It does not learn the vector field , which is unconstrained and conserves nothing. It learns a scalar and derives both components of the vector field from a single gradient:
Because both derivatives come from one scalar potential, the resulting field is symplectic-gradient by construction. , identically, for every parameter setting — including a randomly initialised, completely untrained network. There is no weight to tune down. The conservation is an algebraic identity of the parameterisation, not a term in the objective.
This is a much stronger guarantee than a PINN offers, and it comes with a much sharper failure mode. The loss compares the derived derivatives against observed data:
If the observed field is not a symplectic gradient of anything — if it has curl the parameterisation cannot represent — the loss simply cannot reach zero. The residual floor is not an optimisation failure. It is a measurement of how far the data is from admitting a Hamiltonian. That number is the most useful output of this entire architecture, and it is the number the rest of this post is organised around.
The network itself is unremarkable: an MLP from through two tanh layers of width 200 to a scalar output.
Coordinates
For financial applications, is a generalised position (log-prices, portfolio weights, factor exposures) and a generalised momentum (rates of change, momentum indicators, flow variables). is then whatever invariant the network can find — possibly total market capitalisation, possibly a risk measure, possibly an abstract quantity with no economic reading, possibly nothing.
The choice of pairing is not innocent, and this is the gap in the standard presentation. Nothing about " = log-price, = EMA of returns" establishes that these are conjugate. Conjugacy is a specific structural relationship — must be the momentum canonically paired to , such that holds for some . Asserting it and testing it are different activities, and the literature adapting HNNs to finance mostly does the former.
| (positions) | (momenta) |
|---|---|
| Log-prices | EMA of returns |
| Portfolio weights | Weight change rates |
Symplectic Integrators
Learning is half the problem. Generating trajectories requires integrating Hamilton's equations, and the integrator can destroy what the architecture guarantees.
Symplectic integrators preserve exactly. Phase-space volume is conserved, oscillates around its true value with bounded error rather than drifting secularly, and orbits stay qualitatively correct. RK4 gives you smaller per-step error and none of these properties.
The simplest symplectic scheme is Stormer-Verlet (leapfrog):
For separable these are explicit updates. Financial Hamiltonians are generally non-separable — momentum dynamics depend on prices and vice versa — so the general case needs implicit steps, higher-order methods (Yoshida, Forest-Ruth), or the SympNets route below.
Tong et al. (2020) showed that composing leapfrog steps as network layers produces architectures that are symplectic by construction: each layer is an integration step, depth equals step count, and the learned map preserves symplectic structure exactly.
Stability Guarantees: Real, and Beside the Point
The stability properties follow from theorems, which is precisely why they prove nothing about markets.
Backward error analysis. A symplectic integrator applied to exactly solves a nearby Hamiltonian . The computed trajectory is the exact trajectory of a slightly perturbed system, and KAM theory bounds the energy error over exponentially long times.
Lyapunov stability. If has a minimum at an equilibrium, is itself a Lyapunov function proving that equilibrium stable. No separate analysis required.
Both results are conditional on being the right object. They tell you the simulation of the learned system is faithful; they say nothing about whether the learned system is the market. A perfectly conserved fitted to a residual floor of 0.9 is a stable simulation of an artifact.
| Property | Standard NN + RK4 | HNN + symplectic |
|---|---|---|
| Energy drift over 1000 steps | secular, accumulates | bounded, oscillatory |
| Time reversibility | no | yes |
| Phase-space volume | not preserved | exactly preserved |
| Attractors representable | yes | no (Liouville) |
| Interpretable invariant | none |
Implementation
Core HNN Module
import torch
import torch.nn as nn
class HamiltonianNN(nn.Module):
"""Learns a scalar H(q, p); derives dynamics via Hamilton's equations."""
def __init__(self, input_dim: int, hidden_dim: int = 200):
super().__init__()
self.net = nn.Sequential(
nn.Linear(input_dim, hidden_dim), nn.Tanh(),
nn.Linear(hidden_dim, hidden_dim), nn.Tanh(),
nn.Linear(hidden_dim, 1),
)
def hamiltonian(self, q: torch.Tensor, p: torch.Tensor) -> torch.Tensor:
return self.net(torch.cat([q, p], dim=-1))
def time_derivative(self, q: torch.Tensor, p: torch.Tensor):
q = q.requires_grad_(True)
p = p.requires_grad_(True)
H = self.hamiltonian(q, p)
dH_dq, dH_dp = torch.autograd.grad(H.sum(), [q, p], create_graph=True)
return dH_dp, -dH_dq # dq/dt, dp/dt
Symplectic Leapfrog Integrator
class LeapfrogIntegrator:
"""Stormer-Verlet integrator. Preserves the symplectic structure."""
def __init__(self, hnn: HamiltonianNN, dt: float = 0.01):
self.hnn, self.dt = hnn, dt
def step(self, q: torch.Tensor, p: torch.Tensor):
_, dp_dt = self.hnn.time_derivative(q, p)
p_half = p + 0.5 * self.dt * dp_dt
dq_dt, _ = self.hnn.time_derivative(q, p_half)
q_new = q + self.dt * dq_dt
_, dp_dt = self.hnn.time_derivative(q_new, p_half)
return q_new, p_half + 0.5 * self.dt * dp_dt
def integrate(self, q0, p0, n_steps: int):
traj_q, traj_p, q, p = [q0], [p0], q0, p0
for _ in range(n_steps):
q, p = self.step(q, p)
traj_q.append(q); traj_p.append(p)
return torch.stack(traj_q), torch.stack(traj_p)
Phase-Space Coordinates
Log-prices, diffed returns, EMA momentum and rolling volatility are standard construction on this blog — the diffusion-model pipeline and complex manifolds both ship it. The only two lines that matter here are the ones that define the conjugate pair:
q = np.log(prices)[w:-1] # generalised position
p = ewma(np.diff(np.log(prices), axis=0), w)[w-1:-1] # generalised momentum
Everything downstream — finite-difference , , length alignment, the training loop matching derived derivatives against observed ones with Adam — is mechanical.
Conservation Check
def measure_conservation(hnn, integrator, q0, p0, n_steps=1000):
"""Energy drift along an integrated trajectory."""
with torch.no_grad():
traj_q, traj_p = integrator.integrate(q0, p0, n_steps)
energies = torch.stack([
hnn.hamiltonian(traj_q[i], traj_p[i]) for i in range(n_steps + 1)
]).squeeze().numpy()
return {
"H_initial": energies[0],
"H_final": energies[-1],
"drift": energies[-1] - energies[0],
"oscillation": energies.std(),
"relative_error": abs(energies[-1] - energies[0]) / abs(energies[0]),
}
Note what this function does not test. It measures whether the integrator preserves — which it will, by theorem, whether or not means anything. In-sample conservation of a learned Hamiltonian is not evidence. The test that matters is different.
The Falsification Test
The honest experiment has three parts, and it is designed so that a negative result is informative rather than embarrassing.
1. Residual floor. Fit on real data and report the converged loss, not the conservation. A symplectic-gradient parameterisation cannot represent a vector field with curl; the floor measures the non-Hamiltonian component of the observed dynamics. Fit an unconstrained MLP on the identical data as the reference — it has no structural restriction, so the gap between the two floors is the cost of the Hamiltonian assumption, stated in the loss units of the problem.
2. Out-of-sample conservation. Fit on one window, then evaluate along the observed out-of-sample trajectory — the real price path, not an integrated one. If is genuinely canonical, stays roughly constant along data it never saw. If wanders out of sample, the conserved quantity was an in-sample artifact and the architecture's guarantee is doing no work.
3. Coordinate ablation. This is the part the theory section demands and no presentation of financial HNNs seems to run. Nothing establishes that log-price and EMA-of-returns are conjugate; it is asserted. So test the pairings against each other — (log-price, EMA returns), (log-price, raw returns), (portfolio weights, weight change rates) — and rank them by out-of-sample stability. Against a shuffled-returns surrogate as control: destroy the temporal structure, keep the marginal distribution, refit. If the surrogate yields comparable stability, the "conserved quantity" is a property of the parameterisation rather than of the market.
My prior, stated before the fact so it can be checked against the outcome: this fails. The reasoning is in the primer above — Liouville's theorem forbids attractors, markets appear to have attracting structure, and both of the escape hatches below are admissions that the strict Hamiltonian assumption does not hold. If the residual floor is high and out-of-sample is unstable across every pairing, that is a clean measured negative on a physics-informed prior, and this blog has published those before (the honest negative, deflated Sharpe). A measured negative on an elegant theory is worth more than an untested tutorial.
If Conservation Fails: Dissipative Extensions
Real markets leak. Transaction costs, slippage and information decay remove energy from the system, and a strictly conserved cannot represent that. Two principled relaxations exist, and both should be read as concessions that the assumption was too strong, not as refinements:
- Conformal Hamiltonian: model with a learned dissipation rate . The magnitude of the fitted is itself a measurement — how far the system sits from conservative.
- Port-Hamiltonian: , with the symplectic matrix, the dissipation matrix, and external forcing (news, flows). Here relative to quantifies the departure directly.
Both restore the ability to represent attractors, at the cost of the bounded-error guarantee that motivated the architecture. That is the trade, stated plainly: you can have Liouville or you can have dissipation, and markets appear to want the second.
Related open problems: financial Hamiltonians are non-separable (requiring implicit or higher-order symplectic steps, or SympNets learning symplectic maps directly), and markets operate on multiple time scales that a single is unlikely to capture — hierarchical Hamiltonians for fast and slow dynamics, or an adiabatic treatment of slow variables as parameters of a fast-scale .
Conclusion
The machinery works. Learn a scalar , derive the vector field from its gradient, integrate symplectically, and you get conservation as an algebraic identity, bounded error over thousands of steps, exact time-reversibility, and a Lyapunov function for free. Every one of those claims is a theorem and none of them require validation.
What requires validation is the premise. HNNs do not test whether a conserved quantity exists — they assume it and optimise under the assumption, converging to a smooth, stable, well-behaved model regardless. The residual floor and out-of-sample stability are the only two numbers that distinguish "the market conserves something" from "the parameterisation always produces something."
So the question this post ends on is not whether HNNs are stable. It is whether the elegance survives contact with BTCUSDT — and until those numbers are on the page, the correct posture toward every conservation claim in financial machine learning, including this one, is that it has not been tested.
References
- Greydanus, S., Dzamba, M., & Sprague, J. (2019). Hamiltonian Neural Networks. NeurIPS 2019.
- Tong, Y., Xiong, S., He, X., Pan, G., & Zhu, B. (2020). Deep Hamiltonian Networks Based on Symplectic Integrators. arXiv preprint.
- Baaquie, B. E. (2000). Hamiltonian in Financial Markets. arXiv preprint.
- Cotler, J. & Rezchikov, S. (2023). Efficient Hamiltonian Simulation for Solving Option Price Dynamics. Physical Review Research.
- Gonon, P. & Ortega, J.-P. (2009). Stock Markets and Quantum Dynamics. arXiv preprint.
- Greydanus, S. (2019). Hamiltonian Neural Networks — Blog Post.
- Santos, C. et al. (2022). Symplectic Momentum Neural Networks. PMLR.
Autori
Trading-systems engineer
Trading-systems engineer building bots since 2017: cross-exchange arbitrage (connected up to 30 venues), cointegration-based pairs arbitrage across spot and futures, scalping, news and sentiment-driven strategies, trend algorithms, and portfolio management and balancing algorithms. Also builds sub-millisecond order execution, big-data warehouses, backtesting engines, AI agents, and trading interfaces (incl. open-source profitmaker.cc). Stack: JS/TS, Python, Rust/Zig/Go, DevOps, backend, frontend, architecture.