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)
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
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 | |
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
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 | |
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
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
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
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 | |