Skip to content

Instantly share code, notes, and snippets.

@markusrenepae
Created January 16, 2020 22:55
Show Gist options
  • Save markusrenepae/c4d430718f73621907162c3af8303b7b to your computer and use it in GitHub Desktop.
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
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