<jupyter_start><jupyter_text>Calculating the Risk of a Security *Suggested Answers follow (usually there are multiple ways to solve a problem in Python).* Download the data for Microsoft (‘MSFT’) from Yahoo Finance for the period ‘2000-1-1’ until today. Assess the daily and the annual risk of ‘MSFT’. Repeat the exercise for Apple for the same timeframe.<jupyter_code>import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
tickers = ['MSFT', 'AAPL']

data = pd.DataFrame()

for t in tickers:
    data[t] = wb.DataReader(t, data_source='yahoo', start='2000-1-1')['Adj Close']
returns = np.log(data / data.shift(1))
returns<jupyter_output><empty_output><jupyter_text>MSFT Daily risk:<jupyter_code>returns['MSFT'].std()<jupyter_output><empty_output><jupyter_text>Annual risk:<jupyter_code>returns['MSFT'].std() * 250 ** 0.5<jupyter_output><empty_output><jupyter_text>Apple Daily risk:<jupyter_code>returns['AAPL'].std()<jupyter_output><empty_output><jupyter_text>Annual risk:<jupyter_code>returns['AAPL'].std() * 250 ** 0.5<jupyter_output><empty_output><jupyter_text>****** Store the volatilities of the two stocks in an array called "vols".<jupyter_code>vols = returns[['MSFT', 'AAPL']].std() * 250 ** 0.5
vols<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13580246913580246, 'fraction_numerical': 0.021604938271604937, 'mean_word_length': 7.314102564102564, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '426141', 'license': 'Apache-2.0', 'n_tokens_mistral': 492, 'n_tokens_neox': 463, 'n_words': 115, 'path': "23 - Python for Finance/3_Measuring Investment Risk/2_Calculating a Security's Risk in Python (5:55)/Calculating the Risk of a Security - Solution_Yahoo_Py2.ipynb", 'repo_name': 'olayinka04/365-data-science-courses', 'size': '1296'}