Undergraduate Computational Macro
QuantEcon.jl
is used for some of the code examples for easy simulationThe initial condition \(x_0\) could be given, or it could be a distribution
Given \(\mu_0 \in \mathbb{R}^n\) and \(\Sigma_0 \in \mathbb{R}^{n\times n}\), a (positive semi-definite) covariance matrix \[ x_0 \sim \mathcal{N}(\mu_0, \Sigma_0) \]
Let \(\{y_t\}\) be a deterministic sequence that satisfies \[ y_{t+1} = \phi_0 + \phi_1 y_t + \phi_2 y_{t-1} \]
\[ \begin{aligned} \underbrace{\begin{bmatrix}y_{t+1} \\ y_{t} \\ y_{t-1} \\ y_{t-2}\end{bmatrix}}_{\equiv x_{t+1}} &= \underbrace{\begin{bmatrix}\phi_1 & \phi_2 & \phi_3 & \phi_4 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0\end{bmatrix}}_{\equiv A} \underbrace{\begin{bmatrix}y_t \\ y_{t-1} \\ y_{t-2} \\ y_{t-3}\end{bmatrix}}_{\equiv x_t} + \underbrace{\begin{bmatrix}\sigma \\ 0 \\ 0 \\ 0\end{bmatrix}}_{\equiv C} \underbrace{\begin{bmatrix}w_{t+1}\end{bmatrix}}_{\equiv w_{t+1}}\\ y_t &= \underbrace{\begin{bmatrix}1 & 0 & 0 & 0\end{bmatrix}}_{\equiv G} \underbrace{\begin{bmatrix}y_t & y_{t-1} & y_{t-2} & y_{t-3}\end{bmatrix}^{\top}}_{\equiv x_t} \end{aligned} \]
Given \(x_t \sim \mathcal{N}(\mu_t, \Sigma_t)\), can forecast \(x_{t+1} \sim \mathcal{N}(\mu_{t+1}, \Sigma_{t+1})\) \[ \begin{aligned} \mu_{t+1} &= A \mu_t\\ \Sigma_{t+1} &= A \Sigma_t A^{\top} + C C^{\top} \end{aligned} \]
And given some \(x_t\sim \mathcal{N}(\mu_t, \Sigma_t)\)
\[ y_{t+1} \sim \mathcal{N}(G \mu_t, G \Sigma_t G^{\top}) \]
Given \(x_t \sim \mathcal{N}(\mu_t, \Sigma_t)\), we can forecast \(x_{t+j}\) and \(y_{t+j}\) for any \(j\) \[ \begin{aligned} \mathbb{E}_t x_{t+j} &= A^j \mu_t\\ \mathbb{E}_t y_{t+j} &= G A^j \mu_t \end{aligned} \]
Useful for computing expected net present values of future cash flows
\[ \begin{aligned} \mathbb{E}_t \sum_{j=0}^{\infty} \beta^j y_{t+j} &= \sum_{j=0}^{\infty} \beta^j \mathbb{E}_t y_{t+j} = \sum_{j=0}^{\infty} \beta^j G A^j \mu_t\\ &= G(I - \beta A)^{-1} \mu_t \end{aligned} \]
If they exist, from any gaussian initial condition, the stationary distribution is \(x_{\infty} \sim \mathcal{N}(\mu_{\infty}, \Sigma_{\infty})\)
Must fulfill the fixed points of the previous iteration,
\[ \begin{aligned} \mu_{\infty} &= A \mu_{\infty}\\ \Sigma_{\infty} &= A \Sigma_{\infty} A^{\top} + C C^{\top} \end{aligned} \]
Given \(A\) and \(G\) matrices, you may be able to recover \(x_t\) from the \(y_t\)
What if the observations in the LSS are noisy? Then \(x_t\) is truly “latent” \[ \begin{aligned} x_{t+1} & = A x_t + C w_{t+1} \\ y_t & = G x_t + H v_t\\ w_{t+1} &\sim \mathcal{N}(0,I)\\ v_t &\sim \mathcal{N}(0, I) \end{aligned} \]
The Kalman Filter is the recursive, Bayesian updating of the distribution of the state \(x_{t+1}\) given the observations \(y_{t+1}\) and a prior \(x_t \sim \mathcal{N}(\mu_t, \Sigma_t)\)
See here and other places for the derivation \[ \begin{aligned} K_t &\equiv A \Sigma_t G^{\top} (G \Sigma_t G^{\top} + H H^{\top})^{-1}\\ \mu_{t+1} &= A \mu_t + K_t (y_t - G \mu_t) \\ \Sigma_{t+1} &= A \Sigma_t A^{\top} - K_t G \Sigma_t A^{\top} + C C^{\top} \end{aligned} \]
Consider the simple case where \(x_t \in \mathbb{R}, y_t \in \mathbb{R}, A = 1, G = 1\) and \(C,H \in \mathbb{R}\) \[ \begin{aligned} K_t &= \Sigma_t/(\Sigma_t + H^2)\\ \mu_{t+1} &= \mu_t + K_t \underbrace{(y_t - \mu_t)}_{\text{innovation}} = (1-K_t)\mu_t + K_t y_t\\ \Sigma_{t+1} &= (1-K_t)\Sigma_t + C^2 \end{aligned} \]
Future states are forecasted by the Kalman Filter itself
The state is a hidden Markov variable, but we can forecast the current state and future observations
Current state is constructed to be \(x_t \sim \mathcal{N}(\mu_t, \Sigma_t)\)
Given a \(x_t\) distribution, can get the \(y_t\) distribution as
\[ y_t \sim \mathcal{N}(G \mu_t, G \Sigma_t G^{\top} + H H^{\top}) \]
When looking at software packages, you may need to map to different version
For example, another common one is \[ \begin{aligned} x_{t+1} & = A x_t + w_{t+1} \\ y_t & = G x_t + v_t\\ w_{t+1} &\sim \mathcal{N}(0,Q)\\ v_t &\sim \mathcal{N}(0, R) \end{aligned} \]
We will use the QuantEcon.jl
package for the Kalman Filter, uses the \(Q\) and \(R\) form
Consider a univariate function
\[ \begin{aligned} x_{t+1} &= x_t\\ y_t &= x_t + v_t\\ v_t &\sim \mathcal{N}(0, 1.0^2) \end{aligned} \]
A, G, Q, R = 1.0, 1.0, 0.0, 1.0
x_hat_0, Sigma_0 = 8.0, 1.0
x_true = 10.0
# initialize Kalman filter
kalman = Kalman(A, G, Q, R)
set_state!(kalman, x_hat_0, Sigma_0)
plt = plot(;title="First 5 Densities")
for i in 1:5
# record the current predicted mean and variance, and plot their densities
m, v = kalman.cur_x_hat, kalman.cur_sigma
plot!(Normal(m, sqrt(v)); label = "t = $i")
# Generate signal and update
y = x_true + sqrt(R) * randn() # i.e. x_t + v_t
update!(kalman, y)
end
plt
A good starting point for a model of expectations is to assume that agents use the mathematical expectation
This requires that they have an internal model of a stochastic process for the data-generating process - conditional on their choices
One benefit is that we can use the mathematical expectation and its properties (e.g., linearity)
\[ \mathbb{E}(a X + b Y | Z) = a \mathbb{E}(X|Z) + b \mathbb{E}(Y|Z) \]
Think of the values we can condition on in our expectations as being the “information set” of the agent
\[ \mathbb{E}[X_{t+1} | \underbrace{X_t, X_{t-1}, \ldots}_{\text{Information Set}}] = \mathbb{E}_t[X_{t+1}] \]
If Markov, information set is summarized by the current state
\[ \mathbb{E}[X_{t+1} | X_t, X_{t-1}, \ldots] = \mathbb{E}[X_{t+1} | X_t] \]
Specifying the information set is a key part of economic models
If a state is hidden, then the agent must expectations from observables
Models of learning a hidden value or latent state are often built around some form of state-space model
For example, with a LSS model \(x_{t+1} = A x_t + C w_{t+1}\) and \(y_t = G x_t + H v_t\)
\[ \mathbb{E}(x_{t+1} | y_t, y_{t-1}, \ldots) \]
Not all models of learning are Bayesian, but economists often case about \[ \mathbb{E}[(x_{t+1} - \mathbb{E}[x_{t+1}|y_t, y_{t-1}, \ldots])^2 | y_t, y_{t-1}, \ldots] \]
\[ FE_{t,t+1} \equiv x_{t+1} - \mathbb{E}_t[x_{t+1}] \]
\[ FE_{t,t+1} = A x_t + C w_{t+1} - \mathbb{E}_t[A x_t + C w_{t+1}] = C w_{t+1} \] - The forecast error is a random variable, but it is uncorrelated with the information set
How far off do you expect your forecasts to be?
\[ \mathbb{E}_t[FE_{t,t+1}] = \mathbb{E}_t[x_{t+1} - \mathbb{E}_t[x_{t+1}]] = 0 \]
\[ \begin{aligned} \mathbb{V}_t(FE_{t+1}) &\equiv \mathbb{E}_t[FE_{t,t+1} FE_{t,t+1}^{\top}]\\ &= \mathbb{E}_t[(C w_{t+1})(C w_{t+1})^{\top}] = C C^{\top} \end{aligned} \]
\[ \mathbb{V}_t(FE_{t+1}) = G C C^{\top} G^{\top} + H H^{\top} \]
If \(x_t\) is not in the information set, then (conditional on \(x_t\)) the expected forecast error may not be zero.
\[ \begin{aligned} FE_{t,t+1} &= y_{t+1} - \mathbb{E}[y_{t+1} | y_t, y_{t-1}, \ldots]\\ &= G(A x_t + C w_{t+1}) - G(A \mu_t) = G A (x_t - \mu_t) + C w_{t+1} \end{aligned} \]
In that setup, the agent has an unbiased estimate if they use their \(x_t\) estimate since \(\mathbb{E}_t(x_t) = \mu_t\)
\[ \mathbb{E}_t[FE_{t,t+1}] = \mathbb{E}_t[G A (x_t - \mu_t) + C w_{t+1}] = 0 \]
An important type of stochastic process are Martingales where
\[ \mathbb{E}_t[X_{t+1}] = X_t \]
The canonical Martingale is a random walk \(X_{t+1} = X_t + w_{t+1}\) where \(w_{t+1}\sim \mathcal{N}(0,1)\)
Forecasting: the best guess for the future is the current value
Martingales have many applications in asset pricing and finance, models of learning, and in consumption and savings models
If an agent has no risk aversion, the their preferences can be rationalized by something proportional a linear utility
For our LSS model we can just use the EPDV to calculate a price
\[ \begin{aligned} p(x_t) &= \mathbb{E}\left[\sum_{j=0}^{\infty} \beta^j y_{t+j} | x_t\right]\\ &= G (I - \beta A)^{-1} x_t \end{aligned} \]
If the state is hidden, then the agent must use their internal model to forecast the future
With a Kalman Filter, this becomes
\[ \begin{aligned} p(\mu_t, \Sigma_t) &= \mathbb{E}\left[\sum_{j=0}^{\infty} \beta^j y_{t+j} | \mu_t, \Sigma_t\right]\\ &= G (I - \beta A)^{-1} \mu_t \end{aligned} \]
Consider the AR(1) process \(y_{t+1} = a + \rho y_t + \sigma w_{t+1}\) with \(w_{t+1} \sim \mathcal{N}(0,1)\) and \(y_0 = 0.5\)
Let \(x_t \equiv \begin{bmatrix} y_t & 1 \end{bmatrix}^{\top}\) the LSS is
\[ \begin{aligned} x_{t+1} &= \underbrace{\begin{bmatrix}\rho & a\\ 0 & 1\end{bmatrix}}_{\equiv A} x_t + \underbrace{\begin{bmatrix}\sigma \\ 0\end{bmatrix}}_{\equiv C} w_{t+1}\\ y_t &= \underbrace{\begin{bmatrix}1 & 0\end{bmatrix}}_{\equiv G} x_t \end{aligned} \]
The price of the asset is then \(p(x_t) = G (I - \beta A)^{-1} x_t\)