clophfit.fitting.bayes_config#
Configuration objects for the PyMC binding-fit entry points.
These frozen dataclasses group the many keyword parameters of
clophfit.fitting.bayes.fit_binding_pymc() into cohesive, self-documenting
bundles. They live in a dedicated module (importing only from
clophfit.fitting.data_structures) so clophfit.fitting.bayes can
re-export them without a circular import.
Classes#
NUTS sampling controls forwarded to |
|
Robust-likelihood configuration. |
|
Prior-initialization strategy for the binding parameters. |
|
An informed prior for the latent pH axis, from a previous plate fit. |
|
Observation-noise configuration. |
Module Contents#
- class clophfit.fitting.bayes_config.SamplerConfig#
NUTS sampling controls forwarded to
pm.sample.- Parameters:
n_samples (int) – Number of posterior draws per chain.
nuts_sampler (str) – NUTS backend. Defaults to
"nutpie"— its warmup/mass-matrix adaptation is robust on the multi-well latent-x geometry."pymc"(built-in NUTS, Numba backend) is fine and marginally faster on simple deterministic-x fits."default"lets PyMC auto-select whatever is installed;"numpyro"/"blackjax"(JAX/GPU) request those explicitly but are unusable here (numpyro lacks anerfcximplementation; blackjax is incompatible with PyMC 6.1).n_tune (int | None) – Number of tuning draws.
Noneusesn_samples // 2.target_accept (float | None) – Target acceptance probability.
Noneselects a latent-x-aware default.max_treedepth (int | None) – Maximum NUTS tree depth.
Noneuses the backend default.random_seed (int | None) – Seed forwarded to
pm.samplefor reproducible draws.None(the default) leaves sampling nondeterministic.chains (int | None) – Number of MCMC chains.
Noneuses the PyMC default (4).cores (int | None) – Number of CPU cores for parallel chains.
Noneuses the PyMC default (min of chains and available cores). Set to 1 on a laptop to avoid multiprocessing overhead, or higher on a many-core server.chain_method (ChainMethod) – Chain execution strategy for the JAX backends (
"blackjax","numpyro")."auto"selects"vectorized"so all chains run on a single GPU (jax.vmap);"parallel"maps chains across devices (jax.pmap). Ignored by CPU backends.compute_log_likelihood (bool) – Compute the per-observation
log_likelihoodgroup after sampling. Disabled by default because it adds a full post-sampling pass and is only needed for out-of-sample model comparison (az.loo/az.compare). Enable it when a downstream comparison requires it.backend (str | None) – Compilation backend forwarded to
pm.sample(backend=...), e.g."jax"to JIT-compile the model’s logp/dlogp through JAX (GPU) or"numba".Noneuses the sampler’s default. Cannot be combined withcompile_kwargs={"mode": ...}.compile_kwargs (Mapping[str, Any] | None) – Extra keyword arguments forwarded to the functions compiled by the step methods (
pm.sample(compile_kwargs=...)), e.g. a custom{"mode": ...}.Noneuses the default. Note: use backend to select JAX/Numba —"backend"is not a validcompile_kwargskey.
- class clophfit.fitting.bayes_config.RobustConfig#
Robust-likelihood configuration.
- Parameters:
enabled (bool) – Use a robust likelihood instead of a plain Normal.
likelihood (RobustLikelihood) –
"student_t"heavy-tailed likelihood or"mixture"Normal/outlier contamination mixture.nu (float | None) – Student-t degrees of freedom. Positive values are fixed;
Noneinfersstudent_t_nuwith support above 2.contamination_frac_prior (ContaminationFracPrior) – Prior mean for per-label outlier fractions when
likelihood="mixture". A mapping supplies label-specific means. Each value must be between 0.001 and 0.5.
- class clophfit.fitting.bayes_config.InitConfig#
Prior-initialization strategy for the binding parameters.
- Parameters:
strategy (InitStrategy) –
"lmfit"fits a rawDatasetwith LMFit first and centers PyMC priors on that result."data_priors"skips LMFit and derives weak priors directly from the observed titration endpoints and midpoint.edge_points (int) – Number of active points averaged at each titration edge to initialize
S0/S1whenstrategy="data_priors".signal_sigma_scale (float) – Prior sigma for
S0/S1as a fraction of each label’s observed signal range whenstrategy="data_priors".k_prior (DataKPrior) – K prior family for
strategy="data_priors".k_bounds (tuple[float, float] | None) – Lower and upper K bounds for data-derived priors.
Noneresolves to(4.5, 9.0)for pH datasets or(1e-6, 1e6)otherwise.k_sigma (float) – Truncated-Normal K prior sigma for data-derived priors.
- class clophfit.fitting.bayes_config.XPrior#
An informed prior for the latent pH axis, from a previous plate fit.
create_x_truenormally derives the pipetting random walk from the measuredxand its per-point SD. A two-stage fit instead estimates the plate’s pH axis once with all wells pooled, then hands that estimate to per-well fits so each well refines a shared axis rather than rediscovering it alone.Only plate-level quantities belong here. A well’s own deviation must not be fed back to it: stage one inferred that deviation from that well’s fluorescence, so reusing it as a prior would put the same measurements into both prior and likelihood and shrink the resulting K interval below what the evidence supports. The shared anchor is diluted across every well, so its contamination by any single well is of order
1/n_wells.- Parameters:
x_start_mu (float) – Prior mean for the titration’s starting x.
x_start_sigma (float) – Prior SD for the starting x. Must be positive.
step_mu (ArrayF) – Prior mean per addition step, length
len(x) - 1.step_sigma (ArrayF) – Prior SD per addition step, same length as step_mu. Must be positive.
- class clophfit.fitting.bayes_config.NoiseConfig#
Observation-noise configuration.
Prefer the
ye_mag()andstructured()factories over the raw constructor.NoiseConfig()is equivalent toye_mag().Two mutually exclusive noise families are supported, selected by kind:
"ye_mag"scales each label’s suppliedy_errby a learned multiplier (sigma = ye_mag * y_err)."structured"builds a floor-plus-Poisson-plus-proportional noise model. When noise_model isNonethe model is synthesized from the data using the floor/gain/alpha scale hints, so the*_modeselectors work without hand-building aPlateNoiseModel.
Notes
A
*_modeofNoneresolves at fit time to"centered"for pre-fitFitResultinput and"free"for rawDatasetinput.- classmethod ye_mag(*, shared=False, prior='lognormal', mu=0.0, sigma=1.5)#
Scale supplied
y_errby a learnedye_magmultiplier.- Parameters:
shared (bool)
prior (Literal['halfnormal', 'lognormal'])
mu (float | collections.abc.Mapping[str, float])
sigma (float | collections.abc.Mapping[str, float])
- Return type:
- classmethod structured(*, noise_model=None, floor_mode=None, gain_mode=None, alpha_mode=None, shared_alpha=False, shared_gain=False, shared_floor=False, floor=None, gain=0.0, alpha=0.02, learn_ye_mags=False, shared_ye_mags=False)#
Floor-plus-Poisson-plus-proportional noise model.
When noise_model is
Nonethe model is synthesized from the data: each label getssigma_floorfrom floor (fallback: the label’sy_errscale), plus gain and alpha. Settinggain_mode="free"oralpha_mode="free"activates those terms.The alpha hint is the prior mean, not a hard value: it is the
TruncatedNormalcentre in"centered"mode, and in"free"mode theHalfNormalsigma is scaled so the mean matches the hint. It defaults to0.02(a weak 2% prior); passalpha=0for the tightest around-zero prior (the width is floored at1e-3; in"free"mode that gives a sigma of1e-3 * sqrt(pi/2)~=1.253e-3and a mean of1e-3, while in"centered"mode the term becomesHalfNormal(sigma=1e-3)whose mean is~7.98e-4), a larger value to widen it, oralpha_mode="fixed"to pin/disable the term.- Parameters:
noise_model (clophfit.fitting.data_structures.PlateNoiseModel | None)
floor_mode (NoiseParamMode | None)
gain_mode (NoiseParamMode | None)
alpha_mode (NoiseParamMode | None)
shared_alpha (bool)
shared_gain (bool)
shared_floor (bool)
floor (float | collections.abc.Mapping[str, float] | None)
gain (float | collections.abc.Mapping[str, float])
alpha (float | collections.abc.Mapping[str, float])
learn_ye_mags (bool)
shared_ye_mags (bool)
- Return type: