Correlation Graph in Python

  • Must use either log returns or percentage returns
    • Standard approach is to use log returns
  • The following python code will generate a correlation chart of the rolling 50 day window of the returns, showing the last 200 data points:
    • df[‘SP500’].rolling(50).corr(df[‘NDX’])[-200:].plot()
  • The slice function is used to grab specific data points from a data frame
    • [start:stop:step]
    • data[100:200:2] would return every other row between rows 100 and 200
  • Use matplotlib for prettier graphs to be used in presentations
  • Data Inspection
    • data.to_clipboard() #copies data to clipboard to be pasted in excel
    • data.tail() #prints from the end of the data
    • data.head() #prints from the start of the data
  • When adding the data to a chart you need to create a figure and set the size
    • fig2 = plt.figure(figsize=(12,8))
  • Additionally you will need to define the axis in the figure. The below is creating a figure of 3 plots high and 1 plot wide. The last 1 reflects the plot that the code following this line would be updating
    • ax = fig2.add_subplot(311)
  • Create a heat map when comparing correlations of more than 2 markets

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: