tramdag.scores¶
tramdag.scores ¶
Per-observation scores of the shift coefficients, and the effect-modifier scan.
The scores \(\psi_i = \partial \ell_i / \partial \beta\) are analytic: every shift
coefficient enters the latent additively, so
\(\partial \ell_i / \partial \beta = (\partial \ell_i / \partial s_i)\, x_i\) with the
latent-scale derivative in closed form (_dl_ds). The scan orders
one coefficient's scores by a candidate covariate and reports a CUSUM
statistic with its p-value. The CausalFlowDAG methods scores and
effect_modifier_scan delegate here; docs/scores.md is the guide.
node_scores ¶
node_scores(flow, df, node)
Compute the per-observation scores of the interpretable coefficients.
The scores cover every LS weight and the beta0 of every VC
term. CS terms carry no interpretable coefficient, so this function
skips them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
CausalFlowDAG
|
The fitted flow. |
required |
df
|
DataFrame
|
Observations. Must contain the node, its parents, and the propensity inputs of centered VC terms. |
required |
node
|
str
|
Name of the node whose coefficients are scored. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One column per coefficient, shape |
Notes
The scores are not free of y, because the derivative of the log-likelihood
needs the node column x. The scores also need the e_hat inputs of
centered terms.
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If the node has no |
Source code in src/tramdag/scores.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
sup_bb_pvalue ¶
sup_bb_pvalue(stat)
Give P(sup |Brownian bridge| > stat), the Kolmogorov series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stat
|
float
|
Observed supremum statistic. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The p-value, clipped to [0, 1]. |
Notes
The series has 100 terms, and all are needed. The k-th term is
exp(-2 k^2 stat^2). It underflows past k about 10 only for a large
statistic. A small statistic converges slowly. At stat = 0.02, a cut at
k = 10 returns 0.084 where the full series gives 0.9997. A perfectly
stable coefficient then looks significant. stat = 0.2 still needs 20
terms.
The function returns 1.0 for stat <= 0. There the series alternates to
0.0, which is the wrong tail.
Source code in src/tramdag/scores.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
effect_modifier_scan ¶
effect_modifier_scan(
flow, df, node, *, t, candidates=None, column=None
)
Scan the t-coefficient scores for effect-modifier drift.
This is the M-fluctuation test of Zeileis & Hornik (2007) on the raw treatment score, without the decorrelation from the other coefficients. For each candidate covariate the scan orders the treatment coefficient's scores by it, forms the scaled cumulative sum \(B_j = \sum_{i \le j} \psi_{(i)} / (\mathrm{sd}(\psi)\sqrt{n})\) and reports \(\sup_j |B_j|\) with its Kolmogorov p-value and the 5% critical value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
CausalFlowDAG
|
The fitted flow. |
required |
df
|
DataFrame
|
Observations, as for |
required |
node
|
str
|
Name of the outcome node. |
required |
t
|
str
|
Name of the treatment. Its scores column is |
required |
candidates
|
list[str] | None
|
Candidate covariates. Defaults to every node that is not |
None
|
column
|
str | None
|
Score column to scan, overriding the |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Indexed by candidate, sorted by |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no score column exists for |
ValueError
|
If the score column is constant. |
Source code in src/tramdag/scores.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |