<jupyter_start><jupyter_text>Monte Carlo - Euler Discretization - Part I *Suggested Answers follow (usually there are multiple ways to solve a problem in Python).* Download the data for Microsoft (‘MSFT’) from Google Finance for the period ‘2007-1-1’ until '2017-3-21'.<jupyter_code>import numpy as np  
import pandas as pd  
from pandas_datareader import data as web  
from scipy.stats import norm 
import matplotlib.pyplot as plt  
%matplotlib inline
ticker = 'MSFT'  
data = pd.DataFrame()
data[ticker] = web.DataReader(ticker, data_source='morningstar', start='2007-1-1', end='2017-3-21')['Close']<jupyter_output><empty_output><jupyter_text>Store the annual standard deviation of the log returns in a variable, called “stdev”.<jupyter_code>log_returns = np.log(1 + data.pct_change())
log_returns.tail()
data.plot(figsize=(10, 6));
stdev = log_returns.std() * 250 ** 0.5
stdev<jupyter_output><empty_output><jupyter_text>Set the risk free rate, r, equal to 2.5% (0.025).<jupyter_code>r = 0.025<jupyter_output><empty_output><jupyter_text>To transform the object into an array, reassign stdev.values to stdev.<jupyter_code>type(stdev)
stdev = stdev.values
stdev<jupyter_output><empty_output><jupyter_text>Set the time horizon, T, equal to 1 year, the number of time intervals equal to 250, the iterations equal to 10,000. Create a variable, delta_t, equal to the quotient of T divided by the number of time intervals.<jupyter_code>T = 1.0 
t_intervals = 250 
delta_t = T / t_intervals  
iterations = 10000<jupyter_output><empty_output><jupyter_text>Let Z equal a random matrix with dimension (time intervals + 1) by the number of iterations.<jupyter_code>Z = np.random.standard_normal((t_intervals + 1, iterations))<jupyter_output><empty_output><jupyter_text>Use the .zeros_like() method to create another variable, S, with the same dimension as Z. S is the matrix to be filled with future stock price data.<jupyter_code>S = np.zeros_like(Z)<jupyter_output><empty_output><jupyter_text>Create a variable S0 equal to the last adjusted closing price of Microsoft. Use the “iloc” method.<jupyter_code>S0 = data.iloc[-1]  
S[0] = S0<jupyter_output><empty_output><jupyter_text>Use the following formula to create a loop within the range (1, t_intervals + 1) that reassigns values to S in time t. $$S_t = S_{t-1} \cdot exp((r - 0.5 \cdot stdev^2) \cdot delta_t + stdev \cdot delta_t^{0.5} \cdot Z_t)$$<jupyter_code>for t in range(1, t_intervals + 1):
    S[t] = S[t-1] * np.exp((r - 0.5 * stdev ** 2) * delta_t + stdev * delta_t ** 0.5 * Z[t])
S
S.shape<jupyter_output><empty_output><jupyter_text>Plot the first 10 of the 10,000 generated iterations on a graph.<jupyter_code>plt.figure(figsize=(10, 6))
plt.plot(S[:, :10]);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11544064093226511, 'fraction_numerical': 0.03605243991260015, 'mean_word_length': 5.651331719128329, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '91097', 'license': 'MIT', 'n_tokens_mistral': 1050, 'n_tokens_neox': 973, 'n_words': 290, 'path': 'Python for Finance - Code Files/109 Monte Carlo - Euler Discretization - Part I/Online Financial Data (APIs)/Python 2 APIs/MC - Euler Discretization - Part I - Solution_Morn.ipynb', 'repo_name': 'siddharthjain1611/Python_for_Finance_Investment_Fundamentals-and-Data-Analytics', 'size': '2746'}