From f475d9e2ed097f6345597d713f2ccd89e5c8bf42 Mon Sep 17 00:00:00 2001 From: Alfred Galichon Date: Mon, 9 Dec 2024 13:14:55 +0100 Subject: [PATCH] v__0.190 --- mec/mx.py | 26 +++++++++++++++++++++----- setup.py | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/mec/mx.py b/mec/mx.py index 1693744..066a28b 100644 --- a/mec/mx.py +++ b/mec/mx.py @@ -3,6 +3,27 @@ import numpy as np, scipy.sparse as sp, pandas as pd +def ivgmm(Y_i,X_i_k,Z_i_l,W_l_l=None): + I = len(Y_i) + if W_l_l is None: + W_l_l = np.linalg.inv( Z_i_l.T @ Z_i_l / I ) + Pi_i_i = Z_i_l @ W_l_l @ Z_i_l.T + beta_k = np.linalg.solve(X_i_k.T @ Pi_i_i @ X_i_k, X_i_k.T @ Pi_i_i @ Y_i) + val = (Y_i - X_i_k @ beta_k).T @ Pi_i_i @ (Y_i - X_i_k @ beta_k) / (2* I * I) + return beta_k,val + +def efficient_ivgmm(Y_i,X_i_k,Z_i_l, centering = True): + I=len(Y_i) + beta1_k = ivgmm(Y_i,X_i_k,Z_i_l )[0] # first stage obtained by 2SLS + epsilon1_i = Y_i - X_i_k @ beta1_k + mhat_l_i = Z_i_l.T * epsilon1_i[None,:] + mbar_l = mhat_l_i.mean(axis=1) + Sigmahat1_l_l = (mhat_l_i @ mhat_l_i.T) / I - centering * mbar_l[:,None] * mbar_l[None,:] + W_l_l = np.linalg.inv(Sigmahat1_l_l) + beta2_k, val2 = ivgmm(Y_i,X_i_k,Z_i_l,W_l_l ) + return beta2_k,val2 + + def iv_gmm(Y_i,X_i_k,Z_i_l, efficient=False, centering = True): def beta_gmm(Y_i,X_i_k,Z_i_l,W_l_l=None ): ZtildeT_k_i = (X_i_k.T @ Z_i_l @ Z_i_l.T) if W_l_l is None else (X_i_k.T @ Z_i_l @ W_l_l @ Z_i_l.T) @@ -21,8 +42,3 @@ def beta_gmm(Y_i,X_i_k,Z_i_l,W_l_l=None ): XPiY_k_i = X_i_k.T @ Pi_i_i @ Y_i objval = (Y_i.T @ Pi_i_i @ Y_i - XPiY_k_i.T @ np.linalg.inv( X_i_k.T @ Pi_i_i @ X_i_k ) @ XPiY_k_i )/ (2*I*I) return beta_k,objval - - - - - \ No newline at end of file diff --git a/setup.py b/setup.py index 2072736..49d21bc 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="mec", - version="0.189", + version="0.190", authors=["Alfred Galichon"], author_email="ag133@nyu.edu", licence="",