Skip to content

Instantly share code, notes, and snippets.

@jstac
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstac/d58d4f4273cf5a2deb03 to your computer and use it in GitHub Desktop.
Save jstac/d58d4f4273cf5a2deb03 to your computer and use it in GitHub Desktop.
"""
Generates estimates of the stationary distribution of a an optimal growth model with
Markov shocks discretized according to Tauchen's method. Estimates are via the
empirical distribution. Requires the quantecon package, which can be installed via
pip. See http://quant-econ.net/py/getting_started.html#the-quantecon-package
"""
import numpy as np
import matplotlib.pyplot as plt
import quantecon as qe
def sim_ts(k_init, n=1000000, alpha=0.3, beta=0.96, A=1, rho=0.8, sigma=0.1):
z, P = qe.approx_markov(rho, sigma, n=4, m=1.2)
z = np.exp(z)
dm = qe.MarkovChain(P)
z_vals = z[dm.simulate(sample_size=n)]
k = np.empty(n)
k[0] = k_init
for t in range(n-1):
k[t+1] = beta * alpha * A * k[t]**alpha * z_vals[t]
f = qe.ECDF(k)
return np.vectorize(f.__call__)
kmin, kmax = 0.1, 0.27
f = sim_ts(0.4)
fig, ax = plt.subplots(figsize=(9, 6))
k_grid = np.linspace(kmin, kmax, 150)
ax.set_xlim(kmin, kmax)
ax.plot(k_grid, f(k_grid), 'k-', lw=2, alpha=0.75, label=r'$\sigma=0.1$')
f = sim_ts(0.4, sigma=0.15)
ax.plot(k_grid, f(k_grid), 'g-', lw=2, alpha=0.5, label=r'$\sigma=0.15$')
ax.legend(loc='upper left', fontsize=16, frameon=0)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment