In [ ]:
import requests
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline

Class 7: Deterministic Time Series Models

Time series models are at the foundatation of dynamic macroeconomic theory. A time series model is an equation or system of equations that describes how the variables in the model change with time. Here, we examine some theory about deterministic, i.e., non-random, time series models and we explore methods for simulating them. Leter, we'll examine the properties of stochastic time series models by introducing random variables to the discrete time models covered below.

Discrete Versus Continuous Time

To begin, suppose that we are interested in a variable $y$ that takes on the value $y_t$ at date $t$. The date index $t$ is a real number. We'll say that $y_t$ is a discrete time variable if $t$ takes on values from a countable sequence; e.g. $t = 1, 2, 3 \ldots$ and so on. Otherwise, if $t$ takes on values from an uncountable sequence; e.g. $t\in[0,\infty)$, then we'll say that $y_t$ is a continuous time variable. Discrete and continuous time models both have important places in macroeconomic theory, but we're going to focus on understanding discrete time models.

First-Order Difference Equations

Now, suppose that the variable $y_t$ is determined by a linear function of $y_{t-1}$ and some other exogenously given variable $w_t$

\begin{align} y_{t} & = (1- \rho) \mu + \rho y_{t-1} + w_t, \tag{1}\\ \end{align}

where $\rho$ and $\mu$ are constants. Equation (1) is an example of a linear first-order difference equation. As a difference equation, it specifies how $y_t$ is related to past values of $y$. The equation is a first-order difference equation because it specifies that $y_t$ depends only on $y_{t-1}$ and not $y_{t-2}$ or $y_{t-3}$.

Example: Compounding Interest

Suppose that you have an initial balance of $b_0$ dollars in a savings account that pays an interest rate $i$ per compounding period. Then, after the first compounding, your account will have $b_1 = (1+i)b_0$ dollars in it. Assuming that you never withdraw funds from the account, then your account balance in any subsequent period $t$ is given by the following difference equation:

\begin{align} b_{t} & = \left(1+i\right) b_{t-1}. \tag{2} \end{align}

Equation (2) is linear first-order difference equation in the same form as Equation (1). You can see this by setting $y_t = b_t$, $\rho=1+i$, $\mu=0$, and $w_t=0$ in Equation (1).

Example: Capital Accumulation

Let $K_t$ denote the amont of physical capital in a country at date $t$, let $\delta$ denote the rate at which the capital stock depreciates each period, and let $I_t$ denote the country's investment in new capital in date $t$. Then the law of motion for the stock of physical capital is:

\begin{align} K_{t+1} & = I_t + (1-\delta)K_t. \tag{3} \end{align}

This standard expression for the law of motion for the capital stock is a linear first-order difference equation. To reconcile Equation (3) with Equation (1), set $y_t = K_{t+1}$, $\rho=1-\delta$, $\mu=0$, and $w_t=I_t$.

Note: There is a potentially confusing way in which we identified the $t+1$-dated variable $K_{t+1}$ with the $t$-dated variable $y_t$ in this example. We can do this because the value of $K_{t+1}$ truly is determined at date $t$ even though the capital isn't used for production until the next period.

Computation

From Equation (1), it's easy to compute the value of $y_t$ as long as you know the values of the constants $\rho$ and $\mu$ and the variables $y_{t-1}$ and $w_t$. To begin, let's suppose that the values of the constants are $\mu=0$, $\rho=0.5$. Then Equation (1) in our example looks like this:

\begin{align} y_{t} & = 0.5 y_{t-1} + w_t. \tag{4}\\ \end{align}

Now, suppose that the initial value of $y$ is $y_0=0$ and that $w$ is equal to 1 in the first period and equal to zero in subsequent periods. That is: $w_1=1$ and $w_2=w_3=\cdots =0$. Now, with what we have, we can compute $y_1$. Here's how:


In [ ]:
# Initialize variables: y0, rho, w1


# Compute the period 1 value of y


# Print the result

The variable y1 in the preceding example stores the computed value for $y_1$. We can continue to iterate on Equation (4) to compute $y_2$, $y_3$, and so on. For example:


In [ ]:
# Initialize w2


# Compute the period 2 value of y


# Print the result

We can do this as many times as necessary to reach the desired value of $t$. Note that iteration is necesary. Even though $y_t$ is apparently a function of $t$, we could not, for example, compute $y_{20}$ directly. Rather we'd have to compute $y_1, y_2, y_3, \ldots, y_{19}$ first. The linear first-order difference equation is an example of a recursive model and iteration is necessary for computing recursive models in general.

Of course, there is a better way. Let's define a function called diff1_example()that takes as arguments $\rho$, an array of values for $w$, and $y_0$.


In [ ]:
# Initialize the variables T and w


# Define a function that returns an arrary of y-values given rho, y0, and an array of w values.

Exercise:

Use the function diff1_example() to make a $2\times2$ grid of plots just like the previous exercise but with with $\rho = 0.5$, $-0.5$, $1$, and $1.25$. For each, set $T = 10$, $y_0 = 1$, $w_0 = 1$, and $w_1 = w_2 = \cdots 0$.


In [ ]:
fig = plt.figure(figsize=(12,8))

ax1 = fig.add_subplot(2,2,1)
y = diff1_example(0.5,w,0)
ax1.plot(y,'-',lw=5,alpha = 0.75)
ax1.set_title('$\\rho=0.5$')
ax1.set_ylabel('y')
ax1.set_xlabel('t')
ax1.grid()

Exercise:

Use the function diff1_example() to make a single plot with 4 lines reflecting $\rho = 0.25$, $0.5$, $0.75$, and $0.95$. As before, $y_0 = 1$, $w_0 = 1$, and $w_1 = w_2 = \cdots 0$ for each but this time set $T=20$. Add a legend to clearly identify which line has which $\rho$ value.


In [ ]: