caretrace 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- care_trace/__init__.py +7 -0
- care_trace/acic.py +17 -0
- care_trace/aipw.py +183 -0
- care_trace/analyze_milestone5_final.py +518 -0
- care_trace/arms.py +87 -0
- care_trace/audit.py +85 -0
- care_trace/cli.py +94 -0
- care_trace/clinical/__init__.py +8 -0
- care_trace/clinical/features.py +120 -0
- care_trace/clinical/prepare.py +385 -0
- care_trace/clinical/pubmed.py +148 -0
- care_trace/clinical/selection.py +44 -0
- care_trace/designs.py +157 -0
- care_trace/evidence/__init__.py +5 -0
- care_trace/evidence/corpus.py +67 -0
- care_trace/evidence/decision_cache.py +62 -0
- care_trace/evidence/development_audit.py +192 -0
- care_trace/evidence/freeze.py +60 -0
- care_trace/evidence/graphs.py +273 -0
- care_trace/evidence/hard_evidence.py +206 -0
- care_trace/evidence/heldout_estimation.py +247 -0
- care_trace/evidence/milestone6_cases.py +116 -0
- care_trace/evidence/milestone6_policy.py +106 -0
- care_trace/evidence/milestone6_simulation.py +248 -0
- care_trace/evidence/retrieve.py +211 -0
- care_trace/evidence/revised_cases.py +247 -0
- care_trace/evidence/revised_controls.py +90 -0
- care_trace/evidence/revised_models.py +141 -0
- care_trace/evidence/revised_policy.py +243 -0
- care_trace/evidence/revised_score.py +130 -0
- care_trace/evidence/revised_simulation.py +166 -0
- care_trace/evidence/roles.py +369 -0
- care_trace/evidence/selection.py +243 -0
- care_trace/evidence/structures.py +364 -0
- care_trace/ihdp.py +127 -0
- care_trace/negative_controls.py +71 -0
- care_trace/propensity.py +65 -0
- care_trace/representations.py +199 -0
- care_trace/run_clinical.py +153 -0
- care_trace/run_clinical_llm.py +73 -0
- care_trace/run_milestone1.py +217 -0
- care_trace/run_milestone2.py +157 -0
- care_trace/run_milestone4.py +233 -0
- care_trace/run_milestone4_awq.py +241 -0
- care_trace/run_milestone4_diagnostic.py +247 -0
- care_trace/run_milestone4_extended.py +587 -0
- care_trace/run_milestone5.py +208 -0
- care_trace/run_milestone5_heldout_llm.py +268 -0
- care_trace/run_milestone6.py +417 -0
- care_trace/run_milestone6_llm.py +222 -0
- care_trace/treatment.py +30 -0
- caretrace-0.1.0.dist-info/METADATA +101 -0
- caretrace-0.1.0.dist-info/RECORD +57 -0
- caretrace-0.1.0.dist-info/WHEEL +5 -0
- caretrace-0.1.0.dist-info/entry_points.txt +2 -0
- caretrace-0.1.0.dist-info/licenses/LICENSE +21 -0
- caretrace-0.1.0.dist-info/top_level.txt +1 -0
care_trace/__init__.py
ADDED
care_trace/acic.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""ACIC 2016 column guard.
|
|
2
|
+
|
|
3
|
+
``input_2016`` names its columns ``x_1`` through ``x_58``. The package
|
|
4
|
+
manual lists Collaborative Perinatal Project variables and does not state
|
|
5
|
+
that this list is the column order. Arm B is not built from that list.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def deidentified_names(n_columns: int = 58) -> list[str]:
|
|
12
|
+
return [f"x_{index}" for index in range(1, n_columns + 1)]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def arm_b_is_defined(column_names: list[str]) -> bool:
|
|
16
|
+
"""Return whether the file itself names demographic and clinical columns."""
|
|
17
|
+
return list(column_names) != deidentified_names(len(column_names))
|
care_trace/aipw.py
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""Cross-fit augmented inverse probability weighting.
|
|
2
|
+
|
|
3
|
+
The primary interval is the normal approximation to the cross-fit
|
|
4
|
+
influence function. It is not a bootstrap of frozen scores. Propensity
|
|
5
|
+
clipping keeps every unit in the sample; it does not change the
|
|
6
|
+
population the way trimming would.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
from scipy.stats import norm
|
|
15
|
+
from sklearn.linear_model import LinearRegression, LogisticRegression
|
|
16
|
+
from sklearn.model_selection import StratifiedKFold
|
|
17
|
+
from sklearn.preprocessing import StandardScaler
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
PRIMARY_INTERVAL = "cross_fit_influence_function"
|
|
21
|
+
FROZEN_SCORE_BOOTSTRAP = "frozen_score_bootstrap"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class IntervalResult:
|
|
26
|
+
estimate: float
|
|
27
|
+
se: float
|
|
28
|
+
ci_low: float
|
|
29
|
+
ci_high: float
|
|
30
|
+
procedure: str
|
|
31
|
+
scores: np.ndarray
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _logistic() -> LogisticRegression:
|
|
35
|
+
return LogisticRegression(max_iter=2000, solver="lbfgs", C=1.0)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def nuisance_predictions(
|
|
39
|
+
x_train: np.ndarray,
|
|
40
|
+
t_train: np.ndarray,
|
|
41
|
+
y_train: np.ndarray,
|
|
42
|
+
x_test: np.ndarray,
|
|
43
|
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
44
|
+
"""Out-of-fold propensity and outcome predictions for one test fold."""
|
|
45
|
+
t_train = np.asarray(t_train).astype(int)
|
|
46
|
+
y_train = np.asarray(y_train, dtype=float)
|
|
47
|
+
if len(np.unique(t_train)) < 2:
|
|
48
|
+
raise ValueError("a training fold has only one treatment class")
|
|
49
|
+
treated = t_train == 1
|
|
50
|
+
if treated.sum() < 2 or (~treated).sum() < 2:
|
|
51
|
+
raise ValueError("a training fold has fewer than two units in a class")
|
|
52
|
+
|
|
53
|
+
scaler = StandardScaler()
|
|
54
|
+
scaled_train = scaler.fit_transform(x_train)
|
|
55
|
+
scaled_test = scaler.transform(x_test)
|
|
56
|
+
propensity_model = _logistic()
|
|
57
|
+
propensity_model.fit(scaled_train, t_train)
|
|
58
|
+
propensity = propensity_model.predict_proba(scaled_test)[:, 1]
|
|
59
|
+
|
|
60
|
+
outcome_treated = LinearRegression().fit(x_train[treated], y_train[treated])
|
|
61
|
+
outcome_control = LinearRegression().fit(x_train[~treated], y_train[~treated])
|
|
62
|
+
return (
|
|
63
|
+
propensity,
|
|
64
|
+
outcome_control.predict(x_test),
|
|
65
|
+
outcome_treated.predict(x_test),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def influence_scores(
|
|
70
|
+
t: np.ndarray,
|
|
71
|
+
y: np.ndarray,
|
|
72
|
+
propensity: np.ndarray,
|
|
73
|
+
mu0: np.ndarray,
|
|
74
|
+
mu1: np.ndarray,
|
|
75
|
+
) -> np.ndarray:
|
|
76
|
+
"""AIPW scores. Their mean is the ATE estimate."""
|
|
77
|
+
t = np.asarray(t, dtype=float)
|
|
78
|
+
y = np.asarray(y, dtype=float)
|
|
79
|
+
propensity = np.asarray(propensity, dtype=float)
|
|
80
|
+
mu0 = np.asarray(mu0, dtype=float)
|
|
81
|
+
mu1 = np.asarray(mu1, dtype=float)
|
|
82
|
+
return (
|
|
83
|
+
(mu1 - mu0)
|
|
84
|
+
+ t * (y - mu1) / propensity
|
|
85
|
+
- (1.0 - t) * (y - mu0) / (1.0 - propensity)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def interval_from_scores(
|
|
90
|
+
scores: np.ndarray,
|
|
91
|
+
procedure: str,
|
|
92
|
+
quantile: float = 0.975,
|
|
93
|
+
) -> IntervalResult:
|
|
94
|
+
scores = np.asarray(scores, dtype=float)
|
|
95
|
+
estimate = float(scores.mean())
|
|
96
|
+
se = float(scores.std(ddof=1) / np.sqrt(len(scores)))
|
|
97
|
+
z = float(norm.ppf(quantile))
|
|
98
|
+
return IntervalResult(
|
|
99
|
+
estimate=estimate,
|
|
100
|
+
se=se,
|
|
101
|
+
ci_low=estimate - z * se,
|
|
102
|
+
ci_high=estimate + z * se,
|
|
103
|
+
procedure=procedure,
|
|
104
|
+
scores=scores,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def cross_fit_aipw(
|
|
109
|
+
X: np.ndarray,
|
|
110
|
+
t: np.ndarray,
|
|
111
|
+
y: np.ndarray,
|
|
112
|
+
n_splits: int = 5,
|
|
113
|
+
seed: int = 0,
|
|
114
|
+
clip: float = 1e-3,
|
|
115
|
+
quantile: float = 0.975,
|
|
116
|
+
) -> IntervalResult:
|
|
117
|
+
"""Fit propensity and outcome models out of fold, then form the AIPW interval.
|
|
118
|
+
|
|
119
|
+
``seed`` fixes the stratified fold assignment. The interval uses the
|
|
120
|
+
influence scores from those folds. It does not resample them.
|
|
121
|
+
"""
|
|
122
|
+
X = np.asarray(X, dtype=float)
|
|
123
|
+
t = np.asarray(t).astype(int)
|
|
124
|
+
y = np.asarray(y, dtype=float)
|
|
125
|
+
if not (len(X) == len(t) == len(y)):
|
|
126
|
+
raise ValueError("X, t, and y must have the same number of rows")
|
|
127
|
+
if n_splits < 2:
|
|
128
|
+
raise ValueError("n_splits must be at least 2")
|
|
129
|
+
if len(np.unique(t)) < 2:
|
|
130
|
+
raise ValueError("treatment must contain both 0 and 1")
|
|
131
|
+
if not 0 < clip < 0.5:
|
|
132
|
+
raise ValueError("clip must lie in (0, 0.5)")
|
|
133
|
+
|
|
134
|
+
counts = np.bincount(t)
|
|
135
|
+
if counts.min() < n_splits:
|
|
136
|
+
raise ValueError("each treatment class needs at least n_splits rows")
|
|
137
|
+
|
|
138
|
+
propensity = np.empty(len(t), dtype=float)
|
|
139
|
+
mu0 = np.empty(len(t), dtype=float)
|
|
140
|
+
mu1 = np.empty(len(t), dtype=float)
|
|
141
|
+
splitter = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=seed)
|
|
142
|
+
for train_idx, test_idx in splitter.split(X, t):
|
|
143
|
+
propensity[test_idx], mu0[test_idx], mu1[test_idx] = nuisance_predictions(
|
|
144
|
+
X[train_idx],
|
|
145
|
+
t[train_idx],
|
|
146
|
+
y[train_idx],
|
|
147
|
+
X[test_idx],
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
propensity = np.clip(propensity, clip, 1.0 - clip)
|
|
151
|
+
scores = influence_scores(t, y, propensity, mu0, mu1)
|
|
152
|
+
return interval_from_scores(scores, PRIMARY_INTERVAL, quantile=quantile)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def frozen_score_bootstrap(
|
|
156
|
+
scores: np.ndarray,
|
|
157
|
+
n_boot: int = 200,
|
|
158
|
+
seed: int = 0,
|
|
159
|
+
) -> IntervalResult:
|
|
160
|
+
"""Percentile interval from resampling scores that were already computed.
|
|
161
|
+
|
|
162
|
+
This does not refit the propensity or outcome models. It is not the
|
|
163
|
+
primary interval.
|
|
164
|
+
"""
|
|
165
|
+
scores = np.asarray(scores, dtype=float)
|
|
166
|
+
if n_boot < 2:
|
|
167
|
+
raise ValueError("n_boot must be at least 2")
|
|
168
|
+
rng = np.random.default_rng(seed)
|
|
169
|
+
n = len(scores)
|
|
170
|
+
draws = np.empty(n_boot, dtype=float)
|
|
171
|
+
for draw in range(n_boot):
|
|
172
|
+
draws[draw] = scores[rng.integers(0, n, size=n)].mean()
|
|
173
|
+
estimate = float(scores.mean())
|
|
174
|
+
se = float(scores.std(ddof=1) / np.sqrt(n))
|
|
175
|
+
low, high = np.quantile(draws, [0.025, 0.975])
|
|
176
|
+
return IntervalResult(
|
|
177
|
+
estimate=estimate,
|
|
178
|
+
se=se,
|
|
179
|
+
ci_low=float(low),
|
|
180
|
+
ci_high=float(high),
|
|
181
|
+
procedure=FROZEN_SCORE_BOOTSTRAP,
|
|
182
|
+
scores=scores,
|
|
183
|
+
)
|