This lesson requires a premium membership to access.
Premium membership includes unlimited access to all courses, quizzes, downloadable resources, and future content updates.
We'll calculate both simple returns and log returns using pandas and numpy packages.
1import numpy as np
2
3# Calculate simple returns
4stock_data['Simple Returns'] = stock_data['Close'].pct_change()
5# Calculate log returns
6
7stock_data['Log Returns'] = np.log(stock_data['Close'] / stock_data['Close'].shift(1))
8
9# Display the returns to confirm
10
11stock_data[['Close','Simple Returns','Log Returns']].head()
12This code calculates two types of financial returns from stock closing prices and displays them along with the closing prices. Here's a breakdown of the steps:
Premium membership required
Upgrade to premium to unlock all videos and courses