How it works...
First, we specified the assets (Amazon and S&P 500) we wanted to use and the time frame. In Step 3, we downloaded the data from Yahoo Finance. Then, we only kept the last available price per month and calculated the monthly returns as the percentage change between the subsequent observations.
In Step 5, we calculated the beta as the ratio of the covariance between the risky asset and the benchmark to the benchmark's variance.
In Step 6, we separated the target (Amazon's stock returns) and the features (S&P 500 returns) using the pop method of a pandas DataFrame. Afterward, we added the constant to the features (effectively adding a column of ones) with the add_constant function. The idea behind adding the intercept to this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access