Measure downside deviation for AAPL with about ten lines of NumPy.

By PyQuant News

Published: 2026-10-03

URL: https://pyquantnews.substack.com/p/a-better-way-to-quantify-trading


Each week, I send out one Python tutorial to help you get started with algorithmic trading, market data analysis, and quant finance. Upgrade to a paid plan to access the Python code notebooks, Discord community, and code support.

Subscribe now


Standard deviation counts a 5% gain and a 5% loss as the same size move.

Most intro courses define risk as volatility, meaning how much returns bounce around from day to day, so that’s the number people measure. The formula is one line of NumPy, and it’s where I started too. But it scores a 6% up day as risk, which is not the thing you size a position against.

So a strategy with one huge winning month gets marked risky, while a strategy that bleeds 1% a week looks calm.

Where the number misleads

As a market risk quant, I watched a desk get flagged in a Monday risk report after three unusually strong months. The volatility number worrying the committee came almost entirely from two large winning days.

The retail version is to rank two backtests by standard deviation and drop the one that moved more, even when its worst week was smaller.

The fix is to run the same dispersion calculation using only the returns that fall below a threshold you choose.

What you’ll build

In today’s post, you’ll compute downside deviation for AAPL from daily adjusted closing prices. It lands about 33% below the standard deviation of those same returns, the usual number that counts gains and losses alike.

Let’s go.