liveBook · Manning

Correlation

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

Correlation is a statistical measure of the direction and strength of dependence between two variables or asset returns.

Correlation measures how asset returns move together. In portfolio construction, pairwise correlations combine with each asset’s volatility to determine portfolio risk, so a portfolio’s standard deviation is not merely a weighted average of its components’ standard deviations. section 2.2

For assets i and j, correlation enters covariance as Cov(r_i,r_j) = σ_i σ_j Corr(r_i,r_j). This converts a correlation matrix and a volatility vector into a covariance matrix, which supports the general portfolio-risk calculation σp = (wᵀΣw)^(1/2). section 2.2

Use correlation as an input to a repeatable portfolio calculation:

  • Specify expected returns, volatilities, and pairwise correlations for candidate assets before selecting portfolio weights. section 2.1
  • Scale the correlation matrix by diagonal volatility matrices to produce Cov = diag(σ) @ Corr @ diag(σ). section 2.2
  • Estimate a correlation matrix directly from a pandas DataFrame of historical returns with stock_returns.corr(). section 3.2.1
  • Treat estimated correlations as assumptions: historical covariance estimation becomes impractical across many securities, where multifactor models can reduce the correlations requiring estimation. section 3.2.1

Three-stock covariance input

A correlation matrix for FE, WMT, and AAPL uses 1.0 on its diagonal and pairwise inputs of 0.10, 0.17, and 0.26. Combined with volatilities of 15%, 20%, and 35%, it produces a covariance matrix used with portfolio weights such as [0.2, 0.3, 0.5] to calculate portfolio standard deviation. section 2.1 section 2.2

Table 3.7 Correlation matrix of three assets
AAPL FE WMT
AAPL 1.000000 0.209247 0.261399
FE 0.209247 1.000000 0.265760
WMT 0.261399 0.265760 1.000000

Inspect the historical correlation matrix for AAPL, FE, and WMT before converting estimated relationships into covariance inputs. section 3.2.1

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

How deep it goes
Derives covariance from correlation and volatility, implements matrix calculations, and estimates correlations from return histories.
Examples
Uses FE, WMT, and AAPL matrices and a stock–bond allocation slider with a negative correlation input.
What it assumes
Assumes familiarity with asset returns, volatility, portfolio weights, and basic Python array operations.
What you can do after
Apply it via NumPy and pandas to estimate correlation matrices, form covariances, and calculate portfolio volatility.
How it connects
Connects correlation to covariance, volatility, expected returns, Markowitz portfolio construction, and multifactor models.

Also explained in

Compare how 16 books explain correlation →

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.