liveBook · Manning

Covariance matrix

As explained in Build a Robo-Advisor with Python (From Scratch)

A covariance matrix is a square symmetric matrix containing asset variances on its diagonal and pairwise covariances off the diagonal.

A covariance matrix summarizes how asset returns vary individually and together. It is a square, symmetric matrix: diagonal entries are variances, while off-diagonal entries are covariances. Covariance combines volatility and correlation: Cov(rᵢ,rⱼ) = σᵢσⱼCorr(rᵢ,rⱼ). section 2.2

For portfolio weights w and covariance matrix Σ, portfolio volatility is √(wᵀΣw). This makes Σ a central risk input: it captures both each asset’s standalone variability and the interaction among assets rather than treating holdings independently. section 2.2 section 12.1.1

Build a matrix from correlations and volatilities

Given a correlation matrix Corr and volatility vector σ, form Σ as diag(σ) @ Corr @ diag(σ). For three assets, the resulting matrix has each squared volatility on its diagonal and a matching covariance in positions (i,j) and (j,i). Portfolio standard deviation then follows from (w @ Cov @ w.T) ** 0.5. section 2.2

Listing 2.3 Computing the covariance matrix from volatilities and correlations
Corr = [[ 1.  ,  0.1 ,  0.17],
        [ 0.1 ,  1.  ,  0.26],
        [ 0.17,  0.26,  1.  ]]

Cov = np.diag(sigma) @ Corr @ np.diag(sigma)  #1
print('Covariance matrix: \n', Cov)

This listing shows the direct NumPy construction that scales correlations into covariances. section 2.2

Historical return series provide a direct estimate: in pandas, annualize the daily covariance matrix with stock_returns.cov() * 252. This is straightforward for a few assets, but estimating all pairwise relationships becomes impractical for thousands of securities; multifactor models reduce the number of correlations that must be estimated. section 3.2.1

A usable correlation or covariance matrix must be positive definite, checked by confirming that all eigenvalues are positive. Historical sample estimates also miss volatility clustering, so estimated risk can change as market volatility changes. section 3.3 section 3.2.1

Covariance matrices also enter allocation models. A diagonal Σ is a simplifying assumption that removes cross-asset covariances; under that assumption, a mean-variance solution can yield weights inversely proportional to asset volatilities. In Black–Litterman, a separate covariance matrix Ω represents uncertainty and correlation among investor views. section 11.1.3 section 12.3.2

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Defines matrix entries, derives portfolio volatility, implements construction, and notes estimation and validity limits.
Examples
Uses three-asset correlation and volatility inputs, FE/WMT/AAPL historical returns, and a two-asset risk-parity portfolio.
What you can do after
Apply it via NumPy or pandas to estimate covariance and calculate portfolio volatility.
How it connects
Connects covariance matrices to correlation matrices, portfolio volatility, mean-variance optimization, risk parity, and Black–Litterman.

Also explained in

Compare how 5 books explain covariance matrix →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.