<jupyter_start><jupyter_text>Monte Carlo - Forecasting Stock Prices - Part I *Suggested Answers follow (usually there are multiple ways to solve a problem in Python).* Download the data for Microsoft (‘MSFT’) from Morningstar for the period ‘2002-1-1’ until today.<jupyter_code>import numpy as np  
import pandas as pd  
from pandas_datareader import data as wb  
import matplotlib.pyplot as plt  
from scipy.stats import norm
%matplotlib inline

ticker = 'MSFT' 
data = pd.DataFrame()
data[ticker] = wb.DataReader(ticker, data_source='morningstar', start='2002-1-1')['Close']<jupyter_output><empty_output><jupyter_text>Use the .pct_change() method to obtain the log returns of Microsoft for the designated period.<jupyter_code>log_returns = np.log(1 + data.pct_change())
log_returns.tail()
data.plot(figsize=(10, 6));
log_returns.plot(figsize = (10, 6))<jupyter_output><empty_output><jupyter_text>Assign the mean value of the log returns to a variable, called “U”, and their variance to a variable, called “var”.<jupyter_code>u = log_returns.mean()
u
var = log_returns.var()
var<jupyter_output><empty_output><jupyter_text>Calculate the drift, using the following formula: $$drift = u - \frac{1}{2} \cdot var$$<jupyter_code>drift = u - (0.5 * var)
drift<jupyter_output><empty_output><jupyter_text>Store the standard deviation of the log returns in a variable, called “stdev”.<jupyter_code>stdev = log_returns.std()
stdev<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11172413793103449, 'fraction_numerical': 0.015862068965517243, 'mean_word_length': 6.365482233502538, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '40087', 'license': 'MIT', 'n_tokens_mistral': 512, 'n_tokens_neox': 493, 'n_words': 142, 'path': 'Python for Finance - Code Files/103 Monte Carlo - Predicting Stock Prices - Part I/Online Financial Data (APIs)/Python 2 APIs/MC Predicting Stock Prices - Part I - Solution_Morn.ipynb', 'repo_name': 'siddharthjain1611/Python_for_Finance_Investment_Fundamentals-and-Data-Analytics', 'size': '1450'}