CompEcon Toolbox:
Monte Carlo Simulation of Time Series
Randall Romero Aguilar, PhD

This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.

Last updated: 2020-Sep-10


Simulate time series using Monte Carlo Method.

A commodity price is governed by weekly price movements \begin{equation*} \log(p_{t+1}) = \log(p_t) + \tilde \epsilon_t \end{equation*} where the $\tilde \epsilon_t$ are i.i.d. normal with mean $\mu=0.005$ and standard deviation $\sigma=0.02$.

To simulate three time series of T=40 weekly price changes, starting from a price of 2, execute the script

Initial tasks

In [ ]:
if 'google.colab' in str(get_ipython()):
    print("This notebook is running on Google Colab. Installing the compecon package.")
    !pip install compecon
In [ ]:
import numpy as np
from compecon import demo
from scipy.stats import norm
import matplotlib.pyplot as plt


In [ ]:
m, T = 3, 40
mu, sigma = 0.005, 0.02
e = norm.rvs(mu,sigma,size=[T,m])
logp = np.zeros([T+1,m])
logp[0] = np.log(2)
for t in range(T):
    logp[t+1] = logp[t] + e[t]

Make figure

In [ ]:
fig, ax = plt.subplots()
ax.set(xlabel='Week', ylabel='Price', xlim=[0,T])