py-why / py-why/EconML

Result 0:

Open
#335 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.8k
Forks
827
PR merge metrics
No merged PRs in 30d

Description

Hi,

thanks again for the algorithm.
I am trying, following previous issues, to use a subset of thee X (called here XW) in DMLIVV in order to obtain heterogeneous effects. To do so I am doing:

XW = hstack([X[:, :(n_products)], W]) 
#Z = np.random.rand(X.shape[0],1)
Z = morelagsprice.reshape((n_products*n_months, 1))


cate.fit(Y, T[:,0], XW, Z, store_final=True)

from prototypes.dml_iv.utilities import SubsetWrapper, StatsModelLinearRegression, ConstantModel

colonne = ["A_"+str(k) for k in range(76)]
XW_db = pd.DataFrame(XW, columns=colonne)

subset_names = set(['A_0','A_1','A_2','A_3','A_4','A_5','A_6','A_7','A_8','A_9','A_10'])
# list of indices of features X to use in the final model

feature_inds = np.argwhere([(x in subset_names) for x in XW_db.columns.values]).flatten()
print(feature_inds)

ph_dmliv_model_effect = lambda: SubsetWrapper(LinearRegression(fit_intercept=False),
                                          feature_inds # list of indices of features X to use in the final model
                                         )

#ph_dmliv_model_effect = lambda: SubsetWrapper(LinearDMLCateEstimator(linear_first_stages=False,n_splits = 10), 
#                                       feature_inds)
                                            
    
cate.refit_final(ph_dmliv_model_effect(), dmliv_featurizer())

The idea is to have as X, in the DMLIV algorithm, X[:, :(n_products)] and W as W. So, as far as I have understood I am refitiing the final stage with only X[:, :(n_products)] as X, while estimating model_t and model_x with the complete set XW.

However, when I then look for the effect:

X_test = np.vstack([np.eye(n_products-1)])
dmliv_cate = cate.effect(X_test)
dmliv_cate

it all results in a vector of zeroes. I checked different types of instruments but the result is always a vector of 11 zeros.
Can you please tell me if I am doing something wrong or if it is a theoretical issue which can happen with wrong instruments (consider that I also took the lag of the endogenous treatment variable as instrument and did not work).

Thank you,

Federico

Edited 1: I made some improvement and discovered that the part that disturbs is actually W. Specifically two variables called nfirm and nprod which are very similar. I suspected therefore collinearity. But even after removing one of the two or not including constant term (which should actually not be a worry since I am using methods with regularization), the results of the effect are all 0. Only if I throw away nprod AND nfirm the results are different from 0. Strangely enough, results are different from 0 also if W is composed respectively: only of nprod, only of nfirm, with nfirm and nprod only.

W variable (without nprod and nfirm) looks like this:
W = np.hstack([inflowratio.reshape((n_products*n_months, 1)), outflowratio.reshape((n_products*n_months, 1)), natc.reshape((n_products*n_months, 1)),avg_price.reshape((n_products*n_months, 1)),min_price.reshape((n_products*n_months, 1)),share_generics.reshape((n_products*n_months, 1))])

with inflow and outflowrates being composed by many zeroes. The other variables are basically min of price, average price across categories and share of generic products within a category. Data are panels. Strangely enough when I add both nprod and nfirm the error "ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems." emerges.

Edited 2: it seems that using log nfirm and log nprod, though producing the same error of Objective not converge...at least solves the problem oof 0 coefficients. Still I do not understand why. Is there a rescaling of coefficients? I am using regularized final stage with LassoCV so it should not be collinearity...

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the shown DMLIV workflow using cate.refit_final, dmliv_featurizer(), and cate.effect(), varying W as described. Inspect prototypes.dml_iv.utilities.SubsetWrapper and the LassoCV convergence warning, then compare the zero results with the nprod and nfirm variants. Done means documenting the cause of the zero coefficients or identifying the relevant defect.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, numpy, pandas, python, scikit-learn
Domain
data, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.