Monte Carlo Simulations of Future Stock Prices in Python

A Monte Carlo simulation is a method that allows for the generation of future potential outcomes of a given event. In this case, we are trying to model the price pattern of a given stock or portfolio of assets a predefined amount of days into the future. With Python, R, and other programming languages, we can generate thousands of outcomes on the potential price pattern of a stock. We are much better off running Monte Carlo Simulations with programming languages rather than Excel. Excel simply does not provide the computational necessary to efficiently and quickly run thousands of trials. You could use Excel if you want, but it is extraordinarily inefficient.

Let’s get a better understanding of the background of Monte Carlo simulations specifically being applied to the stock market. The reality of the matter is that Monte Carlo simulations aren’t just used to generate future stock prices. In fact, they are often used to estimate risk! For example, Value at Risk is often calculated using a Monte Carlo method, where we would be using theoretical future data rather than historical data. We could include a method in the class we will build below to estimate Value at Risk over a given confidence interval with our generated stock prices. The key takeaway from Monte Carlo simulations is the fact that there is some sort of random variable involved. The stock market is a perfect application of a model that uses a type of Monte Carlo simulation due to the level of statistical noise within the markets. We are trying to model the probability of different outcomes, simple as that.

In the script I wrote, the intention was to design a class that could use different Monte Carlo methods, and different graphical outputs of the simulation with descriptive statistics as well. Note we can use a single ticker or portfolio of assets in the simulation. We also have multiple models we can use.

Model 1 – Simulation using daily volatility:

Pretty straightforward. The random “shock” element here will be determined by the historical volatility of a stock over a given timeframe.

Model 2 – Simulation using Brownian Motion:

Here, we are assuming the stock(s) will “drift” a certain amount over time. We can calculate this with the average daily return in our series as well as the variance of the returns. This post will not cover the background of the simulation techniques extensively. We are trying to focus on implementing the models in a program.

Disclaimer: You will find that most of the figures used here demonstrate different simulations of the same company. Spyder (the IDE I use) would only let me interact with one chart at a time.

To start, we need to import the various dependencies that will allow for this script to run as well as creating the class and initial variables we will need. In this case, the variables are the start and end date for the time series data for the asset or portfolio of assets.

Next, we need to obtain our data. We can do this using pandas’ remote data access feature. We will make two methods within our class, one for a single asset, and one for a portfolio of assets.

Next, we can create two types of simulations here. We will create a basic simulation that only uses the asset’s daily volatility, while the other simulation will be using the concept of Geometric Brownian Motion.

Next, we want to visualize our simulations! We can create a line graph and histogram to visualize our data with Matplotlib.

For the heck of it, we can actually calculate our theoretical Value at Risk with the given portfolio and simulation outputs. The Value at Risk is simply the difference between the current price and specified price at a given confidence interval.

Next, we want to analyze the outputted simulations. We can do this in a variety of ways.

Finally, to run the script:

Below are the statistics outputted for a simulation consisting of 10 trials with AT&T Stock (Ticker: T)

#——————Simulation Stats——————#
Simulation 1 Mean Price: 31.4382622414
Simulation 1 Median Price: 31.9897921901
Simulation 2 Mean Price: 35.7671671325
Simulation 2 Median Price: 35.9623238149
Simulation 3 Mean Price: 35.6989735203
Simulation 3 Median Price: 35.6337173263
Simulation 4 Mean Price: 33.1080272815
Simulation 4 Median Price: 32.4359524255
Simulation 5 Mean Price: 38.2088369838
Simulation 5 Median Price: 36.0823949931
Simulation 6 Mean Price: 34.8515405456
Simulation 6 Median Price: 34.7751886371
Simulation 7 Mean Price: 35.6820401892
Simulation 7 Median Price: 34.8281466864
Simulation 8 Mean Price: 39.682759584
Simulation 8 Median Price: 37.2205632288
Simulation 9 Mean Price: 35.5834596902
Simulation 9 Median Price: 35.5221844294
Simulation 10 Mean Price: 38.1580107637
Simulation 10 Median Price: 39.228093444

#———————-Last Price Stats——————–#
Mean Price: 36.5581328863
Maximum Price: 45.7485017495
Minimum Price: 25.9968452
Standard Deviation: 6.21986513961

#———————-Descriptive Stats——————-#
count 10.000000
mean 36.558133
std 6.556314
min 25.996845
25% 33.827175
50% 36.277416
75% 40.346060
max 45.748502
Name: 200, dtype: float64

#————–Annual Expected Returns for Trials———–#
Simulation 1 Annual Expected Return -34.42%
Simulation 1 Total Return 638.40%
Simulation 2 Annual Expected Return 15.94%
Simulation 2 Total Return 1508.27%
Simulation 3 Annual Expected Return 4.92%
Simulation 3 Total Return -275.24%
Simulation 4 Annual Expected Return -25.92%
Simulation 4 Total Return -205.23%
Simulation 5 Annual Expected Return 34.57%
Simulation 5 Total Return -471.27%
Simulation 6 Annual Expected Return 8.90%
Simulation 6 Total Return -31.48%
Simulation 7 Annual Expected Return 1.70%
Simulation 7 Total Return -111.73%
Simulation 8 Annual Expected Return 35.25%
Simulation 8 Total Return -898.73%
Simulation 9 Annual Expected Return -2.43%
Simulation 9 Total Return -100.06%
Simulation 10 Annual Expected Return 21.52%
Simulation 10 Total Return 101.22%

#———————-Percentiles——————————–#
5th Percentile: 26.8433098784
10th Percentile: 27.6897745569
15th Percentile: 29.8815727367
20th Percentile: 32.457751917
25th Percentile: 33.8271746117
30th Percentile: 34.2311921181
35th Percentile: 34.6550694547
40th Percentile: 35.1186664518
45th Percentile: 35.6054189947
50th Percentile: 36.2774159036
55th Percentile: 36.9494128126
60th Percentile: 37.9284581889
65th Percentile: 38.9458846236
70th Percentile: 39.7094402515
75th Percentile: 40.3460604761
80th Percentile: 41.6228406181
85th Percentile: 43.6998206569
90th Percentile: 45.3585747934
95th Percentile: 45.5535382715

#—————–Calculate Probabilities————————-#
Probability price is between 30 and 40: 50.00%
Probability price is > 45: 20.00%

About the author

programmingforfinance

Hi, I'm Frank. I have a passion for coding and extend it primarily within the realm of Finance.

View all posts

3 Comments

• Erick says:

Hi! This was a really good read! However, I am having a bit of trouble running it on sypder (python 3.6 ). I’m unsure what is wrong but there seems to be an issue with all the print statements for me. Any help would be greatly appreciated. Thanks again!

• programmingforfinance says:

Email me some screenshots of the error messages you are getting and I can help you out!

• Matt says:

Thank you for the explanation and the code! This is excellent. I have one doubt in the brownian motion function. In line 26 of the function, last_price * math.exp(shock) is not assigned to any variable. I was wondering if it is a part of the np.random.normal just before it or if it should be assigned to some other variable? It would be helpful if you could clarify this doubt. Thank you.