I'm new to monte carlo simulations and have attempted to implement the simplest model in order to validate my current understanding.

Generate some price data and plot it:

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

prices = [234,2,34,2344,23,42,423,43,25,3245,325,32,532,5,235,2345,3245,23,52,345,423 , 32]


which generates:

enter image description here

  1. I will as

Assuming the dataset is normally distributed I calculate the mean and standard deviation of the prices dataset and execute 10000 simulations. The result of each simulation is stored in test_preds. The normality assumption may be incorrect and leveraging ARIMA ( https://en.wikipedia.org/wiki/Autoregressive_integrated_moving_average ) could assist in determining which distribution the dataset more closely following, but for this test will assume normality.

mean = np.mean(prices)
std = np.std(prices)

number_simulations = 10000
test_preds = np.random.normal(mean, std, number_simulations)

To generate the range of predictions I use :

import matplotlib.pyplot as plt

count, bins, ignored = plt.hist(test_preds, 30, density=True)

plt.plot(bins, 1/(std * np.sqrt(2 * np.pi)) *np.exp( - (bins - mean)**2 / (2 * std**2) ),
         linewidth=2, color='r')


which renders:

enter image description here

In order to predict the next price is it correct to take the mean of sampled values?

Therefore, for this example, the prediction of the next price or expected value is the mean of all previous values :


which is 634.022

To predict the price after the previous price (634.022) then recompute the simulated values stored in test_preds with 634.022 appended to the prices list prices


To calculate

$$Y_t = \mu + \varepsilon_t, \quad \varepsilon_t \sim^{\text{iid}} \mathcal{N}(0,\sigma^2)$$

I calculate the mean and variance based on the existing prices data:

prices_mean = np.mean(prices)
variance = np.std(prices) * np.std(prices) 

which is :

mean = 140.82

variance = 19004.06

To calculate :

$$E(Y_{t+1}|\mathcal{F}_t) = \mu$$

I run 10'000'000 simulations:

simulations = prices_mean + np.random.normal(0, variance, 10000000)

then take the mean of the simulations:

prediction = np.mean(simulations)

which works out to be 149.55. 149.55 is the prediction for $E(Y_{t+1}|\mathcal{F}_t)$. Have I implemented the steps correctly?


1 Answer 1


On a fundamental level, Monte Carlo is an approach to approximating integrals. It is not intrinsically a method for forecasting. It becomes a method for forecasting when your model defines a certain integral which corresponds to the forecast that you are trying to approximate.

Because you have not explicitly defined a model, there is no way to tell if your Monte Carlo forecast is correct; we don't know what integral you are trying to approximate. You mention normality but that is just the marginal distribution; a time series model includes the full joint distribution at all time points.

One possible model for which your one-step-ahead forecast would be correct is this:

$$Y_t = \mu + \varepsilon_t, \quad \varepsilon_t \sim^{\text{iid}} \mathcal{N}(0,\sigma^2)$$

You would then fit your model by estimating its two parameters, $\mu$ and $\sigma^2$. Then, the usual point forecast is given by the conditional expectation, which you can compute exactly in this case:

$$E(Y_{t+1}|\mathcal{F}_t) = \mu$$

Your point forecast would then just be your estimate of the mean, $\hat{\mu}$. There is no reason to use Monte Carlo here, but if you really wanted to, you would simulate:

$$Y_{t+1}^{(i)} \sim^{\text{iid}} \mathcal{N}(\hat{\mu}, \hat{\sigma}^2)$$

And you would approximate the integral that corresponds to $E(Y_{t+1}|\mathcal{F}_t)$ by the sample mean of the $Y_{t+1}^{(i)}$.

As far as the 2-step-ahead forecast, there is no reason to include your 1-step-ahead forecast as if it were data: you did not observe it and it should not be used to refit parameters. Also if your model is as described above, then the $h$-step-ahead-forecast is, for any $h$:

$$E(Y_{t+h}|\mathcal{F}_t) = \mu$$

You would simply copy the same point forecast (estimated by Monte Carlo or not) for any horizon.

Finally, it is very clear from the plot that this is not a good model for your data at all, as it seems to (1) have very large spikes, (2) be approximately periodic and (3) from context probably bounded below by zero.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.