The Claude Quant PlaybookDownload ebookDownload ebook

Sample variance (Bessel's correction)

intermediateStatistics2 of 10

The average squared distance from the sample mean, but divided by n − 1 instead of n. Dividing by n would underestimate the true variance on average.

Build a trading system with Claude.Download ebook

01The formula

the sample variance
the i-th observation
the sample mean
the number of observations
the true variance

02See it move

03Where it is used

04Why traders use it

Volatility is the core input to risk and option pricing. With short windows the choice between n and n − 1 changes the answer noticeably.

05Worked example

Over 400 simulated samples of 5 observations, the ÷(n − 1) estimates average 103.8% of the true variance, while the ÷n estimates average 83.1%. Theory says ÷n should give 80.0%.

06How it is built

  1. Split each deviation: .
  2. Squaring and summing, the cross term vanishes, so .
  3. Take expectations: .
  4. So dividing by gives an unbiased estimate; dividing by is too small by the factor .

07The common mistake

Mixing up the defaults. numpy's np.var divides by n unless you pass ddof=1, while pandas' .var() divides by n − 1, so the same data give two different answers.

08In Python and LaTeX

import numpy as np
rng = np.random.default_rng(7)
n, M = 5, 400
X = rng.standard_normal((M, n))   # true variance is 1
unbiased = X.var(axis=1, ddof=1)
biased = X.var(axis=1, ddof=0)
print('theory for /n:', (n - 1) / n)
s^2 = \frac{1}{n - 1}\sum_{i=1}^{n}(x_i - \bar{x})^2, \qquad E\!\left[s^2\right] = \sigma^2, \qquad E\!\left[\tfrac{n-1}{n}s^2\right] = \tfrac{n-1}{n}\sigma^2

09Where it fits

Rather not code the maths yourself? Describe your trading idea and let AI build the bot.

Build a bot with AgenKit

The worked example and the model run the same code, so every number matches. Models simplify markets on purpose. This is education, not financial advice.