Get startedGet started for free

Plotting consecutive portfolio returns

Regression to the mean is also an important concept in investing. Here you'll look at the annual returns from investing in companies in the Standard and Poor 500 index (S&P 500), in 2018 and 2019.

The sp500_yearly_returns dataset contains three columns:

variable meaning
symbol Stock ticker symbol uniquely identifying the company.
return_2018 A measure of investment performance in 2018.
return_2019 A measure of investment performance in 2019.

A positive number for the return means the investment increased in value; negative means it lost value.

Just as with baseball home runs, a naive prediction might be that the investment performance stays the same from year to year, lying on the y equals x line.

sp500_yearly_returns is available as a pandas DataFrame.

This exercise is part of the course

Introduction to Regression with statsmodels in Python

View Course

Exercise instructions

  • Create a new figure, fig, to enable plot layering.
  • Generate a line at y equals x. This has been done for you.
  • Using sp500_yearly_returns, draw a scatter plot of return_2019 vs. return_2018 with a linear regression trend line, without a standard error ribbon.
  • Set the axes so that the distances along the x and y axes look the same.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a new figure, fig
fig = plt.____

# Plot the first layer: y = x
plt.axline(xy1=(0,0), slope=1, linewidth=2, color="green")

# Add scatter plot with linear regression trend line
sns.____(____)

# Set the axes so that the distances along the x and y axes look the same
plt.____(____)

# Show the plot
plt.show()
Edit and Run Code