quivers.formulas.compile¶
The FormulaToQVRModule lens: Formula → quivers.dsl.ast_nodes.Module.
compile
¶
Bidirectional didactic.api.Lens from Formula to a
QVR quivers.dsl.ast_nodes.Module AST.
The compilation from a formula to a QVR program is a panproto-style
lens whose complement is the strict subset of Formula
fields that are not recoverable from the emitted
Module, packaged as FormulaData. The forward
direction produces (module, formula_data) where the structural
fields of the formula (which columns exist, intercept flag, random
effect group / slope structure, response identifier) are encoded as
QVR latent / let / observe shape and the un-encodable fields (per-row
data, original identifier names lost to _qvr_name's
non-alphanumeric → underscore substitution, the term / name
presentation labels, the original formula string) ride in
FormulaData. The backward direction recovers the structure
by calling _decode_module on the target and fuses it with the
complement.
The lens never touches strings: the target is a Module
that the existing quivers.dsl.compiler.Compiler consumes
directly, identical in shape to one produced by
quivers.dsl.parser.parse.
Emitted structure (one named scalar coefficient per design-matrix
column, matching the brms / lme4 canonical layout in which
poly(x, 2) produces two coefficients poly(x, 2)_1 and
poly(x, 2)_2):
- One
object Resp : Ndeclaration per response plate. - One
object G : Kdeclaration per random-effect grouping factor (withKlevels). - For each fixed-design column
c: one scalar latent draw inside the program body,beta_c <- Normal(0, fixed_prior), whose scale is autoscaled by1 / rms(c)so the nominal value reads in contribution space and means the same thing on a raw predictor and on an orthonormalpolycolumn. An explicit per-name prior is emitted as written. The per-row covariate values forcflow in as a free variable via the host-data channel (observations[c]). - For each random-effect group
(slope | g): aHalfNormalscale latent plus a per-level plate draw, with the per-row contribution as a plate-gatheralpha_g[g_idx](orbeta_g_slope[g_idx] * slopefor a random slope). Multiple random slopes per group are emitted as independent random-effect terms (the uncorrelated /(... || g)semantics in lme4). - One
observestep closes the program with the family's observation kernel applied to the inverse-link of the linear predictor. - Categorical responses use category zero as an identified reference
and lift every fixed and random term over the remaining
K - 1logits before applying softmax. - Gaussian mixtures use a configurable component axis, shared softmax weights, centered component offsets around the formula predictor, and one positive scale per component.
GetPut holds for every Formula f: backward(*forward(f)) == f.
FormulaToQVRModule
¶
FormulaToQVRModule(family: Family, *, fixed_prior: str = 'Normal(0.0, 5.0)', random_scale_prior: str = 'HalfNormal(1.0)', user_priors: Mapping[str, str] | None = None, reparameterize: Literal['centered', 'noncentered'] = 'noncentered', binomial_trials: int | str = 1, thresholds_by: str | None = None, mixture_components: int = 2, predictor_name: str | None = None)
Bases: Lens[Formula, Module, FormulaData]
Translate a Formula to a QVR Module AST.
A typed didactic.api.Lens whose complement is a
FormulaData carrier containing the fields of the source
Formula that are not recoverable from the emitted
Module. The per-row data arrays, the original (pre-
_qvr_name) identifiers, the per-column term / name
presentation labels, and the original formula string travel in
the complement; everything else (which columns there are, the
intercept / random-term structure, the family choice) is decoded
back out of the Module by _decode_module.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
Response family from
TYPE:
|
fixed_prior
|
Default prior for fixed-effect coefficients, in the surface
form
TYPE:
|
random_scale_prior
|
Default prior for random-effect scale parameters.
TYPE:
|
user_priors
|
Per-name prior overrides keyed by the latent's variable name in the emitted module.
TYPE:
|
reparameterize
|
Random-effect parameterization. Categorical random effects use the non-centered form.
TYPE:
|
binomial_trials
|
Common trial count or the name of a per-row trial-count column.
TYPE:
|
thresholds_by
|
Grouping factor whose levels receive partially pooled ordinal cutpoint spacings.
TYPE:
|
mixture_components
|
Number of components for
TYPE:
|
predictor_name
|
Host-data name for an external differentiable predictor.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an option is out of range or does not apply to |
Notes
GetPut: backward (forward(f)) = f for every
Formula f. PutGet holds on pairs (t, c) for
which t is in the image of forward and c is the
corresponding FormulaData.
Source code in src/quivers/formulas/compile.py
forward
¶
forward(formula: Formula) -> tuple[Module, FormulaData]
Compile a formula to a QVR module and its complement.
| PARAMETER | DESCRIPTION |
|---|---|
formula
|
The parsed formula with its design data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Module, FormulaData]
|
The emitted QVR module AST, and the fields of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the response is incompatible with the family (for
instance non-integer or non-contiguous category labels
for a categorical or cumulative family), or a prior does
not parse as |
Source code in src/quivers/formulas/compile.py
backward
¶
backward(target: Module, complement: FormulaData) -> Formula
Recover a formula from a QVR module and its complement.
The column, intercept, and random-term structure is decoded
from target; the data arrays and presentation labels come
from complement.
| PARAMETER | DESCRIPTION |
|---|---|
target
|
A QVR module in the image of
TYPE:
|
complement
|
The complement
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Formula
|
The reconstructed formula. |
Source code in src/quivers/formulas/compile.py
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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | |
fixed_column_observations
¶
Per-column free-variable bindings for the host-data
channel. One entry per non-intercept fixed column, shape
(N,).
| PARAMETER | DESCRIPTION |
|---|---|
formula
|
The formula whose fixed-effect columns are bound.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Each non-intercept column's |