<jupyter_start><jupyter_text>Calculating the Expected Return of a Stock (CAPM) *Suggested Answers follow (usually there are multiple ways to solve a problem in Python).* Load the data for Microsoft and S&P 500 for the period 1st of January 2012 – 31st of December 2016. Let S&P 500 act as the market. Calculate the beta of Microsoft.<jupyter_code>import numpy as np
import pandas as pd
from pandas_datareader import data as wb

data = pd.read_csv('D:\Python\CAPM_Exercise_Data.csv', index_col = 'Date')
data
    
sec_returns = np.log( data / data.shift(1) )
cov = sec_returns.cov() * 250
cov_with_market = cov.iloc[0,1]
market_var = sec_returns['^GSPC'].var() * 250

MSFT_beta = cov_with_market / market_var<jupyter_output><empty_output><jupyter_text>Assume a risk-free rate of 2.5% and a risk premium of 5%. Estimate the expected return of Microsoft. **Calculate the expected return of P&G (CAPM):** $$\overline{r_{pg}} = r_f + \beta_{pg}(\overline{r_{m}} - r_f) $$<jupyter_code>MSFT_er = 0.025 + MSFT_beta * 0.05
MSFT_er<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11026615969581749, 'fraction_numerical': 0.034220532319391636, 'mean_word_length': 5.194117647058824, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '415723', 'license': 'Apache-2.0', 'n_tokens_mistral': 402, 'n_tokens_neox': 361, 'n_words': 120, 'path': '23 - Python for Finance/6_The Capital Asset Pricing Model/5_Calculating the Expected Return of a Stock (CAPM) (2:16)/Calculating the Expected Return of a Stock (CAPM) - Solution_CSV.ipynb', 'repo_name': 'olayinka04/365-data-science-courses', 'size': '1052'}