# For portfolio variance, why doesn't $Var(X w) = w^\top \Sigma w$? [closed]

From multivariate asset returns $$X$$, we can calculate the sample covariance matrix $$\Sigma$$.

The definition of (any) portfolio variance is $$w^\top \Sigma w$$, where $$w$$ are portfolio weights.

If $$X w$$ is the portfolio-weighted asset returns series (a vector), shouldn't the variance of this vector of portfolio returns be equal to the variance of the portfolio, $$w^\top \Sigma w$$?

When I calculate both of them for the same dataset and weights, why don't they equal? $$Var(X w) \neq w^\top \Sigma w$$

import numpy as np
from numpy.random import randn

X = randn(1000,3)           #3 assets with 1000 return observations
Sigma = np.cov(X.T)         #covariance matrix
w = np.array([0.2,0.3,0.5]) #portfolio weights for 3 assets

print(np.var(X@w))          #this should equal the next line but doesn't
print(w@Sigma@w)


I'm not a Python programmer, however, reading the reference manual of np.var, you're using the "biased" version of the variance estimator. Instead use the unbiased variance estimator:

import numpy as np
from numpy.random import randn

X = randn(1000,3)
Sigma = np.cov(X.T)
w = np.array([0.2,0.3,0.5])

print(np.var(X@w, ddof=1))
print((w@Sigma)@w)


where "ddof=1" gives the unbiased variance estimator (see link). This should help.