Covariance
Covariance measures whether two quantities tend to sit on the same side of their averages at the same time. Positive means they move together, negative means they move in opposite directions.
01The formula
- two random quantities, here: the daily returns of two stocks
- their means
- the sample means of n observed days
- the divisor that makes the sample estimate unbiased
02See it move
03Where it is used
- Portfolio managerFills a covariance matrix of asset returns, the main input to portfolio risk and optimisation.
- Quant researcherEstimates a stock's beta as its covariance with the market divided by the market's variance.
- Risk managerUses covariances to aggregate risk across positions instead of adding them one by one.
04Why traders use it
Portfolio risk is built from covariances, not just from each asset's volatility. It is also the top half of the correlation and of the regression slope.
05Worked example
With true correlation 0.50, daily volatilities 1.5% and 2.0%, the true covariance is 0.000150. In this sample of 250 days the estimate is 0.000189, and the line through the means has slope 0.748.
06How it is built
- Subtract each variable's mean, so each day is a point measured from the centre of the cloud.
- Multiply the two distances. Days where both are above or both below give positive products; mixed days give negative ones.
- Average the products (divide by for a sample).
- Dividing by gives the slope of the best-fit line through the means; dividing by gives the correlation.
07The common mistake
Reading the size of a covariance as strength. It is in the units of X times the units of Y, so it changes when you rescale either one. Use correlation to compare strength across pairs.
08In Python and LaTeX
rho, sx, sy = 0.5, 0.015, 0.02
print('true covariance:', round(rho * sx * sy, 6)) # 0.00015\text{Cov}(X, Y) = E\big[(X - \mu_X)(Y - \mu_Y)\big] \approx \frac{1}{n - 1}\sum_{i=1}^{n} (x_i - \bar x)(y_i - \bar y)09Where it fits
Rather not code the maths yourself? Describe your trading idea and let AI build the bot.
Build a bot with AgenKitThe worked example and the model run the same code, so every number matches. Models simplify markets on purpose. This is education, not financial advice.