clophfit.fitting.bayes ====================== .. py:module:: clophfit.fitting.bayes .. autoapi-nested-parse:: Bayesian (PyMC) fitting utilities and pipelines. Functions --------- .. autoapisummary:: clophfit.fitting.bayes.process_trace clophfit.fitting.bayes.fit_binding_pymc clophfit.fitting.bayes.fit_binding_pymc_multi Module Contents --------------- .. py:function:: process_trace(trace, p_names, ds) Process the trace to extract parameter estimates and update datasets. :param trace: The posterior samples from PyMC sampling. :type trace: xr.DataTree :param p_names: Parameter names. :type p_names: typing.KeysView[str] :param ds: The dataset containing titration data. :type ds: Dataset :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. :rtype: FitResult[xr.DataTree] :raises TypeError: If az.summary does not return a DataFrame. .. py:function:: 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). :param ds_or_fr: Either a Dataset (will run initial LS fit) or a FitResult with initial params. :type ds_or_fr: Dataset | FitResult[MiniT] :param n_sd: Number of standard deviations for parameter priors. :type n_sd: float :param n_xerr: Scaling factor for x-error. :type n_xerr: float :param n_samples: Number of MCMC samples. :type n_samples: int :param nuts_sampler: NUTS sampler backend: ``"default"`` (PyMC C/pytensor), ``"blackjax"``, ``"numpyro"``, or ``"nutpie"``. :type nuts_sampler: str :param noise_model: 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. :type noise_model: PlateNoiseModel | None :param robust: If ``True``, use StudentT likelihood (nu=3) for robust regression. :type robust: bool :returns: Bayesian fitting results. :rtype: FitResult[xr.DataTree] .. py:function:: 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. :param results: Per-well initial fit results. :type results: dict[str, FitResult[MiniT]] :param scheme: Plate scheme defining control groups for shared-K priors. :type scheme: PlateScheme :param n_sd: Prior width multiplier for per-well S0/S1 parameters. :type n_sd: float :param n_xerr: Scaling factor applied to x-value uncertainties. :type n_xerr: float :param n_samples: Number of MCMC posterior samples per chain. :type n_samples: int :param nuts_sampler: NUTS sampler backend (``"default"``, ``"blackjax"``, ``"numpyro"``, ``"nutpie"``). :type nuts_sampler: str :param noise_model: 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. :type noise_model: PlateNoiseModel | None :param shared_alpha: 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. :type shared_alpha: bool :param shared_gain: 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. :type shared_gain: bool :param n_tune: Number of tuning steps for MCMC. If None, defaults to n_samples // 2. :type n_tune: int | None :param x_error_model: Model for x-error propagation (default: "deterministic"). :type x_error_model: Literal["deterministic", "random_walk"] :param sigma_pip_prior: Prior for random_walk sigma pipette error parameter. :type sigma_pip_prior: float :param ctr_free_k: 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. :type ctr_free_k: bool :param sample_ppc: If True, generates posterior predictive samples and adds them to the returned InferenceData object. Needed for `plot_ppc_well`. :type sample_ppc: bool :param robust: If True, use StudentT likelihood (nu=3) for robust regression instead of Normal. :type robust: bool :returns: The PyMC posterior trace. :rtype: xr.DataTree :raises ValueError: If no valid dataset is found in results.