We are trying to write a Generalizd Method of Moments estimation framework in Python, and use it to estimate an ARMA(2,1) time series.

## Model Description

Assume that now we have already determined the form of a time series model. It is

$X_t=\phi_1X_{t-1}+\phi_2X_{t-2}+\epsilon_t+\theta_1\epsilon_{t-1},\quad \epsilon_t\sim(0,1)$

with at least 3 kinds of moment conditions given by

1. The expectation residual: $E(\epsilon_t)=0$
2. The variance of the residual: $E(\epsilon_t^2)=1$
3. The covariance between $\epsilon_s$ and $\epsilon_t$ ($\forall s<t$): $Cov(\epsilon_s\epsilon_t)=0,\quad\forall s<t$

Now, assume we have a observed sample $x$ of the population $T$. Let the sample moment matrix be

$g(para, x)= \begin{bmatrix} g_1(para,x_1) & g_1(para,x_2) & g_1(para,x_T)\\ g_2(para,x_1) & g_2(para,x_2) & g_2(para,x_T)\\ \vdots & \vdots & \vdots\\ g_k(para,x_1) & g_k(para,x_2) & g_k(para,x_T)\\ \end{bmatrix}$

where $k$ is the number of moment conditions. We define the sample mean of $g$ as $g_m(para, x_t)$:

$g_m(para)=\dfrac{1}{T}\sum_{t=1}^Tg(para,x_t)$

Now, starting with the weight matrix $W\_1=\mathbf{I}\_{k\times k}$ we can proceed the iterated GMM estimation method as follows:

1. Minimize the objective function

$J(para)=Tg_m(para)^{\prime}W_ng_m(para)$

and then solve for $\widehat{para}_n$, that is

$\widehat{para}_n=\underset{para}{\text{argmin }}J$

1. Recompute $W_n$ based on the sample covariance of $g(\widehat{para}_n,x)$

$S_n=\dfrac{1}{T}\sum_{t=1}^Tg(\widehat{para}_n,x)g(\widehat{para}_n,x)^{\prime}$

$W_{n+1}=S_n^{-1}$

1. Repeat step 1 and 2 until

$\left\Vert\widehat{para}_{n+1}-\widehat{para}_n\right\Vert<\delta$

where $\delta$ is a given positive value that is very small. We may also terminate the iteration when reach an interation threshold.

## Data Simulation

First we need to import some necessary Python libraries, including - numpy for massive linear operations based on C, - matplotlib for plotting, - scipy for optimizing, and - statsmodels in which there're various built-in statistical models.

Here we define a function called simulate, which as its name implies, simulates a time series data determined by parameters $\phi_1$, $\phi_2$ and $\theta_1$. The initial values are given by init. The first 1/11 of the data are thrown away as burn-in, so that at last the sample size would be T. There is a built-in parameter called plot which is set True or 1 to plot the graph of the data, and False or 0 otherwise.

Now we can set the parameters and store them in separate lists.

We generate a time series dataset of size 5T/4, yet again we "set aside" the last T/4, so that we may compare the prediction of our estimation with the true data.

## GMM Estimation

Then we define a class named gmm, who has 2 major methods, namely fit and result. When we create an object (or model) of the class gmm, we initialize it with moment condition functions momn as well the jacobian of their mean w.r.t. the parameter vector, deriv_momn. When fitting our model to the data, we use the iterated gmm method, i.e. we alternately compute $\widehat{para}_n$ and weight matrix $W_n$, the major step of which undertaken by scipy's minimize function, a brutal minimization method (with SLSQP method using jacobian matrix). The iteration terminates either when the square of the Euclidean distance between two estimators converges within a given value, $\delta$, or it reaches an iteration threshold (set at 10 here).

Remark 1: when iteration threshold is set at 2, this is immediately a 2-step GMM.

Remark 2: we here also estimate the asymptotic variance when fitting to the data. The derivation of this asymptotic variance estimation will be further discussed later.

Below we define the moment condition functions and the derivatives of their mean.

Initialize the gmm model with moment condition functions and the respective partial derivatives, fit it to x, and then print out the result.

                GMM Iteration
==============================================
No. Observations: 20000
==============================================
Iter      para_1        para_2        para_3
----------------------------------------------
2    1.9091e-01    5.6302e-02    8.1460e-01   (1.074866238949949e-08)
---------------------end----------------------

Iterated GMM Results
==============================================
Kind      para_1        para_2        para_3
----------------------------------------------
Estm    1.9091e-01    5.6302e-02    8.1460e-01
True    2.0000e-01    5.0000e-02    8.0000e-01
----------------------------------------------
Avar    1.9905e+01    9.1790e+00    2.5171e+01
----------------------------------------------

As comparison, check the maximum likelihood estimation with the help of statsmodels.

                              ARMA Model Results
==============================================================================
Dep. Variable:                      y   No. Observations:                20000
Model:                     ARMA(2, 1)   Log Likelihood              -28404.025
Method:                       css-mle   S.D. of innovations              1.001
Date:                Mon, 16 Jan 2017   AIC                          56818.050
Time:                        11:55:46   BIC                          56857.568
Sample:                             0   HQIC                         56830.979

==============================================================================
coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
const          0.0026      0.017      0.155      0.876        -0.031     0.036
ar.L1.y        0.1983      0.010     20.706      0.000         0.179     0.217
ar.L2.y        0.0512      0.009      5.672      0.000         0.033     0.069
ma.L1.y        0.8050      0.006    128.761      0.000         0.793     0.817
Roots
=============================================================================
Real           Imaginary           Modulus         Frequency
-----------------------------------------------------------------------------
AR.1            2.8895           +0.0000j            2.8895            0.0000
AR.2           -6.7643           +0.0000j            6.7643            0.5000
MA.1           -1.2423           +0.0000j            1.2423            0.5000
-----------------------------------------------------------------------------

## Theoretical Asymptotic Variances of the GMM Estimator

Some assumptions:

$e_t'g= \begin{bmatrix} x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2}\\ (x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2})^2 - (1 + \theta_1^2)\\ (x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2})(x_{t-1} - \phi_1 x_{t-2} - \phi_2 x_{t-3}) - \theta_1\\ (x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2})(x_{t-2} - \phi_1 x_{t-3} - \phi_2 x_{t-4})\\ \end{bmatrix}= \begin{bmatrix} \epsilon_t+\theta_1\epsilon_{t-1}\\ (\epsilon_t+\theta_1\epsilon_{t-1})^2-(1+\theta_1^2)\\ (\epsilon_t+\theta_1\epsilon_{t-1})(\epsilon_{t-1}+\theta_1\epsilon_{t-2})-\theta_1\\ (\epsilon_t+\theta_1\epsilon_{t-1})(\epsilon_{t-2}+\theta_1\epsilon_{t-3})\\ \end{bmatrix}$

and

$E(e_t'g)= \begin{bmatrix} 0\\ 0\\ 0\\ 0\\ \end{bmatrix}$

where $e_t\in\mathbb{R}^{T\times1}$ is the $t^th$ basis vector. Hence,

$\frac{\partial e_t'g}{\partial\beta^{\prime}}= \begin{bmatrix} \mathcal{E}_1 & \mathcal{E}_2 & \mathcal{E}_3 \end{bmatrix}$

where

$\mathcal{E}_1 = \begin{bmatrix} -x_{t-1} \\ -2x_{t-1}(x_t-\phi_1 x_{t-1}-\phi_2 x_{t-2}) \\ -x_{t-1}(x_{t-1} - \phi_1 x_{t-2} - \phi_2 x_{t-3}) - x_{t-2}(x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2})\\ -x_{t-1}(x_{t-2} - \phi_1 x_{t-3} - \phi_2 x_{t-4}) - x_{t-3}(x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2}) \end{bmatrix},$

$\mathcal{E}_2 = \begin{bmatrix} -x_{t-2} \\ -2x_{t-2}(x_t-\phi_1 x_{t-1}-\phi_2 x_{t-2}) \\ -x_{t-2}(x_{t-1} - \phi_1 x_{t-2} - \phi_2 x_{t-3}) - x_{t-3}(x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2}) \\ -x_{t-2}(x_{t-2} - \phi_1 x_{t-3} - \phi_2 x_{t-4}) - x_{t-4}(x_t - \phi_1 x_{t-1} - \phi_2 x_{t-2}) \end{bmatrix}$

and

$\mathcal{E}_3 = \begin{bmatrix} 0\\ -2\theta_1\\ -1\\ 0 \end{bmatrix}.$

Let

$G=E\left(\dfrac{\partial e_t'g}{\partial \beta^{\prime}}\right)= \begin{bmatrix} -\mu & -\mu & 0\\ 2\phi_1\gamma_0+2(\phi_2-1)\gamma_1 & 2\phi_2\gamma_0+2\phi_1\gamma_1-2\gamma_2 & -2\theta_1\\ (\phi_2-1)\gamma_0+2\phi_1\gamma_1+(\phi_2-1)\gamma_2 & \phi_1\gamma_0+(2\phi_2-1)\gamma_1+\phi_1\gamma_2-\gamma_3 & -1\\ (\phi_2-1)\gamma_1+2\phi_1\gamma_2+(\phi_2-1)\gamma_3 & -\gamma_0+\phi_1\gamma_1+2\phi_2\gamma_2+\phi_1\gamma_3-\gamma_4 & 0 \end{bmatrix}.$

Since

$\mu = 0$

and the autocovariances $\gamma_i$ follows

$\begin{cases} \gamma_0-\phi_1\gamma_1-\phi_2\gamma_2=\theta_1(\phi_1+\theta_1)+1\\ \phi_1\gamma_0+(\phi_2-1)\gamma_1=-\theta_1\\ \gamma_k = \phi_1\gamma_{k-1} + \phi_2\gamma_{k-2},\quad k\ge 2 \end{cases}$

we may derive $G$ at last.

Also, the covariance matrix of $e_t'g$ is

$S=E(e_t'gg'e_t)= \begin{bmatrix} 1+\theta_1^2 & 0 & 0 & 0\\ 0 & 2\theta_1^4+4\theta_1^2+2 & 2\theta_1^3+2\theta_1 & 0\\ 0 & 2\theta_1^3+2\theta_1 & \theta_1^4+3\theta_1^2+1 & \theta_1^3+\theta_1\\ 0 & 0 & \theta_1^3+\theta_1 & \theta_1^4+2\theta_1^2+1\\ \end{bmatrix}$

Now with $G$ and $S$, since

$\sqrt{T}\ (\widehat{para} - para)\overset{d}{\to}\mathcal{N}(0,(G'S^{-1}G)^{-1}),$

we may derive the theoretical asymptotic variances of $\sqrt{T}\ \widehat{para}$ as follows.

array([[ 0. ,  0. ,  0. ],
[-1.6, -0. , -1.6],
[-1.8, -0.8, -1. ],
[-1.2, -1.8,  0. ]])
array([[ 1.64,  0.  ,  0.  ,  0.  ],
[ 0.  ,  5.38,  2.62,  0.  ],
[ 0.  ,  2.62,  3.33,  1.31],
[ 0.  ,  0.  ,  1.31,  2.69]])
[21.3174392361111, 9.980590277777774, 26.878064236111097]
[19.905472617664184, 9.178999415080208, 25.170769966198534]

The estimated asymptotic variance is quite close to the theory.

## Monte Carlo Simulation

Below we use the true parameters to simulate $N=10000$ times and each time we estimate a $\widehat{para}_n$.

100% |░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| Finished.

[4.422402990203701, 2.9904249354938632, 8.796234771820194]

As we can see, the sample variance of the Monte Carlo simulated distribution of our estimator, is significantly smaller than the theoretical one. This should be a problem.

# References:

• Hall, Alastair R. Generalized method of moments. Oxford University Press, 2005.
• Hall, Alastair R. "Hypothesis testing in models estimated by GMM." Generalized method of moments estimation 5 (1999): 96.
• Hansen, Lars Peter. "Large sample properties of generalized method of moments estimators." Econometrica: Journal of the Econometric Society (1982): 1029-1054.
• Wooldridge, Jeffrey M. "Applications of generalized method of moments estimation." The Journal of Economic Perspectives 15.4 (2001): 87-100.