- Solve real problems with our hands-on interface
- Progress from basic puts and calls to advanced strategies

Posted November 5, 2026 at 9:41 am
Classical portfolio construction is two problems in sequence: estimate expected returns and a covariance matrix, then solve an optimisation. The errors of the first stage dominate the second, and the first stage is never told what the second stage will do with its output. An end-to-end allocation network collapses the two: a sequence model reads a window of market data and emits portfolio weights directly, and the loss function is the performance statistic itself — a Sharpe ratio computed net of transaction costs, with regularisers on concentration and turnover. Nothing in the pipeline ever predicts a return. This article sets out the formulation, explains why differentiating a ratio behaves differently from differentiating a forecast error, gives a complete PyTorch implementation including the causal feature construction that most reproductions get wrong, and sets against the merits a blunt accounting of the statistical problem: the effective sample size is the number of periods, not the number of data points.
Mean–variance optimisation is not usually wrong because the optimiser is wrong. It is wrong because of what it is fed. Expected returns are estimated with standard errors of the same order as the estimates themselves, the optimiser is a maximiser and therefore an error-maximiser, and the resulting weights are famously unstable with respect to inputs that are themselves unstable. Shrinkage, resampling and constraints are all ways of damping a problem created upstream.
A second, subtler objection is that the two stages have different objectives. A forecasting model minimises squared error, weighting every asset and every period equally. The portfolio does not care about squared error; it cares about the realised risk-adjusted return of a particular weighted combination, net of what it costs to trade into it. A forecast can improve on its own metric and make the portfolio worse, and in practice this is common.
End-to-end — or decision-focused — learning removes the intermediate quantity. If a network maps market data to weights, and the weights determine a differentiable performance statistic, then gradient descent can be pointed at the statistic. Expected returns and covariances never appear. Whatever the network learns about them is implicit, task-specific, and never written down.
Weights by construction. The network’s output layer is a softmax across assets:
Long-only and fully invested hold exactly, at every step of training, without a projection or a penalty. The initialisation matters as a consequence: an untrained network with small output weights produces approximately equal weights, so training begins at 1/N and learns tilts away from it.
The realised return. With simple returns ri,t+1 and proportional cost c per unit turnover,
The cost term is what forces the network to care about the path of its own weights. Without it, nothing penalises a model that re-allocates completely every period.
The objective. The loss is the negative Sharpe ratio of that series over the mini-batch, plus two regularisers:
where H(w) = −Σ wi log wi is the entropy of the weight vector. Entropy is maximised at equal weight and falls towards zero as the book concentrates, so the term is a differentiable diversification preference expressed in the objective rather than as a constraint. The turnover term appears twice in effect — once inside the Sharpe ratio through the cost deduction, and once as an explicit penalty — because the first is scaled by the (small) cost and the second controls turnover directly.
Why a ratio behaves differently. The gradient of a Sharpe ratio with respect to a return observation is
Two consequences follow. First, the objective is scale-free in the returns, so the network cannot improve its loss by levering up — the leverage decision is genuinely separate, which is why volatility scaling is applied after training rather than learned. Second, the gradient on a period is reduced, and can reverse sign, when that period’s return is an outlier in the favourable direction: the objective dislikes variance wherever it comes from. This is the behaviour the objective was chosen for, but it means a large gain and a large loss are not treated as opposites.
Volatility scaling, applied afterwards. The book is scaled so that its ex-ante volatility sits near a target:
Every input to mt must be known at t. The volatility estimate is an exponentially weighted average lagged by one period; using a contemporaneous volatility, or one computed over the whole sample, converts a leverage rule into a look-ahead, and the effect on reported risk-adjusted return is large (Moreira & Muir, 2017; Harvey et al., 2018).
Features. Each decision point gets a look-back window of three features per asset: a price normalised by the last price in the window, a raw return, and a return divided by a trailing volatility estimate. The normalisation is what lets assets with very different volatilities share one network — without it, the highest-volatility asset dominates every gradient. All three are computed from information available at the decision point only.
Architecture. An LSTM (Hochreiter & Schmidhuber, 1997) reads the window and produces a hidden state per time step; a self-attention block (Vaswani et al., 2017) lets the model weight time steps by relevance rather than by recency alone; a feed-forward block with residual connections adds capacity; a linear head followed by softmax produces the weights from the final hidden state. The architecture is deliberately ordinary — the idea being tested is the objective, not the network.
Training. Mini-batches must be contiguous in time and must not be shuffled, for two reasons: the turnover term differences consecutive rows, and the Sharpe ratio is a property of a sequence. This single requirement rules out most default training loops. Adam with cosine-decayed learning rate, gradient clipping, dropout and early stopping on validation net Sharpe are standard; the ensemble-over-seeds and exponential smoothing of weights that practitioners add are both variance-reduction devices for an objective whose optimum is shallow.
Evaluation. Weights at t are applied to returns from t to t+1, costs are charged on the change in weights, and only then is the volatility multiplier applied. Walk-forward retraining on an expanding window is the honest protocol; a single train–test split on a fixed window answers a different and easier question.
PyTorch and NumPy. The four blocks are features, network, loss and training.
The look-back tensor. The two causality traps live in this function. The window-normalised price divides by the last price in the window, not the first and not a future price; and the volatility used to standardise returns is lagged by one period. Both are easy to get wrong and neither produces an error message.
import numpy as np
import torch
import torch.nn as nn
def build_tensors(R, window=50, vol_span=50):
"""Three scale-free features per asset: normalised price, raw return, vol-scaled return.
R : (T, N) simple returns. Returns X of shape (T - window, window, 3N) and the
next-period return matrix Y of shape (T - window, N). Every feature at row t
uses information up to and including t, so the map from X[t] to Y[t] is causal.
"""
T, N = R.shape
price = np.cumprod(1.0 + R, axis=0)
ew = np.zeros_like(R)
a = 2.0 / (vol_span + 1.0)
var = R[:vol_span].var(axis=0)
for t in range(T): # EWMA variance, causal
var = (1 - a) * var + a * R[t] ** 2
ew[t] = np.sqrt(var)
ew = np.vstack([ew[:1], ew[:-1]]) # lag by one period
X, Y = [], []
for t in range(window, T - 1):
p = price[t - window:t] / price[t - 1] # window-normalised price
r = R[t - window:t]
z = R[t - window:t] / np.maximum(ew[t - window:t], 1e-8)
X.append(np.concatenate([p, r, z], axis=1))
Y.append(R[t + 1]) # next-period return
return (torch.tensor(np.array(X), dtype=torch.float32),
torch.tensor(np.array(Y), dtype=torch.float32),
torch.tensor(ew[window:T - 1], dtype=torch.float32))The network. Roughly fifty thousand parameters for a small cross-section — worth holding in mind against the sample size discussed in Section 6.
class AllocationNet(nn.Module):
"""LSTM over the look-back window, self-attention across time, softmax over assets."""
def __init__(self, n_features, n_assets, hidden=64, n_heads=4, dropout=0.30):
super().__init__()
self.lstm = nn.LSTM(n_features, hidden, batch_first=True)
self.attn = nn.MultiheadAttention(hidden, n_heads, dropout=dropout,
batch_first=True)
self.ffn = nn.Sequential(nn.Linear(hidden, 2 * hidden), nn.GELU(),
nn.Dropout(dropout), nn.Linear(2 * hidden, hidden))
self.head = nn.Linear(hidden, n_assets)
def forward(self, x): # x: (B, window, 3N)
h, _ = self.lstm(x)
a, _ = self.attn(h, h, h, need_weights=False) # attention over time steps
h = h + a
h = h + self.ffn(h)
return torch.softmax(self.head(h[:, -1, :]), dim=-1) # long-only, sums to oneThe objective. The previous weight vector of the first row in a batch is detached, so gradients do not flow across the batch boundary while the turnover term still sees a sensible first difference.
def sharpe_loss(w, y, cost=1e-4, l_entropy=0.02, l_turnover=0.30, eps=1e-8):
"""Negative Sharpe ratio of the net-of-cost portfolio return, with two regularisers.
The batch must be in time order: the turnover term differences consecutive rows.
"""
w_prev = torch.cat([w[:1].detach(), w[:-1]], dim=0)
turnover = (w - w_prev).abs().sum(-1)
rp = (w * y).sum(-1) - cost * turnover
sharpe = rp.mean() / (rp.std(unbiased=False) + eps)
entropy = -(w * (w + eps).log()).sum(-1).mean() # pushes away from corners
return -(sharpe + l_entropy * entropy) + l_turnover * turnover.mean()Training and the leverage overlay. Note range(0, len(X) - batch + 1, batch) with no shuffling, and that vol_scaled_return takes the lagged volatility array as an argument rather than recomputing it — the signature makes the causality requirement explicit.
def train(model, X, Y, epochs=30, batch=128, lr=1e-3, clip=1.0):
opt = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=1e-5)
sched = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=epochs, eta_min=1e-5)
for epoch in range(epochs):
model.train()
total = 0.0
for i in range(0, len(X) - batch + 1, batch): # sequential, never shuffled
opt.zero_grad()
loss = sharpe_loss(model(X[i:i + batch]), Y[i:i + batch])
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), clip)
opt.step()
total += loss.item()
sched.step()
if epoch % 10 == 0 or epoch == epochs - 1:
print(" epoch %3d loss %+.4f" % (epoch, total / max(1, len(X) // batch)))
return model
def vol_scaled_return(w, y, sigma, target=0.10, periods=252, cap=5.0, cost=1e-4):
"""Scale the whole book so that its ex-ante volatility sits near the target."""
w = w.detach().numpy()
gross = (w * y.numpy()).sum(-1)
turn = np.abs(np.diff(w, axis=0, prepend=w[:1])).sum(-1)
port_vol = np.sqrt(((w ** 2) * (sigma.numpy() ** 2)).sum(-1)) * np.sqrt(periods)
mult = np.clip(target / np.maximum(port_vol, 1e-8), 0.0, cap)
return mult * (gross - cost * turn)A worked example. The demonstration uses a synthetic panel with one slow common factor and a spread of factor loadings, so there is a learnable cross-sectional tilt and nothing else.
torch.manual_seed(0)
rng = np.random.default_rng(0)
T, N = 1400, 6
# synthetic panel: a slow common factor, persistent vol, mild cross-sectional spread
f = np.zeros(T)
for t in range(1, T):
f[t] = 0.97 * f[t - 1] + rng.standard_normal() * 0.004
load = np.linspace(1.2, 0.3, N)
R = f[:, None] * load + rng.standard_normal((T, N)) * 0.008
X, Y, S = build_tensors(R)
n_train = int(0.7 * len(X))
print("tensors", tuple(X.shape), tuple(Y.shape))
model = AllocationNet(X.shape[-1], N)
train(model, X[:n_train], Y[:n_train], epochs=30)
model.eval()
with torch.no_grad():
w = model(X[n_train:])
r = vol_scaled_return(w, Y[n_train:], S[n_train:])
print("mean weight ", np.round(w.numpy().mean(0), 3))
print("mean turnover %.4f" % np.abs(np.diff(w.numpy(), axis=0)).sum(-1).mean())
print("holdout periods %d realised vol (annualised) %.3f"
% (len(r), r.std() * np.sqrt(252)))Output:
tensors (1349, 50, 18) (1349, 6) epoch 0 loss +0.3019 epoch 10 loss -0.0339 epoch 20 loss -0.0567 epoch 29 loss -0.0597 mean weight [0.39 0.108 0.002 0.001 0.001 0.499] mean turnover 0.0363 holdout periods 405 realised vol (annualised) 0.101
Three features of that run are worth noting. The loss falls from positive to about −0.06 and flattens, which is the shape to expect from a shallow objective. The learned weights are highly concentrated — two assets hold roughly ninety per cent of the book despite the entropy term — which is what a softmax does when the cross-sectional signal is clean, and a warning about what it will do when the signal is noise. And the realised holdout volatility of 10.1 % against a 10 % target shows the overlay working: that is a property of the scaling arithmetic, not evidence that the allocation learned anything.
This article is written for educational purposes. It describes a modelling technique and provides reference code run on synthetic data; it is not investment research, a recommendation, or an offer to buy or sell any security. No performance is claimed or implied, and nothing here should be read as a prediction of returns. Any implementation decision remains the responsibility of the reader.
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from Aryan's Quant Blog and is being posted with its permission. The views expressed in this material are solely those of the author and/or Aryan's Quant Blog and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
The third-party code discussed within this article is not investment or trading advice, and is for proof-of-concept, educational, and illustrative purposes only. IBKR makes no representations or warranty regarding its accuracy or completeness. Users are solely responsible for conducting their own independent testing and due diligence before applying any code or concepts in a live or production environment
Please keep in mind that the examples discussed in this material are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.
Join The Conversation
For specific platform feedback and suggestions, please submit it directly to our team using these instructions.
If you have an account-specific question or concern, please reach out to Client Services.
We encourage you to look through our FAQs before posting. Your question may already be covered!