Turning Trading Objectives Into Loss Functions
Off-the-shelf losses like mean squared error assume every mistake costs the same. Real trading mistakes don't, and a loss function can be built from scratch to charge the model exactly what each kind of error actually costs.
Prerequisites: Objective Functions vs Evaluation Metrics, The Sharpe Ratio as a Differentiable Loss
Mean squared error treats a 0.03 underprediction as exactly, symmetrically equal mistakes. For most models trained on generic data, that is a reasonable default. For a trading model, it is frequently wrong, because the actual dollar consequence of the two kinds of error is almost never the same. Missing a genuine rally by predicting flat returns costs you a real, forgone profit. Falsely predicting a rally that never comes costs you transaction fees and a small drawdown. Those are not the same size of mistake, and a loss function that pretends they are will train a model to be wrong in exactly the more expensive way, over and over, because nothing in its training ever told it that one direction of error was worse.
The analogy: an underwriter's two kinds of mistake
An insurance underwriter who prices a policy too cheaply is exposed to a claim that can bankrupt the business. An underwriter who prices a policy too expensively simply loses that one customer to a competitor — annoying, but survivable. A sensible underwriting process does not treat "priced 5% too low" and "priced 5% too high" as equally bad; it is built, deliberately, to be far more afraid of one than the other. A generic loss function like mean squared error is an underwriter with no such fear — it is indifferent to direction, and building a custom loss is the act of telling the model, explicitly, which direction it should actually be afraid of.
Building an asymmetric loss
The simplest way to encode "one direction of error is worse" is a loss with two different slopes on either side of zero error. Let be the true return and the model's prediction, and define the error as . A common construction, the pinball loss (used in quantile regression), applies one weight when the model underpredicted and a different weight when it overpredicted:
In words: whichever way the model was wrong, the loss is the size of the miss, scaled by a weight that depends on the direction of the miss. Setting recovers a symmetric loss (both directions weighted equally, up to a constant factor of the ordinary absolute error). Setting , as in the example below, tells the model that missing a real move costs more than crying wolf, so it should lean toward predicting more aggressively rather than more conservatively.
Worked example 1: the same-sized miss, two different costs
Take — underprediction is weighted more than twice as heavily as overprediction. Two predictions, each off by exactly in absolute terms:
Case A — a missed rally: true return , predicted . Since , this is underprediction:
Case B — a false alarm: true return , predicted . Since , this is overprediction:
Both misses were exactly in raw terms — mean squared error would score them identically, each. The custom loss scores the missed rally at more than double the false alarm, versus , because that is what the business actually asked for.
Worked example 2: a batch, and where the gradient pushes
Four predictions, throughout, each again a miss:
| # | direction | loss | ||
|---|---|---|---|---|
| 1 | 0.05 | 0.02 | under | |
| 2 | −0.01 | 0.02 | over | |
| 3 | 0.01 | 0.04 | over | |
| 4 | 0.06 | 0.03 | under |
The gradient of with respect to is whenever the model underpredicted, and whenever it overpredicted — a bigger push upward on the underprediction cases than the push downward on the overprediction cases. Across many training examples, this asymmetric tug-of-war settles at an equilibrium where the model underpredicts on roughly of examples and overpredicts on the remaining — not because it is trying to hit the average outcome, but because that is the balance point where the strong upward pull on the 70% of harder-to-forgive misses exactly offsets the gentler downward pull on the other 30%. This is why pinball loss is also called quantile loss: minimizing it with trains the model to output the 70th percentile of the outcome distribution, not its mean.
Because the two sides of the kink have different, constant slopes, every gradient step pushes with a fixed strength that depends only on which side of the kink the current prediction sits on, never on how far it is from the truth — unlike squared error, whose push grows with the size of the miss. Drag the step size in the explorer above and notice how a loss with different slopes on either side of its minimum, like this one, still converges the same way ordinary gradient descent does, just asymmetrically.
A loss function is a design choice, not a law of nature. Any consequence you can express as "how much does this specific kind of mistake cost, and does it cost the same in both directions" can be built into the function being minimized — and once it is, the model optimizes for what actually matters, not for a generic proxy that happens to be convenient to compute.
What this means in practice
Custom trading losses show up constantly once you look for them: a market-making model may weight the loss of quoting too wide (missed spread capture, mild) very differently from quoting too tight (adverse selection risk, severe); a signal that only fires on the top decile of predicted moves benefits from a loss that specifically rewards separating the extreme tail from the middle of the distribution, rather than fitting the whole distribution equally well; a risk model predicting drawdown may weight underestimating tail risk far more heavily than overestimating it, because underestimating tail risk is what blows up a fund. In every case, the design process is the same: write down, in dollars, what each direction and each size of error actually costs the business, then find the smallest differentiable function that reproduces those costs closely enough for gradient descent to optimize against.
Practice
- Rebuild the loss above with instead of . Recompute the loss for Case A and Case B from worked example 1, and describe in words how a model trained with this new would behave differently.
- A classifier has a false positive (entering a bad trade) that costs $50 and a false negative (missing a good trade) that costs $200. Write a weighted loss that reflects this, analogous to the pinball loss above but for a binary classification setting.
- Why does minimizing pinball loss with recover an objective very close to minimizing mean absolute error?
The trap is picking (or any other asymmetry parameter) by intuition rather than by actually pricing out the two kinds of error in dollar terms — an chosen too aggressively will train a model that is happy to cry wolf constantly because false alarms are cheap by construction, running up real transaction costs that the loss function never accounted for in the first place. A custom loss is only as good as the cost structure it encodes; if the encoded costs don't match reality, the model will confidently optimize for the wrong thing, and — because gradient descent has no way to know the loss itself was mis-specified — it will do so with exactly the same confidence as if the loss had been correct.
Related concepts
Practice in interviews
Further reading
- Koenker & Bassett, Regression Quantiles (1978)
- Elkan, The Foundations of Cost-Sensitive Learning (2001)