-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTauchen.py
More file actions
77 lines (55 loc) · 1.96 KB
/
Tauchen.py
File metadata and controls
77 lines (55 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Obtained from https://quanteconpy.readthedocs.io/en/0.1.7/_modules/quantecon/tauchen.html#approx_markov
"""
"""
Filename: tauchen.py
Authors: Thomas Sargent, John Stachurski
Discretizes Gaussian linear AR(1) processes via Tauchen's method
"""
import numpy as np
from scipy.stats import norm
def approx_markov(rho, sigma_u, m=3, n=7):
"""
Computes the Markov matrix associated with a discretized version of
the linear Gaussian AR(1) process
y_{t+1} = rho * y_t + u_{t+1}
according to Tauchen's method. Here {u_t} is an iid Gaussian
process with zero mean.
Parameters
----------
rho : scalar(float)
The autocorrelation coefficient
sigma_u : scalar(float)
The standard deviation of the random process
m : scalar(int), optional(default=3)
The number of standard deviations to approximate out to
n : scalar(int), optional(default=7)
The number of states to use in the approximation
Returns
-------
x : array_like(float, ndim=1)
The state space of the discretized process
P : array_like(float, ndim=2)
The Markov transition matrix where P[i, j] is the probability
of transitioning from x[i] to x[j]
"""
F = norm(loc=0, scale=sigma_u).cdf
# standard deviation of y_t
std_y = np.sqrt(sigma_u**2 / (1-rho**2))
# top of discrete state space
x_max = m * std_y
# bottom of discrete state space
x_min = - x_max
# discretized state space
x = np.linspace(x_min, x_max, n)
step = (x_max - x_min) / (n - 1)
half_step = 0.5 * step
P = np.empty((n, n))
for i in range(n):
P[i, 0] = F(x[0]-rho * x[i] + half_step)
P[i, n-1] = 1 - F(x[n-1] - rho * x[i] - half_step)
for j in range(1, n-1):
z = x[j] - rho * x[i]
P[i, j] = F(z + half_step) - F(z - half_step)
return x, P