py-why / py-why/EconML

TMLE when the predictions are provided.

Open
#936 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

I noticed that this code doesn't work when the user has already the predictions. HI wrote this code and can be added to tmle.py.

This code gives slightly different results. I hope the writers of EconML fix the code and add it:

def tmle_est_given_preds(self, X, a, y, Q1, Q0, g):
n = X.shape[0]

Q = a * Q1 + (1-a) * Q0
Q = Q.reshape(-1, 1)

clever = a / g - (1 - a) / (1 - g)
g = g.reshape(-1, 1)

target_scaler_ = sklearn.preprocessing.MinMaxScaler(feature_range=(0, 1))
target_scaler_.fit(y)
y_rescale = target_scaler_.transform(y)
Q_rescale = target_scaler_.transform(Q)
Q1_rescale = target_scaler_.transform(Q1.reshape(-1, 1))
Q0_rescale = target_scaler_.transform(Q0.reshape(-1, 1))

Q_rescale = _logit(Q_rescale)
Q1_pred = _logit(Q1_rescale)
Q0_pred = _logit(Q0_rescale)

targeted_outcome_model = sm.GLM(
                                endog=y_rescale.flatten(), 
                                exog=clever.flatten(), 
                                offset=Q_rescale.flatten(), 
                                family=sm.families.Binomial(),
                            ).fit()

epsilon = targeted_outcome_model.params[0]

logit_Q1_star = Q1_pred + epsilon / g
logit_Q0_star = Q0_pred - epsilon / (1 - g)

Q1_star = target_scaler_.inverse_transform(_expit(logit_Q1_star))
Q0_star = target_scaler_.inverse_transform(_expit(logit_Q0_star))

est = (Q1_star - Q0_star).mean()
phi = (a * (y - Q1_star) / g) - ((1 - a) * (y - Q0_star) / (1 - g)) + (Q1_star - Q0_star) - est
var =( (phi - phi.mean()) ** 2).mean() / n 

return est, var

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

Start by locating tmle.py and reading the existing TMLE implementation and its public entry points. Compare the proposed tmle_est_given_preds path with the current behavior, then confirm the intended estimates and variance when predictions are supplied; done means this use case is supported with results matching the project’s expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.