Skip to content Skip to sidebar Skip to footer

Issue With Using Statsmodels.sandbox.regression.gmm.gmm

I wanna estimate interest rate process using gmm. So, I referenced a this code. https://github.com/josef-pkt/misc/blob/master/notebooks/ex_gmm_gamma.ipynb and following is my cod

Solution 1:

The shape problem is because exog is a column array (vector) and the indexed instrument is 1-D which broadcasts to the 80 columns. I added a squeeze to exog, so that exog is also 1-D

The second problem is that there is a typo in the index of the instrument for moment condition 3, which should use error3 = (endog - p0 - p1 * exog) * inst[:,1] After fixing the shape problem, the fit raises a LinalgError because error1 and error3 were the same.

It works for me after making these two changes, but I don't know whether the estimated parameters make sense in the application.

cd = np.array([1.5, 1.5, 1.7, 2.2, 2.0, 1.8, 1.8, 2.2, 1.9, 1.6, 1.8, 2.2, 2.0, 1.5, 1.1, 1.5, 1.4, 1.7, 1.42, 1.9])
dcd = np.array([0, 0.2 ,0.5, -0.2, -0.2, 0, 0.4, -0.3, -0.3, 0.2, 0.4, -0.2, -0.5, -0.4, 0.4, -0.1, 0.3, -0.28, 0.48, 0.2])
inst = np.column_stack((np.ones(len(cd)), cd))

classgmm(GMM):
    def momcond(self, params):
        p0, p1, p2, p3 = paramsendog= self.endogexog= self.exog.squeeze()
        inst = self.instrumenterror1= endog - p0 - p1 * exogerror2= (endog - p0 - p1 * exog) ** 2 - p2 * (exog ** (2 * p3)) / 12
        error3 = (endog - p0 - p1 * exog) * inst[:,1]
        error4 = ((endog - p0 - p1 * exog) ** 2 - p2 * (exog ** (2 * p3)) / 12) * inst[:,1]
        g = np.column_stack((error1, error2, error3, error4))
        returngbeta0= np.array([0.1, 0.1, 0.01, 1])
res = gmm(endog = dcd, exog = cd, instrument = inst, k_moms=4, k_params=4).fit(beta0)

There is a bug in GMM for summary which is based on an incorrect and too short list of parameter names. We can override the parameter names, then summary works

res.model.exog_names[:]='p0 p1 p2 p3'.split()print(res.summary())gmmResults==============================================================================Dep. Variable:                      y   Hansen J:1.487e-10Model:gmmProb(HansenJ):nanMethod:GMMDate:Wed,14Mar2018Time:09:38:38No. Observations:20==============================================================================coefstderrzP>|z|      [0.0250.975]
------------------------------------------------------------------------------p00.98900.2434.0780.0000.5141.464p1-0.55240.129-4.2810.000-0.805-0.299p21.22240.9401.3000.193-0.6203.065p3-0.33760.641-0.5270.598-1.5930.918==============================================================================

Extra

In the corrected version the constant in the instrument is not used anymore. So it could be removed, or the moment conditions could be vectorized in instruments as in the following. Note, I convert endog to 2-d column array, so it matches the shape of exog and instruments.

classgmm(GMM):
    def momcond(self, params):
        p0, p1, p2, p3 = paramsendog= self.endog[:, None]
        exog = self.exoginst= self.instrumenterror3= (endog - p0 - p1 * exog) * insterror4= ((endog - p0 - p1 * exog) ** 2 - p2 * (exog ** (2 * p3)) / 12) * instg= np.column_stack((error3, error4))
        returngbeta0= np.array([0.1, 0.1, 0.01, 1])
res = gmm(endog = dcd, exog = cd, instrument = inst, k_moms=4, k_params=4).fit(beta0)
res.model.exog_names[:] = 'p0 p1 p2 p3'.split()
print(res.summary())

Debugging

We can check that the user provided moment conditions have the correct shape but just creating the model instance and calling momcond

mod = gmm(endog = dcd, exog = cd, instrument = inst, k_moms=4, k_params=4)
mod.momcond(beta0).shape

Post a Comment for "Issue With Using Statsmodels.sandbox.regression.gmm.gmm"