Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMLE when the predictions are provided. #936

Open
mehdirostami opened this issue Dec 9, 2024 · 0 comments
Open

TMLE when the predictions are provided. #936

mehdirostami opened this issue Dec 9, 2024 · 0 comments

Comments

@mehdirostami
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant