from _api_doc_utils import *GMM
Callback-driven generalized method of moments
1 Where it fits
Group: Estimation interfaces
GMM solves moment restrictions of the form
\[ \mathbb E[g_i(\theta)] = 0. \]
The user supplies a Python callback returning the per-observation moment matrix. In exactly identified cases the class can solve by Gauss-Newton; in overidentified cases it can use identity or two-step weighting and report sandwich covariance estimates.
2 Python API
Constructor: cm.GMM
Construct with GMM(moment_fn, jacobian_fn=None, max_iterations=100, tolerance=1e-6, ridge=1e-8, fd_eps=1e-6). fit(data, theta0, weighting='auto') stores the fitted parameters. fit_sketch(...) row-sketches the moment problem. summary(vcov='sandwich', omega='iid', lags=None, clusters=None) controls inference.
print(inspect.signature(cm.GMM))(moment_fn, jacobian_fn=None, max_iterations=100, tolerance=1e-06, ridge=1e-08, fd_eps=1e-06)
cls = cm.GMM
display(HTML(html_table(["Public method"], public_methods(cls))))| Public method |
|---|
fit(self, /, data, theta0, weighting='auto') |
fit_sketch(self, /, data, theta0, sketch_size, weighting='auto', seed=None) |
summary(self, /, vcov='sandwich', omega='iid', lags=None, clusters=None) |
3 Minimal example
def moments(theta, data):
resid = data["y"] - data["x"] * theta[0]
return data["z"] * resid[:, None]
def jac(theta, data):
return -(data["z"].T @ data["x"][:, None]) / data["x"].shape[0]
rng=np.random.default_rng(20); n=300; z=rng.normal(size=(n,3)); v=rng.normal(size=n); x=z@np.array([.9,.4,-.3])+v; y=1.2*x+.5*v+rng.normal(size=n)*.3
model=cm.GMM(moments, jacobian_fn=jac, max_iterations=200); model.fit({"x":x,"y":y,"z":z}, np.array([0.0]), weighting="identity")
print(model.summary()["coef"])
print(model.summary()["j_stat"])[1.16176096]
0.7350528644433012
4 summary() contract
The table below is generated by fitting the live class in this repository and then inspecting summary(). Shapes are shown because most values are plain NumPy arrays or scalars.
def moments(theta, data):
resid=data['y']-data['x']*theta[0]
return data['z']*resid[:,None]
def jac(theta,data):
return -(data['z'].T@data['x'][:,None])/data['x'].shape[0]
rng=np.random.default_rng(120); n=120; z=rng.normal(size=(n,3)); v=rng.normal(size=n); x=z@np.array([.9,.4,-.3])+v; y=1.2*x+.5*v+rng.normal(size=n)*.3
model=cm.GMM(moments,jacobian_fn=jac,max_iterations=200); model.fit({'x':x,'y':y,'z':z},np.array([0.0]),weighting='identity')
summary = model.summary()
display(HTML(html_table(["summary() key", "shape"], summary_shape_rows(summary))))| summary() key | shape |
|---|---|
coef |
(1,) |
se |
(1,) |
vcov |
(1, 1) |
criterion |
() |
nit |
() |
weighting |
() |
vcov_type |
() |
omega_type |
() |
weight_matrix |
(3, 3) |
nobs |
() |
n_moments |
() |
original_n_moments |
() |
sketch_size |
() |
j_stat |
() |
j_df |
() |