tramdag.flow¶
tramdag.flow ¶
CausalFlowDAG: a single triangular normalizing flow on a user-defined DAG.
The flow maps iid standard-logistic latents U to the observed variables X
in topological order. sample, abduct, pmf and density answer the
observational, interventional (do=) and counterfactual queries; the read-outs
that only read fitted weights live in readouts.py.
CausalFlowDAG ¶
Bases: FitMixin, ReadoutsMixin, Module
A causal normalizing flow defined by a DAG specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict[str, NodeSpec]
|
The DAG specification, |
required |
device
|
str
|
Torch device, by default |
'cpu'
|
seed
|
int | None
|
If given, seeds the weight initialization. Each node draws its
weights from a stream keyed on this seed and the node's name, so a
node starts from the same weights in every spec that contains it.
Construction does not change torch's global RNG state. Without a
seed, the weights come from torch's global RNG. |
None
|
init
|
str
|
Weight initialization of every linear layer (the LS weights and the
CI/CS/VC networks): |
'torch'
|
zero_shift_networks
|
bool
|
Zero the output layer of every |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
nodes |
ModuleDict
|
One |
order |
list[str]
|
The node names in topological order. |
history |
dict
|
The nodes' fit histories, see
|
meta |
dict
|
The provenance that |
Source code in src/tramdag/flow.py
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |
history
property
¶
history
The nodes' histories, one {node: value} dict per epoch per key.
Nodes stop at different epochs. A node that stopped earlier repeats
its last entry, so a summed curve stays defined. A node without
entries for a key shows NaN there. "val_epoch" is the longest
node's, which is exact while every fit validates. Each node's best
epoch is in node.history["best_epoch"].
outcome ¶
outcome()
Give the one node that no other node reads.
Returns:
| Type | Description |
|---|---|
str
|
The name of the only node without children. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the DAG has more than one such node; the message names them. |
Source code in src/tramdag/flow.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
log_prob ¶
log_prob(df, *, nodes=None)
Compute the joint log-likelihood per row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Observations, one column per node. |
required |
nodes
|
list[str] | None
|
Sum only these nodes' contributions. A subset is exact, because
the per-node losses are independent: |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
KeyError
|
If |
Source code in src/tramdag/flow.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
nll ¶
nll(df)
Compute the mean negative log-likelihood per node (a diagnostic).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Observations, one column per node. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
The mean NLL, keyed by node name. |
Source code in src/tramdag/flow.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
sample ¶
sample(n=None, *, do=None, u=None, seed=None)
Sample from the flow, with optional interventions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int | None
|
Number of samples. Ignored if |
None
|
do
|
dict[str, float] | None
|
Interventions, as |
None
|
u
|
DataFrame | None
|
Latent variables, as returned by |
None
|
seed
|
int | None
|
If given, seeds the latent draw. Ignored if |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The samples, one column per node. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Notes
Under do, a centered VC re-derives t_do - e_hat(x), so the
features of a node are never cached. With u given, the result takes
the index of u, so a counterfactual lines up with its factual row.
Source code in src/tramdag/flow.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
abduct ¶
abduct(df, *, seed=None)
Recover the latent variables u from observations (the abduction step).
A continuous node inverts exactly: \(u = h(x) + s\). This is \(\operatorname{logit} P(X \le x \mid \text{parents})\), an interventional quantity at the given parent values. For an ordinal node the latent is only interval-identified, so it is sampled from the standard logistic truncated to the observed level's interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Observations, one column per node. |
required |
seed
|
int | None
|
If given, seeds the truncated draw for the ordinal nodes. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The latents, one column per node, aligned with the rows of
|
Source code in src/tramdag/flow.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
pmf ¶
pmf(df, node, *, do=None)
Give the class probabilities of an ordinal node in closed form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Rows that supply the parent values of the node. |
required |
node
|
str
|
Name of the ordinal node. |
required |
do
|
dict[str, float] | None
|
Parent values |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The class probabilities, shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
KeyError
|
If |
Source code in src/tramdag/flow.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | |
density ¶
density(df, node, grid, *, do=None)
Give the analytic conditional density of a continuous node on a grid.
The continuous counterpart of pmf: for every row of df
the density \(p(\text{node} = g \mid \mathrm{pa})\) at each grid value \(g\), in
closed form from the transform, with no sampling. The evaluation takes one
(row, grid value) pair per entry: the rows repeat and the grid tiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Rows that supply the parent values of the node. |
required |
node
|
str
|
Name of the continuous node. |
required |
grid
|
array - like
|
Values of |
required |
do
|
dict[str, float] | None
|
Parent values |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The densities, shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
KeyError
|
If |
Source code in src/tramdag/flow.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | |
scores ¶
scores(df, node)
Give the scores \(\psi_i = \partial \ell_i / \partial \beta\).
The method form of node_scores, which
documents the arguments and the column naming.
Source code in src/tramdag/flow.py
610 611 612 613 614 615 616 617 | |
effect_modifier_scan ¶
effect_modifier_scan(
df, node, *, t, candidates=None, column=None
)
Rank candidate effect modifiers with a fluctuation scan.
The method form of
effect_modifier_scan, which
documents the method, the arguments and the result columns.
Source code in src/tramdag/flow.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
save ¶
save(path)
Write the model, its history and its provenance to a checkpoint.
The file holds the spec and the weights, the nodes' training
history, and a meta block with the tramdag version, the save
time and the device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Target file. Parent directories are created when missing. |
required |
Source code in src/tramdag/flow.py
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | |
load
classmethod
¶
load(path, device='cpu')
Restore a model from a checkpoint.
The nodes' history and flow.meta are refilled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Checkpoint file written by |
required |
device
|
str
|
Torch device to load onto, by default |
'cpu'
|
Returns:
| Type | Description |
|---|---|
CausalFlowDAG
|
The restored model, in eval mode. |
Source code in src/tramdag/flow.py
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |
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 | |
warm_start ¶
warm_start(train_df)
Warm-start every node that has a VC term.
Each such node runs Node.warm_start.
Call it before fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_df
|
DataFrame
|
Training rows with every node's column. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
The reports of the nodes with a |
Source code in src/tramdag/fitting.py
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 | |
fit ¶
fit(
train_df,
*,
epochs,
learning_rate=None,
batch_size=512,
validation_data=None,
validation_split=None,
verbose=0,
seed=None,
marginal_init=False,
optimizer=None,
callbacks=None,
n_jobs=1
)
Fit every node by maximum likelihood, node by node.
The joint likelihood is a sum of per-node terms with disjoint
parameters, so fitting the nodes one by one is exact. Each node runs
Node.fit, whose docstring describes the
loop, the history and the callback contract. The validation split
happens once here, so all nodes see the same rows. The nodes fit in
topological order; with validation data, the nodes with a centered
VC fit last, so their validation NLL uses the fitted treatment
node.
epochs, learning_rate, optimizer and callbacks each
take one value for every node or a dict keyed by node name. The node
gets value[name]. A missing key gives the argument's default
(None), so a collections.defaultdict gives its own fallback.
epochs has no default, so a missing key raises. A key that names
no node raises too.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_df
|
DataFrame
|
Training data, one column per node, plus the side columns. |
required |
epochs
|
int | dict
|
Passes over the data, one value or one per node. Required; there is no default. |
required |
learning_rate
|
float | dict
|
Adam step size of the default optimizer, one value or one per
node. The default (None) is 1e-2. A node cannot get both a
|
None
|
batch_size
|
int
|
Rows per gradient step, by default 512. |
512
|
validation_data
|
DataFrame | None
|
Validation rows, with the same columns as |
None
|
validation_split
|
float | None
|
Keras' rule: the LAST fraction of |
None
|
verbose
|
int
|
0 (default) is silent; |
0
|
seed
|
int | None
|
Seeds the minibatch shuffling, by default None. Every node fits
with this |
None
|
marginal_init
|
bool
|
Start every simple intercept at its column's marginal, by default
|
False
|
optimizer
|
callable | dict | None
|
A factory |
None
|
callbacks
|
Callback | list | callable | dict | None
|
The callbacks of every node, or a dict of them by node name, by
default None. A bare callable is an |
None
|
n_jobs
|
int
|
Number of processes, by default 1 (serial, topological order).
|
1
|
Returns:
| Type | Description |
|---|---|
CausalFlowDAG
|
|
Notes
Without a seed, n_jobs > 1 draws one from torch's RNG, because
forked workers would otherwise replay one RNG state. Every worker
gets this one seed. A centered VC
validates against the live propensity of its fitted treatment node, as
every query does. Those nodes therefore fit second. The callbacks
normalize once here, so an iterator of callbacks survives a single pass.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
KeyError
|
If a frame lacks a node column, a dict |
ValueError
|
See |
Source code in src/tramdag/fitting.py
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 | |
fit_jointly ¶
fit_jointly(
train_df,
*,
epochs,
learning_rate=None,
batch_size=512,
validation_data=None,
validation_split=None,
verbose=0,
seed=None,
marginal_init=False,
optimizer=None,
callbacks=None
)
Fit all nodes together: one optimizer, one minibatch loop.
Each step draws one batch of rows and minimizes the sum of the nodes'
mean NLLs. The optimum is that of fit;
the training dynamics differ. One learning-rate schedule and one stop
rule act on the summed NLL, which is the protocol of the reference
implementation. Each node's history gets its own train and validation
NLL per epoch, so flow.history, history_frame and
plot_training read a joint fit as they read a per-node one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_df
|
DataFrame
|
Training data, one column per node, plus the side columns. |
required |
epochs
|
int
|
Passes over the data. Required; there is no default. |
required |
learning_rate
|
float
|
Adam step size of the default optimizer. The default (None) is
1e-2. Exclusive with |
None
|
batch_size
|
int
|
Rows per gradient step, by default 512. |
512
|
validation_data
|
DataFrame | None
|
Validation rows, one column per node, by default None. A centered
|
None
|
validation_split
|
float | None
|
Keras' rule, as in |
None
|
verbose
|
int
|
0 (default) is silent; |
0
|
seed
|
int | None
|
Seeds the minibatch shuffling, by default None. The row order of
every epoch is that of |
None
|
marginal_init
|
bool
|
Start every simple intercept at its column's marginal, by default
|
False
|
optimizer
|
Optimizer | callable | None
|
A torch optimizer over |
None
|
callbacks
|
Callback | callable | list | None
|
As in |
None
|
Returns:
| Type | Description |
|---|---|
CausalFlowDAG
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
See |
KeyError
|
If a frame lacks a node column. |
Source code in src/tramdag/fitting.py
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 | |
fit_classical ¶
fit_classical(train_df, *, max_iter=5000, history_size=50)
Fit an all-ls model the classical way, node by node.
Each node runs Node.fit_classical:
float64, full batch, L-BFGS and Newton. The joint objective is a sum of per-node
terms with disjoint parameters, so the per-node optima are the joint
one. Any term other than a simple intercept or LS raises before a
node is fitted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_df
|
DataFrame
|
Training data, one column per node. |
required |
max_iter
|
int
|
Upper limit on L-BFGS iterations and Newton steps per node, by default 5000. |
5000
|
history_size
|
int
|
L-BFGS memory, by default 50. |
50
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Notes
The report holds the float64 values of the fit. The flow keeps float32
parameters, so ls_coefficients, pmf and log_prob use these
values rounded to float32, about 1e-7 relative. Compare the report with
other software.
Warns:
| Type | Description |
|---|---|
UserWarning
|
For each node that did not converge. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a term is not classical, an ordinal value is not a level index, or a node's calibration refuses the frame. |
KeyError
|
If the frame lacks a node column. |
Source code in src/tramdag/fitting.py
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 | |