Skip to content

Commit

Permalink
v__0.191
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredgalichon committed Dec 17, 2024
1 parent 6ce3d48 commit 3b07e44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions mec/mx.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ def efficient_ivgmm(Y_i,X_i_k,Z_i_l, centering = True):
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)
return np.linalg.solve(ZtildeT_k_i @ X_i_k,ZtildeT_k_i @ Y_i)
I=len(Y_i)
W_l_l = np.linalg.inv( Z_i_l.T @ Z_i_l / I)
beta_k = beta_gmm(Y_i,X_i_k,Z_i_l,W_l_l ) # first stage obtained by 2SLS
if efficient:
epsilon_i = Y_i - X_i_k @ beta_k
mhat_l_i = Z_i_l.T * epsilon_i[None,:]
mbar_l = mhat_l_i.mean(axis=1)
Sigmahat_l_l = (mhat_l_i @ mhat_l_i.T) / I - centering * mbar_l[:,None] * mbar_l[None,:]
W_l_l = np.linalg.inv(Sigmahat_l_l)
beta_k = beta_gmm(Y_i,X_i_k,Z_i_l,W_l_l )
Pi_i_i = Z_i_l @ W_l_l @ Z_i_l.T
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
# 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)
# return np.linalg.solve(ZtildeT_k_i @ X_i_k,ZtildeT_k_i @ Y_i)
# I=len(Y_i)
# W_l_l = np.linalg.inv( Z_i_l.T @ Z_i_l / I)
# beta_k = beta_gmm(Y_i,X_i_k,Z_i_l,W_l_l ) # first stage obtained by 2SLS
# if efficient:
# epsilon_i = Y_i - X_i_k @ beta_k
# mhat_l_i = Z_i_l.T * epsilon_i[None,:]
# mbar_l = mhat_l_i.mean(axis=1)
# Sigmahat_l_l = (mhat_l_i @ mhat_l_i.T) / I - centering * mbar_l[:,None] * mbar_l[None,:]
# W_l_l = np.linalg.inv(Sigmahat_l_l)
# beta_k = beta_gmm(Y_i,X_i_k,Z_i_l,W_l_l )
# Pi_i_i = Z_i_l @ W_l_l @ Z_i_l.T
# 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="mec",
version="0.190",
version="0.191",
authors=["Alfred Galichon"],
author_email="ag133@nyu.edu",
licence="",
Expand Down

0 comments on commit 3b07e44

Please sign in to comment.