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.
pip install jax jaxlib numpy scipyfrom 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)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:
where
The GMM estimator minimizes a quadratic form of the sample moments:
where
The choice of weighting matrix
-
First stage: Use
$W = I$ (identity matrix). This gives consistent but inefficient estimates. -
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']$$ -
Second stage: Re-estimate using
$W^*$ computed from first-stage residuals.
This two-stage procedure yields the efficient GMM estimator with minimal asymptotic variance.
Users provide a moment_function(theta, data) that returns an
- 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))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).
Two-stage GMM estimation.
Parameters:
theta_init: Initial parameter guessdata: Data array of shape (N, M)moment_function: Function returning (N, K) moment matrixgtol: Gradient tolerance (default: 1e-12)maxiter: Maximum iterations (optional)
Returns:
result: Optimization result withresult.xcontaining estimatesW_hat: Optimal weighting matrix
Compute asymptotic standard errors using the sandwich formula.
Returns:
se: Standard errors for each parametervar_theta: Full variance-covariance matrix
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
See example.ipynb for a complete worked example with:
- Data generation
- Moment specification
- Estimation and inference
- Hypothesis testing
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
- 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
MIT
