Simulating future stock prices using Monte Carlo methods in Python

Greg Brown
Nerd For Tech
Published in
3 min readMay 13, 2021

--

Photo by Nick Chong on Unsplash

Using Monte Carlo methods, we’ll write a quick simulation to predict future stock price outcomes for Apple ($AAPL) using Python. You can read more about Monte Carlo simulation (in a finance context) here.

1) Pull the data

First, we can import the libraries, and pull the historical stock data for Apple. For this example, I picked the last ~10 years, although it would be valuable to test sensitivities of different ranges as this alone is subjective.

# Import required librariesimport mathimport matplotlib.pyplot as pltimport numpy as npfrom pandas_datareader import dataapple = data.DataReader('AAPL', 'yahoo',start='1/1/2009')apple.head()#Next, we calculate the number of days that have elapsed in our chosen time windowtime_elapsed = (apple.index[-1] - apple.index[0]).days

2) Calculate the compounded annualized growth rate over the length of the dataset + standard deviation (to feed into simulation)

cagr (mean returns) : 0.3746
std_dev (standard deviation of return : ) 0.2878

3) Generate random values, run Monte Carlo simulation

4) Analyze results

#from here, we can check the mean of all ending prices#allowing us to arrive at the most probable ending pointmean_end_price = round(np.mean(closing_prices),2)print("Expected price: ", str(mean_end_price))

Expected price: 193.85

Here, we can see (based solely on using Monte Carlo simulation, of course) there looks to be more upside than downside for the next year, with the expected price running about $193 and only a 10 percent chance of the price landing below 128.

There are of course many, many things that drive a stock price beyond historical percent changes (meaning I would not make investment decisions solely off Monte Carlo simulation!), but this offers a thorough example of using Monto Carlo simulation to better understand the distribution of possible outcomes.

Ace your next data science interview

Get better at data science interviews by solving a few questions per week

Originally published at https://www.interviewqs.com.

--

--

Greg Brown
Nerd For Tech

I’m a product junkie with a passion for data.