August 15, 2021

Video Tutorial: How to Calculate Correlation Between Stocks

Many investors pay close attention to the correlation between stocks in their portfolio. Why is this, and how do you make those calculations? Learn how, step-by-step in Python.

Cloud icon

JP Hwang


Investors have a long list of theories to choose from when it comes to crafting their investment strategy. One approach that remains particularly popular, however, is assessing the correlation between stocks in a portfolio.

Modern Portfolio Theory, or MPT, argues you can maximize returns for a given level of risk by collecting assets based on their correlations. For instance, it proposes that collecting uncorrelated assets may protect a portfolio from fluctuations by reducing the probability of asset prices moving together. Many investors seek to diversify their portfolios for these reasons.

But how do you make those calculations? In this video tutorial, JP Hwang demonstrates how to crunch the numbers and find correlation – which you can then use to inform how you construct your own portfolio and minimize risk.


  • 0:00: Introduction and background on IEX Cloud
  • 0:35: Background on volatility and correlation
  • 4:45: Automation is the answer: Why use Python
  • 5:38: First example and dataset (load data, create a pivot table, calculate correlation)
  • 8:45: Next dataset (plotting, pivot data, and calculate correlation, the Pearson R function)
  • 10:30: Our first real-life dataset (loading, cleaning, calculating correlation)
  • 13:45: Working with a larger dataset (importing, inspecting, calculations, plotting)
  • 17:22: Working with a DataFrame (converting correlation coefficient, extracting a ticker)
  • 18:55: Identifying least corelated symbols
  • 25:10: Creating an “average” data series and comparing to individual stocks
  • 27:22: Getting started with IEX Cloud: Finding data, starting for free, and sandbox mode
  • 30:45: Closing remarks and the Community Blog

To see the full written tutorial step-by-step – including links to code – click below to visit our other blog post, “Portfolio Risk Management: From Correlation to Diversification”.

Read the tutorial

Get the tutorial code

IEX Cloud Services LLC makes no promises or guarantees herein regarding results from particular products and services, and neither the information, nor any opinion expressed here, constitutes a solicitation or offer to buy or sell any securities or provide any investment advice or service.