approximation¶
Collection of functions to approximate a continuous random process by a discrete Markov chain.
- quantecon.markov.approximation.discrete_var(A, C, grid_sizes=None, std_devs=3.1622776601683795, sim_length=1000000, rv=None, order='C', random_state=None)[source]¶
Generate an MarkovChain instance that discretizes a multivariate autorregressive process by a simulation of the process.
This function discretizes a VAR(1) process of the form:
\[x_t = A x_{t-1} + C u_t\]where \({u_t}\) is drawn iid from a distribution with mean 0 and unit standard deviaiton; and \({C}\) is a volatility matrix. Internally, from this process a sample time series of length sim_length is produced, and with a cartesian grid as specified by grid_sizes and std_devs a Markov chain is estimated that fits the time series, where the states that are never visited under the simulation are removed.
For a mathematical derivation check Finite-State Approximation Of VAR Processes: A Simulation Approach by Stephanie Schmitt-Grohé and Martín Uribe, July 11, 2010. In particular, we follow Schmitt-Grohé and Uribe’s method in contructing the grid for approximation.
- Parameters:
- Aarray_like(float, ndim=2)
An m x m matrix containing the process’ autocorrelation parameters. Its eigenvalues are assumed to have moduli bounded by unity.
- Carray_like(float, ndim=2)
An m x r volatility matrix
- grid_sizesarray_like(int, ndim=1), optional(default=None)
An m-vector containing the number of grid points in the discretization of each dimension of x_t. If None, then set to (10, …, 10).
- std_devsfloat, optional(default=np.sqrt(10))
The number of standard deviations the grid should stretch in each dimension, where standard deviations are measured under the stationary distribution.
- sim_lengthint, optional(default=1_000_000)
The length of the simulated time series.
- rvoptional(default=None)
Object that represents the disturbance term u_t. If None, then standard normal distribution from numpy.random is used. Alternatively, one can pass a “frozen” object of a multivariate distribution from scipy.stats. It must have a zero mean and unit standard deviation (of dimension r).
- orderstr, optional(default=’C’)
(‘C’ or ‘F’) order in which the states in the cartesian grid are enumerated.
- random_stateint or np.random.RandomState/Generator, optional
Random seed (integer) or np.random.RandomState or Generator instance to set the initial state of the random number generator for reproducibility. If None, a randomly initialized RandomState is used.
- Returns:
- mcMarkovChain
An instance of the MarkovChain class that stores the transition matrix and state values returned by the discretization method, in the following attributes:
- Pndarray(float, ndim=2)
A 2-dim array containing the transition probability matrix over the discretized states.
- state_valuesndarray(float, ndim=2)
A 2-dim array containing the state vectors (of dimension m) as rows, which are ordered according to the order option.
Examples
This example discretizes the stochastic process used to calibrate the economic model included in “Downward Nominal Wage Rigidity, Currency Pegs, and Involuntary Unemployment” by Stephanie Schmitt-Grohé and Martín Uribe, Journal of Political Economy 124, October 2016, 1466-1514.
>>> rng = np.random.default_rng(12345) >>> A = [[0.7901, -1.3570], ... [-0.0104, 0.8638]] >>> Omega = [[0.0012346, -0.0000776], ... [-0.0000776, 0.0000401]] >>> C = scipy.linalg.sqrtm(Omega) >>> grid_sizes = [21, 11] >>> mc = discrete_var(A, C, grid_sizes, random_state=rng) >>> mc.P.shape (145, 145) >>> mc.state_values.shape (145, 2) >>> mc.state_values[:10] # First 10 states array([[-0.38556417, 0.02155098], [-0.38556417, 0.03232648], [-0.38556417, 0.04310197], [-0.38556417, 0.05387746], [-0.34700776, 0.01077549], [-0.34700776, 0.02155098], [-0.34700776, 0.03232648], [-0.34700776, 0.04310197], [-0.34700776, 0.05387746], [-0.30845134, 0. ]]) >>> mc.simulate(10, random_state=rng) array([[ 0.11566925, -0.01077549], [ 0.11566925, -0.01077549], [ 0.15422567, 0. ], [ 0.15422567, 0. ], [ 0.15422567, -0.01077549], [ 0.11566925, -0.02155098], [ 0.11566925, -0.03232648], [ 0.15422567, -0.03232648], [ 0.15422567, -0.03232648], [ 0.19278209, -0.03232648]])
The simulation below uses the same parameters with \({u_t}\) drawn from a multivariate t-distribution
>>> df = 100 >>> Sigma = np.diag(np.tile((df-2)/df, 2)) >>> mc = discrete_var(A, C, grid_sizes, ... rv=scipy.stats.multivariate_t(shape=Sigma, df=df), ... random_state=rng) >>> mc.P.shape (146, 146) >>> mc.state_values.shape (146, 2) >>> mc.state_values[:10] array([[-0.38556417, 0.02155098], [-0.38556417, 0.03232648], [-0.38556417, 0.04310197], [-0.38556417, 0.05387746], [-0.34700776, 0.01077549], [-0.34700776, 0.02155098], [-0.34700776, 0.03232648], [-0.34700776, 0.04310197], [-0.34700776, 0.05387746], [-0.30845134, 0. ]]) >>> mc.simulate(10, random_state=rng) array([[-0.03855642, -0.02155098], [ 0.03855642, -0.03232648], [ 0.07711283, -0.03232648], [ 0.15422567, -0.03232648], [ 0.15422567, -0.04310197], [ 0.15422567, -0.03232648], [ 0.15422567, -0.03232648], [ 0.2313385 , -0.04310197], [ 0.2313385 , -0.03232648], [ 0.26989492, -0.03232648]])
- quantecon.markov.approximation.rouwenhorst(n, rho, sigma, mu=0.0)[source]¶
Takes as inputs n, mu, sigma, rho. It will then construct a markov chain that estimates an AR(1) process of: \(y_t = \mu + \rho y_{t-1} + \varepsilon_t\) where \(\varepsilon_t\) is i.i.d. normal of mean 0, std dev of sigma
The Rouwenhorst approximation uses the following recursive defintion for approximating a distribution:
\[\begin{split}\theta_2 = \begin{bmatrix} p & 1 - p \\ 1 - q & q \\ \end{bmatrix}\end{split}\]\[\begin{split}\theta_{n+1} = p \begin{bmatrix} \theta_n & 0 \\ 0 & 0 \\ \end{bmatrix} + (1 - p) \begin{bmatrix} 0 & \theta_n \\ 0 & 0 \\ \end{bmatrix} + q \begin{bmatrix} 0 & 0 \\ \theta_n & 0 \\ \end{bmatrix} + (1 - q) \begin{bmatrix} 0 & 0 \\ 0 & \theta_n \\ \end{bmatrix}\end{split}\]where \({p = q = \frac{(1 + \rho)}{2}}\)
- Parameters:
- nint
The number of points to approximate the distribution
- rhofloat
Persistence parameter in AR(1) process, if you are approximating an AR(1) process then this is the autocorrelation across periods.
- sigmafloat
The value of the standard deviation of the \(\varepsilon\) process
- mufloat, optional(default=0.0)
The value \(\mu\) in the process. Note that the mean of this AR(1) process, \(y\), is simply \(\mu/(1 - \rho)\)
- Returns:
- mcMarkovChain
An instance of the MarkovChain class that stores the transition matrix and state values returned by the discretization method
Notes
UserWarning: The API of rouwenhorst was changed from rouwenhorst(n, ybar, sigma, rho) to rouwenhorst(n, rho, sigma, mu=0.) in version 0.6.0.
- quantecon.markov.approximation.tauchen(n, rho, sigma, mu=0.0, n_std=3)[source]¶
Computes a Markov chain associated with a discretized version of the linear Gaussian AR(1) process
\[y_t = \mu + \rho y_{t-1} + \epsilon_t\]using Tauchen’s method. Here \({\epsilon_t}\) is an i.i.d. Gaussian process with zero mean.
- Parameters:
- nscalar(int)
The number of states to use in the approximation
- rhoscalar(float)
The autocorrelation coefficient, Persistence parameter in AR(1) process
- sigmascalar(float)
The standard deviation of the random process
- muscalar(float), optional(default=0.0)
The value \(\mu\) in the process. Note that the mean of this AR(1) process, \(y\), is simply \(\mu/(1 - \rho)\)
- n_stdscalar(int), optional(default=3)
The number of standard deviations to approximate out to
- Returns:
- mcMarkovChain
An instance of the MarkovChain class that stores the transition matrix and state values returned by the discretization method
Notes
UserWarning: The API of tauchen was changed from tauchen(rho, sigma_u, b=0., m=3, n=7) to tauchen(n, rho, sigma, mu=0., n_std=3) in version 0.6.0.