Plate graphs¶
plate_graph
¶
Plate-graph extraction for QVR programs.
A plate diagram in the Bayesian-statistics tradition has three ingredients:
- Nodes: random variables (sample sites, observe sites) and deterministic intermediate values (let-bound terms). Observed nodes are conventionally shaded; latent nodes are unshaded; deterministic nodes are conventionally drawn as smaller dots.
- Edges: directed dependencies between nodes, computed from
the family-argument list of each draw / observe step.
z ~ Categorical(probs=theta)produces an edge fromthetatoz. - Plates: rectangular regions enclosing groups of nodes that
share a common indexing axis. A plate is induced by an
indexannotation on a draw (sample theta : Doc <- ...putsthetaon theDocplate), byiid_overon the same step, by theoveraxis on a marginalize block (which scopes every inner observation), or by the grouping fibration[via=...]on an observe step.
The extractor in this module walks a compiled
:class:quivers.dsl.compiler.Compiler's program declarations and
builds an immutable :class:PlateGraph describing exactly this
structure. Renderers (in-TUI table view, Mermaid output, Daft /
TikZ / DOT code generation) consume the graph; the graph itself is
the single source of truth and never embeds layout decisions.
The walker handles arbitrarily nested marginalize blocks by
treating each block as a new plate scope that lives inside the
enclosing plates. [via=idx] on an observe inside a
marginalize-over-G block puts the observe on G's plate in addition
to its own index plate, matching the standard convention for
grouped observations in pgm notation.
NodeKind
module-attribute
¶
The kind tag of a PlateNode, which selects how a renderer draws it.
Edge
didactic-model
¶
Bases: Model
One directed dependency edge in the plate diagram.
Plate
didactic-model
¶
Bases: Model
One plate (indexing axis) of the diagram.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
The plate object's name (
TYPE:
|
cardinality |
The plate's size if statically inferable from the
module's
TYPE:
|
parent |
Name of an enclosing plate, or
TYPE:
|
PlateNode
didactic-model
¶
PlateNode(*, name: str, kind: NodeKind, family: str | None = None, plates: tuple[str, ...] = (), args: tuple[str, ...] = (), scope_path: str = '')
Bases: Model
One random variable / deterministic intermediate.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
The variable's bound name in the program (
TYPE:
|
kind |
TYPE:
|
family |
The distribution family name (
TYPE:
|
plates |
The plates this node sits inside, ordered from outermost to innermost. Empty tuple for scalar nodes.
TYPE:
|
args |
The variable names this node's family / let-expression references. The graph's edges are derived from these.
TYPE:
|
scope_path |
The
TYPE:
|
PlateGraph
didactic-model
¶
PlateGraph(*, program_name: str, domain: str = '', codomain: str = '', nodes: tuple[PlateNode, ...] = (), plates: tuple[Plate, ...] = (), edges: tuple[Edge, ...] = ())
Bases: Model
Structural plate diagram of a single QVR program.
build_plate_graph
¶
build_plate_graph(compiler, program_name: str) -> PlateGraph | None
Build the :class:PlateGraph for program_name.
Returns None if program_name isn't a registered program
on compiler. Walks the program's draws and any nested
marginalize body recursively. Marginalized sample sites
(the variable bound by the marginalize header) get
kind="marginalized" so renderers can shade them
differently from a plain "latent".
| PARAMETER | DESCRIPTION |
|---|---|
compiler
|
A
TYPE:
|
program_name
|
The name of the program to diagram.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PlateGraph or None
|
The program's plate graph, or |
Source code in src/quivers/analysis/plate_graph.py
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 | |