Search code examples
pythonstatisticsstatsmodelslinear-optimization

Choices for optimization method with generalized method of moments (GMM) in Python


How do I find out the options for optim_method='' with the generalized method of moments in statsmodels? I've been told by a colleague that simplex method is good for my problem. How do I find out its name, and the names of other available optimizer methods? I get a ValueError: Optimizer method not available when I put in 'simplex'.

import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels.sandbox.regression.gmm import GMM

rand_array = np.random.rand(150, 7)
yvar=rand_array[:, [0,1]]
zvar=rand_array[:, [2,3,4]]
xvar=rand_array[:, [5,6]]
xvar=sm.add_constant(xvar)

class GMMREM (GMM):

    def momcond(self, params):
        b0, b1, b2, b3, b4,m0, m1, m2, m3 = params
        x = self.exog
        z = self.instrument
        y=  self.endog

        error1 = x[:,0]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
        error2 = y[:,1]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
        error3 = z[:,0]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
        error4 = z[:,1]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
        error5 = x[:,1]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])

        error6= z[:,2]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1])

        error7 = x[:,0]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2]-m3*x[:,1])
        error8 = y[:,0]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2]-m3*x[:,1])
        error9 = z[:,2]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2]-m3*x[:,1])
        error10= x[:,1]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2]-m3*x[:,1])
   
        error11 = z[:,0]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2])-m3*x[:,1]
        error12= z[:,1]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2])-m3*x[:,1]
    
        return np.column_stack((error1, error2, error3, error4, error5, error6, error7, error8, error9, error10, error11, error12))

model1 = GMMREM(yvar, xvar, zvar, k_moms=12, k_params=9)
b0 = [.01, .08, -.02, 0, 0,.01, 1.7, .05, 0]
res1 = model1.fit(b0, maxiter=100, optim_method='simplex', wargs=dict(centered=False))
print(res1.summary())

Solution

  • Only a selection of scipy optimizers are available in GMM fit.
    Here is the list in the code https://github.com/statsmodels/statsmodels/blob/main/statsmodels/sandbox/regression/gmm.py#L738

    GMM.fit does not go through inherited LikelihoodModel.fit, so not all scipy optimizers are available.

    Note: Generic GMM requires a general nonlinear optimizer for smooth functions, which are available in scipy.optimize. Only GMM for linear modesl, LinearIVGMM, has a special fit method that uses linear algebra for an explicit solution.