Now Reading
A Primer To Monte Carlo Simulation in Python

A Primer To Monte Carlo Simulation in Python

Simulation is acting out or mimicking an actual or probable real life condition, event, or situation to find a cause of a past occurrence (such as an accident), or to forecast future effects (outcomes) of assumed circumstances or factors. Whereas simulations are very useful tools that allow experimentation without exposure to risk, they are gross simplifications of the reality because they include only a few of the real-world factors, and are only as good as their underlying assumptions.

Uncertainty in Forecasting Models

When you develop a forecasting model – any model that plans ahead for the future – you make certain assumptions. These might be assumptions about the investment return on a portfolio, the cost of a construction project, or how long it will take to complete a certain task. Because these are projections into the future, the best you can do is estimate the expected value. You can’t know with certainty what the actual value will be, but based on historical data, or expertise in the field, or past experience, you can draw an estimate. While this estimate is useful for developing a model, it contains some inherent uncertainty and risk, because it’s an estimate of an unknown value.

Register for this Session>>

Estimating Ranges of Values

In some cases, it’s possible to estimate a range of values. In a construction project, you might estimate the time it will take to complete a particular job; based on some expert knowledge, you can also estimate the absolute maximum time it might take, in the worst possible case, and the absolute minimum time, in the best possible case. The same could be done for project costs. In a financial market, you might know the distribution of possible values through the mean and standard deviation of returns. By using a range of possible values, instead of a single guess, you can create a more realistic picture of what might happen in the future. When a model is based on ranges of estimates, the output of the model will also be a range.

This is different from a normal forecasting model, in which you start with some fixed estimates – say the time it will take to complete each of three parts of a project – and end up with another value – the total time for the project. If the same model were based on ranges of estimates for each of the three parts of the project, the result would be a range of times it might take to complete the project. When each part has a minimum and maximum estimate, we can use those values to estimate the total minimum and maximum time for the project.

Monte Carlo Simulation

Monte Carlo simulation, or probability simulation, is a technique used to understand the impact of risk and uncertainty in financial, project management, cost, and other forecasting models. When you have a range of values as a result, you are beginning to understand the risk and uncertainty in the model. The key feature of a Monte Carlo simulation is that it can tell you – based on how you create the ranges of estimates – how likely the resulting outcomes are.

In a Monte Carlo simulation, a random value is selected for each of the tasks, based on the range of estimates. The model is calculated based on this random value. The result of the model is recorded, and the process is repeated. A typical Monte Carlo simulation calculates the model hundreds or thousands of times, each time using different randomly-selected values. When the simulation is complete, we have a large number of results from the model, each based on random input values. These results are used to describe the likelihood, or probability, of reaching various results in the model.

A Monte Carlo Simulation yields risk analysis by generating models of possible results through substituting a range of values (a probability distribution) for any factor that has inherent uncertainty. The Monte Carlo method is based on the generation of multiple trials to determine the expected value of a random variable.

Python Codes

For this exercise the following modules are used: quandl, numpy, pandas, scipy.stats, and matplotlib.pyplot

Get the Data

  • Download the data for Apple (‘AAPL’) for the period ‘2006-1-1’ to ‘2017-12-31’

data = AAPL = quandl.get(‘WIKI/AAPL’,start_date=’2006-01-01′,end_date=’2017-12-31′)[‘Adj. Close’]

  • Obtain the log returns of Apple for the designated period.

log_returns = np.log(1 + data.pct_change())


  • u – the mean value of the log returns

var – the variance to the log returns

std – standard deviation of the log returns

u = log_returns.mean()

var = log_returns.var()

stdev = log_returns.std()

  • Calculate the drift, using the following formula: drift = u – 0.5 * var

drift = u – (0.5 * var)

Forecasting Future Stock Prices

  • Forecast future stock prices for every trading day a year ahead (assign 252 to “t_intervals”)

Examine 10 possible outcomes (assign 10 to “iterations”)

t_intervals = 252

iterations = 10

  • Using the below formula to calculate daily returns:

daily returns = e(drift + stdev * Z)

Z = norm.ppf(np.random.rand(t_intervals, iterations))

See Also

daily_returns = np.exp(drift + stdev * norm.ppf(np.random.rand(t_intervals, iterations)))

  • S0 – the last adjusted closing price of Apple

price_list – the same dimension as the daily_returns matrix

Set the values on the first row of the price_list array equal to S0

S0 = data.iloc[-1]

price_list = np.zeros_like(daily_returns)

price_list[0] = S0

  • Create a loop in the range (1 to t_intervals) that reassigns to the price in time t the product of the price in day (t-1) with the value of the daily returns in t

for t in range(1, t_intervals):

    price_list[t] = price_list[t – 1] * daily_returns[t]

  • Plots



What Do You Think?

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.
Join our Telegram Group. Be part of an engaging community

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top