-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance TMLE estimators with confidence interval calculations #84
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
dcbf224
Enhance TMLE estimators with confidence interval calculations and var…
kirilklein b497126
Refine influence curve calculations in variance.py for numerical stab…
kirilklein 24e4028
Update bootstrap sample tests to verify original dataset values
kirilklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import numpy as np | ||
|
|
||
| from CausalEstimate.utils.constants import CI95_LOWER, CI95_UPPER, STD_ERR | ||
|
|
||
|
|
||
| def compute_ci( | ||
| effect_type: str, | ||
| psi: float, | ||
| Q_star_1: np.ndarray, | ||
| Q_star_0: np.ndarray, | ||
| Y: np.ndarray, | ||
| A: np.ndarray, | ||
| ps: np.ndarray, | ||
| Yhat_star: np.ndarray, | ||
| H: np.ndarray = None, | ||
| ) -> dict: | ||
| """ | ||
| Compute the standard deviation and 95% confidence interval using the influence curve. | ||
| """ | ||
| n = len(Y) | ||
| if n == 0: | ||
| return {STD_ERR: np.nan, CI95_LOWER: np.nan, CI95_UPPER: np.nan} | ||
|
|
||
| # Select the appropriate influence curve based on the effect type | ||
| if effect_type in ["ATE", "ARR"]: | ||
| ic = _compute_ic_ate(psi, Q_star_1, Q_star_0, Y, A, Yhat_star, H) | ||
| elif effect_type == "ATT": | ||
| p_treated = np.mean(A) | ||
| ic = _compute_ic_att(psi, Q_star_1, Q_star_0, Y, A, Yhat_star, H, p_treated) | ||
| elif effect_type == "RR": | ||
| ic = _compute_ic_rr(Q_star_1, Q_star_0, Y, A, ps) | ||
| else: | ||
| raise ValueError( | ||
| f"CI calculation for effect type '{effect_type}' is not supported." | ||
| ) | ||
|
|
||
| if np.any(np.isnan(ic)): | ||
| return {STD_ERR: np.nan, CI95_LOWER: np.nan, CI95_UPPER: np.nan} | ||
|
|
||
| # Compute variance and standard error | ||
| var_ic = np.var(ic, ddof=1) # Use ddof=1 for sample variance | ||
| std_err = np.sqrt(var_ic / n) | ||
|
|
||
| # Compute confidence interval | ||
| if effect_type == "RR": | ||
| # For RR, CIs are calculated on the log scale and then exponentiated | ||
| log_psi = np.log(psi) | ||
| ci_lower = np.exp(log_psi - 1.96 * std_err) | ||
| ci_upper = np.exp(log_psi + 1.96 * std_err) | ||
| else: # ATE, ATT, ARR | ||
| ci_lower = psi - 1.96 * std_err | ||
| ci_upper = psi + 1.96 * std_err | ||
|
|
||
| return {STD_ERR: std_err, CI95_LOWER: ci_lower, CI95_UPPER: ci_upper} | ||
|
|
||
|
|
||
| def _compute_ic_ate( | ||
| psi: float, | ||
| Q_star_1: np.ndarray, | ||
| Q_star_0: np.ndarray, | ||
| Y: np.ndarray, | ||
| A: np.ndarray, | ||
| Yhat_star: np.ndarray, | ||
| H: np.ndarray, | ||
| ) -> np.ndarray: | ||
| """Influence curve for ATE.""" | ||
| return H * (Y - Yhat_star) + (Q_star_1 - Q_star_0) - psi | ||
|
|
||
|
|
||
| def _compute_ic_att( | ||
| psi: float, | ||
| Q_star_1: np.ndarray, | ||
| Q_star_0: np.ndarray, | ||
| Y: np.ndarray, | ||
| A: np.ndarray, | ||
| Yhat_star: np.ndarray, | ||
| H: np.ndarray, | ||
| p_treated: float, | ||
| ) -> np.ndarray: | ||
| """Influence curve for ATT.""" | ||
| if np.isclose(p_treated, 0.0, atol=1e-12): | ||
| return np.full(Y.shape, np.nan, dtype=float) | ||
| ic = H * (Y - Yhat_star) + (A / p_treated) * (Q_star_1 - Q_star_0 - psi) | ||
| return ic | ||
|
|
||
|
|
||
| def _compute_ic_rr( | ||
| Q_star_1: np.ndarray, | ||
| Q_star_0: np.ndarray, | ||
| Y: np.ndarray, | ||
| A: np.ndarray, | ||
| ps: np.ndarray, | ||
| eps: float = 1e-9, | ||
| ) -> np.ndarray: | ||
| """Influence curve for log(Risk Ratio).""" | ||
| mu1_star = np.mean(Q_star_1) | ||
| mu0_star = np.mean(Q_star_0) | ||
|
|
||
| if np.isclose(mu0_star, 0.0, atol=eps) or np.isclose(mu1_star, 0.0, atol=eps): | ||
| return np.full(Y.shape, np.nan, dtype=float) | ||
|
|
||
| # IC for mu1 | ||
| ic_mu1 = (A / (ps + eps)) * (Y - Q_star_1) + Q_star_1 - mu1_star | ||
| # IC for mu0 | ||
| ic_mu0 = ((1 - A) / (1 - ps + eps)) * (Y - Q_star_0) + Q_star_0 - mu0_star | ||
|
|
||
| ic_log_rr = (1 / mu1_star) * ic_mu1 - (1 / mu0_star) * ic_mu0 | ||
| return ic_log_rr | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Remove unused parameter
Afrom function signature.The parameter
Ais not used in the influence curve calculation for ATE. This is correctly flagged by static analysis.Apply this diff:
def _compute_ic_ate( psi: float, Q_star_1: np.ndarray, Q_star_0: np.ndarray, Y: np.ndarray, - A: np.ndarray, Yhat_star: np.ndarray, H: np.ndarray, ) -> np.ndarray:Also update the call site at line 26:
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.13.3)
62-62: Unused function argument:
A(ARG001)