pyuncertainnumber.calibration.utils_bayesian ============================================ .. py:module:: pyuncertainnumber.calibration.utils_bayesian Classes ------- .. autoapisummary:: pyuncertainnumber.calibration.utils_bayesian.Prior pyuncertainnumber.calibration.utils_bayesian.PriorUniform pyuncertainnumber.calibration.utils_bayesian.JointBivariatePDF pyuncertainnumber.calibration.utils_bayesian.BetaGaussCopulaPDF pyuncertainnumber.calibration.utils_bayesian.NormalGaussCopulaPDF pyuncertainnumber.calibration.utils_bayesian.BetaMixtureGaussCopulaPDF Functions --------- .. autoapisummary:: pyuncertainnumber.calibration.utils_bayesian.initial_population pyuncertainnumber.calibration.utils_bayesian.log_prior pyuncertainnumber.calibration.utils_bayesian.log_prior_batch pyuncertainnumber.calibration.utils_bayesian.compute_beta_update_evidence pyuncertainnumber.calibration.utils_bayesian.gaussian_proposals pyuncertainnumber.calibration.utils_bayesian.compute_beta_update_evidence pyuncertainnumber.calibration.utils_bayesian.get_log_prior_samples pyuncertainnumber.calibration.utils_bayesian.initial_population pyuncertainnumber.calibration.utils_bayesian.prepare_all_pars pyuncertainnumber.calibration.utils_bayesian.prepare_prior_beta_model_NASA pyuncertainnumber.calibration.utils_bayesian.propose pyuncertainnumber.calibration.utils_bayesian.MCMC_MH Module Contents --------------- .. py:class:: Prior Abstract prior interface (minimal). .. py:method:: generate_rns(n: int) -> numpy.ndarray :abstractmethod: Return n samples ~ prior (shape: (n, d_i) or (n,)). .. py:method:: log_pdf_eval(x: numpy.ndarray) -> float :abstractmethod: Return log p(x) for a single vector x. .. py:class:: PriorUniform(lb: numpy.ndarray, ub: numpy.ndarray, name: str = 'uniform') Bases: :py:obj:`Prior` Axis-aligned uniform prior over [lb, ub] in R^d (vector bounds). .. py:attribute:: lb .. py:attribute:: ub .. py:attribute:: d .. py:attribute:: name :value: 'uniform' .. py:attribute:: _logZ .. py:method:: generate_rns(n: int) -> numpy.ndarray Vectorized sampling over the hyper-rectangle. .. py:method:: log_pdf_eval(x: numpy.ndarray) -> float log 1/Vol if x within bounds; -inf otherwise. .. py:function:: initial_population(n: int, priors: List[Prior]) -> numpy.ndarray .. py:function:: log_prior(x: numpy.ndarray, priors: List[Prior]) -> float .. py:function:: log_prior_batch(samples: numpy.ndarray, priors: List[Prior]) -> numpy.ndarray .. py:function:: compute_beta_update_evidence(beta: float, log_lik: numpy.ndarray, log_evidence: float, prev_ESS: float, target_frac: float = 0.95, min_particles: int = 50, max_iter: int = 30) -> tuple .. py:function:: gaussian_proposals(current: numpy.ndarray, cov: numpy.ndarray, n: int) -> numpy.ndarray .. py:class:: JointBivariatePDF(params) Base class for 2D aleatoric samplers with copulas. .. py:attribute:: eps :value: 1e-09 .. py:attribute:: params .. py:method:: sample(ns: int = 1000) :abstractmethod: .. py:class:: BetaGaussCopulaPDF(params, s=20) Bases: :py:obj:`JointBivariatePDF` Aleatoric sampler: (xa1, xa2) ~ Beta marginals + Gaussian copula. Params: [a1, a2, b1, b2, rho] with values in [0, 1]. Scaled internally via factor `s` (default: 20). .. py:attribute:: rho .. py:attribute:: cov .. py:method:: sample(ns: int = 1000) .. py:class:: NormalGaussCopulaPDF(params, s_mu=2, s_sigma=2) Bases: :py:obj:`JointBivariatePDF` Aleatoric sampler: (xa1, xa2) ~ Truncated Normal marginals .. py:attribute:: rho .. py:attribute:: cov .. py:method:: sample(ns: int = 1000) .. py:class:: BetaMixtureGaussCopulaPDF(params, n_mix: int = 2, s: int = 20, weights=None) Bases: :py:obj:`JointBivariatePDF` Mixture of Beta marginals with Gaussian copula correlation. Each component uses (a1, a2), (b1, b2) marginals + rho. Params: flat vector of length n_param * n_mix with values in [0, 1]; scaled internally. .. py:attribute:: n_mix :value: 2 .. py:attribute:: s :value: 20 .. py:attribute:: eps :value: 1e-06 .. py:attribute:: components :value: [] .. py:method:: sample(ns: int = 1000) .. py:function:: compute_beta_update_evidence(beta, log_likelihoods, log_evidence, prev_ESS) Computes beta for the next stage and updated model evidence :param beta: :type beta: float stage parameter. :param log_likelihoods: :type log_likelihoods: numpy array of size N log likelihood values at all particles :param log_evidence: :type log_evidence: float log of evidence. :param prev_ESS: :type prev_ESS: int effective sample size of previous stage :returns: * **new_beta** (*float stage parameter for next stage.*) * **log_evidence** (*float updated log evidence.*) * **Wm_n** (*numpy array of size N weights of particles for the next stage*) * **ESS** (*float effective sample size of new stage*) .. py:function:: get_log_prior_samples(samples, all_pars) Computes log_prior value at all particles s : numpy array of size N x Np (N samples x N parameters) all_pars: list of PDFs objects, length Np all_pars[i]: is a PDF object with a callable log_pdf_eval method. Returns log_p : numpy array of size N log prior at all N particles . .. py:function:: initial_population(N, all_pars) -> numpy.ndarray Generates initial population from prior distribution N : float number of particles. all_pars : list of size Np is number of parameters; all_pars[i] is object of type pdfs ini_pop : numpy array of size N x Np initial population. .. py:function:: prepare_all_pars(n_epistemic: int = 3, n_mix: int = 2) Prepare prior for the NASA case study with a mixture of Beta marginals + copula correlation. Each mixture component gets its own (a1, a2, b1, b2, rho), scaled ∈ [0.001, 0.999]. .. py:function:: prepare_prior_beta_model_NASA() Prepare the prior for the beta model used in the NASA case study. .. py:function:: propose(current, covariance, n) proposal distribution for MCMC in perturbation stage .. py:function:: MCMC_MH(particle_num, Em, Nm_steps, current, likelihood_current, posterior_current, beta, numAccepts, all_pars, log_likelihood) Markov chain Monte Carlo using Metropolis-Hastings "perturbs" each particle using MCMC-MH :param particle_num: particle number :type particle_num: int :param Em: proposal covarince matrix. :type Em: numpy array of size Np x Np :param Nm_steps: number of perturbation steps. :type Nm_steps: int :param current: current particle location :type current: numpy array of size Np :param likelihood_current: log likelihood value at current particle :type likelihood_current: float :param posterior_current: log posterior value at current particle :type posterior_current: float :param beta: stage parameter. :type beta: float :param numAccepts: total number of accepts :type numAccepts: int :param all_pars: Np is number of parameters all_pars[i] is object of type pdfs all parameters to be inferred. :type all_pars: : list of size Np :param log_likelihood: log likelihood function to be defined in main.py. :type log_likelihood: function :returns: * **current** (*numpy array of size Np*) -- perturbed particle location * **likelihood_current** (*float*) -- log likelihood value at perturbed particle * **posterior_current** (*float*) -- log posterior value at perturbed particle * **numAccepts** (*int*) -- total number of accepts during perturbation (MCMC - MH)