tramdag.readouts¶
tramdag.readouts ¶
Read-outs of a fitted flow: ReadoutsMixin, composed into CausalFlowDAG.
Coefficients (ls_coefficients), effect curves (shift_curve,
varying_coef), the intercept decomposition (intercept_contributions),
the meta-adjacency view (to_matrix) and the classical design matrix
(design_matrix).
ReadoutsMixin ¶
The stateless read-outs, mixed into CausalFlowDAG.
Source code in src/tramdag/readouts.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 123 124 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 156 157 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
shift_curve ¶
shift_curve(node, parent, grid)
Evaluate one fitted shift term on a 1-D grid of parent values.
The parent runs over grid with the term's other inputs absent, so the
term must read exactly this one parent (an LS or one-parent CS).
The evaluation runs through the term's own shift_value, so custom
terms work too. The grid is copied, as in _tensorize: torch warns on
a non-writable numpy array, which a broadcast or read-only grid is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
str
|
Name of the node that carries the term. |
required |
parent
|
str
|
The term's key in the node's shifts: the parent's name. |
required |
grid
|
array - like
|
Parent values at which to evaluate the term, shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The shift values, shape |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If |
Source code in src/tramdag/readouts.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
varying_coef ¶
varying_coef(df, node, *, t=None)
Evaluate the fitted effect function \(\beta(x)\) of a VC term.
The value is \(\beta_0 + b_\Theta(\text{modifiers})\), in closed form from the
fitted term; only the modifier columns of df are read. It lives on
the node's latent scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Rows at which to evaluate \(\beta\). Must contain every modifier column of the term. |
required |
node
|
str
|
Name of the node that carries the VC term. |
required |
t
|
str | None
|
Treatment name of the VC term. Optional when the node has exactly one VC term. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The \(\beta\) values, shape |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If the node has no VC term, or if |
Source code in src/tramdag/readouts.py
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
ls_coefficients ¶
ls_coefficients()
Give the per-node linear-shift weights.
A continuous parent has one weight. An ordinal parent has one weight
per level (its one-hot encoding), identified only up to a common
constant, so read them as differences w[k] - w[0]. Only LS
terms have a weight; CS and VC shifts are skipped.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, ndarray]]
|
The weights, as |
Source code in src/tramdag/readouts.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
to_matrix ¶
to_matrix()
Give the labeled adjacency matrix of term effects.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Rows are parents and columns are children. A cell holds the
term tag: |
Source code in src/tramdag/readouts.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
intercept_contributions ¶
intercept_contributions(df, node)
Decompose a complex intercept into mean-centered per-parent parts.
Each network's output over the rows of df is centered to mean zero
per parameter; the removed means collect into baseline, so
\(\vartheta(\mathrm{pa}) = \text{baseline} + \sum \text{contributions}\) exactly.
Post-hoc only: it reads the fitted weights and changes nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Rows over which to center and at which to evaluate the contributions. Must contain every intercept-parent column. |
required |
node
|
str
|
Name of a node whose intercept term has parents. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Three keys. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If the node's intercept term has no parents. |
Notes
An additive intercept holds one network per parent group in its
nets; a single intercept term, joint or not, is itself the one
network. The contributions live in the transform's unconstrained
parameter space, before the monotonicity constraint.
Source code in src/tramdag/readouts.py
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 | |
design_matrix ¶
design_matrix(df, node, *, drop_first=False)
Encode a node's parents the way the flow feeds them to its shifts.
A continuous parent stays raw in one column named after it. An
ordinal parent becomes one column per level, named
"{parent}[{k}]", the same one-hot the flow builds internally.
Use drop_first=True to get the design a classical reference
expects (statsmodels OrderedModel, R polr). It drops the
level-0 column of each ordinal parent, so the remaining coefficients
match w[k] - w[0] on the flow side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Rows to encode. Must contain every parent column of |
required |
node
|
str
|
Name of the node whose parents are encoded. |
required |
drop_first
|
bool
|
Drop each ordinal parent's level-0 column, by default |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One column per encoded feature, indexed like |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If an ordinal parent value is not a level index. |
Source code in src/tramdag/readouts.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |