Implement Monte-Carlo Simulation in Python
%matplotlib inline
import numpy as np
import pandas as pd
import seaborn
s, t, r, sigma = 1000, 30 / 365.0, 0.001, 0.2
path_of_monte_carlo = 50000
def future_stock_price(s, r, sigma, t):
return s*np.exp(np.random.normal((r - 0.5 * np.power(sigma, 2)) * t, sigma * np.sqrt(t)))
run 50000 times
%time stock_price_results = [future_stock_price(s, r, sigma, t) for _ in range(path_of_monte_carlo)]
CPU times: user 580 ms, sys: 0 ns, total: 580 ms Wall time: 578 ms
plot histgram
%time pd.Series(stock_price_results).hist(bins=100)
CPU times: user 250 ms, sys: 0 ns, total: 250 ms Wall time: 260 ms
<matplotlib.axes._subplots.AxesSubplot at 0x7f5750dad2e8>