Continuous Programs¶
Probabilistic programs in continuous domains.
programs
¶
Monadic programs: sequenced probabilistic programs as ContinuousMorphisms.
A MonadicProgram defines a ContinuousMorphism via monadic sequencing of draw steps. Each step samples from a named morphism, optionally conditioned on previously drawn variables, and binds the result. The program returns one or more of the bound variables as its output.
This corresponds to the Kleisli composition pattern used in
probabilistic programming languages like PDS (Grove & White),
where a sequence of let' x ~ D in ... bindings threads
probabilistic state through a generative model.
Features
- Single and tuple returns:
return xorreturn (x, y, z) - Named input parameters for product-domain sub-programs
- Multi-argument draw steps:
draw z ~ f(x, y) - Destructuring draws from tuple-returning sub-programs:
draw (a, b) ~ sub_prog(x)
Example
Given morphisms f : A -> B and g : B -> C, the monadic program::
program p : A -> C
draw x ~ f
draw y ~ g(x)
return y
is equivalent to the composition f >> g, but the program form allows fan-out (using the input in multiple draws) and non-linear variable dependency graphs.
PDS-style nested programs::
program cg_update(y, z) : Belief * Belief -> Truth * Truth
draw c ~ bern_c(y)
draw d ~ bern_d(z)
return (c, d)
program factivityPrior : Entity -> Truth * Truth * Truth
draw x ~ prior_x
draw y ~ prior_y
draw z ~ prior_z
draw b ~ bern_b(x)
draw (c, d) ~ cg_update(y, z)
return (b, c, d)
MonadicProgram
¶
MonadicProgram(domain: AnySpace, codomain: AnySpace, steps: list[tuple], return_vars: tuple[str, ...], params: tuple[str, ...] | None = None, return_labels: tuple[str, ...] | None = None, effect_set: frozenset[str] | None = None)
Bases: ContinuousMorphism
A probabilistic program defined by monadic sequencing of draw steps.
Each draw step samples from a ContinuousMorphism and binds the result to one or more named variables. Later steps can reference earlier bindings as their input. The program's output is the value(s) of the designated return variable(s).
| PARAMETER | DESCRIPTION |
|---|---|
domain
|
The program's input space.
TYPE:
|
codomain
|
The program's output space.
TYPE:
|
steps
|
Each entry is either (var_names, morphism, arg_names) for draw steps, or (var_names, None, value) for let bindings where value is a float constant or str variable reference.
TYPE:
|
return_vars
|
Name(s) of the bound variable(s) whose value(s) are the program output.
TYPE:
|
params
|
Named input parameters for product-domain programs. When set, the program input is split along the feature dimension and each component is pre-bound in the env.
TYPE:
|
return_labels
|
Optional labels for tuple return fields. When set, the output dict uses these labels as keys instead of the variable names. Length must match return_vars.
TYPE:
|
Source code in src/quivers/continuous/programs.py
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 | |
observed_names
property
¶
observed_names: set[str]
Return the set of variable names marked as observed in the DSL.
rsample
¶
rsample(x: Tensor, sample_shape: Size = Size(), observations: dict[str, Tensor] | None = None) -> Tensor | dict[str, Tensor]
Run the program forward, returning the designated output(s).
Each draw step is executed in order. Steps that reference
the program input use x directly; steps that reference
bound variables use those variables' sampled values.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input.
TYPE:
|
sample_shape
|
Additional leading sample dimensions (applied to the first draw only; subsequent draws inherit the shape).
TYPE:
|
observations
|
Values to clamp observed variables to. Keys are variable names, values are tensors of the appropriate shape.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor or dict[str, Tensor]
|
The value of the return variable(s). Returns a tensor for single-variable returns, or a dict keyed by variable name for tuple returns. |
Source code in src/quivers/continuous/programs.py
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 | |
log_prob
¶
log_prob(x: Tensor, y: Tensor) -> Tensor
Log-probability is not supported for monadic programs.
Computing log p(y | x) for a monadic program requires
marginalizing over all intermediate variables, which is
intractable in general. Use rsample for forward sampling
and condition via score function estimators or variational
methods.
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
Always. |
Source code in src/quivers/continuous/programs.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
log_joint
¶
log_joint(x: Tensor, intermediates: dict[str, Tensor]) -> Tensor
Joint log-density given all intermediate values.
When all intermediate variables are observed (e.g. during inference with HMC/NUTS), computes the joint log-density:
log p(x_1, ..., x_n | input) = sum_i log p(x_i | pa(x_i))
where pa(x_i) is the parent variable of step i (either the program input or a previously drawn variable).
For destructuring draw steps (tuple-returning sub-programs), the intermediates dict should contain entries for each individual variable name.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input.
TYPE:
|
intermediates
|
Values for ALL bound variables (keyed by variable name or by return label if labels are set).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Joint log-density. Shape (batch,). |
Source code in src/quivers/continuous/programs.py
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 | |