quivers.formulas fit entry point¶
The user-facing fit entry point, the formula_to_qvr source emitter,
and the BayesianFit result wrapper.
_fit
¶
User-facing fit entry point and the
BayesianFit result wrapper.
The compilation path is fully AST-driven: the formula lens emits a
quivers.dsl.ast_nodes.Module, the existing
quivers.dsl.compiler.Compiler consumes it directly (no
source-string round-trip), and inference runs on the resulting
quivers.continuous.programs.MonadicProgram. Source text
is generated only when the user requests it via
formula_to_qvr or BayesianFit.dump_qvr, in which
case quivers.dsl.emit.module_to_source walks the same AST
to produce canonical .qvr source.
BayesianFit
didactic-model
¶
BayesianFit(*, formula: Formula = field(opaque=True), family: Family, program: MonadicProgram = field(opaque=True), posterior: MCMCResult | Guide = field(opaque=True), observations: Mapping[str, Tensor] = field(default_factory=dict, opaque=True), reparameterize: Literal['centered', 'noncentered'] = 'noncentered', binomial_trials: int | str = 1, thresholds_by: str | None = None, mixture_components: int = 2, fixed_prior: str = 'Normal(0.0, 5.0)', random_scale_prior: str = 'HalfNormal(1.0)', priors: Mapping[str, str] = field(default_factory=dict, opaque=True), predictor: Module | None = field(default=None, opaque=True), predictor_data: Tensor | None = field(default=None, opaque=True), predictor_name: str | None = None)
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
¶
Write the AST-equivalent .qvr source to path and
return the resulting Path.
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', binomial_trials: int | str = 1, thresholds_by: str | None = None, mixture_components: int = 2, predictor: Module | None = None, predictor_data: Tensor | None = None, 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.
family="categorical" infers contiguous labels 0..K-1 and uses
label zero as the reference. An attached categorical predictor may return
(N, K-1) reference logits or (N, K) full logits. For
family="mixture", mixture_components selects any finite component
count of at least two; the likelihood integrates assignments exactly.
| PARAMETER | DESCRIPTION |
|---|---|
formula
|
brms/lme4-style response formula.
TYPE:
|
data
|
Pandas, Polars, or another Narwhals-compatible dataframe.
TYPE:
|
family
|
Registered response family or a family value.
TYPE:
|
method
|
Inference algorithm.
TYPE:
|
mixture_components
|
Number of Gaussian components for the mixture family.
TYPE:
|
predictor
|
Optional differentiable contribution on the linear-predictor scale.
TYPE:
|
predictor_data
|
Input passed to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BayesianFit
|
Parsed formula, compiled program, fitted posterior, and observations. |
Source code in src/quivers/formulas/_fit.py
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 169 170 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 210 211 212 213 214 215 216 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 | |
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', binomial_trials: int | str = 1, thresholds_by: str | None = None, mixture_components: int = 2, predictor_name: str | None = None, 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. mixture_components selects
any integer component count of at least two for family="mixture".