clophfit.fitting.bayes#
Bayesian (PyMC) fitting utilities and pipelines.
Functions#
|
Process the trace to extract parameter estimates and update datasets. |
|
Analyze multi-label titration datasets using PyMC (single model). |
|
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]
- clophfit.fitting.bayes.fit_binding_pymc(ds_or_fr, n_sd=10.0, n_xerr=1.0, n_samples=2000, nuts_sampler='default', *, n_tune=None, target_accept=None, max_treedepth=None, noise_model=None, robust=False, floor_mode=None, gain_mode=None, alpha_mode=None, learn_ye_mags=False, min_x_step=0.2)#
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".n_tune (int | None) – Number of tuning steps. If
None(default), usen_samples // 2.target_accept (float | None) – NUTS target acceptance probability. If
None(default), 0.95 is used when n_xerr > 0 and 0.9 otherwise.max_treedepth (int | None) – Maximum tree depth for NUTS sampler. If
None(default), PyMC’s default is used.noise_model (PlateNoiseModel | None) – Noise model specification.
None(default) uses a simple per-labelye_mag_{lbl}HalfNormal to scale the existingy_err. Pass aPlateNoiseModelto 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.floor_mode (NoiseParamMode | None) – How to treat the floor parameter.
None(default) resolves to"centered"for pre-fitFitResultinput and"free"for rawDatasetinput.gain_mode (NoiseParamMode | None) – How to treat the gain parameter.
Nonefollows the same input-based rule as floor_mode.alpha_mode (NoiseParamMode | None) – How to treat the alpha (rel_error) parameter.
Nonefollows the same input-based rule as floor_mode.learn_ye_mags (bool) – If
True, learn per-label scaling factors (ye_mag_{lbl}) even when a full noise_model is provided.min_x_step (float) – Minimum inferred change in
xbetween consecutive titration steps when latent-x modeling is enabled.
- 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, target_accept=None, max_treedepth=None, x_error_model='deterministic', acid_drop_between_sigma=0.005, ctr_free_k=False, sample_ppc=False, robust=False, floor_mode=None, gain_mode=None, alpha_mode=None, learn_ye_mags=False, shared_ye_mags=False, min_x_step=0.2)#
Multi-well PyMC with shared K per control group and per-label noise.
- Parameters:
results (Mapping[str, Dataset | FitResult[MiniT]]) – Per-well datasets or initial fit results. Raw datasets are first fitted with
fit_binding_glob()to seed the Bayesian model.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-labelye_mag_{lbl}HalfNormal to scale existingy_err. Pass aPlateNoiseModelto infer floor, gain, and alpha from the full heteroscedastic noise model.shared_alpha (bool) – If
True(default), use a singlerel_errorvariable for all labels (comprehensive model). IfFalse, use per-labelrel_error_{lbl}(proportional model). Only used when noise_model is provided.shared_gain (bool) – If
True, use a singlegainvariable for all labels. IfFalse(default), use per-labelgain_{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.
target_accept (float | None) – NUTS target acceptance probability. If
None(default), 0.95 is used when n_xerr > 0 and 0.9 otherwise.max_treedepth (int | None) – Maximum tree depth for NUTS sampler. If
None(default), PyMC’s default is used.x_error_model (Literal["deterministic", "per_well", "hierarchical_per_well"]) – Model for x-error propagation.
"deterministic"uses one sharedx_trueacross all wells."per_well"gives each well its own independentx_step(sharedx_start, per-well cumulative additions constrained bymin_x_step)."hierarchical_per_well"uses an acid-addition formulation: sharedacid_drop_globalper step (uncertainty from quadrature sum of adjacentx_errc), per-wellacid_drop_welldeviating at fixedacid_drop_between_sigmascale (not inferred).acid_drop_between_sigma (float) – Fixed between-well scale for the
acid_dropvariation used by"hierarchical_per_well"(not inferred — set to the experimental tolerance). 0.005 (default) is very tight — suitable when all wells receive identical 2 uL additions. Increase to 0.01–0.02 for larger pipetting/buffer differences.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.
floor_mode (NoiseParamMode | None) – How to treat the floor parameter.
None(default) resolves to"centered"when every input is already aFitResultand to"free"when any rawDatasetis supplied.gain_mode (NoiseParamMode | None) – How to treat the gain parameter.
Nonefollows the same input-based rule as floor_mode.alpha_mode (NoiseParamMode | None) – How to treat the alpha (rel_error) parameter.
Nonefollows the same input-based rule as floor_mode.learn_ye_mags (bool) – If
True, learn per-label scaling factors (ye_mag_{lbl}) even when a full noise_model is provided.shared_ye_mags (bool) – If
True, use a single sharedye_magvariable for all labels instead of per-labelye_mag_{lbl}. Only used when learn_ye_mags isTrue. DefaultFalse.min_x_step (float) – Minimum inferred change in
xbetween consecutive titration steps when latent-x modeling is enabled.
- Returns:
Shared PyMC trace together with reconstructed per-well fit results.
- Return type:
- Raises:
ValueError – If no valid dataset is found in results.