clophfit.fitting.bayes ====================== .. py:module:: clophfit.fitting.bayes .. autoapi-nested-parse:: Bayesian (PyMC) fitting utilities and pipelines. Functions --------- .. autoapisummary:: clophfit.fitting.bayes.create_x_true clophfit.fitting.bayes.create_parameter_priors clophfit.fitting.bayes.rename_keys clophfit.fitting.bayes.process_trace clophfit.fitting.bayes.extract_fit clophfit.fitting.bayes.x_true_from_trace_df clophfit.fitting.bayes.fit_binding_pymc clophfit.fitting.bayes.fit_binding_pymc2 clophfit.fitting.bayes.fit_binding_pymc_compare clophfit.fitting.bayes.closest_point_on_curve clophfit.fitting.bayes.fit_binding_pymc_odr clophfit.fitting.bayes.weighted_stats clophfit.fitting.bayes.fit_binding_pymc_multi clophfit.fitting.bayes.fit_binding_pymc_multi2 clophfit.fitting.bayes.plot_ppc_well clophfit.fitting.bayes.compare_posteriors clophfit.fitting.bayes.fit_pymc_hierarchical Module Contents --------------- .. py:function:: create_x_true(xc, x_errc, n_xerr, lower_nsd = 2.5) Create latent variables for x-values with uncertainty. Returns a PyMC Deterministic variable when in a Model context with uncertainty, or a numpy array when there's no uncertainty or no active Model. .. py:function:: create_parameter_priors(params, n_sd, key = '', ctr_name = '', default_sigma = 0.001) Create PyMC parameter prior distributions from lmfit Parameters. :param params: lmfit Parameters to convert to PyMC priors. :type params: Parameters :param n_sd: Scaling factor for parameter standard errors. :type n_sd: float :param key: Optional suffix to add to parameter names. :type key: str :param ctr_name: If specified, skip creating K prior (shared from control group). :type ctr_name: str :param default_sigma: Default sigma when stderr is not available (default: 1e-3). :type default_sigma: float :returns: Dictionary of PyMC distribution objects. :rtype: dict[str, pm.Distribution] .. py:function:: rename_keys(data) Rename dictionary keys coming from multi-trace into base names. .. py:function:: process_trace(trace, p_names, ds, n_xerr) Process the trace to extract parameter estimates and update datasets. :param trace: The posterior samples from PyMC sampling. :type trace: az.InferenceData :param p_names: Parameter names. :type p_names: typing.KeysView[str] :param ds: The dataset containing titration data. :type ds: Dataset :param n_xerr: Scaling factor for `x_errc`. :type n_xerr: float :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[az.InferenceData] :raises TypeError: If az.summary does not return a DataFrame. .. py:function:: extract_fit(key, ctr, trace_df, ds) Compute individual dataset fit from a multi-well trace summary. .. py:function:: x_true_from_trace_df(trace_df) Extract x_true from an ArviZ summary DataFrame. .. py:function:: fit_binding_pymc(ds_or_fr, n_sd = 10.0, n_xerr = 1.0, ye_scaling = 1.0, n_samples = 2000) 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 ye_scaling: Scaling factor for y-error magnitude prior. :type ye_scaling: float :param n_samples: Number of MCMC samples. :type n_samples: int :returns: Bayesian fitting results. :rtype: FitResult[az.InferenceData] .. py:function:: fit_binding_pymc2(ds_or_fr, n_sd = 10.0, n_xerr = 1.0, n_samples = 2000) Analyze multi-label titration datasets using PyMC with separate ye_mag per label. :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 :returns: Bayesian fitting results with per-label error scaling. :rtype: FitResult[az.InferenceData] .. py:function:: fit_binding_pymc_compare(fr, buffer_sd, *, learn_separate_y_mag = False, n_sd = 10.0, n_xerr = 1.0, n_samples = 2000) Fits a Bayesian binding model with two different noise models for comparison. :param fr: The fit result from a previous run, providing initial parameters and dataset. :type fr: FitResult[MiniT] :param buffer_sd: bg_err :type buffer_sd: dict[str, float] :param learn_separate_y_mag: If True, learns a unique noise scaling factor for each dataset label. If False, learns a single scaling factor for all pre-weighted data. :type learn_separate_y_mag: bool :param n_sd: Prior width for parameters in create_parameter_priors. :type n_sd: float :param n_xerr: Scaling factor for x_errc in create_x_true. :type n_xerr: float :param n_samples: Number of MCMC samples to draw. :type n_samples: int :returns: The posterior samples from PyMC for the specified noise model. :rtype: az.InferenceData .. py:function:: closest_point_on_curve(f, x_obs, y_obs) Find the closest point on the model curve. .. py:function:: fit_binding_pymc_odr(fr, n_sd = 10.0, xe_scaling = 1.0, ye_scaling = 10.0, n_samples = 2000) Bayesian ODR-like modeling of x and y errors. .. py:function:: weighted_stats(values, stderr) Weighted mean and stderr for control priors. .. py:function:: fit_binding_pymc_multi(results, scheme, n_sd = 5.0, n_xerr = 1.0, ye_scaling = 1.0, n_samples = 2000) Multi-well PyMC with shared K per control group and per-label noise. .. py:function:: fit_binding_pymc_multi2(results, scheme, bg_err, n_sd = 5.0, n_xerr = 1.0, n_samples = 2000) Multi-well PyMC with heteroscedastic noise combining buffer and signal. .. py:function:: plot_ppc_well(trace, key, labels = None, figsize = (8, 4)) Draw posterior predictive samples for a particular well (and all its labels). The returned figure can be displayed with matplotlib. :param trace: Trace produced by ``fit_binding_pymc_advanced``. :type trace: az.InferenceData :param key: Well identifier (e.g. 'A01'). :type key: str :param labels: Names of the bands to show. If *None* the function will automatically look for all variables starting with ``'y_'`` that contain this key. :type labels: list[str] | None :param figsize: size? :type figsize: tuple[float, float] :returns: Plot :rtype: figure.Figure .. py:function:: compare_posteriors(trace, results) Print posterior mean ± 95 % C.I. For the K parameter for each well, and juxtapose it with the deterministic K (from fit_binding_pymc). :param trace: Output of ``fit_binding_pymc_advanced``. :type trace: az.InferenceData :param results: Deterministic fits produced by the old pipeline. :type results: dict[str, FitResult[MiniT]] .. py:function:: fit_pymc_hierarchical(results, scheme, bg_err, n_sd = 5.0, n_xerr = 1.0, n_samples = 2000) Analyze multiple titrations with a hierarchical Bayesian model. This model shares information about the dissociation constant 'K' among wells belonging to the same control group, leading to more robust estimates. :param results: A dictionary mapping well IDs to their initial `FitResult` from a prior `fit_lm` run. :type results: dict[str, FitResult[MiniT]] :param scheme: The plate scheme defining control groups. :type scheme: PlateScheme :param bg_err: Background error for each signal band. :type bg_err: dict[int, ArrayF] :param n_sd: The number of standard deviations for the prior width of S0/S1. :type n_sd: float :param n_xerr: Scaling factor for x-value uncertainties. :type n_xerr: float :param n_samples: Number of MCMC samples. :type n_samples: int :returns: The PyMC trace containing the posterior distributions. :rtype: az.InferenceData :raises ValueError: With invalid dataset.