Created
January 16, 2020 22:55
-
-
Save markusrenepae/c4d430718f73621907162c3af8303b7b to your computer and use it in GitHub Desktop.
This gist is for MC simulation, at least the basics of it. Usage: Medium article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
ticker = 'BA' | |
data = pd.DataFrame() | |
data[ticker] = wb.DataReader(ticker, data_source='yahoo', start='2015-1-1')['Adj Close'] | |
plt.figure(figsize=(10,6)) | |
plt.plot(data[ticker]) | |
plt.show() | |
log_returns = np.log(1 + data.pct_change()) | |
u = log_returns.mean() | |
var = log_returns.var() | |
drift = u - (0.5 * var) | |
stdev = log_returns.std() | |
t_intervals = 250 | |
scenarios = 2 | |
daily_returns = np.exp(drift.values + stdev.values * norm.ppf(np.random.rand(t_intervals, scenarios))) | |
price_list = np.zeros_like(daily_returns) | |
S0 = data.iloc[-1] | |
price_list[0] = S0 | |
for t in range(1, t_intervals): | |
price_list[t] = price_list[t - 1] * daily_returns[t] | |
plt.figure(figsize=(10,6)) | |
plt.plot(price_list) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment