\n",
" \n",
" \n",
" \n",
"

"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linear State Space Models\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Contents\n",
"\n",
"- [Linear State Space Models](#Linear-State-Space-Models) \n",
" - [Overview](#Overview) \n",
" - [The Linear State Space Model](#The-Linear-State-Space-Model) \n",
" - [Distributions and Moments](#Distributions-and-Moments) \n",
" - [Stationarity and Ergodicity](#Stationarity-and-Ergodicity) \n",
" - [Noisy Observations](#Noisy-Observations) \n",
" - [Prediction](#Prediction) \n",
" - [Code](#Code) \n",
" - [Exercises](#Exercises) \n",
" - [Solutions](#Solutions) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> “We may regard the present state of the universe as the effect of its past and the cause of its future” – Marquis de Laplace\n",
"\n",
"\n",
"In addition to what’s in Anaconda, this lecture will need the following libraries:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": true
},
"outputs": [],
"source": [
"!conda install -y quantecon"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"This lecture introduces the **linear state space** dynamic system.\n",
"\n",
"The linear state space system is a generalization of the scalar AR(1) process [we studied before](https://python-programming.quantecon.org/ar1_processes.html).\n",
"\n",
"This model is a workhorse that carries a powerful theory of prediction.\n",
"\n",
"Its many applications include:\n",
"\n",
"- representing dynamics of higher-order linear systems \n",
"- predicting the position of a system $ j $ steps into the future \n",
"- predicting a geometric sum of future values of a variable like \n",
" \n",
" - non-financial income \n",
" - dividends on a stock \n",
" - the money supply \n",
" - a government deficit or surplus, etc. \n",
" \n",
"- key ingredient of useful models \n",
" \n",
" - Friedman’s permanent income model of consumption smoothing. \n",
" - Barro’s model of smoothing total tax collections. \n",
" - Rational expectations version of Cagan’s model of hyperinflation. \n",
" - Sargent and Wallace’s “unpleasant monetarist arithmetic,” etc. \n",
" \n",
"\n",
"\n",
"Let’s start with some imports:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"from quantecon import LinearStateSpace\n",
"from scipy.stats import norm\n",
"import random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Linear State Space Model\n",
"\n",
"\n",
"\n",
"The objects in play are:\n",
"\n",
"- An $ n \\times 1 $ vector $ x_t $ denoting the **state** at time $ t = 0, 1, 2, \\ldots $. \n",
"- An IID sequence of $ m \\times 1 $ random vectors $ w_t \\sim N(0,I) $. \n",
"- A $ k \\times 1 $ vector $ y_t $ of **observations** at time $ t = 0, 1, 2, \\ldots $. \n",
"- An $ n \\times n $ matrix $ A $ called the **transition matrix**. \n",
"- An $ n \\times m $ matrix $ C $ called the **volatility matrix**. \n",
"- A $ k \\times n $ matrix $ G $ sometimes called the **output matrix**. \n",
"\n",
"\n",
"Here is the linear state-space system\n",
"\n",
"\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" x_{t+1} & = A x_t + C w_{t+1} \\\\\n",
" y_t & = G x_t \\nonumber \\\\\n",
" x_0 & \\sim N(\\mu_0, \\Sigma_0) \\nonumber\n",
"\\end{aligned} \\tag{1}\n",
"$$\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Primitives\n",
"\n",
"The primitives of the model are\n",
"\n",
"1. the matrices $ A, C, G $ \n",
"1. shock distribution, which we have specialized to $ N(0,I) $ \n",
"1. the distribution of the initial condition $ x_0 $, which we have set to $ N(\\mu_0, \\Sigma_0) $ \n",
"\n",
"\n",
"Given $ A, C, G $ and draws of $ x_0 $ and $ w_1, w_2, \\ldots $, the\n",
"model [(1)](#equation-st-space-rep) pins down the values of the sequences $ \\{x_t\\} $ and $ \\{y_t\\} $.\n",
"\n",
"Even without these draws, the primitives 1–3 pin down the *probability distributions* of $ \\{x_t\\} $ and $ \\{y_t\\} $.\n",
"\n",
"Later we’ll see how to compute these distributions and their moments."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Martingale Difference Shocks\n",
"\n",
"\n",
"\n",
"We’ve made the common assumption that the shocks are independent standardized normal vectors.\n",
"\n",
"But some of what we say will be valid under the assumption that $ \\{w_{t+1}\\} $ is a **martingale difference sequence**.\n",
"\n",
"A martingale difference sequence is a sequence that is zero mean when conditioned on past information.\n",
"\n",
"In the present case, since $ \\{x_t\\} $ is our state sequence, this means that it satisfies\n",
"\n",
"$$\n",
"\\mathbb{E} [w_{t+1} | x_t, x_{t-1}, \\ldots ] = 0\n",
"$$\n",
"\n",
"This is a weaker condition than that $ \\{w_t\\} $ is IID with $ w_{t+1} \\sim N(0,I) $."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Examples\n",
"\n",
"By appropriate choice of the primitives, a variety of dynamics can be represented in terms of the linear state space model.\n",
"\n",
"The following examples help to highlight this point.\n",
"\n",
"They also illustrate the wise dictum *finding the state is an art*.\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Second-order Difference Equation\n",
"\n",
"Let $ \\{y_t\\} $ be a deterministic sequence that satisfies\n",
"\n",
"\n",
"\n",
"$$\n",
"y_{t+1} = \\phi_0 + \\phi_1 y_t + \\phi_2 y_{t-1}\n",
"\\quad \\text{s.t.} \\quad\n",
"y_0, y_{-1} \\text{ given} \\tag{2}\n",
"$$\n",
"\n",
"To map [(2)](#equation-st-ex-1) into our state space system [(1)](#equation-st-space-rep), we set\n",
"\n",
"$$\n",
"x_t=\n",
"\\begin{bmatrix}\n",
" 1 \\\\\n",
" y_t \\\\\n",
" y_{t-1}\n",
"\\end{bmatrix}\n",
"\\qquad\n",
"A = \\begin{bmatrix}\n",
" 1 & 0 & 0 \\\\\n",
" \\phi_0 & \\phi_1 & \\phi_2 \\\\\n",
" 0 & 1 & 0\n",
" \\end{bmatrix}\n",
"\\qquad\n",
"C= \\begin{bmatrix}\n",
" 0 \\\\\n",
" 0 \\\\\n",
" 0\n",
" \\end{bmatrix}\n",
"\\qquad\n",
"G = \\begin{bmatrix} 0 & 1 & 0 \\end{bmatrix}\n",
"$$\n",
"\n",
"You can confirm that under these definitions, [(1)](#equation-st-space-rep) and [(2)](#equation-st-ex-1) agree.\n",
"\n",
"The next figure shows the dynamics of this process when $ \\phi_0 = 1.1, \\phi_1=0.8, \\phi_2 = -0.8, y_0 = y_{-1} = 1 $.\n",
"\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"def plot_lss(A,\n",
" C,\n",
" G,\n",
" n=3,\n",
" ts_length=50):\n",
"\n",
" ar = LinearStateSpace(A, C, G, mu_0=np.ones(n))\n",
" x, y = ar.simulate(ts_length)\n",
"\n",
" fig, ax = plt.subplots()\n",
" y = y.flatten()\n",
" ax.plot(y, 'b-', lw=2, alpha=0.7)\n",
" ax.grid()\n",
" ax.set_xlabel('time', fontsize=12)\n",
" ax.set_ylabel('$y_t$', fontsize=12)\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"ϕ_0, ϕ_1, ϕ_2 = 1.1, 0.8, -0.8\n",
"\n",
"A = [[1, 0, 0 ],\n",
" [ϕ_0, ϕ_1, ϕ_2],\n",
" [0, 1, 0 ]]\n",
"\n",
"C = np.zeros((3, 1))\n",
"G = [0, 1, 0]\n",
"\n",
"plot_lss(A, C, G)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Later you’ll be asked to recreate this figure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Univariate Autoregressive Processes\n",
"\n",
"\n",
"\n",
"We can use [(1)](#equation-st-space-rep) to represent the model\n",
"\n",
"\n",
"\n",
"$$\n",
"y_{t+1} = \\phi_1 y_{t} + \\phi_2 y_{t-1} + \\phi_3 y_{t-2} + \\phi_4 y_{t-3} + \\sigma w_{t+1} \\tag{3}\n",
"$$\n",
"\n",
"where $ \\{w_t\\} $ is IID and standard normal.\n",
"\n",
"To put this in the linear state space format we take $ x_t = \\begin{bmatrix} y_t & y_{t-1} & y_{t-2} & y_{t-3} \\end{bmatrix}' $ and\n",
"\n",
"$$\n",
"A =\n",
"\\begin{bmatrix}\n",
" \\phi_1 & \\phi_2 & \\phi_3 & \\phi_4 \\\\\n",
" 1 & 0 & 0 & 0 \\\\\n",
" 0 & 1 & 0 & 0 \\\\\n",
" 0 & 0 & 1 & 0\n",
"\\end{bmatrix}\n",
"\\qquad\n",
"C = \\begin{bmatrix}\n",
" \\sigma \\\\\n",
" 0 \\\\\n",
" 0 \\\\\n",
" 0\n",
" \\end{bmatrix}\n",
"\\qquad\n",
" G = \\begin{bmatrix}\n",
" 1 & 0 & 0 & 0\n",
" \\end{bmatrix}\n",
"$$\n",
"\n",
"The matrix $ A $ has the form of the *companion matrix* to the vector\n",
"$ \\begin{bmatrix}\\phi_1 & \\phi_2 & \\phi_3 & \\phi_4 \\end{bmatrix} $.\n",
"\n",
"The next figure shows the dynamics of this process when\n",
"\n",
"$$\n",
"\\phi_1 = 0.5, \\phi_2 = -0.2, \\phi_3 = 0, \\phi_4 = 0.5, \\sigma = 0.2, y_0 = y_{-1} = y_{-2} =\n",
"y_{-3} = 1\n",
"$$\n",
"\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"ϕ_1, ϕ_2, ϕ_3, ϕ_4 = 0.5, -0.2, 0, 0.5\n",
"σ = 0.2\n",
"\n",
"A_1 = [[ϕ_1, ϕ_2, ϕ_3, ϕ_4],\n",
" [1, 0, 0, 0 ],\n",
" [0, 1, 0, 0 ],\n",
" [0, 0, 1, 0 ]]\n",
"\n",
"C_1 = [[σ],\n",
" [0],\n",
" [0],\n",
" [0]]\n",
"\n",
"G_1 = [1, 0, 0, 0]\n",
"\n",
"plot_lss(A_1, C_1, G_1, n=4, ts_length=200)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Vector Autoregressions\n",
"\n",
"\n",
"\n",
"Now suppose that\n",
"\n",
"- $ y_t $ is a $ k \\times 1 $ vector \n",
"- $ \\phi_j $ is a $ k \\times k $ matrix and \n",
"- $ w_t $ is $ k \\times 1 $ \n",
"\n",
"\n",
"Then [(3)](#equation-eq-ar-rep) is termed a *vector autoregression*.\n",
"\n",
"To map this into [(1)](#equation-st-space-rep), we set\n",
"\n",
"$$\n",
"x_t =\n",
"\\begin{bmatrix}\n",
" y_t \\\\\n",
" y_{t-1} \\\\\n",
" y_{t-2} \\\\\n",
" y_{t-3}\n",
" \\end{bmatrix}\n",
"\\quad\n",
"A =\n",
"\\begin{bmatrix}\n",
"\\phi_1 & \\phi_2 & \\phi_3 & \\phi_4 \\\\\n",
"I & 0 & 0 & 0 \\\\\n",
"0 & I & 0 & 0 \\\\\n",
"0 & 0 & I & 0\n",
"\\end{bmatrix}\n",
"\\quad\n",
"C =\n",
"\\begin{bmatrix}\n",
" \\sigma \\\\\n",
" 0 \\\\\n",
" 0 \\\\\n",
" 0\n",
" \\end{bmatrix}\n",
"\\quad\n",
"G =\n",
"\\begin{bmatrix}\n",
" I & 0 & 0 & 0\n",
" \\end{bmatrix}\n",
"$$\n",
"\n",
"where $ I $ is the $ k \\times k $ identity matrix and $ \\sigma $ is a $ k \\times k $ matrix."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Seasonals\n",
"\n",
"\n",
"\n",
"We can use [(1)](#equation-st-space-rep) to represent\n",
"\n",
"1. the *deterministic seasonal* $ y_t = y_{t-4} $ \n",
"1. the *indeterministic seasonal* $ y_t = \\phi_4 y_{t-4} + w_t $ \n",
"\n",
"\n",
"In fact, both are special cases of [(3)](#equation-eq-ar-rep).\n",
"\n",
"With the deterministic seasonal, the transition matrix becomes\n",
"\n",
"$$\n",
"A = \\begin{bmatrix}\n",
" 0 & 0 & 0 & 1 \\\\\n",
" 1 & 0 & 0 & 0 \\\\\n",
" 0 & 1 & 0 & 0 \\\\\n",
" 0 & 0 & 1 & 0\n",
" \\end{bmatrix}\n",
"$$\n",
"\n",
"It is easy to check that $ A^4 = I $, which implies that $ x_t $ is strictly periodic with period 4:**[1]** The eigenvalues of $ A $ are $ (1,-1, i,-i) $.\n",
"\n",
"

**[2]** The correct way to argue this is by induction. Suppose that\n",
"$ x_t $ is Gaussian. Then [(1)](#equation-st-space-rep) and\n",
"[(10)](#equation-lss-glig) imply that $ x_{t+1} $ is Gaussian. Since $ x_0 $\n",
"is assumed to be Gaussian, it follows that every $ x_t $ is Gaussian.\n",
"Evidently, this implies that each $ y_t $ is Gaussian."
]
}
],
"metadata": {
"date": 1595224865.3063483,
"filename": "linear_models.rst",
"kernelspec": {
"display_name": "Python",
"language": "python3",
"name": "python3"
},
"next_doc": {
"link": "samuelson",
"title": "Application: The Samuelson Multiplier-Accelerator"
},
"prev_doc": {
"link": "inventory_dynamics",
"title": "Inventory Dynamics"
},
"title": "Linear State Space Models"
},
"nbformat": 4,
"nbformat_minor": 2
}