Skip to content

Commit

Permalink
v__0.190
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredgalichon committed Dec 9, 2024
1 parent b993b95 commit f475d9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 21 additions & 5 deletions mec/mx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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





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.189",
version="0.190",
authors=["Alfred Galichon"],
author_email="ag133@nyu.edu",
licence="",
Expand Down

0 comments on commit f475d9e

Please sign in to comment.