clophfit.fitting.bayes#

Bayesian (PyMC) fitting utilities and pipelines.

Functions#

process_trace(trace, p_names, ds)

Process the trace to extract parameter estimates and update datasets.

fit_binding_pymc(ds_or_fr[, n_sd, n_xerr, n_samples, ...])

Analyze multi-label titration datasets using PyMC (single model).

fit_binding_pymc_multi(results, scheme[, n_sd, ...])

Multi-well PyMC with shared K per control group and per-label noise.

Module Contents#

clophfit.fitting.bayes.process_trace(trace, p_names, ds)#

Process the trace to extract parameter estimates and update datasets.

Parameters:
  • trace (xr.DataTree) – The posterior samples from PyMC sampling.

  • p_names (KeysView[str]) – Parameter names.

  • ds (Dataset) – The dataset containing titration data.

Returns:

The updated fit result with extracted parameter values and datasets. Residuals are WEIGHTED (weight * (obs - pred)) where weight = 1/y_err, computed using posterior mean parameter estimates.

Return type:

FitResult[xr.DataTree]

Raises:

TypeError – If az.summary does not return a DataFrame.

clophfit.fitting.bayes.fit_binding_pymc(ds_or_fr, n_sd=10.0, n_xerr=1.0, n_samples=2000, nuts_sampler='default', *, noise_model=None, robust=False)#

Analyze multi-label titration datasets using PyMC (single model).

Parameters:
  • ds_or_fr (Dataset | FitResult[MiniT]) – Either a Dataset (will run initial LS fit) or a FitResult with initial params.

  • n_sd (float) – Number of standard deviations for parameter priors.

  • n_xerr (float) – Scaling factor for x-error.

  • n_samples (int) – Number of MCMC samples.

  • nuts_sampler (str) – NUTS sampler backend: "default" (PyMC C/pytensor), "blackjax", "numpyro", or "nutpie".

  • noise_model (PlateNoiseModel | None) – Noise model specification. None (default) uses a simple shared ye_mag HalfNormal to scale the existing y_err. Pass a PlateNoiseModel to infer per-label floor, gain, and alpha from the full heteroscedastic noise model.

  • robust (bool) – If True, use StudentT likelihood (nu=3) for robust regression.

Returns:

Bayesian fitting results.

Return type:

FitResult[xr.DataTree]

clophfit.fitting.bayes.fit_binding_pymc_multi(results, scheme, n_sd=5.0, n_xerr=1.0, n_samples=2000, nuts_sampler='default', *, noise_model=None, shared_alpha=True, shared_gain=False, n_tune=None, x_error_model='deterministic', sigma_pip_prior=0.02, ctr_free_k=False, sample_ppc=False, robust=False)#

Multi-well PyMC with shared K per control group and per-label noise.

Parameters:
  • results (dict[str, FitResult[MiniT]]) – Per-well initial fit results.

  • scheme (PlateScheme) – Plate scheme defining control groups for shared-K priors.

  • n_sd (float) – Prior width multiplier for per-well S0/S1 parameters.

  • n_xerr (float) – Scaling factor applied to x-value uncertainties.

  • n_samples (int) – Number of MCMC posterior samples per chain.

  • nuts_sampler (str) – NUTS sampler backend ("default", "blackjax", "numpyro", "nutpie").

  • noise_model (PlateNoiseModel | None) – Noise model specification. None (default) uses per-label ye_mag_{lbl} HalfNormal to scale existing y_err. Pass a PlateNoiseModel to infer floor, gain, and alpha from the full heteroscedastic noise model.

  • shared_alpha (bool) – If True (default), use a single rel_error variable for all labels (comprehensive model). If False, use per-label rel_error_{lbl} (proportional model). Only used when noise_model is provided.

  • shared_gain (bool) – If True, use a single gain variable for all labels. If False (default), use per-label gain_{lbl}. Only used when noise_model is provided and gain terms are present.

  • n_tune (int | None) – Number of tuning steps for MCMC. If None, defaults to n_samples // 2.

  • x_error_model (Literal["deterministic", "random_walk"]) – Model for x-error propagation (default: “deterministic”).

  • sigma_pip_prior (float) – Prior for random_walk sigma pipette error parameter.

  • ctr_free_k (bool) – If True, each CTR replicate well gets its own independent flat K prior Normal(group_mean, 0.2) — identical to UNK well treatment, no hierarchical shrinkage. The spread of K posteriors across replicates then quantifies between-replicate accuracy. If False (default), all replicates of the same CTR share a single K.

  • sample_ppc (bool) – If True, generates posterior predictive samples and adds them to the returned InferenceData object. Needed for plot_ppc_well.

  • robust (bool) – If True, use StudentT likelihood (nu=3) for robust regression instead of Normal.

Returns:

The PyMC posterior trace.

Return type:

xr.DataTree

Raises:

ValueError – If no valid dataset is found in results.