Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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:

NameDescription
InferResultImmutable result of inference dispatch.

Functions:

NameDescription
augment_spread_with_profile_ciAugment variance components with profile likelihood confidence intervals.
build_emm_reference_gridBuild reference grid X matrix for EMM computation.
compute_bootstrap_paramsGenerate bootstrap distribution of coefficient estimates.
compute_bootstrap_pvalueCompute bootstrap p-values.
compute_cv_metricsCompute k-fold or leave-one-out cross-validation metrics.
compute_jackknife_coefsCompute leave-one-out jackknife coefficient estimates.
compute_mee_bootstrapCompute bootstrap inference for marginal effects.
compute_mee_permutationCompute permutation-based inference for marginal effects.
compute_params_asymptoticCompute asymptotic (Wald) inference for model parameters.
compute_params_bootstrapCompute bootstrap inference for parameters.
compute_params_bootstrap_mixedCompute bootstrap inference for mixed model parameters.
compute_params_cv_inferenceCompute CV-based parameter importance via ablation.
compute_params_permutationCompute permutation-based inference for model parameters.
compute_prediction_asymptoticCompute asymptotic inference for predictions via delta method.
compute_prediction_bootstrapCompute bootstrap inference for predictions.
compute_profile_inferenceCompute profile likelihood CIs for variance components.
compute_satterthwaite_emm_dfCompute Satterthwaite denominator df for EMM contrast rows.
compute_simulation_inferenceCompute inference for simulations.
dispatch_inferDispatch inference to the correct backend based on method and last operation.
dispatch_mee_inferenceDispatch marginal effects inference to the appropriate method.
dispatch_params_inferenceDispatch parameter inference to the appropriate method.
dispatch_prediction_inferenceDispatch prediction inference to the appropriate method.
generate_group_kfold_splitsGenerate group-aware k-fold cross-validation indices.
generate_kfold_splitsGenerate k-fold cross-validation train/test indices.

Modules:

NameDescription
asymptoticAsymptotic inference for parameters and predictions.
bootstrapBootstrap inference methods.
cvCross-validation inference and metrics.
dispatchInfer dispatch: routes .infer() calls to the correct inference backend.
formula_utilsFormula utility functions shared across operations modules.
meeMarginal effects inference dispatch.
paramsParameter inference dispatch.
permutationPermutation inference for parameters and marginal effects.
predictionPrediction inference dispatch.
profileProfile likelihood inference for variance components.
resampleInternal resampling operations.
resample_bundleShared helper for resampling DataBundle observations.
satterthwaite_emmSatterthwaite degrees of freedom for EMM contrasts in linear mixed models.
simulationSimulation 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:

NameTypeDescription
params_inferenceInferenceState | NoneUpdated InferenceState (fit branch).
meeMeeState | JointTestState | NoneUpdated MeeState or JointTestState (explore/joint branch).
predPredictionState | NoneUpdated PredictionState (predict branch).
cvCVState | NoneCVState from cross-validation (predict + cv).
sim_inferenceSimulationInferenceState | NoneSimulationInferenceState (simulate branch).
resamplesResamplesState | NoneResamplesState from bootstrap/permutation.
varying_spreadVaryingSpreadState | NoneUpdated VaryingSpreadState (profile branch).
profileProfileState | NoneProfileState from profile likelihood.
last_opstr | NoneOverridden last_op (only set by joint test branch).

Attributes

cv
cv: CVState | None = None
last_op
last_op: str | None = None
mee
mee: MeeState | JointTestState | None = None
params_inference
params_inference: InferenceState | None = None
pred
pred: PredictionState | None = None
profile
profile: ProfileState | None = None
resamples
resamples: ResamplesState | None = None
sim_inference
sim_inference: SimulationInferenceState | None = None
varying_spread
varying_spread: VaryingSpreadState | None = None

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:

NameTypeDescriptionDefault
spread‘VaryingSpreadState’Variance component state from .fit().required
profile‘ProfileState’Profile likelihood state with ci_sd dict.required
conf_levelfloatConfidence level used for CIs.required

Returns:

TypeDescription
‘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.ndarray

Build reference grid X matrix for EMM computation.

Parameters:

NameTypeDescriptionDefault
dataDataFrameThe data DataFrame.required
bundle‘DataBundle’Data bundle.required
focal_varstrThe focal variable for EMMs.required

Returns:

TypeDescription
ndarrayReference 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.ndarray

Generate bootstrap distribution of coefficient estimates.

Uses case resampling: resample observations with replacement, refit the model, and collect coefficient estimates.

Parameters:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle with X, y, etc.required
n_bootintNumber of bootstrap samples.1000
seedint | NoneRandom seed for reproducibility.None
n_jobsintNumber of parallel jobs. 1 (default) for sequential, -1 for all available cores.1

Returns:

TypeDescription
ndarrayArray 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.ndarray

Compute bootstrap p-values.

Uses the proportion of bootstrap samples as extreme or more extreme than the observed value (relative to null).

Parameters:

NameTypeDescriptionDefault
observedndarrayObserved statistics.required
boot_samplesndarrayBootstrap sample statistics, shape (n_boot, n_params).required
nullfloatNull hypothesis value (default 0.0).0.0
alternativestr“two-sided” (default), “greater”, or “less”.‘two-sided’

Returns:

TypeDescription
ndarrayP-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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification (family, link, etc.).required
bundle‘DataBundle’Data bundle with X, y, and metadata.required
kint | strNumber of folds (default 10), or "loo" for leave-one-out cross-validation.10
seedint | NoneRandom seed for reproducibility. Ignored for LOO.None
holdout_group_idsndarray | NonePre-resolved group ID array for group-aware splits. When provided, entire groups are held out per fold.None

Returns:

TypeDescription
‘CVState’CVState with RMSE, MAE, R², and optionally deviance/accuracy.

compute_jackknife_coefs

compute_jackknife_coefs(spec: 'ModelSpec', bundle: 'DataBundle') -> np.ndarray

Compute leave-one-out jackknife coefficient estimates.

Used for BCa acceleration factor calculation.

Parameters:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required

Returns:

TypeDescription
ndarrayArray 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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required
mee‘MeeState’Current MEE state.required
dataDataFrameThe data DataFrame.required
conf_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type (“bca”, “percentile”, “basic”).‘bca’
seedint | NoneRandom seed for reproducibility.None
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’
save_resamplesboolIf True (default), return raw bootstrap samples alongside the MeeState. If False, discard to save memory.True

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for intervals.required
se_obsndarrayPre-computed standard errors for t-statistic denominator (from asymptotic SE computation in the model class).required
n_permintNumber of permutations.1000
seedint | NoneRandom seed for reproducibility.None
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’
save_resamplesboolIf True (default), return null t-statistics alongside the MeeState. If False, discard to save memory.True

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle with design matrix X.required
fit‘FitState’Fit state with coefficients and vcov.required
conf_levelfloatConfidence level for intervals (e.g. 0.95).required
errorsstr | NoneError 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
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required
fit‘FitState’Current fit state.required
conf_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type (“bca”, “percentile”, “basic”).‘bca’
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolIf True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory.True
n_jobsintNumber of parallel jobs. 1 (default) for sequential, -1 for all available cores.1
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type ("percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolIf True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory.True
n_jobsintNumber of parallel jobs. 1 (default) for sequential, -1 for all available cores.1
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default "two-sided").‘two-sided’

Returns:

TypeDescription
‘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):

  1. 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).

  2. Fit the ablated model and run k-fold CV.

  3. PRE = full_metric − ablated_metric per fold.

  4. 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:

NameTypeDescriptionDefault
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_levelfloatConfidence level (kept for API consistency).required
link_overridestr | NoneNon-default link function override (None uses spec default).None
kint | strNumber of folds for cross-validation (default 10).10
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
‘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_rsquared

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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle with X, y, etc.required
fit‘FitState’Fit state with coefficients and vcov.required
conf_levelfloatConfidence level for intervals.required
n_permintNumber of permutations.1000
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolIf True (default), store null distribution in InferenceState. If False, discard to save memory.True
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for intervals (e.g. 0.95).required

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required
pred‘PredictionState’Current prediction state (must carry config).required
conf_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type (“percentile”, “basic”).‘percentile’
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for profile CIs.0.95
n_stepsintNumber of profiling steps (default: 20).20
verboseboolPrint profiling progress (default: False).False
thresholdfloat | NoneCustom deviance threshold for CIs (default: None, uses chi-squared quantile).None

Returns:

TypeDescription
‘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.ndarray

Compute 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:

NameTypeDescriptionDefault
bundleDataBundleData bundle with X, Z, y, and re_metadata.required
fitFitStateFit state with theta, vcov, and sigma.required
specModelSpecModel specification (method, family, etc.).required
LndarrayContrast matrix, shape (n_contrasts, n_coef).required

Returns:

TypeDescription
ndarrayArray 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:

NameTypeDescriptionDefault
simulations‘pl.DataFrame’DataFrame of simulation results. Post-fit simulations have columns named sim_1, sim_2, etc.required
conf_levelfloatConfidence level for interval quantiles.required

Returns:

TypeDescription
‘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) -> InferResult

Dispatch 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:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "perm", "cv", "profile", "joint").required
conf_levelfloatConfidence level (already normalized to 0–1 float).required
errorsstrError structure ("iid", "HC0""HC3", "hetero", "unequal_var").required
nullfloatNull hypothesis value.required
alternativestrAlternative hypothesis direction.required
last_opstr | NoneLast model operation ("fit", "explore", "predict", "simulate").required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
dataDataFrame | NoneOriginal data DataFrame.required
link_overridestr | NoneNon-default link function override (None uses spec).required
meeMeeState | JointTestState | NoneCurrent MeeState or JointTestState from explore.required
predPredictionState | NoneCurrent PredictionState from predict.required
simulationsDataFrame | NoneSimulated data DataFrame.required
varying_spreadVaryingSpreadState | NoneCurrent VaryingSpreadState.required
is_mixedboolWhether the model has random effects.required
n_bootintNumber of bootstrap samples (default: 1000).1000
n_permintNumber of permutation samples (default: 1000).1000
ci_typestrBootstrap CI type ("bca", "percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
n_jobsintNumber of parallel jobs (default: 1).1
save_resamplesboolWhether to store raw resample arrays (default: True).True
kint | strNumber of CV folds or "loo" (default: 10).10
n_stepsintProfile likelihood steps (default: 20).20
verboseboolProfile likelihood verbosity (default: False).False
thresholdfloat | NoneProfile likelihood deviance threshold (default: None).None
profile_autoboolAuto-compute profile CIs for variance components when how is "boot" or "perm" on a mixed model (default: True).True

Returns:

TypeDescription
InferResultInferResult 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:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "perm").required
meeMeeStateCurrent MeeState from explore().required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
dataDataFrameOriginal data as Polars DataFrame.required
conf_levelfloatConfidence level for intervals.required
errorsstrError type for robust SEs (e.g. "HC3", "hetero", "unequal_var").‘iid’
nullfloatNull hypothesis value.0.0
alternativestrAlternative hypothesis direction.‘two-sided’
n_bootintNumber of bootstrap samples (default: 1000).1000
n_permintNumber of permutation samples (default: 1000).1000
ci_typestrBootstrap CI type ("bca", "percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolWhether to store raw resample arrays (default: True).True

Returns:

TypeDescription
‘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) -> InferenceState

Dispatch parameter inference to the appropriate method.

Parameters:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "perm", "cv").required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
dataDataFrame | NoneOriginal data DataFrame (needed for CV and robust SEs).required
conf_levelfloatConfidence level for intervals.required
errorsstrError type for robust SEs (e.g. "HC3", "hetero", "unequal_var").‘iid’
nullfloatNull hypothesis value.0.0
alternativestrAlternative hypothesis direction.‘two-sided’
link_overridestr | NoneNon-default link function override for CV (None uses spec).None
n_bootintNumber of bootstrap samples (default: 1000).1000
n_permintNumber of permutation samples (default: 1000).1000
ci_typestrBootstrap CI type ("bca", "percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
n_jobsintNumber of parallel jobs (default: 1).1
save_resamplesboolWhether to store raw resample arrays (default: True).True
kint | strNumber of CV folds or "loo" (default: 10).10

Returns:

TypeDescription
InferenceStateInferenceState 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:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "cv").required
predPredictionStateCurrent prediction state.required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
conf_levelfloatConfidence level for intervals.required
n_bootintNumber of bootstrap samples (default: 1000).1000
ci_typestrBootstrap CI type (default: "percentile").‘percentile’
seedint | NoneRandom seed for reproducibility.None
kint | strNumber of CV folds or "loo" (default: 10).10

Returns:

TypeDescription
PredictionStateTuple of (augmented PredictionState, CVState or None). CVState is
CVState | Nonepopulated 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:

NameTypeDescriptionDefault
group_idsndarrayArray of group IDs for each observation (length n).required
kintNumber of folds.required
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
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)
2

generate_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:

NameTypeDescriptionDefault
nintTotal number of observations.required
kintNumber of folds.required
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
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
10

Modules

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:

NameDescription
augment_spread_with_profile_ciAugment variance components with profile likelihood confidence intervals.
compute_params_asymptoticCompute asymptotic (Wald) inference for model parameters.
compute_prediction_asymptoticCompute asymptotic inference for predictions via delta method.
resolve_error_typeNormalize errors parameter to canonical type string.
resolve_welchCompute 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:

NameTypeDescriptionDefault
spread‘VaryingSpreadState’Variance component state from .fit().required
profile‘ProfileState’Profile likelihood state with ci_sd dict.required
conf_levelfloatConfidence level used for CIs.required

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle with design matrix X.required
fit‘FitState’Fit state with coefficients and vcov.required
conf_levelfloatConfidence level for intervals (e.g. 0.95).required
errorsstr | NoneError 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
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for intervals (e.g. 0.95).required

Returns:

TypeDescription
‘PredictionState’Evolved PredictionState with se, ci_lower, ci_upper fields.
resolve_error_type
resolve_error_type(errors: str, family: str = 'gaussian') -> str

Normalize errors parameter to canonical type string.

Parameters:

NameTypeDescriptionDefault
errorsstrUser-facing error type (e.g. “HC3”, “hetero”, “unequal_var”).required
familystrModel family. When non-gaussian, “hetero” maps to HC0 (matching main branch behavior) instead of HC3.‘gaussian’

Returns:

TypeDescription
strCanonical 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:

NameTypeDescriptionDefault
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:

TypeDescription
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:

NameDescription
build_emm_reference_gridBuild reference grid X matrix for EMM computation.
compute_bootstrap_paramsGenerate bootstrap distribution of coefficient estimates.
compute_bootstrap_pvalueCompute bootstrap p-values.
compute_jackknife_coefsCompute leave-one-out jackknife coefficient estimates.
compute_mee_bootstrapCompute bootstrap inference for marginal effects.
compute_params_bootstrapCompute bootstrap inference for parameters.
compute_params_bootstrap_mixedCompute bootstrap inference for mixed model parameters.
compute_prediction_bootstrapCompute bootstrap inference for predictions.

Classes

Functions

build_emm_reference_grid
build_emm_reference_grid(data: pl.DataFrame, bundle: 'DataBundle', focal_var: str) -> np.ndarray

Build reference grid X matrix for EMM computation.

Parameters:

NameTypeDescriptionDefault
dataDataFrameThe data DataFrame.required
bundle‘DataBundle’Data bundle.required
focal_varstrThe focal variable for EMMs.required

Returns:

TypeDescription
ndarrayReference 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.ndarray

Generate bootstrap distribution of coefficient estimates.

Uses case resampling: resample observations with replacement, refit the model, and collect coefficient estimates.

Parameters:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle with X, y, etc.required
n_bootintNumber of bootstrap samples.1000
seedint | NoneRandom seed for reproducibility.None
n_jobsintNumber of parallel jobs. 1 (default) for sequential, -1 for all available cores.1

Returns:

TypeDescription
ndarrayArray 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.ndarray

Compute bootstrap p-values.

Uses the proportion of bootstrap samples as extreme or more extreme than the observed value (relative to null).

Parameters:

NameTypeDescriptionDefault
observedndarrayObserved statistics.required
boot_samplesndarrayBootstrap sample statistics, shape (n_boot, n_params).required
nullfloatNull hypothesis value (default 0.0).0.0
alternativestr“two-sided” (default), “greater”, or “less”.‘two-sided’

Returns:

TypeDescription
ndarrayP-values for each parameter.
compute_jackknife_coefs
compute_jackknife_coefs(spec: 'ModelSpec', bundle: 'DataBundle') -> np.ndarray

Compute leave-one-out jackknife coefficient estimates.

Used for BCa acceleration factor calculation.

Parameters:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required

Returns:

TypeDescription
ndarrayArray 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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required
mee‘MeeState’Current MEE state.required
dataDataFrameThe data DataFrame.required
conf_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type (“bca”, “percentile”, “basic”).‘bca’
seedint | NoneRandom seed for reproducibility.None
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’
save_resamplesboolIf True (default), return raw bootstrap samples alongside the MeeState. If False, discard to save memory.True

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required
fit‘FitState’Current fit state.required
conf_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type (“bca”, “percentile”, “basic”).‘bca’
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolIf True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory.True
n_jobsintNumber of parallel jobs. 1 (default) for sequential, -1 for all available cores.1
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type ("percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolIf True (default), store raw bootstrap samples in InferenceState. If False, discard to save memory.True
n_jobsintNumber of parallel jobs. 1 (default) for sequential, -1 for all available cores.1
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default "two-sided").‘two-sided’

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle.required
pred‘PredictionState’Current prediction state (must carry config).required
conf_levelfloatConfidence level for intervals.0.95
n_bootintNumber of bootstrap samples.1000
ci_typestrCI type (“percentile”, “basic”).‘percentile’
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
‘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:

NameDescription
compute_cv_metricsCompute k-fold or leave-one-out cross-validation metrics.
compute_params_cv_inferenceCompute CV-based parameter importance via ablation.
generate_group_kfold_splitsGenerate group-aware k-fold cross-validation indices.
generate_kfold_splitsGenerate k-fold cross-validation train/test indices.
resolve_holdout_group_idsResolve 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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification (family, link, etc.).required
bundle‘DataBundle’Data bundle with X, y, and metadata.required
kint | strNumber of folds (default 10), or "loo" for leave-one-out cross-validation.10
seedint | NoneRandom seed for reproducibility. Ignored for LOO.None
holdout_group_idsndarray | NonePre-resolved group ID array for group-aware splits. When provided, entire groups are held out per fold.None

Returns:

TypeDescription
‘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):

  1. 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).

  2. Fit the ablated model and run k-fold CV.

  3. PRE = full_metric − ablated_metric per fold.

  4. 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:

NameTypeDescriptionDefault
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_levelfloatConfidence level (kept for API consistency).required
link_overridestr | NoneNon-default link function override (None uses spec default).None
kint | strNumber of folds for cross-validation (default 10).10
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
‘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_rsquared
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:

NameTypeDescriptionDefault
group_idsndarrayArray of group IDs for each observation (length n).required
kintNumber of folds.required
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
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)
2
generate_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:

NameTypeDescriptionDefault
nintTotal number of observations.required
kintNumber of folds.required
seedint | NoneRandom seed for reproducibility.None

Returns:

TypeDescription
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
10
resolve_holdout_group_ids
resolve_holdout_group_ids(holdout_group: str | None, data: 'pl.DataFrame | None', spec: 'ModelSpec', bundle: 'DataBundle') -> np.ndarray | None

Resolve holdout group IDs for group-aware CV splitting.

Logic:

  1. If holdout_group is provided, look up the column in data.

  2. 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.

  3. No RE and no holdout_group: return None (use naive splits).

Parameters:

NameTypeDescriptionDefault
holdout_groupstr | NoneUser-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:

TypeDescription
ndarray | NoneInteger-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:

NameDescription
InferResultImmutable result of inference dispatch.

Functions:

NameDescription
dispatch_inferDispatch 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:

NameTypeDescription
params_inferenceInferenceState | NoneUpdated InferenceState (fit branch).
meeMeeState | JointTestState | NoneUpdated MeeState or JointTestState (explore/joint branch).
predPredictionState | NoneUpdated PredictionState (predict branch).
cvCVState | NoneCVState from cross-validation (predict + cv).
sim_inferenceSimulationInferenceState | NoneSimulationInferenceState (simulate branch).
resamplesResamplesState | NoneResamplesState from bootstrap/permutation.
varying_spreadVaryingSpreadState | NoneUpdated VaryingSpreadState (profile branch).
profileProfileState | NoneProfileState from profile likelihood.
last_opstr | NoneOverridden last_op (only set by joint test branch).
Attributes
cv
cv: CVState | None = None
last_op
last_op: str | None = None
mee
mee: MeeState | JointTestState | None = None
params_inference
params_inference: InferenceState | None = None
pred
pred: PredictionState | None = None
profile
profile: ProfileState | None = None
resamples
resamples: ResamplesState | None = None
sim_inference
sim_inference: SimulationInferenceState | None = None
varying_spread
varying_spread: VaryingSpreadState | None = None

Functions

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) -> InferResult

Dispatch 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:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "perm", "cv", "profile", "joint").required
conf_levelfloatConfidence level (already normalized to 0–1 float).required
errorsstrError structure ("iid", "HC0""HC3", "hetero", "unequal_var").required
nullfloatNull hypothesis value.required
alternativestrAlternative hypothesis direction.required
last_opstr | NoneLast model operation ("fit", "explore", "predict", "simulate").required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
dataDataFrame | NoneOriginal data DataFrame.required
link_overridestr | NoneNon-default link function override (None uses spec).required
meeMeeState | JointTestState | NoneCurrent MeeState or JointTestState from explore.required
predPredictionState | NoneCurrent PredictionState from predict.required
simulationsDataFrame | NoneSimulated data DataFrame.required
varying_spreadVaryingSpreadState | NoneCurrent VaryingSpreadState.required
is_mixedboolWhether the model has random effects.required
n_bootintNumber of bootstrap samples (default: 1000).1000
n_permintNumber of permutation samples (default: 1000).1000
ci_typestrBootstrap CI type ("bca", "percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
n_jobsintNumber of parallel jobs (default: 1).1
save_resamplesboolWhether to store raw resample arrays (default: True).True
kint | strNumber of CV folds or "loo" (default: 10).10
n_stepsintProfile likelihood steps (default: 20).20
verboseboolProfile likelihood verbosity (default: False).False
thresholdfloat | NoneProfile likelihood deviance threshold (default: None).None
profile_autoboolAuto-compute profile CIs for variance components when how is "boot" or "perm" on a mixed model (default: True).True

Returns:

TypeDescription
InferResultInferResult with populated fields for the caller to assign.

formula_utils

Formula utility functions shared across operations modules.

Functions:

NameDescription
build_ablated_formulaBuild a formula with one source term ablated.
parse_valueParse a single value as float or string.
remove_predictor_from_formulaRemove 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, ...] = ()) -> str

Build 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:

NameTypeDescriptionDefault
response_varstrName of the response variable.required
source_termslist[str]All source terms (excluding Intercept) in the model.required
ablate_termstrThe term to remove.required
random_termstuple[str, ...]Random effect terms (e.g., ``("(1group)",)``).

Returns:

TypeDescription
strFormula 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 | str

Parse a single value as float or string.

Tries to parse as float first; falls back to unquoted or quoted string.

Parameters:

NameTypeDescriptionDefault
sstrString to parse.required

Returns:

TypeDescription
float | strParsed 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) -> str

Remove a predictor term from a formula string.

Handles various predictor formats including raw names, factor(), C(), c().

Parameters:

NameTypeDescriptionDefault
formulastrR-style formula string (e.g., “y ~ x1 + x2”).required
predictorstrName of predictor to remove.required

Returns:

TypeDescription
strModified 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:

NameDescription
dispatch_mee_inferenceDispatch 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:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "perm").required
meeMeeStateCurrent MeeState from explore().required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
dataDataFrameOriginal data as Polars DataFrame.required
conf_levelfloatConfidence level for intervals.required
errorsstrError type for robust SEs (e.g. "HC3", "hetero", "unequal_var").‘iid’
nullfloatNull hypothesis value.0.0
alternativestrAlternative hypothesis direction.‘two-sided’
n_bootintNumber of bootstrap samples (default: 1000).1000
n_permintNumber of permutation samples (default: 1000).1000
ci_typestrBootstrap CI type ("bca", "percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolWhether to store raw resample arrays (default: True).True

Returns:

TypeDescription
‘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:

NameDescription
dispatch_params_inferenceDispatch 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) -> InferenceState

Dispatch parameter inference to the appropriate method.

Parameters:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "perm", "cv").required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
dataDataFrame | NoneOriginal data DataFrame (needed for CV and robust SEs).required
conf_levelfloatConfidence level for intervals.required
errorsstrError type for robust SEs (e.g. "HC3", "hetero", "unequal_var").‘iid’
nullfloatNull hypothesis value.0.0
alternativestrAlternative hypothesis direction.‘two-sided’
link_overridestr | NoneNon-default link function override for CV (None uses spec).None
n_bootintNumber of bootstrap samples (default: 1000).1000
n_permintNumber of permutation samples (default: 1000).1000
ci_typestrBootstrap CI type ("bca", "percentile", "basic").‘bca’
seedint | NoneRandom seed for reproducibility.None
n_jobsintNumber of parallel jobs (default: 1).1
save_resamplesboolWhether to store raw resample arrays (default: True).True
kint | strNumber of CV folds or "loo" (default: 10).10

Returns:

TypeDescription
InferenceStateInferenceState 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:

NameDescription
compute_mee_permutationCompute permutation-based inference for marginal effects.
compute_params_permutationCompute 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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for intervals.required
se_obsndarrayPre-computed standard errors for t-statistic denominator (from asymptotic SE computation in the model class).required
n_permintNumber of permutations.1000
seedint | NoneRandom seed for reproducibility.None
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’
save_resamplesboolIf True (default), return null t-statistics alongside the MeeState. If False, discard to save memory.True

Returns:

TypeDescription
‘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:

NameTypeDescriptionDefault
spec‘ModelSpec’Model specification.required
bundle‘DataBundle’Data bundle with X, y, etc.required
fit‘FitState’Fit state with coefficients and vcov.required
conf_levelfloatConfidence level for intervals.required
n_permintNumber of permutations.1000
seedint | NoneRandom seed for reproducibility.None
save_resamplesboolIf True (default), store null distribution in InferenceState. If False, discard to save memory.True
nullfloatNull hypothesis value (default 0.0).0.0
alternativestrAlternative hypothesis direction (default “two-sided”).‘two-sided’

Returns:

TypeDescription
‘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:

NameDescription
dispatch_prediction_inferenceDispatch 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:

NameTypeDescriptionDefault
howstrInference method ("asymp", "boot", "cv").required
predPredictionStateCurrent prediction state.required
specModelSpecModel specification.required
bundleDataBundleData bundle with design matrices.required
fitFitStateFit state with coefficients and vcov.required
conf_levelfloatConfidence level for intervals.required
n_bootintNumber of bootstrap samples (default: 1000).1000
ci_typestrBootstrap CI type (default: "percentile").‘percentile’
seedint | NoneRandom seed for reproducibility.None
kint | strNumber of CV folds or "loo" (default: 10).10

Returns:

TypeDescription
PredictionStateTuple of (augmented PredictionState, CVState or None). CVState is
CVState | Nonepopulated 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:

NameDescription
compute_profile_inferenceCompute 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:

NameTypeDescriptionDefault
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_levelfloatConfidence level for profile CIs.0.95
n_stepsintNumber of profiling steps (default: 20).20
verboseboolPrint profiling progress (default: False).False
thresholdfloat | NoneCustom deviance threshold for CIs (default: None, uses chi-squared quantile).None

Returns:

TypeDescription
‘ProfileState’ProfileState with profile traces and CIs.

resample

Internal resampling operations.

Provides bootstrap procedures for bossanova models.

Module layout:

Modules:

NameDescription
commonCommon bootstrap execution patterns shared across model types.
coreCore resampling utilities.
glm_operatorsGLM bootstrap operators (vmap-accelerated IRLS).
glmerGLMER bootstrap with full PIRLS refitting.
lm_operatorsLinear model coefficient operator (hat-matrix trick).
lmerLMER bootstrap with full PLS refitting.
resultsResult containers for resampling procedures.
simulateResponse simulation for parametric bootstrap.

Classes:

NameDescription
BootstrapResultResults from a bootstrap procedure.

Functions:

NameDescription
build_lm_coefficient_operatorCreate reusable linear operator Y -> coefficients.
compute_batched_glm_bootstrapCompute bootstrap GLM coefficients using chunked vmap.
compute_batched_ols_bootstrapCompute bootstrap OLS coefficients using chunked vmap.
compute_bootstrap_ci_basicCompute basic (pivotal) bootstrap confidence intervals.
compute_bootstrap_ci_bcaBCa (bias-corrected and accelerated) bootstrap confidence intervals.
compute_bootstrap_ci_percentileCompute percentile bootstrap confidence intervals.
compute_pvaluesCompute permutation p-values.
generate_bootstrap_indicesGenerate n_boot bootstrap index arrays (with replacement).
generate_kfold_indicesGenerate k-fold cross-validation train/test index splits.
generate_loo_indicesGenerate leave-one-out cross-validation train/test index splits.
generate_permutation_indicesGenerate n_perm permutation index arrays.
glmer_bootstrapBootstrap inference for GLMER coefficients.
lmer_bootstrapBootstrap inference for lmer parameters.
simulate_responseSimulate response from GLM fitted values.

Attributes:

NameTypeDescription
BCa_MIN_NBOOT

Attributes

BCa_MIN_NBOOT
BCa_MIN_NBOOT = 50

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) -> None

Results from a bootstrap procedure.

Attributes:

NameTypeDescription
observedArrayObserved statistics, shape [n_params].
boot_samplesArrayBootstrap samples, shape [n_boot, n_params].
ci_lowerArrayLower confidence bounds, shape [n_params].
ci_upperArrayUpper confidence bounds, shape [n_params].
param_nameslist[str]Names of parameters.
n_bootintNumber of bootstrap samples.
ci_typeLiteral[‘percentile’, ‘basic’, ‘bca’]Type of confidence interval (“percentile”, “basic”, or “bca”).
levelfloatConfidence level (e.g., 0.95).
term_typeslist[str] | NoneOptional list indicating “fixef” or “ranef” for each parameter. Only populated for mixed model bootstrap with which=“all”.
Attributes
boot_samples
boot_samples: Array
ci
ci: tuple[Array, Array]

Return confidence intervals as a tuple (lower, upper).

ci_lower
ci_lower: Array
ci_type
ci_type: Literal['percentile', 'basic', 'bca']
ci_upper
ci_upper: Array
level
level: float
n_boot
n_boot: int
observed
observed: Array
param_names
param_names: list[str]
se
se: Array

Bootstrap standard errors (std of boot_samples).

term_types
term_types: list[str] | None = None

Functions

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:

NameTypeDescriptionDefault
XArrayDesign matrix, shape (n, p).required
methodLiteral[‘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:

TypeDescription
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.ndarray

Compute 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:

NameTypeDescriptionDefault
boot_indicesndarrayBootstrap index array, shape (n_boot, n).required
XndarrayDesign matrix, shape (n, p).required
yndarrayResponse vector, shape (n,).required
familyFamilyFamily object with link, variance, and deviance functions.required
weightsndarray | NoneOptional prior observation weights, shape (n,). If None, equal weights (ones) are used.None
max_iterintMaximum IRLS iterations per bootstrap sample.25
tolfloatConvergence tolerance for relative deviance change.1e-08
chunk_sizeint | NoneNumber of bootstrap samples per chunk. If None, computed automatically from available memory.None

Returns:

TypeDescription
ndarrayCoefficient 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.ndarray

Compute 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:

NameTypeDescriptionDefault
boot_indicesndarrayBootstrap index array, shape (n_boot, n).required
XndarrayDesign matrix, shape (n, p).required
yndarrayResponse vector, shape (n,).required
chunk_sizeint | NoneNumber of bootstrap samples per chunk. If None, computed automatically from available memory.None

Returns:

TypeDescription
ndarrayCoefficient 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:

NameTypeDescriptionDefault
observed‘ArrayLike’Observed statistic, shape [n_params] or scalar.required
boot_stats‘ArrayLike’Bootstrap statistics, shape [n_boot] or [n_boot, n_params].required
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95

Returns:

TypeDescription
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:

NameTypeDescriptionDefault
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
levelfloatConfidence level (default 0.95).0.95

Returns:

TypeDescription
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:

NameTypeDescriptionDefault
boot_stats‘ArrayLike’Bootstrap statistics, shape [n_boot] or [n_boot, n_params].required
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95

Returns:

TypeDescription
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.ndarray

Compute permutation p-values.

Uses formula: p = (1 + sum(|T_null| >= |T_obs|)) / (B + 1) where B is the number of null samples.

Parameters:

NameTypeDescriptionDefault
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
alternativeLiteral[‘two-sided’, ‘greater’, ‘less’]Type of test: “two-sided”, “greater”, or “less”.‘two-sided’

Returns:

TypeDescription
ndarrayP-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.ndarray

Generate n_boot bootstrap index arrays (with replacement).

Parameters:

NameTypeDescriptionDefault
rng‘RNG’RNG object (from rng).required
nintSize of each bootstrap sample.required
n_bootintNumber of bootstrap samples to generate.required

Returns:

TypeDescription
ndarrayShape [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:

NameTypeDescriptionDefault
rng‘RNG’RNG object (from rng). Used if shuffle=True.required
nintTotal number of observations.required
kintNumber of folds.required
shuffleboolWhether to shuffle indices before splitting.True

Returns:

TypeDescription
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)
# 10
generate_loo_indices
generate_loo_indices(n: int) -> list[tuple[np.ndarray, np.ndarray]]

Generate leave-one-out cross-validation train/test index splits.

Parameters:

NameTypeDescriptionDefault
nintTotal number of observations.required

Returns:

TypeDescription
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)
# 1
generate_permutation_indices
generate_permutation_indices(rng: 'RNG', n: int, n_perm: int) -> np.ndarray

Generate n_perm permutation index arrays.

Parameters:

NameTypeDescriptionDefault
rng‘RNG’RNG object (from rng).required
nintLength of each permutation.required
n_permintNumber of permutations to generate.required

Returns:

TypeDescription
ndarrayShape [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) -> BootstrapResult

Bootstrap 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:

NameTypeDescriptionDefault
X‘Array’Fixed effects design matrix, shape (n, p).required
Zcsc_matrix | ‘Array’Random effects design matrix, sparse CSC or dense array (n, q).required
y‘Array’Response vector, shape (n,).required
X_nameslist[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
familyFamilyGLM family object (binomial or poisson).required
n_groups_listlist[int]List of group sizes per RE term.required
re_structurestrRandom effects structure metadata.required
metadatadictAdditional metadata for Lambda construction.required
weights‘Array | None’Prior observation weights (n,) or None. For binomial: number of trials per observation.None
n_bootintNumber of bootstrap samples.999
seedint | NoneRandom seed for reproducibility.None
boot_typeLiteral[‘parametric’]Type of bootstrap. Only "parametric" is supported.‘parametric’
ci_typeLiteral[‘percentile’, ‘basic’, ‘bca’]Confidence interval type: “percentile”, “basic”, or “bca”.‘percentile’
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95
max_iterintMaximum PIRLS iterations per refit.25
tolfloatConvergence tolerance for PIRLS.1e-08
max_outer_iterintMaximum BOBYQA iterations for theta optimization.10000
nAGQintNumber 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
verboseboolIf True, show tqdm progress bar.False
n_jobsintNumber of parallel jobs. Default 1 (sequential). Use -1 for all cores.1

Returns:

TypeDescription
BootstrapResultBootstrapResult 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) -> BootstrapResult

Bootstrap 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:

NameTypeDescriptionDefault
X‘Array’Fixed effects design matrix, shape (n, p).required
Zcsc_matrix | ‘Array’Random effects design matrix, shape (n, q), scipy sparse CSC.required
y‘Array’Response vector, shape (n,).required
X_nameslist[str]Fixed effects coefficient names.required
theta‘Array’Variance parameters from fitted model.required
beta‘Array’Fixed effects coefficients from fitted model.required
sigmafloatResidual standard deviation from fitted model.required
n_groups_listlist[int]Number of groups per grouping factor.required
re_structurestrRandom effects structure type.required
metadatadict | NoneAdditional metadata for complex structures.None
lower_boundslist[float] | NoneLower bounds for theta optimization. If None, uses 0 for all.None
n_bootintNumber of bootstrap samples.999
seedint | NoneRandom seed for reproducibility.None
boot_typeLiteral[‘parametric’]Type of bootstrap. Only "parametric" is supported.‘parametric’
ci_typeLiteral[‘percentile’, ‘basic’, ‘bca’]Confidence interval type: “percentile”, “basic”, or “bca”.‘percentile’
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95
methodLiteral[‘REML’, ‘ML’]Estimation method for refitting: “REML” or “ML”.‘REML’
max_iterintMaximum optimizer iterations for each refit.10000
tolfloatConvergence tolerance for optimizer.1e-08
verboseboolPrint progress information.False
whichLiteral[‘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_nameslist[str] | NoneGrouping factor names (required if which=“ranef” or “all”).None
random_nameslist[str] | NoneRandom effect names (required if which=“ranef” or “all”).None
n_jobsintNumber 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:

TypeDescription
BootstrapResultBootstrapResult with observed stats, bootstrap samples, and CIs.
BootstrapResultFor which=“ranef”, param_names are variance component names
BootstrapResult(e.g., “Subject:Intercept_sd”, “Subject:corr_Intercept:Days”, “Residual_sd”).
BootstrapResultFor 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) -> Array

Simulate response from GLM fitted values.

Dispatches to family-specific simulation functions.

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random sampling.required
muArrayFitted values on response scale (n,).required
familyFamilyGLM family object.required
dispersionfloatDispersion parameter (phi). For Gaussian family, dispersion = sigma^2. For other families, typically fixed at 1.0.1.0
weightsArray | NonePrior weights (only used for binomial trials).None

Returns:

TypeDescription
ArraySimulated 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:

NameDescription
collect_boot_samplesStack bootstrap samples and compute valid-sample mask.
compute_bootstrap_ciCompute bootstrap confidence intervals using the specified method.
run_bootstrap_iterationsRun bootstrap iterations sequentially or in parallel via joblib.
warn_on_failuresEmit a RuntimeWarning if failure rate exceeds 10%.

Attributes:

NameTypeDescription
Array
Attributes
Array
Array = Any
Classes
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:

NameTypeDescriptionDefault
boot_samples_listlist[ndarray]List of per-iteration coefficient arrays.required
n_bootintTotal number of iterations attempted.required
contextstrLabel for the warning message (e.g., “Bootstrap”, “Permutation”).‘Bootstrap’

Returns:

TypeDescription
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:

NameTypeDescriptionDefault
coef_obsndarrayObserved coefficient estimates, shape (p,).required
boot_samples_validndarrayValid (non-NaN) bootstrap samples, shape (n_valid, p).required
ci_typeLiteral[‘percentile’, ‘basic’, ‘bca’]Type of CI: “percentile”, “basic”, or “bca”.required
levelfloatConfidence level (e.g. 0.95).required
jackknife_statsndarray | NoneJackknife leave-one-out estimates, shape (n, p). Required for ci_type="bca".None

Returns:

TypeDescription
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:

NameTypeDescriptionDefault
iteration_fnCallable..., [ndarray]Standalone function for a single bootstrap iteration. Must accept rng as first keyword argument plus all iter_kwargs.required
rngslist[RNG]Pre-split RNG objects, one per iteration.required
n_bootintNumber of bootstrap iterations.required
iter_kwargsdict[str, Any]Keyword arguments forwarded to iteration_fn (excluding rng).required
n_jobsintNumber of parallel jobs. 1 = sequential, -1 = all cores.1
verboseboolIf True, show tqdm progress bar.False
descstrDescription label for the progress bar.‘Bootstrap’

Returns:

TypeDescription
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') -> None

Emit a RuntimeWarning if failure rate exceeds 10%.

Parameters:

NameTypeDescriptionDefault
n_failedintNumber of iterations that produced NaN.required
n_totalintTotal number of iterations attempted.required
contextstrLabel for the warning message.‘Bootstrap’
core

Core resampling utilities.

Functions:

NameDescription
compute_bootstrap_ci_basicCompute basic (pivotal) bootstrap confidence intervals.
compute_bootstrap_ci_bcaBCa (bias-corrected and accelerated) bootstrap confidence intervals.
compute_bootstrap_ci_percentileCompute percentile bootstrap confidence intervals.
compute_pvaluesCompute permutation p-values.
generate_bootstrap_indicesGenerate n_boot bootstrap index arrays (with replacement).
generate_kfold_indicesGenerate k-fold cross-validation train/test index splits.
generate_loo_indicesGenerate leave-one-out cross-validation train/test index splits.
generate_permutation_indicesGenerate n_perm permutation index arrays.

Attributes:

NameTypeDescription
BCa_MIN_NBOOT
Attributes
BCa_MIN_NBOOT
BCa_MIN_NBOOT = 50
Classes
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:

NameTypeDescriptionDefault
observed‘ArrayLike’Observed statistic, shape [n_params] or scalar.required
boot_stats‘ArrayLike’Bootstrap statistics, shape [n_boot] or [n_boot, n_params].required
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95

Returns:

TypeDescription
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:

NameTypeDescriptionDefault
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
levelfloatConfidence level (default 0.95).0.95

Returns:

TypeDescription
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:

NameTypeDescriptionDefault
boot_stats‘ArrayLike’Bootstrap statistics, shape [n_boot] or [n_boot, n_params].required
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95

Returns:

TypeDescription
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.ndarray

Compute permutation p-values.

Uses formula: p = (1 + sum(|T_null| >= |T_obs|)) / (B + 1) where B is the number of null samples.

Parameters:

NameTypeDescriptionDefault
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
alternativeLiteral[‘two-sided’, ‘greater’, ‘less’]Type of test: “two-sided”, “greater”, or “less”.‘two-sided’

Returns:

TypeDescription
ndarrayP-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.ndarray

Generate n_boot bootstrap index arrays (with replacement).

Parameters:

NameTypeDescriptionDefault
rng‘RNG’RNG object (from rng).required
nintSize of each bootstrap sample.required
n_bootintNumber of bootstrap samples to generate.required

Returns:

TypeDescription
ndarrayShape [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:

NameTypeDescriptionDefault
rng‘RNG’RNG object (from rng). Used if shuffle=True.required
nintTotal number of observations.required
kintNumber of folds.required
shuffleboolWhether to shuffle indices before splitting.True

Returns:

TypeDescription
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)
# 10
generate_loo_indices
generate_loo_indices(n: int) -> list[tuple[np.ndarray, np.ndarray]]

Generate leave-one-out cross-validation train/test index splits.

Parameters:

NameTypeDescriptionDefault
nintTotal number of observations.required

Returns:

TypeDescription
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)
# 1
generate_permutation_indices
generate_permutation_indices(rng: 'RNG', n: int, n_perm: int) -> np.ndarray

Generate n_perm permutation index arrays.

Parameters:

NameTypeDescriptionDefault
rng‘RNG’RNG object (from rng).required
nintLength of each permutation.required
n_permintNumber of permutations to generate.required

Returns:

TypeDescription
ndarrayShape [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:

Functions:

NameDescription
compute_batched_glm_bootstrapCompute 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.ndarray

Compute 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:

NameTypeDescriptionDefault
boot_indicesndarrayBootstrap index array, shape (n_boot, n).required
XndarrayDesign matrix, shape (n, p).required
yndarrayResponse vector, shape (n,).required
familyFamilyFamily object with link, variance, and deviance functions.required
weightsndarray | NoneOptional prior observation weights, shape (n,). If None, equal weights (ones) are used.None
max_iterintMaximum IRLS iterations per bootstrap sample.25
tolfloatConvergence tolerance for relative deviance change.1e-08
chunk_sizeint | NoneNumber of bootstrap samples per chunk. If None, computed automatically from available memory.None

Returns:

TypeDescription
ndarrayCoefficient 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:

Functions:

NameDescription
glmer_bootstrapBootstrap inference for GLMER coefficients.
glmer_bootstrap_single_iterationExecute a single GLMER bootstrap iteration.
simulate_from_glmerSimulate 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) -> BootstrapResult

Bootstrap 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:

NameTypeDescriptionDefault
X‘Array’Fixed effects design matrix, shape (n, p).required
Zcsc_matrix | ‘Array’Random effects design matrix, sparse CSC or dense array (n, q).required
y‘Array’Response vector, shape (n,).required
X_nameslist[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
familyFamilyGLM family object (binomial or poisson).required
n_groups_listlist[int]List of group sizes per RE term.required
re_structurestrRandom effects structure metadata.required
metadatadictAdditional metadata for Lambda construction.required
weights‘Array | None’Prior observation weights (n,) or None. For binomial: number of trials per observation.None
n_bootintNumber of bootstrap samples.999
seedint | NoneRandom seed for reproducibility.None
boot_typeLiteral[‘parametric’]Type of bootstrap. Only "parametric" is supported.‘parametric’
ci_typeLiteral[‘percentile’, ‘basic’, ‘bca’]Confidence interval type: “percentile”, “basic”, or “bca”.‘percentile’
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95
max_iterintMaximum PIRLS iterations per refit.25
tolfloatConvergence tolerance for PIRLS.1e-08
max_outer_iterintMaximum BOBYQA iterations for theta optimization.10000
nAGQintNumber 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
verboseboolIf True, show tqdm progress bar.False
n_jobsintNumber of parallel jobs. Default 1 (sequential). Use -1 for all cores.1

Returns:

TypeDescription
BootstrapResultBootstrapResult 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.ndarray

Execute a single GLMER bootstrap iteration.

This is a standalone function (not a closure) to enable joblib parallelization.

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random number generation.required
X_npndarrayFixed effects design matrix as numpy array (n, p).required
Z_sparsecsc_matrixRandom effects design matrix as sparse CSC.required
beta_npndarrayFixed effects as numpy array (p,).required
theta_npndarrayVariance parameters as numpy array.required
familyFamilyGLM family object.required
n_groups_listlist[int]List of group sizes per RE term.required
re_structurestrRandom effects structure metadata.required
metadatadictAdditional metadata for Lambda construction.required
weights_npndarray | NonePrior weights as numpy array or None.required
boot_typestrOnly "parametric" is supported.required
max_iterintMaximum PIRLS iterations.required
tolfloatConvergence tolerance.required
max_outer_iterintMaximum BOBYQA iterations.required
nAGQintNumber of adaptive Gauss-Hermite quadrature points (0 or 1).required
pintNumber of fixed effects coefficients.required
lambda_templateobject | NonePre-built Lambda template for efficient updates.None

Returns:

TypeDescription
ndarrayBootstrap 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:

  1. Simulate random effects: b* ~ N(0, LL’)

  2. Compute linear predictor: eta* = X beta_hat + Z b*

  3. Compute mean: mu* = g^{-1}(eta*)

  4. Simulate response from family distribution

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random number generation.required
XndarrayFixed effects design matrix (n, p).required
Zcsc_matrixRandom effects design matrix (sparse CSC, n, q).required
betandarrayFixed effects coefficients (p,).required
thetandarrayVariance parameters (n_theta,).required
familyFamilyGLM family object.required
n_groups_listlist[int]List of group sizes per RE term.required
re_structurestrRandom effects structure metadata.required
metadatadictAdditional metadata for Lambda construction.required
weightsndarray | NonePrior observation weights (only for binomial trials).None

Returns:

TypeDescription
‘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:

NameDescription
build_lm_coefficient_operatorCreate reusable linear operator Y -> coefficients.
compute_batched_ols_bootstrapCompute bootstrap OLS coefficients using chunked vmap.

Attributes:

NameTypeDescription
Array
Attributes
Array
Array = Any
Functions
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:

NameTypeDescriptionDefault
XArrayDesign matrix, shape (n, p).required
methodLiteral[‘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:

TypeDescription
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.ndarray

Compute 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:

NameTypeDescriptionDefault
boot_indicesndarrayBootstrap index array, shape (n_boot, n).required
XndarrayDesign matrix, shape (n, p).required
yndarrayResponse vector, shape (n,).required
chunk_sizeint | NoneNumber of bootstrap samples per chunk. If None, computed automatically from available memory.None

Returns:

TypeDescription
ndarrayCoefficient 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:

Functions:

NameDescription
lmer_bootstrapBootstrap inference for lmer parameters.
lmer_bootstrap_single_iterationExecute a single LMER bootstrap iteration.
simulate_from_lmerSimulate 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) -> BootstrapResult

Bootstrap 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:

NameTypeDescriptionDefault
X‘Array’Fixed effects design matrix, shape (n, p).required
Zcsc_matrix | ‘Array’Random effects design matrix, shape (n, q), scipy sparse CSC.required
y‘Array’Response vector, shape (n,).required
X_nameslist[str]Fixed effects coefficient names.required
theta‘Array’Variance parameters from fitted model.required
beta‘Array’Fixed effects coefficients from fitted model.required
sigmafloatResidual standard deviation from fitted model.required
n_groups_listlist[int]Number of groups per grouping factor.required
re_structurestrRandom effects structure type.required
metadatadict | NoneAdditional metadata for complex structures.None
lower_boundslist[float] | NoneLower bounds for theta optimization. If None, uses 0 for all.None
n_bootintNumber of bootstrap samples.999
seedint | NoneRandom seed for reproducibility.None
boot_typeLiteral[‘parametric’]Type of bootstrap. Only "parametric" is supported.‘parametric’
ci_typeLiteral[‘percentile’, ‘basic’, ‘bca’]Confidence interval type: “percentile”, “basic”, or “bca”.‘percentile’
levelfloatConfidence level (e.g., 0.95 for 95% CI).0.95
methodLiteral[‘REML’, ‘ML’]Estimation method for refitting: “REML” or “ML”.‘REML’
max_iterintMaximum optimizer iterations for each refit.10000
tolfloatConvergence tolerance for optimizer.1e-08
verboseboolPrint progress information.False
whichLiteral[‘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_nameslist[str] | NoneGrouping factor names (required if which=“ranef” or “all”).None
random_nameslist[str] | NoneRandom effect names (required if which=“ranef” or “all”).None
n_jobsintNumber 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:

TypeDescription
BootstrapResultBootstrapResult with observed stats, bootstrap samples, and CIs.
BootstrapResultFor which=“ranef”, param_names are variance component names
BootstrapResult(e.g., “Subject:Intercept_sd”, “Subject:corr_Intercept:Days”, “Residual_sd”).
BootstrapResultFor 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.ndarray

Execute 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:

NameTypeDescriptionDefault
rngRNGRNG object for random number generation.required
X_npndarrayFixed effects design matrix.required
Z_sparsecsc_matrixRandom effects design matrix (sparse CSC).required
beta_npndarrayFixed effects coefficients.required
theta_npndarrayVariance parameters (starting values).required
sigmafloatResidual standard deviation.required
n_groups_listlist[int]Number of groups per factor.required
re_structurestrRandom effects structure type.required
metadatadict | NoneModel metadata.required
methodstrEstimation method (“REML” or “ML”).required
lower_boundslist[float]Lower bounds for theta optimization.required
upper_boundslist[float]Upper bounds for theta optimization.required
max_iterintMaximum optimizer iterations.required
lambda_templateobject | NonePre-built Lambda template (or None).required
X_arrndarrayX as array (pre-computed).required
XtXndarrayX’X matrix (pre-computed).required
nintNumber of observations.required
pintNumber of fixed effects.required
n_paramsintNumber of parameters to return.required
whichstrWhich parameters (“fixef”, “ranef”, or “all”).required
group_nameslist[str] | NoneNames of grouping factors.required
random_nameslist[str] | NoneNames of random effects.required

Returns:

TypeDescription
ndarrayArray 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.ndarray

Simulate response from fitted lmer model.

Generates y* = X beta + Z b* + eps* where:

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random number generation.required
XndarrayFixed effects design matrix, shape (n, p).required
Zcsc_matrixRandom effects design matrix, shape (n, q), scipy sparse CSC.required
betandarrayFixed effects coefficients, shape (p,).required
thetandarrayVariance parameters for Lambda.required
sigmafloatResidual standard deviation.required
n_groups_listlist[int]Number of groups per grouping factor.required
re_structurestrRandom effects structure type.required
metadatadict | NoneAdditional metadata for complex structures.None

Returns:

TypeDescription
ndarraySimulated response vector, shape (n,).
results

Result containers for resampling procedures.

Classes:

NameDescription
BootstrapResultResults 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) -> None

Results from a bootstrap procedure.

Attributes:

NameTypeDescription
observedArrayObserved statistics, shape [n_params].
boot_samplesArrayBootstrap samples, shape [n_boot, n_params].
ci_lowerArrayLower confidence bounds, shape [n_params].
ci_upperArrayUpper confidence bounds, shape [n_params].
param_nameslist[str]Names of parameters.
n_bootintNumber of bootstrap samples.
ci_typeLiteral[‘percentile’, ‘basic’, ‘bca’]Type of confidence interval (“percentile”, “basic”, or “bca”).
levelfloatConfidence level (e.g., 0.95).
term_typeslist[str] | NoneOptional list indicating “fixef” or “ranef” for each parameter. Only populated for mixed model bootstrap with which=“all”.
Attributes
boot_samples
boot_samples: Array
ci
ci: tuple[Array, Array]

Return confidence intervals as a tuple (lower, upper).

ci_lower
ci_lower: Array
ci_type
ci_type: Literal['percentile', 'basic', 'bca']
ci_upper
ci_upper: Array
level
level: float
n_boot
n_boot: int
observed
observed: Array
param_names
param_names: list[str]
se
se: Array

Bootstrap standard errors (std of boot_samples).

term_types
term_types: list[str] | None = None
simulate

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:

NameDescription
simulate_binomialSimulate binomial response from fitted probabilities.
simulate_gaussianSimulate Gaussian response from fitted values.
simulate_poissonSimulate Poisson response from fitted rates.
simulate_responseSimulate response from GLM fitted values.
Attributes
Classes
Functions
simulate_binomial
simulate_binomial(rng: RNG, mu: Array, weights: Array | None = None) -> Array

Simulate binomial response from fitted probabilities.

For standard Bernoulli trials (weights=1), samples 0/1. For binomial with n trials, samples proportion.

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random sampling.required
muArrayFitted probabilities (n,), must be in (0, 1).required
weightsArray | NoneNumber of trials per observation. If None, uses 1 (Bernoulli).None

Returns:

TypeDescription
ArraySimulated response (n,) as proportions in [0, 1].
simulate_gaussian
simulate_gaussian(rng: RNG, mu: Array, dispersion: float) -> Array

Simulate Gaussian response from fitted values.

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random sampling.required
muArrayFitted values (n,).required
dispersionfloatDispersion parameter (phi). For Gaussian family, phi = sigma^2.required

Returns:

TypeDescription
ArraySimulated response (n,).
simulate_poisson
simulate_poisson(rng: RNG, mu: Array) -> Array

Simulate Poisson response from fitted rates.

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random sampling.required
muArrayFitted rates (n,), must be positive.required

Returns:

TypeDescription
ArraySimulated response (n,) as counts.
simulate_response
simulate_response(rng: RNG, mu: Array, family: Family, dispersion: float = 1.0, weights: Array | None = None) -> Array

Simulate response from GLM fitted values.

Dispatches to family-specific simulation functions.

Parameters:

NameTypeDescriptionDefault
rngRNGRNG object for random sampling.required
muArrayFitted values on response scale (n,).required
familyFamilyGLM family object.required
dispersionfloatDispersion parameter (phi). For Gaussian family, dispersion = sigma^2. For other families, typically fixed at 1.0.1.0
weightsArray | NonePrior weights (only used for binomial trials).None

Returns:

TypeDescription
ArraySimulated response (n,).

resample_bundle

Shared helper for resampling DataBundle observations.

Functions:

NameDescription
build_resampled_bundleCreate a new DataBundle by selecting observations at given indices.

Classes

Functions

build_resampled_bundle
build_resampled_bundle(bundle: DataBundle, indices: np.ndarray) -> DataBundle

Create 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:

NameTypeDescriptionDefault
bundleDataBundleOriginal data bundle.required
indicesndarrayInteger array of observation indices (may contain repeats for bootstrap, or be a boolean mask for jackknife).required

Returns:

TypeDescription
DataBundleNew 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).
  1. Compute pwrss_0 at fitted theta (seeds cache)

  2. Compute pwrss_0 at fitted theta (seeds cache)

  3. Define deviance_theta(th) — sigma fixed from fit

  4. Compute theta-only Hessian via Richardson extrapolation

  5. Compute dpwrss/dtheta via central difference (2m evals)

  6. Assemble full (m+1) x (m+1) Hessian with analytical sigma block

  7. vcov_varpar = 2 * H_full^{-1}

  8. Compute theta-only Jacobian via Richardson extrapolation

  9. Append analytical sigma column: (2/sigma) * fit.vcov

  10. Call satterthwaite_df_for_contrasts(L, vcov, vcov_varpar, jac_list)

Functions:

NameDescription
compute_satterthwaite_emm_dfCompute 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.ndarray

Compute 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:

NameTypeDescriptionDefault
bundleDataBundleData bundle with X, Z, y, and re_metadata.required
fitFitStateFit state with theta, vcov, and sigma.required
specModelSpecModel specification (method, family, etc.).required
LndarrayContrast matrix, shape (n_contrasts, n_coef).required

Returns:

TypeDescription
ndarrayArray 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:

NameDescription
compute_simulation_inferenceCompute 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:

NameTypeDescriptionDefault
simulations‘pl.DataFrame’DataFrame of simulation results. Post-fit simulations have columns named sim_1, sim_2, etc.required
conf_levelfloatConfidence level for interval quantiles.required

Returns:

TypeDescription
‘SimulationInferenceState’SimulationInferenceState with summary statistics.