Member-only story

Monte Carlo for the Stock Market

Patrick Chirdon
2 min readOct 3, 2021

--

The Monte Carlo algorithm is a famous algorithm. It was used by Oppenheimer at Los Alamos labs during the Manhattan project to design the atomic bomb. In chemical engineering, you can use it to simulate the movement of particles in a box under various temperatures and pressures. It can also be used for simulating the stock market.

For stock market simulations, it can be used to calculate the value at risk ratio of the portfolio. The Monte Carlo algorithm is commonly used to simulate risk. You can use it to calculate the worst likely loss for a portfolio given a confidence interval over a specified time. There’s different types of Monte Carlo algorithms, but the most common one is the Markov. This means the stock price follows a random walk. Past price information is already incorporated and the next price is “conditionally independent” of past price movements.

We run simulations to predict the market many times.

import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy.stats import norm
def get_simulation(ticker, name):
data=pd.DataFrame()
data[ticker]=wb.DataReader(ticker, data_source='yahoo', start='2007-1-1')['Adj Close']
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=365
iterations=10
daily_returns=np.exp(drift.values + stdev.values * norm.ppf(np.random.rand(t_intervals, iterations)))…

--

--

No responses yet