-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve unittest: Move setup to a base class (only for tmle so far)
- Loading branch information
1 parent
dbabff8
commit e8d5073
Showing
3 changed files
with
82 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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,62 @@ | ||
import unittest | ||
from typing import List | ||
|
||
import numpy as np | ||
from scipy.special import expit | ||
|
||
from CausalEstimate.simulation.binary_simulation import ( | ||
compute_ATE_theoretical_from_data, | ||
compute_ATT_theoretical_from_data, | ||
simulate_binary_data, | ||
) | ||
|
||
|
||
class TestEffectBase(unittest.TestCase): | ||
n: int = 2000 | ||
alpha: List[float] = [0.1, 0.2, -0.3, 0] | ||
beta: List[float] = [0.5, 0.8, -0.6, 0.3, 0] | ||
seed: int = 42 | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
# Simulate realistic data for testing | ||
rng = np.random.default_rng(cls.seed) | ||
# Covariates | ||
data = simulate_binary_data( | ||
cls.n, alpha=cls.alpha, beta=cls.beta, seed=cls.seed | ||
) | ||
|
||
# Predicted outcomes | ||
X = data[["X1", "X2"]].values | ||
A = data["A"].values | ||
Y = data["Y"].values | ||
ps = expit( | ||
cls.alpha[0] + cls.alpha[1] * X[:, 0] + cls.alpha[2] * X[:, 1] | ||
) + 0.01 * rng.normal(size=cls.n) | ||
Y1_hat = expit( | ||
cls.beta[0] | ||
+ cls.beta[1] * 1 | ||
+ cls.beta[2] * X[:, 0] | ||
+ cls.beta[3] * X[:, 1] | ||
) + 0.01 * rng.normal(size=cls.n) | ||
Y0_hat = expit( | ||
cls.beta[0] + cls.beta[2] * X[:, 0] + cls.beta[3] * X[:, 1] | ||
) + 0.01 * rng.normal(size=cls.n) | ||
Yhat = expit( | ||
cls.beta[0] | ||
+ cls.beta[1] * A | ||
+ cls.beta[2] * X[:, 0] | ||
+ cls.beta[3] * X[:, 1] | ||
) + 0.01 * rng.normal(size=cls.n) | ||
|
||
cls.A = A | ||
cls.Y = Y | ||
cls.ps = ps | ||
cls.Y1_hat = Y1_hat | ||
cls.Y0_hat = Y0_hat | ||
cls.Yhat = Yhat | ||
|
||
true_ate = compute_ATE_theoretical_from_data(data, beta=cls.beta) | ||
true_att = compute_ATT_theoretical_from_data(data, beta=cls.beta) | ||
cls.true_ate = true_ate | ||
cls.true_att = true_att |
This file contains 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 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