Inference operations: asymptotic, bootstrap, permutation, cross-validation, profile, and simulation.
Call chain:
model.infer() -> dispatch_infer() -> dispatch_{params,mee,prediction}_inference()
Each dispatcher routes to the method-specific backend (asymptotic, bootstrap, etc.)Classes:
| Name | Description |
|---|---|
InferResult | Immutable result of inference dispatch. |
Functions:
| Name | Description |
|---|---|
augment_spread_with_profile_ci | Augment variance components with profile likelihood confidence intervals. |
build_emm_reference_grid | Build reference grid X matrix for EMM computation. |
compute_bootstrap_params | Generate bootstrap distribution of coefficient estimates. |
compute_bootstrap_pvalue | Compute bootstrap p-values. |
compute_cv_metrics | Compute k-fold or leave-one-out cross-validation metrics. |
compute_jackknife_coefs | Compute leave-one-out jackknife coefficient estimates. |
compute_mee_bootstrap | Compute bootstrap inference for marginal effects. |
compute_mee_permutation | Compute permutation-based inference for marginal effects. |
compute_params_asymptotic | Compute asymptotic (Wald) inference for model parameters. |
compute_params_bootstrap | Compute bootstrap inference for parameters. |
compute_params_bootstrap_mixed | Compute bootstrap inference for mixed model parameters. |
compute_params_cv_inference | Compute CV-based parameter importance via ablation. |
compute_params_permutation | Compute permutation-based inference for model parameters. |
compute_prediction_asymptotic | Compute asymptotic inference for predictions via delta method. |
compute_prediction_bootstrap | Compute bootstrap inference for predictions. |
compute_profile_inference | Compute profile likelihood CIs for variance components. |
compute_satterthwaite_emm_df | Compute Satterthwaite denominator df for EMM contrast rows. |
compute_simulation_inference | Compute inference for simulations. |
dispatch_infer | Dispatch inference to the correct backend based on method and last operation. |
dispatch_mee_inference | Dispatch marginal effects inference to the appropriate method. |
dispatch_params_inference | Dispatch parameter inference to the appropriate method. |
dispatch_prediction_inference | Dispatch prediction inference to the appropriate method. |
generate_group_kfold_splits | Generate group-aware k-fold cross-validation indices. |
generate_kfold_splits | Generate k-fold cross-validation train/test indices. |
Modules:
| Name | Description |
|---|---|
asymptotic | Asymptotic inference for parameters and predictions. |
bootstrap | Bootstrap inference methods. |
cv | Cross-validation inference and metrics. |
dispatch | Infer dispatch: routes .infer() calls to the correct inference backend. |
formula_utils | Formula utility functions shared across operations modules. |
mee | Marginal effects inference dispatch. |
params | Parameter inference dispatch. |
permutation | Permutation inference for parameters and marginal effects. |
prediction | Prediction inference dispatch. |
profile | Profile likelihood inference for variance components. |
resample | Internal resampling operations. |
resample_bundle | Shared helper for resampling DataBundle observations. |
satterthwaite_emm | Satterthwaite degrees of freedom for EMM contrasts in linear mixed models. |
simulation | Simulation inference: summary statistics across simulation replicates. |
Classes¶
InferResult¶
Immutable result of inference dispatch.
Each field corresponds to a model attribute that may be updated by
.infer(). Fields left as None are not modified.
Attributes:
| Name | Type | Description |
|---|---|---|
params_inference | InferenceState | None | Updated InferenceState (fit branch). |
mee | MeeState | JointTestState | None | Updated MeeState or JointTestState (explore/joint branch). |
pred | PredictionState | None | Updated PredictionState (predict branch). |
cv | CVState | None | CVState from cross-validation (predict + cv). |
sim_inference | SimulationInferenceState | None | SimulationInferenceState (simulate branch). |
resamples | ResamplesState | None | ResamplesState from bootstrap/permutation. |
varying_spread | VaryingSpreadState | None | Updated VaryingSpreadState (profile branch). |
profile | ProfileState | None | ProfileState from profile likelihood. |
last_op | str | None | Overridden last_op (only set by joint test branch). |
Attributes¶
cv¶
cv: CVState | None = Nonelast_op¶
last_op: str | None = Nonemee¶
mee: MeeState | JointTestState | None = Noneparams_inference¶
params_inference: InferenceState | None = Nonepred¶
pred: PredictionState | None = Noneprofile¶
profile: ProfileState | None = Noneresamples¶
resamples: ResamplesState | None = Nonesim_inference¶
sim_inference: SimulationInferenceState | None = Nonevarying_spread¶
varying_spread: VaryingSpreadState | None = NoneFunctions¶
augment_spread_with_profile_ci¶
augment_spread_with_profile_ci(spread: 'VaryingSpreadState', profile: 'ProfileState', conf_level: float) -> 'VaryingSpreadState'Augment variance components with profile likelihood confidence intervals.
Maps profile CIs (on the SD scale) to variance component CIs by squaring,
matching component names in spread.components to keys in profile.ci_sd.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spread | ‘VaryingSpreadState’ | Variance component state from .fit(). | required |
profile | ‘ProfileState’ | Profile likelihood state with ci_sd dict. | required |
conf_level | float | Confidence level used for CIs. | required |
Returns:
| Type | Description |
|---|---|
‘VaryingSpreadState’ | Evolved VaryingSpreadState with ci_lower, ci_upper, conf_level, ci_method. |
build_emm_reference_grid¶
build_emm_reference_grid(data: pl.DataFrame, bundle: 'DataBundle', focal_var: str) -> np.ndarrayBuild reference grid X matrix for EMM computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | The data DataFrame. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
focal_var | str | The focal variable for EMMs. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Reference design matrix, shape (n_levels, n_params). |
compute_bootstrap_params¶
compute_bootstrap_params(spec: 'ModelSpec', bundle: 'DataBundle', n_boot: int = 1000, seed: int | None = None, n_jobs: int = 1) -> np.ndarrayGenerate bootstrap distribution of coefficient estimates.
Uses case resampling: resample observations with replacement, refit the model, and collect coefficient estimates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, y, etc. | required |
n_boot | int | Number of bootstrap samples. | 1000 |
seed | int | None | Random seed for reproducibility. | None |
n_jobs | int | Number of parallel jobs. 1 (default) for sequential, -1 for all available cores. | 1 |
Returns:
| Type | Description |
|---|---|
ndarray | Array of shape (n_boot, n_params) with bootstrap coefficient estimates. |
compute_bootstrap_pvalue¶
compute_bootstrap_pvalue(observed: np.ndarray, boot_samples: np.ndarray, null: float = 0.0, alternative: str = 'two-sided') -> np.ndarrayCompute bootstrap p-values.
Uses the proportion of bootstrap samples as extreme or more extreme than the observed value (relative to null).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed | ndarray | Observed statistics. | required |
boot_samples | ndarray | Bootstrap sample statistics, shape (n_boot, n_params). | required |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | “two-sided” (default), “greater”, or “less”. | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
ndarray | P-values for each parameter. |
compute_cv_metrics¶
compute_cv_metrics(spec: 'ModelSpec', bundle: 'DataBundle', k: int | str = 10, seed: int | None = None, holdout_group_ids: np.ndarray | None = None) -> 'CVState'Compute k-fold or leave-one-out cross-validation metrics.
Splits the data into k folds, trains on k-1 folds, and predicts on the held-out fold. Aggregates out-of-sample prediction metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification (family, link, etc.). | required |
bundle | ‘DataBundle’ | Data bundle with X, y, and metadata. | required |
k | int | str | Number of folds (default 10), or "loo" for leave-one-out cross-validation. | 10 |
seed | int | None | Random seed for reproducibility. Ignored for LOO. | None |
holdout_group_ids | ndarray | None | Pre-resolved group ID array for group-aware splits. When provided, entire groups are held out per fold. | None |
Returns:
| Type | Description |
|---|---|
‘CVState’ | CVState with RMSE, MAE, R², and optionally deviance/accuracy. |
compute_jackknife_coefs¶
compute_jackknife_coefs(spec: 'ModelSpec', bundle: 'DataBundle') -> np.ndarrayCompute leave-one-out jackknife coefficient estimates.
Used for BCa acceleration factor calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Array of shape (n, n_params) with jackknife coefficient estimates. |
compute_mee_bootstrap¶
compute_mee_bootstrap(spec: 'ModelSpec', bundle: 'DataBundle', mee: 'MeeState', data: pl.DataFrame, conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'bca', seed: int | None = None, null: float = 0.0, alternative: str = 'two-sided', save_resamples: bool = True) -> 'tuple[MeeState, np.ndarray | None]'Compute bootstrap inference for marginal effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
mee | ‘MeeState’ | Current MEE state. | required |
data | DataFrame | The data DataFrame. | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type (“bca”, “percentile”, “basic”). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
save_resamples | bool | If True (default), return raw bootstrap samples alongside the MeeState. If False, discard to save memory. | True |
Returns:
| Type | Description |
|---|---|
‘tuple[MeeState, np.ndarray | None]’ | Tuple of (MeeState augmented with bootstrap inference, |
‘tuple[MeeState, np.ndarray | None]’ | raw bootstrap samples array or None). |
compute_mee_permutation¶
compute_mee_permutation(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', mee: 'MeeState', data: 'pl.DataFrame', conf_level: float, se_obs: np.ndarray, n_perm: int = 1000, seed: int | None = None, null: float = 0.0, alternative: str = 'two-sided', save_resamples: bool = True) -> 'tuple[MeeState, np.ndarray | None]'Compute permutation-based inference for marginal effects.
Permutes the response variable, refits and re-explores on each permutation, and computes p-values based on the null distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, y, etc. | required |
fit | ‘FitState’ | Fit state with coefficients. | required |
mee | ‘MeeState’ | Current MeeState with observed estimates. | required |
data | ‘pl.DataFrame’ | Original data frame (for categorical detection). | required |
conf_level | float | Confidence level for intervals. | required |
se_obs | ndarray | Pre-computed standard errors for t-statistic denominator (from asymptotic SE computation in the model class). | required |
n_perm | int | Number of permutations. | 1000 |
seed | int | None | Random seed for reproducibility. | None |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
save_resamples | bool | If True (default), return null t-statistics alongside the MeeState. If False, discard to save memory. | True |
Returns:
| Type | Description |
|---|---|
‘tuple[MeeState, np.ndarray | None]’ | Tuple of (MeeState augmented with permutation inference, |
‘tuple[MeeState, np.ndarray | None]’ | null t-statistics array or None). |
compute_params_asymptotic¶
compute_params_asymptotic(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float, errors: str | None = None, data: 'pl.DataFrame | None' = None, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute asymptotic (Wald) inference for model parameters.
Resolves vcov (robust via sandwich or standard from fit), resolves df
(Welch, t, or z), and delegates to compute_coefficient_inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with design matrix X. | required |
fit | ‘FitState’ | Fit state with coefficients and vcov. | required |
conf_level | float | Confidence level for intervals (e.g. 0.95). | required |
errors | str | None | Error type for robust SEs (e.g. “HC3”, “hetero”, “unequal_var”) or None for standard errors. | None |
data | ‘pl.DataFrame | None’ | Original data frame (needed for Welch df with errors=“unequal_var”). | None |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with SEs, test statistics, p-values, and CIs. |
compute_params_bootstrap¶
compute_params_bootstrap(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'bca', seed: int | None = None, save_resamples: bool = True, n_jobs: int = 1, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute bootstrap inference for parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
fit | ‘FitState’ | Current fit state. | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type (“bca”, “percentile”, “basic”). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | If True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory. | True |
n_jobs | int | Number of parallel jobs. 1 (default) for sequential, -1 for all available cores. | 1 |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with bootstrap-based SE, CI, and p-values. |
compute_params_bootstrap_mixed¶
compute_params_bootstrap_mixed(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'bca', seed: int | None = None, save_resamples: bool = True, n_jobs: int = 1, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute bootstrap inference for mixed model parameters.
Uses parametric bootstrap (simulate y* from fitted model, refit) via
the existing lmer_bootstrap() / glmer_bootstrap() infrastructure
instead of observation-level case resampling. This properly respects
the cluster structure and matches R’s bootMer(type="parametric").
BCa is not supported for mixed models (no principled cluster jackknife
definition exists). Use ci_type="percentile" or "basic".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification (must have has_random_effects=True). | required |
bundle | ‘DataBundle’ | Data bundle with RE metadata. | required |
fit | ‘FitState’ | Current fit state with coef, theta, sigma. | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type ("percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | If True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory. | True |
n_jobs | int | Number of parallel jobs. 1 (default) for sequential, -1 for all available cores. | 1 |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default "two-sided"). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with bootstrap-based SE, CI, and p-values. |
compute_params_cv_inference¶
compute_params_cv_inference(spec: 'ModelSpec', bundle: 'DataBundle', data: 'pl.DataFrame', conf_level: float, *, link_override: str | None = None, k: int | str = 10, seed: int | None = None, holdout_group_ids: np.ndarray | None = None) -> tuple['InferenceState', 'CVState']Compute CV-based parameter importance via ablation.
Operates at the source term level rather than the design matrix column level. For each source term (excluding Intercept):
Build an ablated formula from the remaining source terms (plus any random effects). Main-effect ablation also removes interactions that contain that effect (hierarchical principle).
Fit the ablated model and run k-fold CV.
PRE = full_metric − ablated_metric per fold.
Broadcast the per-term PRE to every design matrix column that belongs to that source term.
A positive PRE value means the predictor reduces prediction error (improves model fit).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification for the full model. | required |
bundle | ‘DataBundle’ | Data bundle for the full model. | required |
data | ‘pl.DataFrame’ | Original data frame (polars DataFrame). | required |
conf_level | float | Confidence level (kept for API consistency). | required |
link_override | str | None | Non-default link function override (None uses spec default). | None |
k | int | str | Number of folds for cross-validation (default 10). | 10 |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | Tuple of (InferenceState with pre and pre_sd arrays, CVState with |
‘CVState’ | full-model cross-validation metrics). |
Examples:
>>> m = model("y ~ x + z", data).fit().infer(how="cv")
>>> m.params # Includes pre, pre_sd columns
>>> m.diagnostics # Includes cv_rmse, cv_mae, cv_rsquaredcompute_params_permutation¶
compute_params_permutation(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float, n_perm: int = 1000, seed: int | None = None, save_resamples: bool = True, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute permutation-based inference for model parameters.
Permutes the response variable to break the relationship with predictors, refits the model on each permutation, and computes p-values based on the null distribution of test statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, y, etc. | required |
fit | ‘FitState’ | Fit state with coefficients and vcov. | required |
conf_level | float | Confidence level for intervals. | required |
n_perm | int | Number of permutations. | 1000 |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | If True (default), store null distribution in InferenceState. If False, discard to save memory. | True |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with permutation p-values and percentile CIs. |
compute_prediction_asymptotic¶
compute_prediction_asymptotic(pred: 'PredictionState', bundle: 'DataBundle', fit: 'FitState', spec: 'ModelSpec', conf_level: float) -> 'PredictionState'Compute asymptotic inference for predictions via delta method.
Computes standard errors using the delta method and confidence intervals using t or z critical values depending on the model family.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred | ‘PredictionState’ | Prediction state with fitted values. | required |
bundle | ‘DataBundle’ | Data bundle with design matrix X. | required |
fit | ‘FitState’ | Fit state with vcov and df_resid. | required |
spec | ‘ModelSpec’ | Model specification (family, random effects). | required |
conf_level | float | Confidence level for intervals (e.g. 0.95). | required |
Returns:
| Type | Description |
|---|---|
‘PredictionState’ | Evolved PredictionState with se, ci_lower, ci_upper fields. |
compute_prediction_bootstrap¶
compute_prediction_bootstrap(spec: 'ModelSpec', bundle: 'DataBundle', pred: 'PredictionState', conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'percentile', seed: int | None = None) -> 'PredictionState'Compute bootstrap inference for predictions.
Each bootstrap replicate refits the model on resampled training data,
then runs the same prediction path (inverse link, random effects, etc.)
used by the original predict() call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
pred | ‘PredictionState’ | Current prediction state (must carry config). | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type (“percentile”, “basic”). | ‘percentile’ |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
‘PredictionState’ | PredictionState augmented with bootstrap inference. |
compute_profile_inference¶
compute_profile_inference(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float = 0.95, *, n_steps: int = 20, verbose: bool = False, threshold: float | None = None) -> 'ProfileState'Compute profile likelihood CIs for variance components.
Builds a deviance function adapter from the unified model’s internal state and runs profile_likelihood(). Returns a ProfileState with profile traces, splines, and CIs on the theta and SD scales.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, Z, y, and re_metadata. | required |
fit | ‘FitState’ | Fit state with theta, sigma, and variance components. | required |
conf_level | float | Confidence level for profile CIs. | 0.95 |
n_steps | int | Number of profiling steps (default: 20). | 20 |
verbose | bool | Print profiling progress (default: False). | False |
threshold | float | None | Custom deviance threshold for CIs (default: None, uses chi-squared quantile). | None |
Returns:
| Type | Description |
|---|---|
‘ProfileState’ | ProfileState with profile traces and CIs. |
compute_satterthwaite_emm_df¶
compute_satterthwaite_emm_df(bundle: DataBundle, fit: FitState, spec: ModelSpec, L: np.ndarray) -> np.ndarrayCompute Satterthwaite denominator df for EMM contrast rows.
For each row of the contrast matrix L, computes the Satterthwaite approximation to the denominator degrees of freedom.
Uses a hybrid approach: differentiates numerically w.r.t. theta only, then fills in sigma’s contributions analytically. This produces identical results to the full numerical approach but with ~52% fewer function evaluations for single random-intercept models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bundle | DataBundle | Data bundle with X, Z, y, and re_metadata. | required |
fit | FitState | Fit state with theta, vcov, and sigma. | required |
spec | ModelSpec | Model specification (method, family, etc.). | required |
L | ndarray | Contrast matrix, shape (n_contrasts, n_coef). | required |
Returns:
| Type | Description |
|---|---|
ndarray | Array of Satterthwaite df, shape (n_contrasts,). |
compute_simulation_inference¶
compute_simulation_inference(simulations: 'pl.DataFrame', conf_level: float) -> 'SimulationInferenceState'Compute inference for simulations.
For post-fit simulations (sim_1, sim_2, ...): Computes summary statistics across simulation replicates: - Mean and SD per observation across simulations - Quantiles (e.g., 2.5%, 97.5% for 95% interval)
Returns a basic state indicating generated data. Returns a basic state indicating generated data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulations | ‘pl.DataFrame’ | DataFrame of simulation results. Post-fit simulations have columns named sim_1, sim_2, etc. | required |
conf_level | float | Confidence level for interval quantiles. | required |
Returns:
| Type | Description |
|---|---|
‘SimulationInferenceState’ | SimulationInferenceState with summary statistics. |
dispatch_infer¶
dispatch_infer(*, how: str, conf_level: float, errors: str, null: float, alternative: str, last_op: str | None, spec: ModelSpec, bundle: DataBundle, fit: FitState, data: pl.DataFrame | None, link_override: str | None, mee: MeeState | JointTestState | None, pred: PredictionState | None, simulations: pl.DataFrame | None, varying_spread: VaryingSpreadState | None, is_mixed: bool, n_boot: int = 1000, n_perm: int = 1000, ci_type: str = 'bca', seed: int | None = None, n_jobs: int = 1, save_resamples: bool = True, k: int | str = 10, n_steps: int = 20, verbose: bool = False, threshold: float | None = None, profile_auto: bool = True, holdout_group_ids: np.ndarray | None = None) -> InferResultDispatch inference to the correct backend based on method and last operation.
Pure function that implements the body of model.infer(). Accepts all
required state as explicit parameters and returns an InferResult
with the populated fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "perm", "cv", "profile", "joint"). | required |
conf_level | float | Confidence level (already normalized to 0–1 float). | required |
errors | str | Error structure ("iid", "HC0"–"HC3", "hetero", "unequal_var"). | required |
null | float | Null hypothesis value. | required |
alternative | str | Alternative hypothesis direction. | required |
last_op | str | None | Last model operation ("fit", "explore", "predict", "simulate"). | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
data | DataFrame | None | Original data DataFrame. | required |
link_override | str | None | Non-default link function override (None uses spec). | required |
mee | MeeState | JointTestState | None | Current MeeState or JointTestState from explore. | required |
pred | PredictionState | None | Current PredictionState from predict. | required |
simulations | DataFrame | None | Simulated data DataFrame. | required |
varying_spread | VaryingSpreadState | None | Current VaryingSpreadState. | required |
is_mixed | bool | Whether the model has random effects. | required |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
n_perm | int | Number of permutation samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type ("bca", "percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
n_jobs | int | Number of parallel jobs (default: 1). | 1 |
save_resamples | bool | Whether to store raw resample arrays (default: True). | True |
k | int | str | Number of CV folds or "loo" (default: 10). | 10 |
n_steps | int | Profile likelihood steps (default: 20). | 20 |
verbose | bool | Profile likelihood verbosity (default: False). | False |
threshold | float | None | Profile likelihood deviance threshold (default: None). | None |
profile_auto | bool | Auto-compute profile CIs for variance components when how is "boot" or "perm" on a mixed model (default: True). | True |
Returns:
| Type | Description |
|---|---|
InferResult | InferResult with populated fields for the caller to assign. |
dispatch_mee_inference¶
dispatch_mee_inference(*, how: str, mee: MeeState, spec: ModelSpec, bundle: DataBundle, fit: FitState, data: pl.DataFrame, conf_level: float, errors: str = 'iid', null: float = 0.0, alternative: str = 'two-sided', n_boot: int = 1000, n_perm: int = 1000, ci_type: str = 'bca', seed: int | None = None, save_resamples: bool = True) -> 'tuple[MeeState, np.ndarray | None]'Dispatch marginal effects inference to the appropriate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "perm"). | required |
mee | MeeState | Current MeeState from explore(). | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
data | DataFrame | Original data as Polars DataFrame. | required |
conf_level | float | Confidence level for intervals. | required |
errors | str | Error type for robust SEs (e.g. "HC3", "hetero", "unequal_var"). | ‘iid’ |
null | float | Null hypothesis value. | 0.0 |
alternative | str | Alternative hypothesis direction. | ‘two-sided’ |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
n_perm | int | Number of permutation samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type ("bca", "percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | Whether to store raw resample arrays (default: True). | True |
Returns:
| Type | Description |
|---|---|
‘tuple[MeeState, np.ndarray | None]’ | Tuple of (MeeState augmented with inference, raw resample array |
‘tuple[MeeState, np.ndarray | None]’ | or None). The second element is non-None only for boot/perm |
‘tuple[MeeState, np.ndarray | None]’ | with save_resamples=True. |
dispatch_params_inference¶
dispatch_params_inference(*, how: str, spec: ModelSpec, bundle: DataBundle, fit: FitState, data: pl.DataFrame | None, conf_level: float, errors: str = 'iid', null: float = 0.0, alternative: str = 'two-sided', link_override: str | None = None, n_boot: int = 1000, n_perm: int = 1000, ci_type: str = 'bca', seed: int | None = None, n_jobs: int = 1, save_resamples: bool = True, k: int | str = 10) -> InferenceStateDispatch parameter inference to the appropriate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "perm", "cv"). | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
data | DataFrame | None | Original data DataFrame (needed for CV and robust SEs). | required |
conf_level | float | Confidence level for intervals. | required |
errors | str | Error type for robust SEs (e.g. "HC3", "hetero", "unequal_var"). | ‘iid’ |
null | float | Null hypothesis value. | 0.0 |
alternative | str | Alternative hypothesis direction. | ‘two-sided’ |
link_override | str | None | Non-default link function override for CV (None uses spec). | None |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
n_perm | int | Number of permutation samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type ("bca", "percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
n_jobs | int | Number of parallel jobs (default: 1). | 1 |
save_resamples | bool | Whether to store raw resample arrays (default: True). | True |
k | int | str | Number of CV folds or "loo" (default: 10). | 10 |
Returns:
| Type | Description |
|---|---|
InferenceState | InferenceState with SE, statistics, p-values, and CIs. |
dispatch_prediction_inference¶
dispatch_prediction_inference(*, how: str, pred: PredictionState, spec: ModelSpec, bundle: DataBundle, fit: FitState, conf_level: float, n_boot: int = 1000, ci_type: str = 'percentile', seed: int | None = None, k: int | str = 10, holdout_group_ids: np.ndarray | None = None) -> tuple[PredictionState, CVState | None]Dispatch prediction inference to the appropriate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "cv"). | required |
pred | PredictionState | Current prediction state. | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
conf_level | float | Confidence level for intervals. | required |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type (default: "percentile"). | ‘percentile’ |
seed | int | None | Random seed for reproducibility. | None |
k | int | str | Number of CV folds or "loo" (default: 10). | 10 |
Returns:
| Type | Description |
|---|---|
PredictionState | Tuple of (augmented PredictionState, CVState or None). CVState is |
CVState | None | populated only for how="cv". |
generate_group_kfold_splits¶
generate_group_kfold_splits(group_ids: np.ndarray, k: int, seed: int | None = None) -> list[tuple[np.ndarray, np.ndarray]]Generate group-aware k-fold cross-validation indices.
All observations from a group stay in the same fold, preventing data leakage from shared random effects. Groups are shuffled and assigned to roughly equal-sized folds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_ids | ndarray | Array of group IDs for each observation (length n). | required |
k | int | Number of folds. | required |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of (train_indices, test_indices) tuples, one per fold. |
Examples:
>>> group_ids = np.array([0, 0, 1, 1, 2, 2, 3, 3])
>>> splits = generate_group_kfold_splits(group_ids, k=2, seed=42)
>>> len(splits)
2generate_kfold_splits¶
generate_kfold_splits(n: int, k: int, seed: int | None = None) -> list[tuple[np.ndarray, np.ndarray]]Generate k-fold cross-validation train/test indices.
Shuffles the indices and splits into k roughly equal-sized folds. Each fold is used exactly once as the test set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n | int | Total number of observations. | required |
k | int | Number of folds. | required |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of (train_indices, test_indices) tuples, one per fold. |
Examples:
>>> splits = generate_kfold_splits(100, k=10, seed=42)
>>> len(splits)
10
>>> train_idx, test_idx = splits[0]
>>> len(test_idx) # ~10 observations per fold
10Modules¶
asymptotic¶
Asymptotic inference for parameters and predictions.
Provides orchestration functions for asymptotic (Wald-based) inference: choosing vcov (robust vs standard), choosing df (Welch vs t vs z), and wiring results into InferenceState/PredictionState containers.
The underlying math lives in internal/maths/inference/. This module
is pure orchestration — it accepts containers and returns containers.
Functions:
| Name | Description |
|---|---|
augment_spread_with_profile_ci | Augment variance components with profile likelihood confidence intervals. |
compute_params_asymptotic | Compute asymptotic (Wald) inference for model parameters. |
compute_prediction_asymptotic | Compute asymptotic inference for predictions via delta method. |
resolve_error_type | Normalize errors parameter to canonical type string. |
resolve_welch | Compute both Welch sandwich vcov and per-coefficient Satterthwaite df. |
Attributes¶
Classes¶
Functions¶
augment_spread_with_profile_ci¶
augment_spread_with_profile_ci(spread: 'VaryingSpreadState', profile: 'ProfileState', conf_level: float) -> 'VaryingSpreadState'Augment variance components with profile likelihood confidence intervals.
Maps profile CIs (on the SD scale) to variance component CIs by squaring,
matching component names in spread.components to keys in profile.ci_sd.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spread | ‘VaryingSpreadState’ | Variance component state from .fit(). | required |
profile | ‘ProfileState’ | Profile likelihood state with ci_sd dict. | required |
conf_level | float | Confidence level used for CIs. | required |
Returns:
| Type | Description |
|---|---|
‘VaryingSpreadState’ | Evolved VaryingSpreadState with ci_lower, ci_upper, conf_level, ci_method. |
compute_params_asymptotic¶
compute_params_asymptotic(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float, errors: str | None = None, data: 'pl.DataFrame | None' = None, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute asymptotic (Wald) inference for model parameters.
Resolves vcov (robust via sandwich or standard from fit), resolves df
(Welch, t, or z), and delegates to compute_coefficient_inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with design matrix X. | required |
fit | ‘FitState’ | Fit state with coefficients and vcov. | required |
conf_level | float | Confidence level for intervals (e.g. 0.95). | required |
errors | str | None | Error type for robust SEs (e.g. “HC3”, “hetero”, “unequal_var”) or None for standard errors. | None |
data | ‘pl.DataFrame | None’ | Original data frame (needed for Welch df with errors=“unequal_var”). | None |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with SEs, test statistics, p-values, and CIs. |
compute_prediction_asymptotic¶
compute_prediction_asymptotic(pred: 'PredictionState', bundle: 'DataBundle', fit: 'FitState', spec: 'ModelSpec', conf_level: float) -> 'PredictionState'Compute asymptotic inference for predictions via delta method.
Computes standard errors using the delta method and confidence intervals using t or z critical values depending on the model family.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred | ‘PredictionState’ | Prediction state with fitted values. | required |
bundle | ‘DataBundle’ | Data bundle with design matrix X. | required |
fit | ‘FitState’ | Fit state with vcov and df_resid. | required |
spec | ‘ModelSpec’ | Model specification (family, random effects). | required |
conf_level | float | Confidence level for intervals (e.g. 0.95). | required |
Returns:
| Type | Description |
|---|---|
‘PredictionState’ | Evolved PredictionState with se, ci_lower, ci_upper fields. |
resolve_error_type¶
resolve_error_type(errors: str, family: str = 'gaussian') -> strNormalize errors parameter to canonical type string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
errors | str | User-facing error type (e.g. “HC3”, “hetero”, “unequal_var”). | required |
family | str | Model family. When non-gaussian, “hetero” maps to HC0 (matching main branch behavior) instead of HC3. | ‘gaussian’ |
Returns:
| Type | Description |
|---|---|
str | Canonical type string (“HC0”-“HC3” or “unequal_var”). |
resolve_welch¶
resolve_welch(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', data: 'pl.DataFrame | None') -> tuple[np.ndarray, np.ndarray]Compute both Welch sandwich vcov and per-coefficient Satterthwaite df.
Shares the cell_info computation between vcov and df so the factor grouping is done only once. Returns per-coefficient df via the generalized Satterthwaite decomposition of the sandwich variance.
For Gaussian mixed models (lmer), this provides a repeated-measures
Welch analog: per-cell variance of marginal residuals y - X @ beta
is used instead of raw response variance, accounting for the fixed-effect
structure while allowing unequal variances across factor cells.
Note:
This differs from R’s approach. R’s sandwich/emmeans/coeftest
use residual df (n - p) with sandwich SEs, ignoring the df correction
entirely. Bossanova instead computes per-coefficient Satterthwaite df,
which generalizes the classic Welch t-test to a regression framework:
for a two-group y ~ factor(group) model, the slope’s df exactly
matches scipy.stats.ttest_ind(equal_var=False) and R’s
t.test(var.equal=FALSE).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with design matrix X and valid_mask. | required |
fit | ‘FitState’ | Fit state with residuals. | required |
data | ‘pl.DataFrame | None’ | Original data frame. | required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (welch_vcov, welch_df) where welch_df has shape (p,). |
bootstrap¶
Bootstrap inference methods.
Provides helper functions for bootstrap-based inference used by the model class’s infer(how=‘boot’) method. Resampling logic is shared via build_resampled_bundle().
Functions:
| Name | Description |
|---|---|
build_emm_reference_grid | Build reference grid X matrix for EMM computation. |
compute_bootstrap_params | Generate bootstrap distribution of coefficient estimates. |
compute_bootstrap_pvalue | Compute bootstrap p-values. |
compute_jackknife_coefs | Compute leave-one-out jackknife coefficient estimates. |
compute_mee_bootstrap | Compute bootstrap inference for marginal effects. |
compute_params_bootstrap | Compute bootstrap inference for parameters. |
compute_params_bootstrap_mixed | Compute bootstrap inference for mixed model parameters. |
compute_prediction_bootstrap | Compute bootstrap inference for predictions. |
Classes¶
Functions¶
build_emm_reference_grid¶
build_emm_reference_grid(data: pl.DataFrame, bundle: 'DataBundle', focal_var: str) -> np.ndarrayBuild reference grid X matrix for EMM computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | The data DataFrame. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
focal_var | str | The focal variable for EMMs. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Reference design matrix, shape (n_levels, n_params). |
compute_bootstrap_params¶
compute_bootstrap_params(spec: 'ModelSpec', bundle: 'DataBundle', n_boot: int = 1000, seed: int | None = None, n_jobs: int = 1) -> np.ndarrayGenerate bootstrap distribution of coefficient estimates.
Uses case resampling: resample observations with replacement, refit the model, and collect coefficient estimates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, y, etc. | required |
n_boot | int | Number of bootstrap samples. | 1000 |
seed | int | None | Random seed for reproducibility. | None |
n_jobs | int | Number of parallel jobs. 1 (default) for sequential, -1 for all available cores. | 1 |
Returns:
| Type | Description |
|---|---|
ndarray | Array of shape (n_boot, n_params) with bootstrap coefficient estimates. |
compute_bootstrap_pvalue¶
compute_bootstrap_pvalue(observed: np.ndarray, boot_samples: np.ndarray, null: float = 0.0, alternative: str = 'two-sided') -> np.ndarrayCompute bootstrap p-values.
Uses the proportion of bootstrap samples as extreme or more extreme than the observed value (relative to null).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed | ndarray | Observed statistics. | required |
boot_samples | ndarray | Bootstrap sample statistics, shape (n_boot, n_params). | required |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | “two-sided” (default), “greater”, or “less”. | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
ndarray | P-values for each parameter. |
compute_jackknife_coefs¶
compute_jackknife_coefs(spec: 'ModelSpec', bundle: 'DataBundle') -> np.ndarrayCompute leave-one-out jackknife coefficient estimates.
Used for BCa acceleration factor calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Array of shape (n, n_params) with jackknife coefficient estimates. |
compute_mee_bootstrap¶
compute_mee_bootstrap(spec: 'ModelSpec', bundle: 'DataBundle', mee: 'MeeState', data: pl.DataFrame, conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'bca', seed: int | None = None, null: float = 0.0, alternative: str = 'two-sided', save_resamples: bool = True) -> 'tuple[MeeState, np.ndarray | None]'Compute bootstrap inference for marginal effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
mee | ‘MeeState’ | Current MEE state. | required |
data | DataFrame | The data DataFrame. | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type (“bca”, “percentile”, “basic”). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
save_resamples | bool | If True (default), return raw bootstrap samples alongside the MeeState. If False, discard to save memory. | True |
Returns:
| Type | Description |
|---|---|
‘tuple[MeeState, np.ndarray | None]’ | Tuple of (MeeState augmented with bootstrap inference, |
‘tuple[MeeState, np.ndarray | None]’ | raw bootstrap samples array or None). |
compute_params_bootstrap¶
compute_params_bootstrap(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'bca', seed: int | None = None, save_resamples: bool = True, n_jobs: int = 1, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute bootstrap inference for parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
fit | ‘FitState’ | Current fit state. | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type (“bca”, “percentile”, “basic”). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | If True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory. | True |
n_jobs | int | Number of parallel jobs. 1 (default) for sequential, -1 for all available cores. | 1 |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with bootstrap-based SE, CI, and p-values. |
compute_params_bootstrap_mixed¶
compute_params_bootstrap_mixed(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'bca', seed: int | None = None, save_resamples: bool = True, n_jobs: int = 1, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute bootstrap inference for mixed model parameters.
Uses parametric bootstrap (simulate y* from fitted model, refit) via
the existing lmer_bootstrap() / glmer_bootstrap() infrastructure
instead of observation-level case resampling. This properly respects
the cluster structure and matches R’s bootMer(type="parametric").
BCa is not supported for mixed models (no principled cluster jackknife
definition exists). Use ci_type="percentile" or "basic".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification (must have has_random_effects=True). | required |
bundle | ‘DataBundle’ | Data bundle with RE metadata. | required |
fit | ‘FitState’ | Current fit state with coef, theta, sigma. | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type ("percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | If True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory. | True |
n_jobs | int | Number of parallel jobs. 1 (default) for sequential, -1 for all available cores. | 1 |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default "two-sided"). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with bootstrap-based SE, CI, and p-values. |
compute_prediction_bootstrap¶
compute_prediction_bootstrap(spec: 'ModelSpec', bundle: 'DataBundle', pred: 'PredictionState', conf_level: float = 0.95, n_boot: int = 1000, ci_type: str = 'percentile', seed: int | None = None) -> 'PredictionState'Compute bootstrap inference for predictions.
Each bootstrap replicate refits the model on resampled training data,
then runs the same prediction path (inverse link, random effects, etc.)
used by the original predict() call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle. | required |
pred | ‘PredictionState’ | Current prediction state (must carry config). | required |
conf_level | float | Confidence level for intervals. | 0.95 |
n_boot | int | Number of bootstrap samples. | 1000 |
ci_type | str | CI type (“percentile”, “basic”). | ‘percentile’ |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
‘PredictionState’ | PredictionState augmented with bootstrap inference. |
cv¶
Cross-validation inference and metrics.
Provides k-fold cross-validation for model evaluation: splits data into folds, fits on training folds, predicts on held-out folds, and computes out-of-sample metrics (RMSE, MAE, R², deviance, accuracy).
For mixed models, group-aware splitting ensures all observations from a
group stay in the same fold (generate_group_kfold_splits).
Functions:
| Name | Description |
|---|---|
compute_cv_metrics | Compute k-fold or leave-one-out cross-validation metrics. |
compute_params_cv_inference | Compute CV-based parameter importance via ablation. |
generate_group_kfold_splits | Generate group-aware k-fold cross-validation indices. |
generate_kfold_splits | Generate k-fold cross-validation train/test indices. |
resolve_holdout_group_ids | Resolve holdout group IDs for group-aware CV splitting. |
Attributes¶
Classes¶
Functions¶
compute_cv_metrics¶
compute_cv_metrics(spec: 'ModelSpec', bundle: 'DataBundle', k: int | str = 10, seed: int | None = None, holdout_group_ids: np.ndarray | None = None) -> 'CVState'Compute k-fold or leave-one-out cross-validation metrics.
Splits the data into k folds, trains on k-1 folds, and predicts on the held-out fold. Aggregates out-of-sample prediction metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification (family, link, etc.). | required |
bundle | ‘DataBundle’ | Data bundle with X, y, and metadata. | required |
k | int | str | Number of folds (default 10), or "loo" for leave-one-out cross-validation. | 10 |
seed | int | None | Random seed for reproducibility. Ignored for LOO. | None |
holdout_group_ids | ndarray | None | Pre-resolved group ID array for group-aware splits. When provided, entire groups are held out per fold. | None |
Returns:
| Type | Description |
|---|---|
‘CVState’ | CVState with RMSE, MAE, R², and optionally deviance/accuracy. |
compute_params_cv_inference¶
compute_params_cv_inference(spec: 'ModelSpec', bundle: 'DataBundle', data: 'pl.DataFrame', conf_level: float, *, link_override: str | None = None, k: int | str = 10, seed: int | None = None, holdout_group_ids: np.ndarray | None = None) -> tuple['InferenceState', 'CVState']Compute CV-based parameter importance via ablation.
Operates at the source term level rather than the design matrix column level. For each source term (excluding Intercept):
Build an ablated formula from the remaining source terms (plus any random effects). Main-effect ablation also removes interactions that contain that effect (hierarchical principle).
Fit the ablated model and run k-fold CV.
PRE = full_metric − ablated_metric per fold.
Broadcast the per-term PRE to every design matrix column that belongs to that source term.
A positive PRE value means the predictor reduces prediction error (improves model fit).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification for the full model. | required |
bundle | ‘DataBundle’ | Data bundle for the full model. | required |
data | ‘pl.DataFrame’ | Original data frame (polars DataFrame). | required |
conf_level | float | Confidence level (kept for API consistency). | required |
link_override | str | None | Non-default link function override (None uses spec default). | None |
k | int | str | Number of folds for cross-validation (default 10). | 10 |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | Tuple of (InferenceState with pre and pre_sd arrays, CVState with |
‘CVState’ | full-model cross-validation metrics). |
Examples:
>>> m = model("y ~ x + z", data).fit().infer(how="cv")
>>> m.params # Includes pre, pre_sd columns
>>> m.diagnostics # Includes cv_rmse, cv_mae, cv_rsquaredgenerate_group_kfold_splits¶
generate_group_kfold_splits(group_ids: np.ndarray, k: int, seed: int | None = None) -> list[tuple[np.ndarray, np.ndarray]]Generate group-aware k-fold cross-validation indices.
All observations from a group stay in the same fold, preventing data leakage from shared random effects. Groups are shuffled and assigned to roughly equal-sized folds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_ids | ndarray | Array of group IDs for each observation (length n). | required |
k | int | Number of folds. | required |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of (train_indices, test_indices) tuples, one per fold. |
Examples:
>>> group_ids = np.array([0, 0, 1, 1, 2, 2, 3, 3])
>>> splits = generate_group_kfold_splits(group_ids, k=2, seed=42)
>>> len(splits)
2generate_kfold_splits¶
generate_kfold_splits(n: int, k: int, seed: int | None = None) -> list[tuple[np.ndarray, np.ndarray]]Generate k-fold cross-validation train/test indices.
Shuffles the indices and splits into k roughly equal-sized folds. Each fold is used exactly once as the test set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n | int | Total number of observations. | required |
k | int | Number of folds. | required |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of (train_indices, test_indices) tuples, one per fold. |
Examples:
>>> splits = generate_kfold_splits(100, k=10, seed=42)
>>> len(splits)
10
>>> train_idx, test_idx = splits[0]
>>> len(test_idx) # ~10 observations per fold
10resolve_holdout_group_ids¶
resolve_holdout_group_ids(holdout_group: str | None, data: 'pl.DataFrame | None', spec: 'ModelSpec', bundle: 'DataBundle') -> np.ndarray | NoneResolve holdout group IDs for group-aware CV splitting.
Logic:
If
holdout_groupis provided, look up the column indata.If any model has random effects:
Single or nested RE: auto-use the primary grouping factor.
Crossed REs: raise with a helpful error listing available vars.
No RE and no holdout_group: return None (use naive splits).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
holdout_group | str | None | User-specified column name, or None. | required |
data | ‘pl.DataFrame | None’ | Original DataFrame (may contain non-formula columns). | required |
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with optional RE metadata. | required |
Returns:
| Type | Description |
|---|---|
ndarray | None | Integer-encoded group ID array (length n), or None. |
dispatch¶
Infer dispatch: routes .infer() calls to the correct inference backend.
Extracts the dispatch logic from model/core.py into a pure function. Accepts all state objects as explicit params, returns an InferResult container with populated fields.
Classes:
| Name | Description |
|---|---|
InferResult | Immutable result of inference dispatch. |
Functions:
| Name | Description |
|---|---|
dispatch_infer | Dispatch inference to the correct backend based on method and last operation. |
Classes¶
InferResult¶
Immutable result of inference dispatch.
Each field corresponds to a model attribute that may be updated by
.infer(). Fields left as None are not modified.
Attributes:
| Name | Type | Description |
|---|---|---|
params_inference | InferenceState | None | Updated InferenceState (fit branch). |
mee | MeeState | JointTestState | None | Updated MeeState or JointTestState (explore/joint branch). |
pred | PredictionState | None | Updated PredictionState (predict branch). |
cv | CVState | None | CVState from cross-validation (predict + cv). |
sim_inference | SimulationInferenceState | None | SimulationInferenceState (simulate branch). |
resamples | ResamplesState | None | ResamplesState from bootstrap/permutation. |
varying_spread | VaryingSpreadState | None | Updated VaryingSpreadState (profile branch). |
profile | ProfileState | None | ProfileState from profile likelihood. |
last_op | str | None | Overridden last_op (only set by joint test branch). |
Attributes¶
cv¶
cv: CVState | None = Nonelast_op¶
last_op: str | None = Nonemee¶
mee: MeeState | JointTestState | None = Noneparams_inference¶
params_inference: InferenceState | None = Nonepred¶
pred: PredictionState | None = Noneprofile¶
profile: ProfileState | None = Noneresamples¶
resamples: ResamplesState | None = Nonesim_inference¶
sim_inference: SimulationInferenceState | None = Nonevarying_spread¶
varying_spread: VaryingSpreadState | None = NoneFunctions¶
dispatch_infer¶
dispatch_infer(*, how: str, conf_level: float, errors: str, null: float, alternative: str, last_op: str | None, spec: ModelSpec, bundle: DataBundle, fit: FitState, data: pl.DataFrame | None, link_override: str | None, mee: MeeState | JointTestState | None, pred: PredictionState | None, simulations: pl.DataFrame | None, varying_spread: VaryingSpreadState | None, is_mixed: bool, n_boot: int = 1000, n_perm: int = 1000, ci_type: str = 'bca', seed: int | None = None, n_jobs: int = 1, save_resamples: bool = True, k: int | str = 10, n_steps: int = 20, verbose: bool = False, threshold: float | None = None, profile_auto: bool = True, holdout_group_ids: np.ndarray | None = None) -> InferResultDispatch inference to the correct backend based on method and last operation.
Pure function that implements the body of model.infer(). Accepts all
required state as explicit parameters and returns an InferResult
with the populated fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "perm", "cv", "profile", "joint"). | required |
conf_level | float | Confidence level (already normalized to 0–1 float). | required |
errors | str | Error structure ("iid", "HC0"–"HC3", "hetero", "unequal_var"). | required |
null | float | Null hypothesis value. | required |
alternative | str | Alternative hypothesis direction. | required |
last_op | str | None | Last model operation ("fit", "explore", "predict", "simulate"). | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
data | DataFrame | None | Original data DataFrame. | required |
link_override | str | None | Non-default link function override (None uses spec). | required |
mee | MeeState | JointTestState | None | Current MeeState or JointTestState from explore. | required |
pred | PredictionState | None | Current PredictionState from predict. | required |
simulations | DataFrame | None | Simulated data DataFrame. | required |
varying_spread | VaryingSpreadState | None | Current VaryingSpreadState. | required |
is_mixed | bool | Whether the model has random effects. | required |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
n_perm | int | Number of permutation samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type ("bca", "percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
n_jobs | int | Number of parallel jobs (default: 1). | 1 |
save_resamples | bool | Whether to store raw resample arrays (default: True). | True |
k | int | str | Number of CV folds or "loo" (default: 10). | 10 |
n_steps | int | Profile likelihood steps (default: 20). | 20 |
verbose | bool | Profile likelihood verbosity (default: False). | False |
threshold | float | None | Profile likelihood deviance threshold (default: None). | None |
profile_auto | bool | Auto-compute profile CIs for variance components when how is "boot" or "perm" on a mixed model (default: True). | True |
Returns:
| Type | Description |
|---|---|
InferResult | InferResult with populated fields for the caller to assign. |
formula_utils¶
Formula utility functions shared across operations modules.
Functions:
| Name | Description |
|---|---|
build_ablated_formula | Build a formula with one source term ablated. |
parse_value | Parse a single value as float or string. |
remove_predictor_from_formula | Remove a predictor term from a formula string. |
Functions¶
build_ablated_formula¶
build_ablated_formula(response_var: str, source_terms: list[str], ablate_term: str, random_terms: tuple[str, ...] = ()) -> strBuild a formula with one source term ablated.
For main effects, also removes interactions containing that term (hierarchical principle). For interactions, removes only that interaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_var | str | Name of the response variable. | required |
source_terms | list[str] | All source terms (excluding Intercept) in the model. | required |
ablate_term | str | The term to remove. | required |
random_terms | tuple[str, ...] | Random effect terms (e.g., ``("(1 | group)",)``). |
Returns:
| Type | Description |
|---|---|
str | Formula string with the ablated term removed. |
Examples:
>>> build_ablated_formula("y", ["x", "z", "x:z"], "x")
'y ~ z'
>>> build_ablated_formula("y", ["x", "z", "x:z"], "x:z")
'y ~ x + z'
>>> build_ablated_formula("y", ["x"], "x")
'y ~ 1'
>>> build_ablated_formula("y", ["x", "z"], "x", ("(1|group)",))
'y ~ z + (1|group)'parse_value¶
parse_value(s: str) -> float | strParse a single value as float or string.
Tries to parse as float first; falls back to unquoted or quoted string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s | str | String to parse. | required |
Returns:
| Type | Description |
|---|---|
float | str | Parsed value as float or string. |
Examples:
>>> parse_value("3.14")
3.14
>>> parse_value("hello")
'hello'
>>> parse_value("'quoted'")
'quoted'remove_predictor_from_formula¶
remove_predictor_from_formula(formula: str, predictor: str) -> strRemove a predictor term from a formula string.
Handles various predictor formats including raw names, factor(), C(), c().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula | str | R-style formula string (e.g., “y ~ x1 + x2”). | required |
predictor | str | Name of predictor to remove. | required |
Returns:
| Type | Description |
|---|---|
str | Modified formula with predictor removed, or original if removal fails. |
Examples:
remove_predictor_from_formula("y ~ a + b + c", "b")
# 'y ~ a + c'
remove_predictor_from_formula("y ~ factor(a) + b", "a")
# 'y ~ b'mee¶
Marginal effects inference dispatch.
Routes MEE inference requests to the appropriate method: asymptotic (delta method), bootstrap, or permutation.
Functions:
| Name | Description |
|---|---|
dispatch_mee_inference | Dispatch marginal effects inference to the appropriate method. |
Classes¶
Functions¶
dispatch_mee_inference¶
dispatch_mee_inference(*, how: str, mee: MeeState, spec: ModelSpec, bundle: DataBundle, fit: FitState, data: pl.DataFrame, conf_level: float, errors: str = 'iid', null: float = 0.0, alternative: str = 'two-sided', n_boot: int = 1000, n_perm: int = 1000, ci_type: str = 'bca', seed: int | None = None, save_resamples: bool = True) -> 'tuple[MeeState, np.ndarray | None]'Dispatch marginal effects inference to the appropriate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "perm"). | required |
mee | MeeState | Current MeeState from explore(). | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
data | DataFrame | Original data as Polars DataFrame. | required |
conf_level | float | Confidence level for intervals. | required |
errors | str | Error type for robust SEs (e.g. "HC3", "hetero", "unequal_var"). | ‘iid’ |
null | float | Null hypothesis value. | 0.0 |
alternative | str | Alternative hypothesis direction. | ‘two-sided’ |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
n_perm | int | Number of permutation samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type ("bca", "percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | Whether to store raw resample arrays (default: True). | True |
Returns:
| Type | Description |
|---|---|
‘tuple[MeeState, np.ndarray | None]’ | Tuple of (MeeState augmented with inference, raw resample array |
‘tuple[MeeState, np.ndarray | None]’ | or None). The second element is non-None only for boot/perm |
‘tuple[MeeState, np.ndarray | None]’ | with save_resamples=True. |
params¶
Parameter inference dispatch.
Routes parameter inference requests to the appropriate method: asymptotic (Wald-based), bootstrap, permutation, or cross-validation.
Functions:
| Name | Description |
|---|---|
dispatch_params_inference | Dispatch parameter inference to the appropriate method. |
Classes¶
Functions¶
dispatch_params_inference¶
dispatch_params_inference(*, how: str, spec: ModelSpec, bundle: DataBundle, fit: FitState, data: pl.DataFrame | None, conf_level: float, errors: str = 'iid', null: float = 0.0, alternative: str = 'two-sided', link_override: str | None = None, n_boot: int = 1000, n_perm: int = 1000, ci_type: str = 'bca', seed: int | None = None, n_jobs: int = 1, save_resamples: bool = True, k: int | str = 10) -> InferenceStateDispatch parameter inference to the appropriate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "perm", "cv"). | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
data | DataFrame | None | Original data DataFrame (needed for CV and robust SEs). | required |
conf_level | float | Confidence level for intervals. | required |
errors | str | Error type for robust SEs (e.g. "HC3", "hetero", "unequal_var"). | ‘iid’ |
null | float | Null hypothesis value. | 0.0 |
alternative | str | Alternative hypothesis direction. | ‘two-sided’ |
link_override | str | None | Non-default link function override for CV (None uses spec). | None |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
n_perm | int | Number of permutation samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type ("bca", "percentile", "basic"). | ‘bca’ |
seed | int | None | Random seed for reproducibility. | None |
n_jobs | int | Number of parallel jobs (default: 1). | 1 |
save_resamples | bool | Whether to store raw resample arrays (default: True). | True |
k | int | str | Number of CV folds or "loo" (default: 10). | 10 |
Returns:
| Type | Description |
|---|---|
InferenceState | InferenceState with SE, statistics, p-values, and CIs. |
permutation¶
Permutation inference for parameters and marginal effects.
Provides permutation test functions: permute the response variable, refit the model, collect null distributions of test statistics, and compute permutation p-values. Accepts containers and returns containers.
Functions:
| Name | Description |
|---|---|
compute_mee_permutation | Compute permutation-based inference for marginal effects. |
compute_params_permutation | Compute permutation-based inference for model parameters. |
Classes¶
Functions¶
compute_mee_permutation¶
compute_mee_permutation(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', mee: 'MeeState', data: 'pl.DataFrame', conf_level: float, se_obs: np.ndarray, n_perm: int = 1000, seed: int | None = None, null: float = 0.0, alternative: str = 'two-sided', save_resamples: bool = True) -> 'tuple[MeeState, np.ndarray | None]'Compute permutation-based inference for marginal effects.
Permutes the response variable, refits and re-explores on each permutation, and computes p-values based on the null distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, y, etc. | required |
fit | ‘FitState’ | Fit state with coefficients. | required |
mee | ‘MeeState’ | Current MeeState with observed estimates. | required |
data | ‘pl.DataFrame’ | Original data frame (for categorical detection). | required |
conf_level | float | Confidence level for intervals. | required |
se_obs | ndarray | Pre-computed standard errors for t-statistic denominator (from asymptotic SE computation in the model class). | required |
n_perm | int | Number of permutations. | 1000 |
seed | int | None | Random seed for reproducibility. | None |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
save_resamples | bool | If True (default), return null t-statistics alongside the MeeState. If False, discard to save memory. | True |
Returns:
| Type | Description |
|---|---|
‘tuple[MeeState, np.ndarray | None]’ | Tuple of (MeeState augmented with permutation inference, |
‘tuple[MeeState, np.ndarray | None]’ | null t-statistics array or None). |
compute_params_permutation¶
compute_params_permutation(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float, n_perm: int = 1000, seed: int | None = None, save_resamples: bool = True, null: float = 0.0, alternative: str = 'two-sided') -> 'InferenceState'Compute permutation-based inference for model parameters.
Permutes the response variable to break the relationship with predictors, refits the model on each permutation, and computes p-values based on the null distribution of test statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, y, etc. | required |
fit | ‘FitState’ | Fit state with coefficients and vcov. | required |
conf_level | float | Confidence level for intervals. | required |
n_perm | int | Number of permutations. | 1000 |
seed | int | None | Random seed for reproducibility. | None |
save_resamples | bool | If True (default), store null distribution in InferenceState. If False, discard to save memory. | True |
null | float | Null hypothesis value (default 0.0). | 0.0 |
alternative | str | Alternative hypothesis direction (default “two-sided”). | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
‘InferenceState’ | InferenceState with permutation p-values and percentile CIs. |
prediction¶
Prediction inference dispatch.
Routes prediction inference requests to the appropriate method: asymptotic (delta method), bootstrap, or cross-validation.
Functions:
| Name | Description |
|---|---|
dispatch_prediction_inference | Dispatch prediction inference to the appropriate method. |
Classes¶
Functions¶
dispatch_prediction_inference¶
dispatch_prediction_inference(*, how: str, pred: PredictionState, spec: ModelSpec, bundle: DataBundle, fit: FitState, conf_level: float, n_boot: int = 1000, ci_type: str = 'percentile', seed: int | None = None, k: int | str = 10, holdout_group_ids: np.ndarray | None = None) -> tuple[PredictionState, CVState | None]Dispatch prediction inference to the appropriate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
how | str | Inference method ("asymp", "boot", "cv"). | required |
pred | PredictionState | Current prediction state. | required |
spec | ModelSpec | Model specification. | required |
bundle | DataBundle | Data bundle with design matrices. | required |
fit | FitState | Fit state with coefficients and vcov. | required |
conf_level | float | Confidence level for intervals. | required |
n_boot | int | Number of bootstrap samples (default: 1000). | 1000 |
ci_type | str | Bootstrap CI type (default: "percentile"). | ‘percentile’ |
seed | int | None | Random seed for reproducibility. | None |
k | int | str | Number of CV folds or "loo" (default: 10). | 10 |
Returns:
| Type | Description |
|---|---|
PredictionState | Tuple of (augmented PredictionState, CVState or None). CVState is |
CVState | None | populated only for how="cv". |
profile¶
Profile likelihood inference for variance components.
Builds a deviance function adapter from the model’s internal state and runs profile_likelihood() to compute CIs for variance components.
compute_profile_inference: Profile likelihood CIs for mixed models. compute_profile_inference: Profile likelihood CIs for mixed models.
Functions:
| Name | Description |
|---|---|
compute_profile_inference | Compute profile likelihood CIs for variance components. |
Classes¶
Functions¶
compute_profile_inference¶
compute_profile_inference(spec: 'ModelSpec', bundle: 'DataBundle', fit: 'FitState', conf_level: float = 0.95, *, n_steps: int = 20, verbose: bool = False, threshold: float | None = None) -> 'ProfileState'Compute profile likelihood CIs for variance components.
Builds a deviance function adapter from the unified model’s internal state and runs profile_likelihood(). Returns a ProfileState with profile traces, splines, and CIs on the theta and SD scales.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec | ‘ModelSpec’ | Model specification. | required |
bundle | ‘DataBundle’ | Data bundle with X, Z, y, and re_metadata. | required |
fit | ‘FitState’ | Fit state with theta, sigma, and variance components. | required |
conf_level | float | Confidence level for profile CIs. | 0.95 |
n_steps | int | Number of profiling steps (default: 20). | 20 |
verbose | bool | Print profiling progress (default: False). | False |
threshold | float | None | Custom deviance threshold for CIs (default: None, uses chi-squared quantile). | None |
Returns:
| Type | Description |
|---|---|
‘ProfileState’ | ProfileState with profile traces and CIs. |
resample¶
Internal resampling operations.
Provides bootstrap procedures for bossanova models.
Module layout:
core.py— index generation, p-values, CI methodscommon.py— shared bootstrap execution loop, NaN handlingresults.py— result container (BootstrapResult)simulate.py— family-specific response simulationlm_operators.py— hat-matrix operator for LMlmer.py— LMER parametric bootstrapglmer.py— GLMER parametric bootstrap
Modules:
| Name | Description |
|---|---|
common | Common bootstrap execution patterns shared across model types. |
core | Core resampling utilities. |
glm_operators | GLM bootstrap operators (vmap-accelerated IRLS). |
glmer | GLMER bootstrap with full PIRLS refitting. |
lm_operators | Linear model coefficient operator (hat-matrix trick). |
lmer | LMER bootstrap with full PLS refitting. |
results | Result containers for resampling procedures. |
simulate | Response simulation for parametric bootstrap. |
Classes:
| Name | Description |
|---|---|
BootstrapResult | Results from a bootstrap procedure. |
Functions:
| Name | Description |
|---|---|
build_lm_coefficient_operator | Create reusable linear operator Y -> coefficients. |
compute_batched_glm_bootstrap | Compute bootstrap GLM coefficients using chunked vmap. |
compute_batched_ols_bootstrap | Compute bootstrap OLS coefficients using chunked vmap. |
compute_bootstrap_ci_basic | Compute basic (pivotal) bootstrap confidence intervals. |
compute_bootstrap_ci_bca | BCa (bias-corrected and accelerated) bootstrap confidence intervals. |
compute_bootstrap_ci_percentile | Compute percentile bootstrap confidence intervals. |
compute_pvalues | Compute permutation p-values. |
generate_bootstrap_indices | Generate n_boot bootstrap index arrays (with replacement). |
generate_kfold_indices | Generate k-fold cross-validation train/test index splits. |
generate_loo_indices | Generate leave-one-out cross-validation train/test index splits. |
generate_permutation_indices | Generate n_perm permutation index arrays. |
glmer_bootstrap | Bootstrap inference for GLMER coefficients. |
lmer_bootstrap | Bootstrap inference for lmer parameters. |
simulate_response | Simulate response from GLM fitted values. |
Attributes:
| Name | Type | Description |
|---|---|---|
BCa_MIN_NBOOT |
Attributes¶
BCa_MIN_NBOOT¶
BCa_MIN_NBOOT = 50Classes¶
BootstrapResult¶
BootstrapResult(observed: Array, boot_samples: Array, ci_lower: Array, ci_upper: Array, param_names: list[str], n_boot: int, ci_type: Literal['percentile', 'basic', 'bca'], level: float, term_types: list[str] | None = None) -> NoneResults from a bootstrap procedure.
Attributes:
| Name | Type | Description |
|---|---|---|
observed | Array | Observed statistics, shape [n_params]. |
boot_samples | Array | Bootstrap samples, shape [n_boot, n_params]. |
ci_lower | Array | Lower confidence bounds, shape [n_params]. |
ci_upper | Array | Upper confidence bounds, shape [n_params]. |
param_names | list[str] | Names of parameters. |
n_boot | int | Number of bootstrap samples. |
ci_type | Literal[‘percentile’, ‘basic’, ‘bca’] | Type of confidence interval (“percentile”, “basic”, or “bca”). |
level | float | Confidence level (e.g., 0.95). |
term_types | list[str] | None | Optional list indicating “fixef” or “ranef” for each parameter. Only populated for mixed model bootstrap with which=“all”. |
Attributes¶
boot_samples¶
boot_samples: Arrayci¶
ci: tuple[Array, Array]Return confidence intervals as a tuple (lower, upper).
ci_lower¶
ci_lower: Arrayci_type¶
ci_type: Literal['percentile', 'basic', 'bca']ci_upper¶
ci_upper: Arraylevel¶
level: floatn_boot¶
n_boot: intobserved¶
observed: Arrayparam_names¶
param_names: list[str]se¶
se: ArrayBootstrap standard errors (std of boot_samples).
term_types¶
term_types: list[str] | None = NoneFunctions¶
build_lm_coefficient_operator¶
build_lm_coefficient_operator(X: Array, method: Literal['cholesky', 'qr'] = 'cholesky') -> Callable[[Array], Array]Create reusable linear operator Y -> coefficients.
Precomputes and caches the expensive factorization. Returns a function that maps Y -> coefficients.
This implements the “hat-matrix trick” for efficient permutation testing: For fixed design matrix X, coefficients are a linear map from Y -> beta_hat: beta_hat = (X’X)^-1 X’y. We precompute the factorization of (X’X) once and reuse it for all permuted/bootstrapped Y values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X | Array | Design matrix, shape (n, p). | required |
method | Literal[‘cholesky’, ‘qr’] | Factorization method. - ‘cholesky’: Uses Cholesky decomposition of X’X (faster, more stable). - ‘qr’: Uses QR decomposition of X (handles rank deficiency better). | ‘cholesky’ |
Returns:
| Type | Description |
|---|---|
Callable[[Array], Array] | Function that maps Y (shape [n] or [n, m]) to coefficients. |
Examples:
X = np.array([[1, 2], [1, 3], [1, 4]])
operator = build_lm_coefficient_operator(X)
y = np.array([5, 8, 11])
coef = operator(y)compute_batched_glm_bootstrap¶
compute_batched_glm_bootstrap(boot_indices: np.ndarray, X: np.ndarray, y: np.ndarray, family: Family, *, weights: np.ndarray | None = None, max_iter: int = 25, tol: float = 1e-08, chunk_size: int | None = None) -> np.ndarrayCompute bootstrap GLM coefficients using chunked vmap.
For GLM models (non-robust, no random effects), each bootstrap replicate
is an independent IRLS solve. This function batches those solves via
jax.vmap — on JAX, this compiles to a single XLA program where each
lane runs its own lax.while_loop.
Memory safety is ensured by chunking: compute_batch_size determines
how many solves to batch at once based on available memory. The IRLS
state tuple is larger than OLS, so chunk sizes are smaller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_indices | ndarray | Bootstrap index array, shape (n_boot, n). | required |
X | ndarray | Design matrix, shape (n, p). | required |
y | ndarray | Response vector, shape (n,). | required |
family | Family | Family object with link, variance, and deviance functions. | required |
weights | ndarray | None | Optional prior observation weights, shape (n,). If None, equal weights (ones) are used. | None |
max_iter | int | Maximum IRLS iterations per bootstrap sample. | 25 |
tol | float | Convergence tolerance for relative deviance change. | 1e-08 |
chunk_size | int | None | Number of bootstrap samples per chunk. If None, computed automatically from available memory. | None |
Returns:
| Type | Description |
|---|---|
ndarray | Coefficient array, shape (n_boot, p). |
compute_batched_ols_bootstrap¶
compute_batched_ols_bootstrap(boot_indices: np.ndarray, X: np.ndarray, y: np.ndarray, chunk_size: int | None = None) -> np.ndarrayCompute bootstrap OLS coefficients using chunked vmap.
For LM models (gaussian family, identity link, no random effects),
each bootstrap replicate is an independent OLS solve. This function
batches those solves via ops.vmap — on JAX, this compiles to
a single XLA program; on NumPy, it degrades to a loop+stack
(equivalent to sequential but without Python overhead from
fit_model).
Memory safety is ensured by chunking: compute_batch_size
determines how many solves to batch at once based on available
memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_indices | ndarray | Bootstrap index array, shape (n_boot, n). | required |
X | ndarray | Design matrix, shape (n, p). | required |
y | ndarray | Response vector, shape (n,). | required |
chunk_size | int | None | Number of bootstrap samples per chunk. If None, computed automatically from available memory. | None |
Returns:
| Type | Description |
|---|---|
ndarray | Coefficient array, shape (n_boot, p). |
compute_bootstrap_ci_basic¶
compute_bootstrap_ci_basic(observed: 'ArrayLike', boot_stats: 'ArrayLike', level: float = 0.95) -> tuple[np.ndarray, np.ndarray]Compute basic (pivotal) bootstrap confidence intervals.
The basic bootstrap CI is: [2theta_hat - q_upper, 2theta_hat - q_lower] where q_lower and q_upper are the quantiles of the bootstrap distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed | ‘ArrayLike’ | Observed statistic, shape [n_params] or scalar. | required |
boot_stats | ‘ArrayLike’ | Bootstrap statistics, shape [n_boot] or [n_boot, n_params]. | required |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (lower, upper) confidence bounds. |
Examples:
observed = jnp.array([1.0, 2.0])
boot_stats = jnp.array([[1.1, 2.1], [0.9, 1.9], [1.2, 2.2]])
lower, upper = compute_bootstrap_ci_basic(observed, boot_stats, level=0.95)compute_bootstrap_ci_bca¶
compute_bootstrap_ci_bca(boot_stats: 'ArrayLike', observed: 'ArrayLike', jackknife_stats: 'ArrayLike | None' = None, level: float = 0.95) -> tuple[np.ndarray, np.ndarray]BCa (bias-corrected and accelerated) bootstrap confidence intervals.
BCa intervals adjust for bias and skewness in the bootstrap distribution, providing second-order accuracy. This is R’s gold standard method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_stats | ‘ArrayLike’ | Bootstrap statistics, shape [n_boot, n_params]. | required |
observed | ‘ArrayLike’ | Observed statistic from original data, shape [n_params]. | required |
jackknife_stats | ‘ArrayLike | None’ | Jackknife statistics for acceleration, shape [n, n_params]. If None, uses a=0 (bias-corrected percentile only, no acceleration). | None |
level | float | Confidence level (default 0.95). | 0.95 |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (lower, upper) confidence bounds. |
Efron, B. (1987). Better Bootstrap Confidence Intervals. Efron, B. (1987). Better Bootstrap Confidence Intervals. DiCiccio, T. J., & Efron, B. (1996). Bootstrap confidence intervals.
compute_bootstrap_ci_percentile¶
compute_bootstrap_ci_percentile(boot_stats: 'ArrayLike', level: float = 0.95) -> tuple[np.ndarray, np.ndarray]Compute percentile bootstrap confidence intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_stats | ‘ArrayLike’ | Bootstrap statistics, shape [n_boot] or [n_boot, n_params]. | required |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (lower, upper) confidence bounds. |
Examples:
boot_stats = jnp.array([[1.0, 2.0], [1.5, 2.5], [0.8, 1.8]])
lower, upper = compute_bootstrap_ci_percentile(boot_stats, level=0.95)compute_pvalues¶
compute_pvalues(observed_stats: 'ArrayLike', null_stats: 'ArrayLike', alternative: Literal['two-sided', 'greater', 'less'] = 'two-sided') -> np.ndarrayCompute permutation p-values.
Uses formula: p = (1 + sum(|T_null| >= |T_obs|)) / (B + 1) where B is the number of null samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed_stats | ‘ArrayLike’ | Observed test statistics, shape [n_params] or scalar. | required |
null_stats | ‘ArrayLike’ | Null distribution statistics, shape [n_perm, n_params] or [n_perm]. | required |
alternative | Literal[‘two-sided’, ‘greater’, ‘less’] | Type of test: “two-sided”, “greater”, or “less”. | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
ndarray | P-values for each parameter. |
Examples:
observed = jnp.array([2.5, -1.0])
null = jnp.array([[0.1, 0.2], [-0.3, 0.5], [0.8, -0.1]])
pvals = compute_pvalues(observed, null, alternative="two-sided")generate_bootstrap_indices¶
generate_bootstrap_indices(rng: 'RNG', n: int, n_boot: int) -> np.ndarrayGenerate n_boot bootstrap index arrays (with replacement).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | ‘RNG’ | RNG object (from rng). | required |
n | int | Size of each bootstrap sample. | required |
n_boot | int | Number of bootstrap samples to generate. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Shape [n_boot, n], where each row contains indices sampled with replacement. |
Examples:
from rng import RNG
rng = RNG.from_seed(42)
indices = generate_bootstrap_indices(rng, n=5, n_boot=3)
indices.shape
# (3, 5)generate_kfold_indices¶
generate_kfold_indices(rng: 'RNG', n: int, k: int, shuffle: bool = True) -> list[tuple[np.ndarray, np.ndarray]]Generate k-fold cross-validation train/test index splits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | ‘RNG’ | RNG object (from rng). Used if shuffle=True. | required |
n | int | Total number of observations. | required |
k | int | Number of folds. | required |
shuffle | bool | Whether to shuffle indices before splitting. | True |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of k tuples (train_indices, test_indices). |
Examples:
from rng import RNG
rng = RNG.from_seed(42)
splits = generate_kfold_indices(rng, n=10, k=5)
len(splits)
# 5
train, test = splits[0]
len(train) + len(test)
# 10generate_loo_indices¶
generate_loo_indices(n: int) -> list[tuple[np.ndarray, np.ndarray]]Generate leave-one-out cross-validation train/test index splits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n | int | Total number of observations. | required |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of n tuples (train_indices, test_indices), where each test set |
list[tuple[ndarray, ndarray]] | contains exactly one observation. |
Examples:
splits = generate_loo_indices(n=5)
len(splits)
# 5
train, test = splits[0]
len(test)
# 1generate_permutation_indices¶
generate_permutation_indices(rng: 'RNG', n: int, n_perm: int) -> np.ndarrayGenerate n_perm permutation index arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | ‘RNG’ | RNG object (from rng). | required |
n | int | Length of each permutation. | required |
n_perm | int | Number of permutations to generate. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Shape [n_perm, n], where each row is a permutation of [0, 1, ..., n-1]. |
Examples:
from rng import RNG
rng = RNG.from_seed(42)
indices = generate_permutation_indices(rng, n=5, n_perm=3)
indices.shape
# (3, 5)glmer_bootstrap¶
glmer_bootstrap(X: 'Array', Z: sp.csc_matrix | 'Array', y: 'Array', X_names: list[str], theta: 'Array', beta: 'Array', family: Family, n_groups_list: list[int], re_structure: str, metadata: dict, weights: 'Array | None' = None, n_boot: int = 999, seed: int | None = None, boot_type: Literal['parametric'] = 'parametric', ci_type: Literal['percentile', 'basic', 'bca'] = 'percentile', level: float = 0.95, max_iter: int = 25, tol: float = 1e-08, max_outer_iter: int = 10000, nAGQ: int = 0, verbose: bool = False, n_jobs: int = 1) -> BootstrapResultBootstrap inference for GLMER coefficients.
Each bootstrap iteration requires full PIRLS optimization. Supports parallel execution via joblib.
Uses parametric bootstrap: simulate b* ~ N(0, LL’), compute
eta* = X beta + Z b*, then y* from the family distribution and refit.
This is the standard approach for mixed models, matching R’s
lme4::bootMer(type="parametric").
Note: Only parametric bootstrap is supported for mixed models, matching R/lme4. Residual and case bootstrap are not applicable because the residual structure involves multiple variance components (within-group and between-group). Parametric bootstrap properly accounts for this hierarchical structure by simulating new random effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X | ‘Array’ | Fixed effects design matrix, shape (n, p). | required |
Z | csc_matrix | ‘Array’ | Random effects design matrix, sparse CSC or dense array (n, q). | required |
y | ‘Array’ | Response vector, shape (n,). | required |
X_names | list[str] | Fixed effects coefficient names. | required |
theta | ‘Array’ | Variance parameters from fitted model (n_theta,). | required |
beta | ‘Array’ | Fixed effects from fitted model (p,). | required |
family | Family | GLM family object (binomial or poisson). | required |
n_groups_list | list[int] | List of group sizes per RE term. | required |
re_structure | str | Random effects structure metadata. | required |
metadata | dict | Additional metadata for Lambda construction. | required |
weights | ‘Array | None’ | Prior observation weights (n,) or None. For binomial: number of trials per observation. | None |
n_boot | int | Number of bootstrap samples. | 999 |
seed | int | None | Random seed for reproducibility. | None |
boot_type | Literal[‘parametric’] | Type of bootstrap. Only "parametric" is supported. | ‘parametric’ |
ci_type | Literal[‘percentile’, ‘basic’, ‘bca’] | Confidence interval type: “percentile”, “basic”, or “bca”. | ‘percentile’ |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
max_iter | int | Maximum PIRLS iterations per refit. | 25 |
tol | float | Convergence tolerance for PIRLS. | 1e-08 |
max_outer_iter | int | Maximum BOBYQA iterations for theta optimization. | 10000 |
nAGQ | int | Number of adaptive Gauss-Hermite quadrature points. Default 0 (Stage 1 only) for faster bootstrap. Use 1 for full two-stage optimization matching lme4 default. | 0 |
verbose | bool | If True, show tqdm progress bar. | False |
n_jobs | int | Number of parallel jobs. Default 1 (sequential). Use -1 for all cores. | 1 |
Returns:
| Type | Description |
|---|---|
BootstrapResult | BootstrapResult with observed stats, bootstrap samples, and CIs. |
Examples:
from family import binomial
family = binomial()
result = glmer_bootstrap(
X, Z, y, ["Intercept", "x"],
theta=theta_hat, beta=beta_hat,
family=family, n_groups_list=[15],
re_structure=[{"type": "intercept"}],
metadata={}, n_boot=999
)
print(result.ci)lmer_bootstrap¶
lmer_bootstrap(X: 'Array', Z: sp.csc_matrix | 'Array', y: 'Array', X_names: list[str], theta: 'Array', beta: 'Array', sigma: float, n_groups_list: list[int], re_structure: str, metadata: dict | None = None, lower_bounds: list[float] | None = None, n_boot: int = 999, seed: int | None = None, boot_type: Literal['parametric'] = 'parametric', ci_type: Literal['percentile', 'basic', 'bca'] = 'percentile', level: float = 0.95, method: Literal['REML', 'ML'] = 'REML', max_iter: int = 10000, tol: float = 1e-08, verbose: bool = False, which: Literal['fixef', 'ranef', 'all'] = 'fixef', group_names: list[str] | None = None, random_names: list[str] | None = None, n_jobs: int = 1) -> BootstrapResultBootstrap inference for lmer parameters.
Supports parallel execution via joblib for significant speedups on multi-core machines. Each bootstrap iteration requires a full PLS optimization, making parallelization highly beneficial.
Uses parametric bootstrap: simulate y* from the fitted model
(y* = X beta_hat + Z b* + eps*) and refit. This is the standard approach
for mixed models, matching R’s lme4::bootMer(type="parametric").
Note: Only parametric bootstrap is supported for mixed models, matching R/lme4. Residual and case bootstrap are not applicable because the residual structure involves multiple variance components (within-group and between-group). Parametric bootstrap properly accounts for this hierarchical structure by simulating new random effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X | ‘Array’ | Fixed effects design matrix, shape (n, p). | required |
Z | csc_matrix | ‘Array’ | Random effects design matrix, shape (n, q), scipy sparse CSC. | required |
y | ‘Array’ | Response vector, shape (n,). | required |
X_names | list[str] | Fixed effects coefficient names. | required |
theta | ‘Array’ | Variance parameters from fitted model. | required |
beta | ‘Array’ | Fixed effects coefficients from fitted model. | required |
sigma | float | Residual standard deviation from fitted model. | required |
n_groups_list | list[int] | Number of groups per grouping factor. | required |
re_structure | str | Random effects structure type. | required |
metadata | dict | None | Additional metadata for complex structures. | None |
lower_bounds | list[float] | None | Lower bounds for theta optimization. If None, uses 0 for all. | None |
n_boot | int | Number of bootstrap samples. | 999 |
seed | int | None | Random seed for reproducibility. | None |
boot_type | Literal[‘parametric’] | Type of bootstrap. Only "parametric" is supported. | ‘parametric’ |
ci_type | Literal[‘percentile’, ‘basic’, ‘bca’] | Confidence interval type: “percentile”, “basic”, or “bca”. | ‘percentile’ |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
method | Literal[‘REML’, ‘ML’] | Estimation method for refitting: “REML” or “ML”. | ‘REML’ |
max_iter | int | Maximum optimizer iterations for each refit. | 10000 |
tol | float | Convergence tolerance for optimizer. | 1e-08 |
verbose | bool | Print progress information. | False |
which | Literal[‘fixef’, ‘ranef’, ‘all’] | Which parameters to bootstrap: - “fixef”: Fixed effects coefficients (default). - “ranef”: Variance components (SDs and correlations). - “all”: Both fixed effects and variance components. | ‘fixef’ |
group_names | list[str] | None | Grouping factor names (required if which=“ranef” or “all”). | None |
random_names | list[str] | None | Random effect names (required if which=“ranef” or “all”). | None |
n_jobs | int | Number of parallel jobs. Default 1 (sequential). Use -1 for all available cores. Parallelization uses joblib with the loky backend for process-based parallelism. | 1 |
Returns:
| Type | Description |
|---|---|
BootstrapResult | BootstrapResult with observed stats, bootstrap samples, and CIs. |
BootstrapResult | For which=“ranef”, param_names are variance component names |
BootstrapResult | (e.g., “Subject:Intercept_sd”, “Subject:corr_Intercept:Days”, “Residual_sd”). |
BootstrapResult | For which=“all”, fixed effects come first, then variance components. |
Examples:
# Parametric bootstrap for fixed effects
result = lmer_bootstrap(
X, Z, y, ["Intercept", "Days"], theta, beta, sigma,
n_groups_list=[18], re_structure="slope",
n_boot=999, boot_type="parametric"
)
print(result.ci)
# Bootstrap for variance components
result = lmer_bootstrap(
X, Z, y, ["Intercept", "Days"], theta, beta, sigma,
n_groups_list=[18], re_structure="slope",
n_boot=999, which="ranef",
group_names=["Subject"], random_names=["Intercept", "Days"]
)Bootstrap refitting can fail to converge for some samples. Convergence Bootstrap refitting can fail to converge for some samples. Convergence failures > 10% will trigger a warning. Failed samples are excluded from CI computation.
simulate_response¶
simulate_response(rng: RNG, mu: Array, family: Family, dispersion: float = 1.0, weights: Array | None = None) -> ArraySimulate response from GLM fitted values.
Dispatches to family-specific simulation functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random sampling. | required |
mu | Array | Fitted values on response scale (n,). | required |
family | Family | GLM family object. | required |
dispersion | float | Dispersion parameter (phi). For Gaussian family, dispersion = sigma^2. For other families, typically fixed at 1.0. | 1.0 |
weights | Array | None | Prior weights (only used for binomial trials). | None |
Returns:
| Type | Description |
|---|---|
Array | Simulated response (n,). |
Modules¶
common¶
Common bootstrap execution patterns shared across model types.
This module provides the shared bootstrap execution loop (sequential/parallel), NaN-aware CI computation, and failure-handling patterns that are duplicated across lm.py, glm.py, and mixed.py.
All iteration functions should be standalone (not closures) for joblib serialization compatibility.
Functions:
| Name | Description |
|---|---|
collect_boot_samples | Stack bootstrap samples and compute valid-sample mask. |
compute_bootstrap_ci | Compute bootstrap confidence intervals using the specified method. |
run_bootstrap_iterations | Run bootstrap iterations sequentially or in parallel via joblib. |
warn_on_failures | Emit a RuntimeWarning if failure rate exceeds 10%. |
Attributes:
| Name | Type | Description |
|---|---|---|
Array |
Attributes¶
Array¶
Array = AnyClasses¶
Functions¶
collect_boot_samples¶
collect_boot_samples(boot_samples_list: list[np.ndarray], n_boot: int, context: str = 'Bootstrap') -> tuple[np.ndarray, np.ndarray, int]Stack bootstrap samples and compute valid-sample mask.
Counts NaN failures, issues a warning if > 10% failed, and returns the full array plus a boolean mask of valid (non-NaN) rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_samples_list | list[ndarray] | List of per-iteration coefficient arrays. | required |
n_boot | int | Total number of iterations attempted. | required |
context | str | Label for the warning message (e.g., “Bootstrap”, “Permutation”). | ‘Bootstrap’ |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, int] | Tuple of (boot_samples, valid_mask, n_failed) where: - boot_samples: stacked array of shape (n_boot, n_params) - valid_mask: boolean array of shape (n_boot,), True for non-NaN rows - n_failed: count of failed (all-NaN) iterations |
compute_bootstrap_ci¶
compute_bootstrap_ci(coef_obs: np.ndarray, boot_samples_valid: np.ndarray, ci_type: Literal['percentile', 'basic', 'bca'], level: float, jackknife_stats: np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]Compute bootstrap confidence intervals using the specified method.
Dispatches to percentile, basic, or BCa CI computation from core.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coef_obs | ndarray | Observed coefficient estimates, shape (p,). | required |
boot_samples_valid | ndarray | Valid (non-NaN) bootstrap samples, shape (n_valid, p). | required |
ci_type | Literal[‘percentile’, ‘basic’, ‘bca’] | Type of CI: “percentile”, “basic”, or “bca”. | required |
level | float | Confidence level (e.g. 0.95). | required |
jackknife_stats | ndarray | None | Jackknife leave-one-out estimates, shape (n, p). Required for ci_type="bca". | None |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (ci_lower, ci_upper) arrays, each shape (p,). |
run_bootstrap_iterations¶
run_bootstrap_iterations(iteration_fn: Callable[..., np.ndarray], rngs: list[RNG], n_boot: int, iter_kwargs: dict[str, Any], n_jobs: int = 1, verbose: bool = False, desc: str = 'Bootstrap') -> list[np.ndarray]Run bootstrap iterations sequentially or in parallel via joblib.
This is the shared execution loop used by all bootstrap functions. The iteration function must be a standalone callable (not a closure) to support joblib serialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iteration_fn | Callable..., [ndarray] | Standalone function for a single bootstrap iteration. Must accept rng as first keyword argument plus all iter_kwargs. | required |
rngs | list[RNG] | Pre-split RNG objects, one per iteration. | required |
n_boot | int | Number of bootstrap iterations. | required |
iter_kwargs | dict[str, Any] | Keyword arguments forwarded to iteration_fn (excluding rng). | required |
n_jobs | int | Number of parallel jobs. 1 = sequential, -1 = all cores. | 1 |
verbose | bool | If True, show tqdm progress bar. | False |
desc | str | Description label for the progress bar. | ‘Bootstrap’ |
Returns:
| Type | Description |
|---|---|
list[ndarray] | List of arrays from each iteration (may contain NaN for failures). |
warn_on_failures¶
warn_on_failures(n_failed: int, n_total: int, context: str = 'Bootstrap') -> NoneEmit a RuntimeWarning if failure rate exceeds 10%.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_failed | int | Number of iterations that produced NaN. | required |
n_total | int | Total number of iterations attempted. | required |
context | str | Label for the warning message. | ‘Bootstrap’ |
core¶
Core resampling utilities.
Functions:
| Name | Description |
|---|---|
compute_bootstrap_ci_basic | Compute basic (pivotal) bootstrap confidence intervals. |
compute_bootstrap_ci_bca | BCa (bias-corrected and accelerated) bootstrap confidence intervals. |
compute_bootstrap_ci_percentile | Compute percentile bootstrap confidence intervals. |
compute_pvalues | Compute permutation p-values. |
generate_bootstrap_indices | Generate n_boot bootstrap index arrays (with replacement). |
generate_kfold_indices | Generate k-fold cross-validation train/test index splits. |
generate_loo_indices | Generate leave-one-out cross-validation train/test index splits. |
generate_permutation_indices | Generate n_perm permutation index arrays. |
Attributes:
| Name | Type | Description |
|---|---|---|
BCa_MIN_NBOOT |
Attributes¶
BCa_MIN_NBOOT¶
BCa_MIN_NBOOT = 50Classes¶
Functions¶
compute_bootstrap_ci_basic¶
compute_bootstrap_ci_basic(observed: 'ArrayLike', boot_stats: 'ArrayLike', level: float = 0.95) -> tuple[np.ndarray, np.ndarray]Compute basic (pivotal) bootstrap confidence intervals.
The basic bootstrap CI is: [2theta_hat - q_upper, 2theta_hat - q_lower] where q_lower and q_upper are the quantiles of the bootstrap distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed | ‘ArrayLike’ | Observed statistic, shape [n_params] or scalar. | required |
boot_stats | ‘ArrayLike’ | Bootstrap statistics, shape [n_boot] or [n_boot, n_params]. | required |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (lower, upper) confidence bounds. |
Examples:
observed = jnp.array([1.0, 2.0])
boot_stats = jnp.array([[1.1, 2.1], [0.9, 1.9], [1.2, 2.2]])
lower, upper = compute_bootstrap_ci_basic(observed, boot_stats, level=0.95)compute_bootstrap_ci_bca¶
compute_bootstrap_ci_bca(boot_stats: 'ArrayLike', observed: 'ArrayLike', jackknife_stats: 'ArrayLike | None' = None, level: float = 0.95) -> tuple[np.ndarray, np.ndarray]BCa (bias-corrected and accelerated) bootstrap confidence intervals.
BCa intervals adjust for bias and skewness in the bootstrap distribution, providing second-order accuracy. This is R’s gold standard method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_stats | ‘ArrayLike’ | Bootstrap statistics, shape [n_boot, n_params]. | required |
observed | ‘ArrayLike’ | Observed statistic from original data, shape [n_params]. | required |
jackknife_stats | ‘ArrayLike | None’ | Jackknife statistics for acceleration, shape [n, n_params]. If None, uses a=0 (bias-corrected percentile only, no acceleration). | None |
level | float | Confidence level (default 0.95). | 0.95 |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (lower, upper) confidence bounds. |
Efron, B. (1987). Better Bootstrap Confidence Intervals. Efron, B. (1987). Better Bootstrap Confidence Intervals. DiCiccio, T. J., & Efron, B. (1996). Bootstrap confidence intervals.
compute_bootstrap_ci_percentile¶
compute_bootstrap_ci_percentile(boot_stats: 'ArrayLike', level: float = 0.95) -> tuple[np.ndarray, np.ndarray]Compute percentile bootstrap confidence intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_stats | ‘ArrayLike’ | Bootstrap statistics, shape [n_boot] or [n_boot, n_params]. | required |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | Tuple of (lower, upper) confidence bounds. |
Examples:
boot_stats = jnp.array([[1.0, 2.0], [1.5, 2.5], [0.8, 1.8]])
lower, upper = compute_bootstrap_ci_percentile(boot_stats, level=0.95)compute_pvalues¶
compute_pvalues(observed_stats: 'ArrayLike', null_stats: 'ArrayLike', alternative: Literal['two-sided', 'greater', 'less'] = 'two-sided') -> np.ndarrayCompute permutation p-values.
Uses formula: p = (1 + sum(|T_null| >= |T_obs|)) / (B + 1) where B is the number of null samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed_stats | ‘ArrayLike’ | Observed test statistics, shape [n_params] or scalar. | required |
null_stats | ‘ArrayLike’ | Null distribution statistics, shape [n_perm, n_params] or [n_perm]. | required |
alternative | Literal[‘two-sided’, ‘greater’, ‘less’] | Type of test: “two-sided”, “greater”, or “less”. | ‘two-sided’ |
Returns:
| Type | Description |
|---|---|
ndarray | P-values for each parameter. |
Examples:
observed = jnp.array([2.5, -1.0])
null = jnp.array([[0.1, 0.2], [-0.3, 0.5], [0.8, -0.1]])
pvals = compute_pvalues(observed, null, alternative="two-sided")generate_bootstrap_indices¶
generate_bootstrap_indices(rng: 'RNG', n: int, n_boot: int) -> np.ndarrayGenerate n_boot bootstrap index arrays (with replacement).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | ‘RNG’ | RNG object (from rng). | required |
n | int | Size of each bootstrap sample. | required |
n_boot | int | Number of bootstrap samples to generate. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Shape [n_boot, n], where each row contains indices sampled with replacement. |
Examples:
from rng import RNG
rng = RNG.from_seed(42)
indices = generate_bootstrap_indices(rng, n=5, n_boot=3)
indices.shape
# (3, 5)generate_kfold_indices¶
generate_kfold_indices(rng: 'RNG', n: int, k: int, shuffle: bool = True) -> list[tuple[np.ndarray, np.ndarray]]Generate k-fold cross-validation train/test index splits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | ‘RNG’ | RNG object (from rng). Used if shuffle=True. | required |
n | int | Total number of observations. | required |
k | int | Number of folds. | required |
shuffle | bool | Whether to shuffle indices before splitting. | True |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of k tuples (train_indices, test_indices). |
Examples:
from rng import RNG
rng = RNG.from_seed(42)
splits = generate_kfold_indices(rng, n=10, k=5)
len(splits)
# 5
train, test = splits[0]
len(train) + len(test)
# 10generate_loo_indices¶
generate_loo_indices(n: int) -> list[tuple[np.ndarray, np.ndarray]]Generate leave-one-out cross-validation train/test index splits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n | int | Total number of observations. | required |
Returns:
| Type | Description |
|---|---|
list[tuple[ndarray, ndarray]] | List of n tuples (train_indices, test_indices), where each test set |
list[tuple[ndarray, ndarray]] | contains exactly one observation. |
Examples:
splits = generate_loo_indices(n=5)
len(splits)
# 5
train, test = splits[0]
len(test)
# 1generate_permutation_indices¶
generate_permutation_indices(rng: 'RNG', n: int, n_perm: int) -> np.ndarrayGenerate n_perm permutation index arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | ‘RNG’ | RNG object (from rng). | required |
n | int | Length of each permutation. | required |
n_perm | int | Number of permutations to generate. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Shape [n_perm, n], where each row is a permutation of [0, 1, ..., n-1]. |
Examples:
from rng import RNG
rng = RNG.from_seed(42)
indices = generate_permutation_indices(rng, n=5, n_perm=3)
indices.shape
# (3, 5)glm_operators¶
GLM bootstrap operators (vmap-accelerated IRLS).
Provides compute_batched_glm_bootstrap which runs bootstrap IRLS solves
in parallel via jax.vmap on the JAX backend. Memory safety is ensured
by chunking via compute_batch_size.
This mirrors lm_operators.py but for GLM models (binomial, poisson,
gamma). Each bootstrap replicate runs an independent IRLS solve. On JAX,
lax.while_loop inside vmap runs all lanes in parallel — converged
lanes just check the condition and skip (minimal cost under XLA).
Only used when:
Backend is JAX
Family has no robust_weights (excludes t-distribution)
Model has no random effects (excludes LMER/GLMER)
Functions:
| Name | Description |
|---|---|
compute_batched_glm_bootstrap | Compute bootstrap GLM coefficients using chunked vmap. |
Attributes¶
Classes¶
Functions¶
compute_batched_glm_bootstrap¶
compute_batched_glm_bootstrap(boot_indices: np.ndarray, X: np.ndarray, y: np.ndarray, family: Family, *, weights: np.ndarray | None = None, max_iter: int = 25, tol: float = 1e-08, chunk_size: int | None = None) -> np.ndarrayCompute bootstrap GLM coefficients using chunked vmap.
For GLM models (non-robust, no random effects), each bootstrap replicate
is an independent IRLS solve. This function batches those solves via
jax.vmap — on JAX, this compiles to a single XLA program where each
lane runs its own lax.while_loop.
Memory safety is ensured by chunking: compute_batch_size determines
how many solves to batch at once based on available memory. The IRLS
state tuple is larger than OLS, so chunk sizes are smaller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_indices | ndarray | Bootstrap index array, shape (n_boot, n). | required |
X | ndarray | Design matrix, shape (n, p). | required |
y | ndarray | Response vector, shape (n,). | required |
family | Family | Family object with link, variance, and deviance functions. | required |
weights | ndarray | None | Optional prior observation weights, shape (n,). If None, equal weights (ones) are used. | None |
max_iter | int | Maximum IRLS iterations per bootstrap sample. | 25 |
tol | float | Convergence tolerance for relative deviance change. | 1e-08 |
chunk_size | int | None | Number of bootstrap samples per chunk. If None, computed automatically from available memory. | None |
Returns:
| Type | Description |
|---|---|
ndarray | Coefficient array, shape (n_boot, p). |
glmer¶
GLMER bootstrap with full PIRLS refitting.
This module provides parametric bootstrap for generalized linear mixed-effects models (GLMER). Each bootstrap iteration simulates a new response from the fitted model and refits via PIRLS optimization.
Key characteristics:
Sequential loop (cannot vmap due to scipy.sparse operations)
Parametric bootstrap: simulate random effects b* ~ N(0, LL’), then response
Full PIRLS refit per sample (expensive but necessary)
Optional parallelization via joblib
Functions:
| Name | Description |
|---|---|
glmer_bootstrap | Bootstrap inference for GLMER coefficients. |
glmer_bootstrap_single_iteration | Execute a single GLMER bootstrap iteration. |
simulate_from_glmer | Simulate response from fitted GLMER model. |
Classes¶
Functions¶
glmer_bootstrap¶
glmer_bootstrap(X: 'Array', Z: sp.csc_matrix | 'Array', y: 'Array', X_names: list[str], theta: 'Array', beta: 'Array', family: Family, n_groups_list: list[int], re_structure: str, metadata: dict, weights: 'Array | None' = None, n_boot: int = 999, seed: int | None = None, boot_type: Literal['parametric'] = 'parametric', ci_type: Literal['percentile', 'basic', 'bca'] = 'percentile', level: float = 0.95, max_iter: int = 25, tol: float = 1e-08, max_outer_iter: int = 10000, nAGQ: int = 0, verbose: bool = False, n_jobs: int = 1) -> BootstrapResultBootstrap inference for GLMER coefficients.
Each bootstrap iteration requires full PIRLS optimization. Supports parallel execution via joblib.
Uses parametric bootstrap: simulate b* ~ N(0, LL’), compute
eta* = X beta + Z b*, then y* from the family distribution and refit.
This is the standard approach for mixed models, matching R’s
lme4::bootMer(type="parametric").
Note: Only parametric bootstrap is supported for mixed models, matching R/lme4. Residual and case bootstrap are not applicable because the residual structure involves multiple variance components (within-group and between-group). Parametric bootstrap properly accounts for this hierarchical structure by simulating new random effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X | ‘Array’ | Fixed effects design matrix, shape (n, p). | required |
Z | csc_matrix | ‘Array’ | Random effects design matrix, sparse CSC or dense array (n, q). | required |
y | ‘Array’ | Response vector, shape (n,). | required |
X_names | list[str] | Fixed effects coefficient names. | required |
theta | ‘Array’ | Variance parameters from fitted model (n_theta,). | required |
beta | ‘Array’ | Fixed effects from fitted model (p,). | required |
family | Family | GLM family object (binomial or poisson). | required |
n_groups_list | list[int] | List of group sizes per RE term. | required |
re_structure | str | Random effects structure metadata. | required |
metadata | dict | Additional metadata for Lambda construction. | required |
weights | ‘Array | None’ | Prior observation weights (n,) or None. For binomial: number of trials per observation. | None |
n_boot | int | Number of bootstrap samples. | 999 |
seed | int | None | Random seed for reproducibility. | None |
boot_type | Literal[‘parametric’] | Type of bootstrap. Only "parametric" is supported. | ‘parametric’ |
ci_type | Literal[‘percentile’, ‘basic’, ‘bca’] | Confidence interval type: “percentile”, “basic”, or “bca”. | ‘percentile’ |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
max_iter | int | Maximum PIRLS iterations per refit. | 25 |
tol | float | Convergence tolerance for PIRLS. | 1e-08 |
max_outer_iter | int | Maximum BOBYQA iterations for theta optimization. | 10000 |
nAGQ | int | Number of adaptive Gauss-Hermite quadrature points. Default 0 (Stage 1 only) for faster bootstrap. Use 1 for full two-stage optimization matching lme4 default. | 0 |
verbose | bool | If True, show tqdm progress bar. | False |
n_jobs | int | Number of parallel jobs. Default 1 (sequential). Use -1 for all cores. | 1 |
Returns:
| Type | Description |
|---|---|
BootstrapResult | BootstrapResult with observed stats, bootstrap samples, and CIs. |
Examples:
from family import binomial
family = binomial()
result = glmer_bootstrap(
X, Z, y, ["Intercept", "x"],
theta=theta_hat, beta=beta_hat,
family=family, n_groups_list=[15],
re_structure=[{"type": "intercept"}],
metadata={}, n_boot=999
)
print(result.ci)glmer_bootstrap_single_iteration¶
glmer_bootstrap_single_iteration(rng: RNG, X_np: np.ndarray, Z_sparse: sp.csc_matrix, beta_np: np.ndarray, theta_np: np.ndarray, family: Family, n_groups_list: list[int], re_structure: str, metadata: dict, weights_np: np.ndarray | None, boot_type: str, max_iter: int, tol: float, max_outer_iter: int, nAGQ: int, p: int, lambda_template: object | None = None) -> np.ndarrayExecute a single GLMER bootstrap iteration.
This is a standalone function (not a closure) to enable joblib parallelization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random number generation. | required |
X_np | ndarray | Fixed effects design matrix as numpy array (n, p). | required |
Z_sparse | csc_matrix | Random effects design matrix as sparse CSC. | required |
beta_np | ndarray | Fixed effects as numpy array (p,). | required |
theta_np | ndarray | Variance parameters as numpy array. | required |
family | Family | GLM family object. | required |
n_groups_list | list[int] | List of group sizes per RE term. | required |
re_structure | str | Random effects structure metadata. | required |
metadata | dict | Additional metadata for Lambda construction. | required |
weights_np | ndarray | None | Prior weights as numpy array or None. | required |
boot_type | str | Only "parametric" is supported. | required |
max_iter | int | Maximum PIRLS iterations. | required |
tol | float | Convergence tolerance. | required |
max_outer_iter | int | Maximum BOBYQA iterations. | required |
nAGQ | int | Number of adaptive Gauss-Hermite quadrature points (0 or 1). | required |
p | int | Number of fixed effects coefficients. | required |
lambda_template | object | None | Pre-built Lambda template for efficient updates. | None |
Returns:
| Type | Description |
|---|---|
ndarray | Bootstrap coefficient estimates (p,), or NaN array if failed. |
simulate_from_glmer¶
simulate_from_glmer(rng: RNG, X: np.ndarray, Z: sp.csc_matrix, beta: np.ndarray, theta: np.ndarray, family: Family, n_groups_list: list[int], re_structure: str, metadata: dict, weights: np.ndarray | None = None) -> 'Array'Simulate response from fitted GLMER model.
Algorithm:
Simulate random effects: b* ~ N(0, LL’)
Compute linear predictor: eta* = X beta_hat + Z b*
Compute mean: mu* = g^{-1}(eta*)
Simulate response from family distribution
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random number generation. | required |
X | ndarray | Fixed effects design matrix (n, p). | required |
Z | csc_matrix | Random effects design matrix (sparse CSC, n, q). | required |
beta | ndarray | Fixed effects coefficients (p,). | required |
theta | ndarray | Variance parameters (n_theta,). | required |
family | Family | GLM family object. | required |
n_groups_list | list[int] | List of group sizes per RE term. | required |
re_structure | str | Random effects structure metadata. | required |
metadata | dict | Additional metadata for Lambda construction. | required |
weights | ndarray | None | Prior observation weights (only for binomial trials). | None |
Returns:
| Type | Description |
|---|---|
‘Array’ | Simulated response (n,) as numpy/JAX array. |
lm_operators¶
Linear model coefficient operator (hat-matrix trick).
Provides build_lm_coefficient_operator which pre-computes the expensive
factorization of (X’X) once and returns a lightweight function that maps
any response vector Y to OLS coefficients. Used by hypothesis property
tests (BS.4) and the generic bootstrap engine.
Functions:
| Name | Description |
|---|---|
build_lm_coefficient_operator | Create reusable linear operator Y -> coefficients. |
compute_batched_ols_bootstrap | Compute bootstrap OLS coefficients using chunked vmap. |
Attributes:
| Name | Type | Description |
|---|---|---|
Array |
Attributes¶
Array¶
Array = AnyFunctions¶
build_lm_coefficient_operator¶
build_lm_coefficient_operator(X: Array, method: Literal['cholesky', 'qr'] = 'cholesky') -> Callable[[Array], Array]Create reusable linear operator Y -> coefficients.
Precomputes and caches the expensive factorization. Returns a function that maps Y -> coefficients.
This implements the “hat-matrix trick” for efficient permutation testing: For fixed design matrix X, coefficients are a linear map from Y -> beta_hat: beta_hat = (X’X)^-1 X’y. We precompute the factorization of (X’X) once and reuse it for all permuted/bootstrapped Y values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X | Array | Design matrix, shape (n, p). | required |
method | Literal[‘cholesky’, ‘qr’] | Factorization method. - ‘cholesky’: Uses Cholesky decomposition of X’X (faster, more stable). - ‘qr’: Uses QR decomposition of X (handles rank deficiency better). | ‘cholesky’ |
Returns:
| Type | Description |
|---|---|
Callable[[Array], Array] | Function that maps Y (shape [n] or [n, m]) to coefficients. |
Examples:
X = np.array([[1, 2], [1, 3], [1, 4]])
operator = build_lm_coefficient_operator(X)
y = np.array([5, 8, 11])
coef = operator(y)compute_batched_ols_bootstrap¶
compute_batched_ols_bootstrap(boot_indices: np.ndarray, X: np.ndarray, y: np.ndarray, chunk_size: int | None = None) -> np.ndarrayCompute bootstrap OLS coefficients using chunked vmap.
For LM models (gaussian family, identity link, no random effects),
each bootstrap replicate is an independent OLS solve. This function
batches those solves via ops.vmap — on JAX, this compiles to
a single XLA program; on NumPy, it degrades to a loop+stack
(equivalent to sequential but without Python overhead from
fit_model).
Memory safety is ensured by chunking: compute_batch_size
determines how many solves to batch at once based on available
memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_indices | ndarray | Bootstrap index array, shape (n_boot, n). | required |
X | ndarray | Design matrix, shape (n, p). | required |
y | ndarray | Response vector, shape (n,). | required |
chunk_size | int | None | Number of bootstrap samples per chunk. If None, computed automatically from available memory. | None |
Returns:
| Type | Description |
|---|---|
ndarray | Coefficient array, shape (n_boot, p). |
lmer¶
LMER bootstrap with full PLS refitting.
This module provides parametric bootstrap for linear mixed-effects models (LMER). Each bootstrap iteration simulates y* = X beta + Z b* + eps* and refits via PLS optimization of the deviance / REML criterion.
Key characteristics:
Sequential loop (cannot vmap due to scipy.sparse operations)
Parametric bootstrap: simulate b* ~ N(0, sigma^2 LL’), eps* ~ N(0, sigma^2 I)
Full PLS refit per sample (expensive but necessary)
Optional parallelization via joblib
Functions:
| Name | Description |
|---|---|
lmer_bootstrap | Bootstrap inference for lmer parameters. |
lmer_bootstrap_single_iteration | Execute a single LMER bootstrap iteration. |
simulate_from_lmer | Simulate response from fitted lmer model. |
Classes¶
Functions¶
lmer_bootstrap¶
lmer_bootstrap(X: 'Array', Z: sp.csc_matrix | 'Array', y: 'Array', X_names: list[str], theta: 'Array', beta: 'Array', sigma: float, n_groups_list: list[int], re_structure: str, metadata: dict | None = None, lower_bounds: list[float] | None = None, n_boot: int = 999, seed: int | None = None, boot_type: Literal['parametric'] = 'parametric', ci_type: Literal['percentile', 'basic', 'bca'] = 'percentile', level: float = 0.95, method: Literal['REML', 'ML'] = 'REML', max_iter: int = 10000, tol: float = 1e-08, verbose: bool = False, which: Literal['fixef', 'ranef', 'all'] = 'fixef', group_names: list[str] | None = None, random_names: list[str] | None = None, n_jobs: int = 1) -> BootstrapResultBootstrap inference for lmer parameters.
Supports parallel execution via joblib for significant speedups on multi-core machines. Each bootstrap iteration requires a full PLS optimization, making parallelization highly beneficial.
Uses parametric bootstrap: simulate y* from the fitted model
(y* = X beta_hat + Z b* + eps*) and refit. This is the standard approach
for mixed models, matching R’s lme4::bootMer(type="parametric").
Note: Only parametric bootstrap is supported for mixed models, matching R/lme4. Residual and case bootstrap are not applicable because the residual structure involves multiple variance components (within-group and between-group). Parametric bootstrap properly accounts for this hierarchical structure by simulating new random effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X | ‘Array’ | Fixed effects design matrix, shape (n, p). | required |
Z | csc_matrix | ‘Array’ | Random effects design matrix, shape (n, q), scipy sparse CSC. | required |
y | ‘Array’ | Response vector, shape (n,). | required |
X_names | list[str] | Fixed effects coefficient names. | required |
theta | ‘Array’ | Variance parameters from fitted model. | required |
beta | ‘Array’ | Fixed effects coefficients from fitted model. | required |
sigma | float | Residual standard deviation from fitted model. | required |
n_groups_list | list[int] | Number of groups per grouping factor. | required |
re_structure | str | Random effects structure type. | required |
metadata | dict | None | Additional metadata for complex structures. | None |
lower_bounds | list[float] | None | Lower bounds for theta optimization. If None, uses 0 for all. | None |
n_boot | int | Number of bootstrap samples. | 999 |
seed | int | None | Random seed for reproducibility. | None |
boot_type | Literal[‘parametric’] | Type of bootstrap. Only "parametric" is supported. | ‘parametric’ |
ci_type | Literal[‘percentile’, ‘basic’, ‘bca’] | Confidence interval type: “percentile”, “basic”, or “bca”. | ‘percentile’ |
level | float | Confidence level (e.g., 0.95 for 95% CI). | 0.95 |
method | Literal[‘REML’, ‘ML’] | Estimation method for refitting: “REML” or “ML”. | ‘REML’ |
max_iter | int | Maximum optimizer iterations for each refit. | 10000 |
tol | float | Convergence tolerance for optimizer. | 1e-08 |
verbose | bool | Print progress information. | False |
which | Literal[‘fixef’, ‘ranef’, ‘all’] | Which parameters to bootstrap: - “fixef”: Fixed effects coefficients (default). - “ranef”: Variance components (SDs and correlations). - “all”: Both fixed effects and variance components. | ‘fixef’ |
group_names | list[str] | None | Grouping factor names (required if which=“ranef” or “all”). | None |
random_names | list[str] | None | Random effect names (required if which=“ranef” or “all”). | None |
n_jobs | int | Number of parallel jobs. Default 1 (sequential). Use -1 for all available cores. Parallelization uses joblib with the loky backend for process-based parallelism. | 1 |
Returns:
| Type | Description |
|---|---|
BootstrapResult | BootstrapResult with observed stats, bootstrap samples, and CIs. |
BootstrapResult | For which=“ranef”, param_names are variance component names |
BootstrapResult | (e.g., “Subject:Intercept_sd”, “Subject:corr_Intercept:Days”, “Residual_sd”). |
BootstrapResult | For which=“all”, fixed effects come first, then variance components. |
Examples:
# Parametric bootstrap for fixed effects
result = lmer_bootstrap(
X, Z, y, ["Intercept", "Days"], theta, beta, sigma,
n_groups_list=[18], re_structure="slope",
n_boot=999, boot_type="parametric"
)
print(result.ci)
# Bootstrap for variance components
result = lmer_bootstrap(
X, Z, y, ["Intercept", "Days"], theta, beta, sigma,
n_groups_list=[18], re_structure="slope",
n_boot=999, which="ranef",
group_names=["Subject"], random_names=["Intercept", "Days"]
)Bootstrap refitting can fail to converge for some samples. Convergence Bootstrap refitting can fail to converge for some samples. Convergence failures > 10% will trigger a warning. Failed samples are excluded from CI computation.
lmer_bootstrap_single_iteration¶
lmer_bootstrap_single_iteration(rng: RNG, X_np: np.ndarray, Z_sparse: sp.csc_matrix, beta_np: np.ndarray, theta_np: np.ndarray, sigma: float, n_groups_list: list[int], re_structure: str, metadata: dict | None, method: str, lower_bounds: list[float], upper_bounds: list[float], max_iter: int, lambda_template: object | None, X_arr: np.ndarray, XtX: np.ndarray, n: int, p: int, n_params: int, which: str, group_names: list[str] | None, random_names: list[str] | None) -> np.ndarrayExecute a single LMER bootstrap iteration.
This is a standalone function (not a closure) to enable joblib parallelization. All arguments are passed explicitly to avoid pickling issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random number generation. | required |
X_np | ndarray | Fixed effects design matrix. | required |
Z_sparse | csc_matrix | Random effects design matrix (sparse CSC). | required |
beta_np | ndarray | Fixed effects coefficients. | required |
theta_np | ndarray | Variance parameters (starting values). | required |
sigma | float | Residual standard deviation. | required |
n_groups_list | list[int] | Number of groups per factor. | required |
re_structure | str | Random effects structure type. | required |
metadata | dict | None | Model metadata. | required |
method | str | Estimation method (“REML” or “ML”). | required |
lower_bounds | list[float] | Lower bounds for theta optimization. | required |
upper_bounds | list[float] | Upper bounds for theta optimization. | required |
max_iter | int | Maximum optimizer iterations. | required |
lambda_template | object | None | Pre-built Lambda template (or None). | required |
X_arr | ndarray | X as array (pre-computed). | required |
XtX | ndarray | X’X matrix (pre-computed). | required |
n | int | Number of observations. | required |
p | int | Number of fixed effects. | required |
n_params | int | Number of parameters to return. | required |
which | str | Which parameters (“fixef”, “ranef”, or “all”). | required |
group_names | list[str] | None | Names of grouping factors. | required |
random_names | list[str] | None | Names of random effects. | required |
Returns:
| Type | Description |
|---|---|
ndarray | Array of bootstrap estimates, or NaN array if iteration failed. |
simulate_from_lmer¶
simulate_from_lmer(rng: RNG, X: np.ndarray, Z: sp.csc_matrix, beta: np.ndarray, theta: np.ndarray, sigma: float, n_groups_list: list[int], re_structure: str, metadata: dict | None = None) -> np.ndarraySimulate response from fitted lmer model.
Generates y* = X beta + Z b* + eps* where:
b* ~ N(0, sigma^2 LL’) are simulated random effects
eps* ~ N(0, sigma^2 I) are simulated residuals
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random number generation. | required |
X | ndarray | Fixed effects design matrix, shape (n, p). | required |
Z | csc_matrix | Random effects design matrix, shape (n, q), scipy sparse CSC. | required |
beta | ndarray | Fixed effects coefficients, shape (p,). | required |
theta | ndarray | Variance parameters for Lambda. | required |
sigma | float | Residual standard deviation. | required |
n_groups_list | list[int] | Number of groups per grouping factor. | required |
re_structure | str | Random effects structure type. | required |
metadata | dict | None | Additional metadata for complex structures. | None |
Returns:
| Type | Description |
|---|---|
ndarray | Simulated response vector, shape (n,). |
results¶
Result containers for resampling procedures.
Classes:
| Name | Description |
|---|---|
BootstrapResult | Results from a bootstrap procedure. |
Classes¶
BootstrapResult¶
BootstrapResult(observed: Array, boot_samples: Array, ci_lower: Array, ci_upper: Array, param_names: list[str], n_boot: int, ci_type: Literal['percentile', 'basic', 'bca'], level: float, term_types: list[str] | None = None) -> NoneResults from a bootstrap procedure.
Attributes:
| Name | Type | Description |
|---|---|---|
observed | Array | Observed statistics, shape [n_params]. |
boot_samples | Array | Bootstrap samples, shape [n_boot, n_params]. |
ci_lower | Array | Lower confidence bounds, shape [n_params]. |
ci_upper | Array | Upper confidence bounds, shape [n_params]. |
param_names | list[str] | Names of parameters. |
n_boot | int | Number of bootstrap samples. |
ci_type | Literal[‘percentile’, ‘basic’, ‘bca’] | Type of confidence interval (“percentile”, “basic”, or “bca”). |
level | float | Confidence level (e.g., 0.95). |
term_types | list[str] | None | Optional list indicating “fixef” or “ranef” for each parameter. Only populated for mixed model bootstrap with which=“all”. |
Attributes¶
boot_samples¶
boot_samples: Arrayci¶
ci: tuple[Array, Array]Return confidence intervals as a tuple (lower, upper).
ci_lower¶
ci_lower: Arrayci_type¶
ci_type: Literal['percentile', 'basic', 'bca']ci_upper¶
ci_upper: Arraylevel¶
level: floatn_boot¶
n_boot: intobserved¶
observed: Arrayparam_names¶
param_names: list[str]se¶
se: ArrayBootstrap standard errors (std of boot_samples).
term_types¶
term_types: list[str] | None = Nonesimulate¶
Response simulation for parametric bootstrap.
Provides family-specific response simulation used by GLMER and LMER parametric bootstrap procedures. Each function simulates observations from a fitted mean vector using the appropriate distribution.
Functions:
| Name | Description |
|---|---|
simulate_binomial | Simulate binomial response from fitted probabilities. |
simulate_gaussian | Simulate Gaussian response from fitted values. |
simulate_poisson | Simulate Poisson response from fitted rates. |
simulate_response | Simulate response from GLM fitted values. |
Attributes¶
Classes¶
Functions¶
simulate_binomial¶
simulate_binomial(rng: RNG, mu: Array, weights: Array | None = None) -> ArraySimulate binomial response from fitted probabilities.
For standard Bernoulli trials (weights=1), samples 0/1. For binomial with n trials, samples proportion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random sampling. | required |
mu | Array | Fitted probabilities (n,), must be in (0, 1). | required |
weights | Array | None | Number of trials per observation. If None, uses 1 (Bernoulli). | None |
Returns:
| Type | Description |
|---|---|
Array | Simulated response (n,) as proportions in [0, 1]. |
simulate_gaussian¶
simulate_gaussian(rng: RNG, mu: Array, dispersion: float) -> ArraySimulate Gaussian response from fitted values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random sampling. | required |
mu | Array | Fitted values (n,). | required |
dispersion | float | Dispersion parameter (phi). For Gaussian family, phi = sigma^2. | required |
Returns:
| Type | Description |
|---|---|
Array | Simulated response (n,). |
simulate_poisson¶
simulate_poisson(rng: RNG, mu: Array) -> ArraySimulate Poisson response from fitted rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random sampling. | required |
mu | Array | Fitted rates (n,), must be positive. | required |
Returns:
| Type | Description |
|---|---|
Array | Simulated response (n,) as counts. |
simulate_response¶
simulate_response(rng: RNG, mu: Array, family: Family, dispersion: float = 1.0, weights: Array | None = None) -> ArraySimulate response from GLM fitted values.
Dispatches to family-specific simulation functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng | RNG | RNG object for random sampling. | required |
mu | Array | Fitted values on response scale (n,). | required |
family | Family | GLM family object. | required |
dispersion | float | Dispersion parameter (phi). For Gaussian family, dispersion = sigma^2. For other families, typically fixed at 1.0. | 1.0 |
weights | Array | None | Prior weights (only used for binomial trials). | None |
Returns:
| Type | Description |
|---|---|
Array | Simulated response (n,). |
resample_bundle¶
Shared helper for resampling DataBundle observations.
Functions:
| Name | Description |
|---|---|
build_resampled_bundle | Create a new DataBundle by selecting observations at given indices. |
Classes¶
Functions¶
build_resampled_bundle¶
build_resampled_bundle(bundle: DataBundle, indices: np.ndarray) -> DataBundleCreate a new DataBundle by selecting observations at given indices.
Handles dense X/y/valid_mask, sparse Z matrices, and observation-level REInfo fields (group_indices, group_ids_list, X_re).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bundle | DataBundle | Original data bundle. | required |
indices | ndarray | Integer array of observation indices (may contain repeats for bootstrap, or be a boolean mask for jackknife). | required |
Returns:
| Type | Description |
|---|---|
DataBundle | New DataBundle with resampled observations. |
satterthwaite_emm¶
Satterthwaite degrees of freedom for EMM contrasts in linear mixed models.
Computes per-EMM denominator df using the Satterthwaite approximation,
matching lmerTest’s approach where derivatives are taken w.r.t. the full
variance parameter vector varpar = [theta, sigma].
Hybrid analytical/numerical strategy:
The REML deviance separates into theta-dependent and sigma-dependent parts.
logdet_L, logdet_RX2, and pwrss depend on theta only (not sigma),
so sigma’s contributions can be computed analytically:
- ``d²dev/dsigma² = -2*df_dev/sigma² + 6*pwrss_0/sigma⁴`` (fully analytical)
- ``d²dev/(dtheta_i dsigma) = -2/sigma³ * dpwrss/dtheta_i`` (central diff on pwrss)
- ``dvcov/dsigma = (2/sigma) * fit.vcov`` (analytical, free)
This reduces Hessian evals from 25 to ~11 and Jacobian evals from 17 to ~9
for a single random-intercept model (52% reduction overall).Compute
pwrss_0at fitted theta (seeds cache)Compute
pwrss_0at fitted theta (seeds cache)Define
deviance_theta(th)— sigma fixed from fitCompute theta-only Hessian via Richardson extrapolation
Compute
dpwrss/dthetavia central difference (2m evals)Assemble full
(m+1) x (m+1)Hessian with analytical sigma blockvcov_varpar = 2 * H_full^{-1}Compute theta-only Jacobian via Richardson extrapolation
Append analytical sigma column:
(2/sigma) * fit.vcovCall
satterthwaite_df_for_contrasts(L, vcov, vcov_varpar, jac_list)
Functions:
| Name | Description |
|---|---|
compute_satterthwaite_emm_df | Compute Satterthwaite denominator df for EMM contrast rows. |
Classes¶
Functions¶
compute_satterthwaite_emm_df¶
compute_satterthwaite_emm_df(bundle: DataBundle, fit: FitState, spec: ModelSpec, L: np.ndarray) -> np.ndarrayCompute Satterthwaite denominator df for EMM contrast rows.
For each row of the contrast matrix L, computes the Satterthwaite approximation to the denominator degrees of freedom.
Uses a hybrid approach: differentiates numerically w.r.t. theta only, then fills in sigma’s contributions analytically. This produces identical results to the full numerical approach but with ~52% fewer function evaluations for single random-intercept models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bundle | DataBundle | Data bundle with X, Z, y, and re_metadata. | required |
fit | FitState | Fit state with theta, vcov, and sigma. | required |
spec | ModelSpec | Model specification (method, family, etc.). | required |
L | ndarray | Contrast matrix, shape (n_contrasts, n_coef). | required |
Returns:
| Type | Description |
|---|---|
ndarray | Array of Satterthwaite df, shape (n_contrasts,). |
simulation¶
Simulation inference: summary statistics across simulation replicates.
For post-fit simulations (nsim=), computes per-observation mean, SD, and quantiles across simulation columns. For pre-fit data generation, returns a minimal state.
compute_simulation_inference: Summarize simulation replicates. compute_simulation_inference: Summarize simulation replicates.
Functions:
| Name | Description |
|---|---|
compute_simulation_inference | Compute inference for simulations. |
Classes¶
Functions¶
compute_simulation_inference¶
compute_simulation_inference(simulations: 'pl.DataFrame', conf_level: float) -> 'SimulationInferenceState'Compute inference for simulations.
For post-fit simulations (sim_1, sim_2, ...): Computes summary statistics across simulation replicates: - Mean and SD per observation across simulations - Quantiles (e.g., 2.5%, 97.5% for 95% interval)
Returns a basic state indicating generated data. Returns a basic state indicating generated data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulations | ‘pl.DataFrame’ | DataFrame of simulation results. Post-fit simulations have columns named sim_1, sim_2, etc. | required |
conf_level | float | Confidence level for interval quantiles. | required |
Returns:
| Type | Description |
|---|---|
‘SimulationInferenceState’ | SimulationInferenceState with summary statistics. |