Skip to content

Repository files navigation

GMMick

A Python library for Generalized Method of Moments (GMM) estimation using JAX.

GMMick = GMM for (i)deally (c)ontinuous (k)ernels — the name reflects the library's design: by leveraging JAX's automatic differentiation, GMMick works best with moment functions that are smooth and differentiable with respect to parameters.

GMMick

Installation

pip install jax jaxlib numpy scipy

Quick Start

from gmmick import GMM2S, compute_standard_errors, hansen_j_test_optimal
import jax.numpy as jnp

# Define your moment conditions
def moment_function(theta, data):
    # Returns (N, K) matrix of moment conditions
    ...

# Estimate parameters
result, W_opt = GMM2S(theta_init, data, moment_function)
print("Estimates:", result.x)

# Compute standard errors
se, var_theta = compute_standard_errors(result.x, data, W_opt, moment_function)

# Test overidentifying restrictions
J_stat, df, p_value = hansen_j_test_optimal(result.x, data, W_opt, moment_function)

GMM Overview

What is GMM?

The Generalized Method of Moments (GMM) is an estimation method that uses moment conditions to identify parameters. Given a parameter vector θ and data, we specify K moment conditions that should equal zero at the true parameter value:

$$\mathbb{E}[g(x_i, \theta_0)] = 0$$

where $g(x_i, \theta)$ returns a K-dimensional vector for each observation.

The GMM Estimator

The GMM estimator minimizes a quadratic form of the sample moments:

$$\hat{\theta} = \arg\min_\theta , \bar{g}(\theta)' , W , \bar{g}(\theta)$$

where $\bar{g}(\theta) = \frac{1}{N}\sum_{i=1}^N g(x_i, \theta)$ is the sample average of moments and $W$ is a positive definite weighting matrix.

Why Two-Stage GMM?

The choice of weighting matrix $W$ affects efficiency:

  1. First stage: Use $W = I$ (identity matrix). This gives consistent but inefficient estimates.

  2. Optimal weighting: The efficient GMM uses $W^* = S^{-1}$, where $S$ is the variance of the moments: $$S = \text{Var}(g_i) = \mathbb{E}[g_i g_i'] - \mathbb{E}[g_i]\mathbb{E}[g_i']$$

  3. Second stage: Re-estimate using $W^*$ computed from first-stage residuals.

This two-stage procedure yields the efficient GMM estimator with minimal asymptotic variance.

Design Philosophy

Moment Function Pattern

Users provide a moment_function(theta, data) that returns an $(N, K)$ matrix:

  • Each row $i$ contains the K moment conditions evaluated at observation $i$
  • Moments should satisfy $\mathbb{E}[g_i(\theta_0)] = 0$ at the true parameter
@jax.jit
def my_moments(theta, data):
    x = data[:, 0]
    z = data[:, 1]
    theta0, theta1 = theta

    m1 = x - theta0           # E[x] = theta0
    m2 = z**2 - theta1        # E[z^2] = theta1

    return jnp.column_stack((m1, m2))

Why JAX?

GMMick uses JAX for:

  • JIT compilation: Fast execution through just-in-time compilation
  • Automatic differentiation: Compute Jacobians for standard errors without manual derivatives
  • Functional style: Clean, composable code that works well with optimization

Moment functions should be JAX-compatible (use jax.numpy instead of numpy).

API Reference

GMM2S(theta_init, data, moment_function, gtol=1e-12, maxiter=None)

Two-stage GMM estimation.

Parameters:

  • theta_init: Initial parameter guess
  • data: Data array of shape (N, M)
  • moment_function: Function returning (N, K) moment matrix
  • gtol: Gradient tolerance (default: 1e-12)
  • maxiter: Maximum iterations (optional)

Returns:

  • result: Optimization result with result.x containing estimates
  • W_hat: Optimal weighting matrix

compute_standard_errors(theta, data, W, moment_function)

Compute asymptotic standard errors using the sandwich formula.

Returns:

  • se: Standard errors for each parameter
  • var_theta: Full variance-covariance matrix

hansen_j_test_optimal(theta, data, W, moment_function)

Hansen's J-test for overidentifying restrictions.

Returns:

  • J_stat: Test statistic (asymptotically $\chi^2_{K-p}$)
  • df: Degrees of freedom (K - p)
  • p_value: P-value under the null of correct specification

Examples

Basic Example

See example.ipynb for a complete worked example with:

  • Data generation
  • Moment specification
  • Estimation and inference
  • Hypothesis testing

Advanced Examples

See examples_advanced.ipynb for more sophisticated applications:

  • Instrumental Variables Regression: Handling endogeneity with GMM
  • Distribution Parameter Estimation: Fitting Gamma distribution parameters via method of moments

References

  • Hansen, L. P. (1982). "Large Sample Properties of Generalized Method of Moments Estimators." Econometrica, 50(4), 1029-1054.
  • Hayashi, F. (2000). Econometrics. Princeton University Press. Chapter 3.
  • Wooldridge, J. M. (2010). Econometric Analysis of Cross Section and Panel Data. MIT Press. Chapter 14.
  • JAX Documentation

License

MIT

About

GMMick is a GMM library for (ideally) continuous kernels using JAX

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages