quivers.formulas¶
Brms-style formula frontend over the QVR DSL. A user writes a
regression formula and gets a fitted Bayesian model without
hand-writing .qvr source.
formulas
¶
Brms-style formula frontend over the QVR DSL.
A user writes a regression formula (Wilkinson notation, extended with
brms-style random-effect groups) and gets a fitted Bayesian model
without hand-writing .qvr source. The compiler emits programs
that route through the existing QVR DSL surface:
- fixed-effect linear predictors as morphism composition
X >> betaoverobserveddesign-matrix morphisms, - random-effect groups as plate-gather of per-level latent draws,
- responses as
observesites with the family-link kernel (Gaussian / Bernoulli / Categorical / Poisson / NegBin / Cumulative / Beta / Gamma / Student-t / ZIP / hurdle / mixture) registered inquivers.formulas.family.
The implementation reuses formulae
for formula parsing (the Bambi team's pure-Python parser; supports
brms-style (slope | group) random effects, smooth terms, and
custom contrasts) and lifts the resulting DesignMatrices
into a typed Formula didactic.api.Model.
The frontend is the formula→QVR direction of a panproto lens; the QVR DSL is the canonical source of truth, and the formula compiler is a structure-preserving translation from the smaller formula language to the QVR DSL. Future versions will register the formula schema as a panproto protocol so the get/put bidirectional machinery applies.
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')
Bases: Lens[Formula, Module, FormulaData]
Translate a Formula to a QVR Module AST.
A typed didactic.api.Lens whose complement is a
FormulaData carrier: just 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:
|
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
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
fixed_column_observations
¶
fixed_column_observations(formula: Formula) -> dict[str, Tensor]
Per-column free-variable bindings for the host-data
channel. One entry per non-intercept fixed column, shape
(N,).
Source code in src/quivers/formulas/compile.py
473 474 475 476 477 478 479 480 481 482 483 | |
Family
¶
Bases: Model
Response family: observation kernel plus its link and any auxiliary parameters.
The compiler emits, for each family-keyed regression:
- The auxiliary-parameter latent draws (intercept-only by
default; one per parameter named in
aux_params). - A
let eta = <linear predictor>binding the location's linear predictor. - A
let mu = <link.inverse_expr>binding the natural parameter. - A single
observe y : Resp <- <observe_family>(mu, ...)step parameterised bymuand the auxiliary parameters.
Link
¶
Bases: Model
Inverse-link function bridging a linear predictor on
:math:\mathbb{R} to the family's natural parameter space.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Canonical name (e.g.
TYPE:
|
inverse_expr |
QVR let-expression template applied to a linear predictor
TYPE:
|
BayesianFit
¶
Bases: Model
A fitted Bayesian regression: the compiled program, the parsed formula, the family, the user-supplied data, and the posterior samples.
| ATTRIBUTE | DESCRIPTION |
|---|---|
formula |
Parsed formula IR.
TYPE:
|
family |
Response family used at compile time.
TYPE:
|
program |
The compiled program.
TYPE:
|
posterior |
TYPE:
|
observations |
Inference-time observations dict (response + per-column covariates + per-group plate indices).
TYPE:
|
dump_qvr
¶
dump_qvr(path: str | Path) -> Path
Write the AST-equivalent .qvr source to path and
return the resulting Path.
Source code in src/quivers/formulas/_fit.py
79 80 81 82 83 84 85 | |
Formula
¶
Bases: Model
A parsed regression formula plus the data it was parsed against.
| ATTRIBUTE | DESCRIPTION |
|---|---|
formula |
Original formula string.
TYPE:
|
response_name |
Name of the response column.
TYPE:
|
fixed_columns |
One entry per design-matrix column (matches R/brms's one-coefficient-per-column convention).
TYPE:
|
random_terms |
Random-effect group specifications.
TYPE:
|
response_values |
Response column values, shape
TYPE:
|
group_levels |
Canonical level ordering per grouping factor, used to derive deterministic plate-index tensors.
TYPE:
|
group_indices |
Per-group integer index array, shape
TYPE:
|
RandomTerm
¶
Bases: Model
One random-effect group, e.g. (1 | g) or (x | g).
| ATTRIBUTE | DESCRIPTION |
|---|---|
slope |
TYPE:
|
group |
Grouping factor name.
TYPE:
|
fit
¶
fit(formula: str, *, data: IntoDataFrame, family: str | Family = 'gaussian', method: Literal['nuts', 'hmc', 'svi'] = 'nuts', num_warmup: int = 500, num_samples: int = 1000, num_chains: int = 4, fixed_prior: str = 'Normal(0.0, 5.0)', random_scale_prior: str = 'HalfNormal(1.0)', priors: Mapping[str, str] | None = None, guide: type | None = None, reparameterize: Literal['centered', 'noncentered'] = 'noncentered', seed: int = 0) -> BayesianFit
Compile a brms-style formula, fit it, and return the result.
See quivers.formulas for surface details. This entry
point composes formula_from_data, FormulaToQVRModule,
Compiler, and the inference layer in one call.
Source code in src/quivers/formulas/_fit.py
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 | |
formula_to_qvr
¶
formula_to_qvr(formula: str, *, data: IntoDataFrame, family: str | Family = 'gaussian', fixed_prior: str = 'Normal(0.0, 5.0)', random_scale_prior: str = 'HalfNormal(1.0)', priors: Mapping[str, str] | None = None, reparameterize: Literal['centered', 'noncentered'] = 'noncentered', path: str | Path | None = None) -> str
Emit .qvr source for a brms-style formula without fitting.
Builds the formula AST → QVR Module via FormulaToQVRModule,
then serialises the module via quivers.dsl.emit.module_to_source.
Optionally writes the result to path.
Source code in src/quivers/formulas/_fit.py
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 | |
formula_from_data
¶
formula_from_data(formula: str, data: IntoDataFrame, *, extra_namespace: Mapping[str, object] | None = None) -> Formula
Build a typed Formula IR by lifting
formulae.design_matrices over a dataframe.
This is an adapter, not a parser: the brms-style formula syntax
is parsed by the formulae
library; we lift its formulae.matrices.DesignMatrices
result into a typed didactic record, augmented with deterministic
per-group level orderings and integer-code arrays derived from
the dataframe.
The R-style numeric transforms (log, exp, sqrt,
abs, sin, cos, tan, log10, log2,
log1p, expm1, asin, acos, atan, sinh,
cosh, tanh) are pre-loaded into the formulae evaluation
namespace so users coming from R / brms get the expected base
R behaviour without explicit registration. Polynomial terms via
poly(x, k) are orthogonal by default, matching R's
stats::poly.
| PARAMETER | DESCRIPTION |
|---|---|
formula
|
Formula string in brms / lme4 syntax.
TYPE:
|
data
|
Pandas, polars, or any other Narwhals-compatible dataframe.
TYPE:
|
extra_namespace
|
Additional names visible inside the formula's expression evaluation, merged on top of the R-style transforms.
TYPE:
|
Source code in src/quivers/formulas/formula.py
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 | |