Correlation of Stocks and Bonds
Investors are often interested in the correlation between the returns of two different assets for asset allocation and hedging purposes. In this exercise, you'll try to answer the question of whether stocks are positively or negatively correlated with bonds. Scatter plots are also useful for visualizing the correlation between the two variables.
Keep in mind that you should compute the correlations on the percentage changes rather than the levels.
Stock prices and 10-year bond yields are combined in a DataFrame called stocks_and_bonds
under columns SP500
and US10Y
The pandas
and plotting modules have already been imported for you. For the remainder of the course, pandas
is imported as pd
and matplotlib.pyplot
is imported as plt
.
This exercise is part of the course
Time Series Analysis in Python
Exercise instructions
- Compute percent changes on the
stocks_and_bonds
DataFrame using the.pct_change()
method and call the new DataFramereturns
. - Compute the correlation of the columns
SP500
andUS10Y
in thereturns
DataFrame using the.corr()
method for Series which has the syntaxseries1.corr(series2)
. - Show a scatter plot of the percentage change in stock and bond yields.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Compute percent change using pct_change()
returns = stocks_and_bonds.___
# Compute correlation using corr()
correlation = ___
print("Correlation of stocks and interest rates: ", correlation)
# Make scatter plot
plt.___
plt.show()