infer_utils
#
Utility methods managing inference based on a trained model
Module Contents#
Functions#
|
Evaluate the log kappa likelihood of the test set, |
|
Run MCMC sampling |
|
Plot MCMC chain |
|
Evaluate the log likelihood, log p(k|Omega_int), |
|
Evaluate the log likelihood, log p(k|Omega_int), |
|
Sample from p(Omega|{d}) using MCMC |
|
Sample from p(Omega|{d}) using MCMC |
|
Evaluate the MCMC objective |
|
Evaluate the MCMC objective |
|
Load the samples from saved MCMC run |
|
Evaluate the log weights used to reweight individual kappa posteriors |
|
Evaluate the log weights used to reweight individual kappa posteriors |
|
|
|
Fit a KDE on weighted samples |
|
Resample from a distribution defined by weighted samples |
Attributes#
- infer_utils.get_normal_logpdf(mu, log_sigma, x, bounds_lower=-np.inf, bounds_upper=np.inf)[source]#
Evaluate the log kappa likelihood of the test set, log p(k_j|Omega), exactly
Note#
Only normal likelihood supported for now.
Returns#
- np.ndarray or float
Log PDF, of shape broadcasted across mu, log_sigma, and x
- infer_utils.run_mcmc(log_prob, log_prob_kwargs, p0, n_run, n_burn, chain_path, run_name='mcmc', n_walkers=100, plot_chain=True, clear=False, n_cores=None)[source]#
Run MCMC sampling
Parameters#
p0 : np.array of shape [n_walkers, n_dim] n_run : int n_burn : int chain_path : os.path or str n_walkers : int plot_chain : bool
- infer_utils.get_chain_plot(samples, out_path='mcmc_chain.png')[source]#
Plot MCMC chain
Note#
Borrowed from https://emcee.readthedocs.io/en/stable/tutorials/line/
- infer_utils.get_log_p_k_given_omega_int_kde(k_train, k_bnn, kwargs=None)[source]#
Evaluate the log likelihood, log p(k|Omega_int), using kernel density estimation (KDE) on training kappa, on the BNN kappa samples of test sightlines
Parameters#
- k_trainnp.array of shape [n_train]
kappa in the training set
k_bnn : np.array of shape [n_test, n_samples] kwargs : dict
currently unused, placeholder for symmetry with analytic version
Returns#
- np.array of shape [n_test, n_samples]
log p(k|Omega_int)
- infer_utils.get_log_p_k_given_omega_int_analytic(k_train, k_bnn, interim_pdf_func)[source]#
Evaluate the log likelihood, log p(k|Omega_int), using kernel density estimation (KDE) on training kappa, on the BNN kappa samples of test sightlines
Parameters#
- k_trainnp.array of shape [n_train]
kappa in the training set. Unused.
k_bnn : np.array of shape [n_test, n_samples] interim_pdf_func : callable
function that evaluates the PDF of the interim prior
Returns#
- np.array of shape [n_test, n_samples]
log p(k|Omega_int)
- infer_utils.get_omega_post(k_bnn, log_p_k_given_omega_int, mcmc_kwargs, bounds_lower, bounds_upper)[source]#
Sample from p(Omega|{d}) using MCMC
Parameters#
- k_bnnnp.array of shape [n_test, n_samples]
BNN samples for n_test sightlines
- log_p_k_given_omega_intnp.array of shape [n_test, n_samples]
log p(k_bnn|Omega_int)
- infer_utils.get_omega_post_loop(k_samples_list, log_p_k_given_omega_int_list, mcmc_kwargs, bounds_lower, bounds_upper)[source]#
Sample from p(Omega|{d}) using MCMC
Parameters#
- k_samples_listlist
Each element is the array of samples for a sightline, so the list has length n_test
- log_p_k_given_omega_int_listlist
Each element is the array of log p(k_samples|Omega_int) for a sightline, so the list has length n_test
- infer_utils.log_prob_mcmc(omega, log_p_k_given_omega_func, log_p_k_given_omega_int)[source]#
Evaluate the MCMC objective
Parameters#
- omegalist
Current MCMC sample of [mu, log_sigma] = Omega
- log_p_k_given_omega_funccallable
function that returns p(k|Omega) of shape [n_test, n_samples] for given omega and k fixed to be the BNN samples
- log_p_k_given_omega_intnp.ndarray
Values of p(k|Omega_int) of shape [n_test, n_samples] for k fixed to be the BNN samples
Returns#
- float
Description
- infer_utils.log_prob_mcmc_loop(omega, log_p_k_given_omega_func_list, log_p_k_given_omega_int_list)[source]#
Evaluate the MCMC objective
Parameters#
- omegalist
Current MCMC sample of [mu, log_sigma] = Omega
- log_p_k_given_omega_func_listlist of callable
List of functions that returns p(k|Omega) of shape [n_samples] for given omega and k fixed to be the posterior samples
- log_p_k_given_omega_int_listnp.ndarray
List of values of p(k|Omega_int) of shape [n_samples] for k fixed to be the posterior samples
Returns#
- float
Description
- infer_utils.get_mcmc_samples(chain_path, chain_kwargs)[source]#
Load the samples from saved MCMC run
Parameters#
- chain_pathstr
Path to the stored chain
- chain_kwargsdict
Options for chain postprocessing, including flat, thin, discard
Returns#
- np.array of shape [n_omega, 2]
omega samples from MCMC chain
- infer_utils.get_kappa_log_weights(k_bnn, log_p_k_given_omega_int, omega_post_samples=None)[source]#
Evaluate the log weights used to reweight individual kappa posteriors
Parameters#
- k_bnnnp.ndarray of shape [n_samples]
BNN posterior samples
- log_p_k_given_omega_intnp.ndarray of shape [n_samples]
Likelihood of BNN kappa given the interim prior.
- omega_post_samplesnp.ndarray, optional
Omega posterior samples used as the prior to apply. Should be np.array of shape [n_omega, 2]. If None, only division by the interim prior will be done.
Returns#
- np.ndarray
log weights evaluated at k_bnn
- infer_utils.get_kappa_log_weights_vectorized(k_bnn, omega_post_samples, log_p_k_given_omega_int)[source]#
Evaluate the log weights used to reweight individual kappa posteriors
Parameters#
k_bnn : np.array of shape [n_test, n_samples] omega_post_samples : np.array of shape [n_omega, 2] log_p_k_given_omega_int : np.array of shape [n_test, n_samples]