Skip to content

Commit 3712b1c

Browse files
author
zhouxiaoyu
committed
release 0.0.5
1 parent a8a99c3 commit 3712b1c

5 files changed

Lines changed: 489 additions & 169 deletions

File tree

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
FixedEffectModel: A Python Package for Linear Model with High Dimensional Fixed Effects.
22
=======================
3-
<img width="150" src="https://user-images.githubusercontent.com/61887305/126601384-35ab02e9-447c-4977-8d29-aa89397727bd.png" alt="Logo" />
4-
5-
3+
<img width="223" alt="image" src="https://user-images.githubusercontent.com/61887305/154887555-abd074bc-e69d-4793-b850-94a0ce437b01.png">
64

75
[![Downloads](https://pepy.tech/badge/fixedeffectmodel)](https://pepy.tech/project/fixedeffectmodel)
86
[![Downloads](https://pepy.tech/badge/fixedeffectmodel/month)](https://pepy.tech/project/fixedeffectmodel)
@@ -271,6 +269,29 @@ Return an object of results:
271269

272270
Return the same object of results as fixedeffect does.
273271

272+
We also provide two-step GMM estimator if you set thet option "gmm2=True".
273+
Define a matrix <img src="https://latex.codecogs.com/svg.image?P_Z&space;=&space;Z(Z'Z)^{-1}Z'" title="P_Z = Z(Z'Z)^{-1}Z'" />
274+
275+
- "ivgmm", the one-step GMM estimator generate
276+
<img src="https://latex.codecogs.com/svg.image?\begin{equation}\beta_{2SLS}&space;=&space;(X'&space;P_Z&space;X)^{-1}(X'&space;P_Z&space;y)\end{equation}" title="\begin{equation}\beta_{2SLS} = (X' P_Z X)^{-1}(X' P_Z y)\end{equation}" /> with variance-covariance matrices equal
277+
- Unadjusted. Define <img src="https://latex.codecogs.com/svg.image?\sigma_u^2=u'u/df" title="\sigma_u^2=u'u/df" />, the variance-covariance matrix is <img src="https://latex.codecogs.com/svg.image?Var(\beta)=\sigma_u^2[X'Z(Z'Z)^{-1}Z'X]^{-1}" title="Var(\beta)=\sigma_u^2[X'Z(Z'Z)^{-1}Z'X]^{-1}" />
278+
- Heteroskedasticity robust. Define <img src="https://latex.codecogs.com/svg.image?\hat{\Omega}=&space;diag(\hat{u}_1^2,\dotsm,&space;\hat{u}_N^2)" title="\hat{\Omega}= diag(\hat{u}_1^2,\dotsm, \hat{u}_N^2)" /> and <img src="https://latex.codecogs.com/svg.image?W_2&space;=&space;Z'\hat{\Omega}&space;Z" title="W_2 = Z'\hat{\Omega} Z" />
279+
, the variance-covariance matrix is <img src="https://latex.codecogs.com/svg.image?Var_r(\beta)=[X'P_z&space;X]^{-1}[X'Z(Z'Z)^{-1}&space;W_2&space;(Z'Z)^{-1}&space;Z'X][X'&space;P_z&space;X]^{-1}" title="Var_r(\beta)=[X'P_z X]^{-1}[X'Z(Z'Z)^{-1} W_2 (Z'Z)^{-1} Z'X][X' P_z X]^{-1}" />
280+
- Cluster. Deine <img src="https://latex.codecogs.com/svg.image?W_2&space;=&space;\sum_g&space;Z_g'u_g&space;u_g'&space;Z_g" title="W_2 = \sum_g Z_g'u_g u_g' Z_g" />
281+
, the variance-covariance matrix is <img src="https://latex.codecogs.com/svg.image?Var_c(\beta)=[X'P_z&space;X]^{-1}[X'Z(Z'Z)^{-1}&space;W_2&space;(Z'Z)^{-1}&space;Z'X][X'&space;P_z&space;X]^{-1}" title="Var_c(\beta)=[X'P_z X]^{-1}[X'Z(Z'Z)^{-1} W_2 (Z'Z)^{-1} Z'X][X' P_z X]^{-1}" />
282+
- "ivgmm" with "gmm2=True", the two-step GMM estimator generate <img src="https://latex.codecogs.com/svg.image?\begin{equation}\beta_{GMM}&space;=&space;[X'&space;Z&space;W_2^{-1}Z'&space;X]^{-1}[X'&space;Z&space;W_2^{-1}Z'&space;Xy]\end{equation}" title="\begin{equation}\beta_{GMM} = [X' Z W_2^{-1}Z' X]^{-1}[X' Z W_2^{-1}Z' Xy]\end{equation}" />
283+
- Unadjusted. <img src="https://latex.codecogs.com/svg.image?Var(\beta_{GMM})&space;=&space;(X'ZW_2^{-1}Z'X)^{-1}" title="Var(\beta_{GMM}) = (X'ZW_2^{-1}Z'X)^{-1}" />
284+
- Heteroskedasticity robust. Define <img src="https://latex.codecogs.com/svg.image?W_3=Z'\Omega_2&space;Z" title="W_3=Z'\Omega_2 Z" />
285+
and <img src="https://latex.codecogs.com/svg.image?\Omega_2" title="\Omega_2" /> as the
286+
diagonal matrix generated using the residual from the two-step GMM.
287+
, the variance-covariance matrix is <img src="https://latex.codecogs.com/svg.image?\begin{equation}Var_r(\beta_{GMM})=[X'Z(W_2)^{-1}Z'X]^{-1}&space;[X'Z(W_2)^{-1}W_3(W_2)^{-1}Z'X]&space;[X'Z(W_2)^{-1}Z'X]^{-1}\end{equation}" title="\begin{equation}Var_r(\beta_{GMM})=[X'Z(W_2)^{-1}Z'X]^{-1} [X'Z(W_2)^{-1}W_3(W_2)^{-1}Z'X] [X'Z(W_2)^{-1}Z'X]^{-1}\end{equation}" />
288+
- Cluster. Define
289+
290+
<img src="https://latex.codecogs.com/svg.image?\begin{aligned}&W_2&space;=&space;\sum_g&space;Z_g'u_g&space;u_g'&space;Z_g\\&W_3&space;=&space;\sum_g&space;Z_g'u_{2,g}&space;u_{2,g}'&space;Z_g\\\end{aligned}" title="\begin{aligned}&W_2 = \sum_g Z_g'u_g u_g' Z_g\\&W_3 = \sum_g Z_g'u_{2,g} u_{2,g}' Z_g\\\end{aligned}" />
291+
292+
, the variance-covariance matrix is <img src="https://latex.codecogs.com/svg.image?\begin{equation}Var_c(\beta_{GMM})=[X'Z(W_2)^{-1}Z'X]^{-1}&space;[X'Z(W_2)^{-1}W_3(W_2)^{-1}Z'X]&space;[X'Z(W_2)^{-1}Z'X]^{-1}\end{equation}" title="\begin{equation}Var_c(\beta_{GMM})=[X'Z(W_2)^{-1}Z'X]^{-1} [X'Z(W_2)^{-1}W_3(W_2)^{-1}Z'X] [X'Z(W_2)^{-1}Z'X]^{-1}\end{equation}" />.
293+
294+
274295
### DID
275296
*model = did (data_df = None, dependent = None, exog_x = None, treatment = None, csid = None, tsid = None, exp_date = None, group_effect = 'treatment', cluster = None, formula = None, robust = False, noint = False, c_method = 'cgm', psdef = True)*
276297

@@ -370,6 +391,7 @@ result.summary()
370391
ivtest(result)
371392

372393
#------------------------------#
394+
373395
#define fixed effect model
374396
exog_x = ['x_1']
375397
y = ['y']

fixedeffect/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.0.4'
1+
__version__ = '0.0.5'

fixedeffect/iv/iv.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
import pandas as pd
1414
import warnings
1515

16+
1617
class iv2sls:
1718
def __init__(self,
1819
data_df,
19-
dependent = None,
20-
exog_x = None,
21-
endog_x = [],
22-
iv = [],
20+
dependent=None,
21+
exog_x=None,
22+
endog_x=[],
23+
iv=[],
2324
category=[],
2425
cluster=[],
25-
formula = None,
26-
robust = False,
27-
noint = False,
26+
formula=None,
27+
robust=False,
28+
noint=False,
2829
**kwargs
2930
):
3031
"""
@@ -42,6 +43,7 @@ def __init__(self,
4243
**kwargs:some hidden option not supposed to be used by user
4344
"""
4445

46+
warnings.warn('"iv2sls" will no longer be supported beyond version 0.0.5, please use "ivgmm" instead')
4547

4648
# grammar check
4749
if (exog_x is None) & (formula is None):
@@ -59,8 +61,8 @@ def __init__(self,
5961
raise NameError('You have to input endogenous variables for iv2sls')
6062

6163
else:
62-
dependent, exog_x, category_input, cluster_input, endog_x, iv = dependent, exog_x, category, \
63-
cluster, endog_x, iv
64+
dependent, exog_x, category_input, cluster_input, endog_x, iv = dependent, exog_x, category, \
65+
cluster, endog_x, iv
6466

6567
# df preprocess
6668
data_df.fillna(0, inplace=True)
@@ -80,8 +82,8 @@ def __init__(self,
8082
self.orignal_exog_x = orignal_exog_x
8183

8284
def fit(self,
83-
epsilon = 1e-8,
84-
max_iter = 1e6):
85+
epsilon=1e-8,
86+
max_iter=1e6):
8587

8688
data_df = self.data_df
8789
dependent = self.dependent
@@ -115,7 +117,7 @@ def fit(self,
115117
for i in iv:
116118
all_cols.append(i)
117119
all_cols.append(dependent[0])
118-
demeaned_df = demean_dataframe(data_df, all_cols, category_input, epsilon = epsilon, max_iter = max_iter)
120+
demeaned_df = demean_dataframe(data_df, all_cols, category_input, epsilon=epsilon, max_iter=max_iter)
119121
if noint is False:
120122
for i in all_cols:
121123
demeaned_df[i] = demeaned_df[i].add(data_df[i].mean())
@@ -127,9 +129,8 @@ def fit(self,
127129
iv_model = []
128130
iv_result = []
129131
endog_x_hat = []
130-
131132

132-
#----------------- First stage -------------------#
133+
# ----------------- First stage -------------------#
133134
# if OLS on raw data:
134135
if noint is False:
135136
const_x_first_stage = sm.add_constant(demeaned_df[x_first_stage], has_constant='add')
@@ -155,10 +156,10 @@ def fit(self,
155156
x_second_stage = exog_x + endog_x_hat
156157
x_second_stage_original = exog_x + endog_x
157158

158-
#----------------- Second stage -----------------#
159+
# ----------------- Second stage -----------------#
159160
# if OLS on raw data:
160161
if noint is False:
161-
#x_second_stage_ = sm.add_constant(demeaned_df[x_second_stage])
162+
# x_second_stage_ = sm.add_constant(demeaned_df[x_second_stage])
162163
x_first_stage = ['const'] + x_first_stage
163164
x_second_stage = ['const'] + x_second_stage
164165
x_second_stage_original = ['const'] + x_second_stage_original
@@ -173,7 +174,7 @@ def fit(self,
173174
n = demeaned_df.shape[0]
174175
k = len(x_second_stage)
175176

176-
#------ initiate result object ------#
177+
# ------ initiate result object ------#
177178
f_result = OLSFixed()
178179
f_result.model = 'iv2sls'
179180
f_result.dependent = dependent
@@ -197,14 +198,12 @@ def fit(self,
197198
else:
198199
std_error = result.bse * np.sqrt((n - k) / (n - k + k0 - rank)) # for fe if k0=1 need to add it back
199200
f_result.bse = std_error
200-
201201

202202
# compute summary statistics and save in result
203203
self.compute_summary_statistics(result, f_result, rank)
204204

205205
return f_result
206206

207-
208207
def compute_summary_statistics(self,
209208
result,
210209
f_result,
@@ -216,7 +215,6 @@ def compute_summary_statistics(self,
216215

217216
x_second_stage = f_result.x_second_stage
218217

219-
220218
if self.noint is True:
221219
k0 = 0
222220
else:
@@ -247,7 +245,6 @@ def compute_summary_statistics(self,
247245
else:
248246
raise NameError('Total sum of square equal 0, program quit.')
249247

250-
251248
f_result.rsquared_adj = 1 - (len(data_df) - k0) / (result.df_resid) * (1 - f_result.rsquared)
252249

253250
if k0 == 0:
@@ -282,7 +279,6 @@ def compute_summary_statistics(self,
282279
else:
283280
f_result.fvalue = 0
284281

285-
286282
f_result.f_pvalue = f.sf(f_result.fvalue, result.df_model, f_result.df)
287283
f_result.f_df_proj = [result.df_model, f_result.df]
288284

0 commit comments

Comments
 (0)