liveBook · Manning

Correlation

As explained in Investing for Programmers

Correlation is a statistical measure of the strength and direction of the relationship between two variables, bounded between −1 and 1.

Correlation measures how asset movements relate: positive correlation indicates movement in the same direction, while negative correlation indicates movement in opposite directions. Use returns rather than absolute share prices when comparing performance, because prices depend on factors such as share splits. section 7.2.2

def collect_prices_returns(tickers: list):
    close_prices = pd.DataFrame()
    log_returns = pd.DataFrame()

    for ticker in tickers:
        close_prices[ticker] = yf.Ticker(ticker).history(
            start='2024-01-01', 
            end='2024-12-31', 
            interval="1d")['Close']
        log_returns[ticker] = np.log(close_prices[ticker] 
                                     / close_prices[ticker].shift(1))
    return close_prices, log_returns
tickers = ["MSFT", "AMZN", "GOOGL"]
close_prices, log_returns  = collect_prices_returns(tickers)

Load closing prices for several tickers and calculate log returns before analyzing their relationships. section 7.2.2

A practical correlation workflow connects several risk measures:

  • Calculate log returns, then inspect standard deviation, variance, covariance, and correlation rather than comparing raw share prices. section 7.2.2
  • Use correlation to assess whether holdings may move together when constructing a diversified portfolio. section 7.7
  • Treat historical correlation cautiously: diversification requires understanding which assets may correlate, and correlation alone does not remove investment risk. section 7.7

Comparing technology stocks

The calculated log-return correlations are 0.68765 for Microsoft and Amazon, 0.57522 for Microsoft and Alphabet, and 0.54331 for Amazon and Alphabet. Walmart and NVIDIA produce a correlation of −0.00532, indicating almost no linear co-movement in the observed data. section 7.2.2

How Investing for Programmers covers it

How deep it goes
Defines directional movement and computes return-based covariance and correlation matrices alongside variance and standard deviation.
Examples
Uses Microsoft, Amazon, Alphabet, Walmart, and NVIDIA return matrices to illustrate positive, near-zero, and comparative relationships.
What it assumes
Assumes access to closing prices and log-return data, while explaining why returns provide more meaningful comparisons than share prices.
What you can do after
Evaluate historical asset co-movement with Python data and correlation matrices when considering portfolio risk.
How it connects
Connects correlation with log returns, covariance, variance, diversification, hedging, pair trading, and portfolio risk.

Also explained in

Compare how 16 books explain correlation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Investing for Programmers. The section links point to the parts of the book it draws on.