h0rton.h0_inference
¶
Submodules¶
Package Contents¶
Classes¶
H0Posterior |
Represents the posterior over H0 |
BaseGaussianBNNPosterior |
Abstract base class to represent the Gaussian BNN posterior |
DiagonalGaussianBNNPosterior |
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix |
LowRankGaussianBNNPosterior |
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix |
DoubleLowRankGaussianBNNPosterior |
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix |
FullRankGaussianBNNPosterior |
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix |
DoubleGaussianBNNPosterior |
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix |
Functions¶
plot_weighted_h0_histogram (all_samples, all_weights, lens_i=0, true_h0=None, include_fit_gaussian=True, save_dir=’.’) |
Plot the histogram of H0 samples, overlaid with a Gaussian fit and truth H0 |
plot_h0_histogram (samples, lens_i=0, true_h0=None, include_fit_gaussian=True, save_dir=’.’) |
Plot the histogram of H0 samples, overlaid with a Gaussian fit and truth H0 |
plot_D_dt_histogram (all_samples, lens_i=0, true_D_dt=None, save_dir=’.’) |
Plot the histogram of D_dt samples, overlaid with a Gaussian fit and truth D_dt |
plot_mcmc_corner (mcmc_samples, truth, col_labels, save_path) |
|
gaussian (x, mean, standard_deviation, amplitude) |
|
plot_forward_modeling_comparisons (model_plot_instance, out_dir) |
Plot the data vs. model comparisons using the Lenstronomy modelPlot tool |
-
class
h0rton.h0_inference.
H0Posterior
(H0_prior, kappa_ext_prior, kwargs_model, baobab_time_delays, Om0, define_src_pos_wrt_lens, exclude_vel_disp=True, aniso_param_prior=None, kinematics=None, kappa_transformed=True, kwargs_lens_eqn_solver={})[source]¶ Represents the posterior over H0
-
required_params
= ['lens_mass_center_x', 'src_light_center_x', 'lens_mass_center_y', 'src_light_center_y', 'lens_mass_gamma', 'lens_mass_theta_E', 'lens_mass_e1', 'lens_mass_e2', 'external_shear_gamma1', 'external_shear_gamma2', 'src_light_R_sersic']¶
-
classmethod
from_dict
(cls, lens_dict)¶ Initialize H0Posterior from a dictionary
- lens_dict : dict
- contains properties required to initialize H0Posterior. See __init__ method above for the required parameters and their formats.
-
set_cosmology_observables
(self, z_lens, z_src, measured_td_wrt0, measured_td_err, abcd_ordering_i, true_img_dec, true_img_ra, kappa_ext, measured_vd=None, measured_vd_err=None)¶ Set the cosmology observables for a given lens system, persistent across all the samples for that system
z_lens : float z_src : float lens_mass_dict : dict
dict of lens mass kwargs- ext_shear_dict : dict
- dict of external shear kwargs
- ps_dict : dict
- dict of point source kwargs
- measured_vd : float
- measured velocity dispersion
- measured_vd_err : float
- measurement error of velocity dispersion
- lens_light_R_sersic : float
- effective radius of lens light in arcsec
- measured_td : np.array of shape [n_images,]
- the measured time delays in days
- measured_td_err : float
- the time delay measurement error in days
- abcd_ordering_i : np.array of shape [n_images,]
- the image ordering followed by measured_td in increasing dec. Example: if the measured_td are [a, b, c, d] and the corresponding image dec are [0.3, -0.1, 0.8, 0.4], then abcd_ordering_i are [1, 0, 3, 2].
- true_img_dec : np.array of shape [n_images, ]
- dec of the true image positions in arcsec
- true_img_ra : np.array of shape [n_images, ]
- ra of the true image positions in arcsec
-
_reorder_measured_td_to_tdlmc
(self)¶ Reorder the measured time delays (same for all lens model samples)
Unused!
-
format_lens_model
(self, sample)¶ Set the lens model parameters for a given lens mass model
- sample : dict
- a sampled set of lens model parameters
-
get_img_pos
(self, ps_dict, kwargs_lens)¶ Sets the kwargs_ps class attribute as those coresponding to the point source model LENSED_POSITION
- ps_dict : dict
- point source parameters definitions, either of SOURCE_POSITION or LENSED_POSITION
-
sample_H0
(self, random_state)¶
-
sample_kappa_ext_original
(self, random_state)¶
-
sample_kappa_ext_transformed
(self, random_state)¶
-
sample_aniso_param
(self, random_state)¶
-
calculate_offset_from_true_image_positions
(self, model_ra, model_dec, true_img_ra, true_img_dec, increasing_dec_i, abcd_ordering_i)¶ Calculates the difference in arcsec between the (inferred or fed-in) image positions known to H0Posterior and the provided true image positions
- true_img_ra : array-like, of length self.n_img
- ra of true image positions in TDLMC order
- true_img_dec : array-like, of length self.n_img
- dec of true image positions in TDLMC order
- array-like
- offset in arcsec for each image
-
get_h0_sample
(self, sampled_lens_model_raw, random_state)¶ Get MC samples from the H0Posterior
- sampled_lens_model_raw : dict
- sampled lens model parameters, pre-formatting
random_state : np.random.RandomState object
- tuple of floats
- the candidate H0 and its weight
-
set_truth_lens_model
(self, sampled_lens_model_raw)¶
-
get_h0_sample_truth
(self, random_state)¶ Get MC samples from the H0Posterior
- sampled_lens_model_raw : dict
- sampled lens model parameters, pre-formatting
random_state : np.random.RandomState object
- tuple of floats
- the candidate H0 and its weight
-
chuck_images
(self, inferred_td, x_image, y_image)¶ If the number of predicted images are greater than the measured, choose the images that best correspond to the measured.
-
-
class
h0rton.h0_inference.
BaseGaussianBNNPosterior
(Y_dim, device, Y_mean=None, Y_std=None)[source]¶ Bases:
abc.ABC
Abstract base class to represent the Gaussian BNN posterior
Gaussian posteriors or mixtures thereof with various forms of the covariance matrix inherit from this class.
-
seed_samples
(self, sample_seed)¶ Seed the sampling for reproducibility
sample_seed : int
-
sample
(self, n_samples, sample_seed=None)¶ Sample from the Gaussian posterior. Must be overridden by subclasses.
- n_samples : int
- how many samples to obtain
- sample_seed : int
- seed for the samples. Default: None
- np.array of shape [n_samples, self.Y_dim]
- samples
-
get_hpd_interval
(self)¶ Get the highest posterior density (HPD) interval
-
transform_back_mu
(self, tensor)¶ Transform back, i.e. unwhiten, the tensor of central values
tensor : torch.Tensor of shape [batch_size, Y_dim]
- torch.tensor of shape [batch_size, Y_dim]
- the original tensor
-
transform_back_logvar
(self, logvar)¶ Transform back, i.e. unwhiten, the tensor of predicted log of the diagonal entries of the cov mat
tensor : torch.Tensor of shape [batch_size, Y_dim]
- torch.tensor of shape [batch_size, Y_dim]
- the original tensor
-
transform_back_cov_mat
(self, cov_mat)¶ Transform back, i.e. unwhiten, the tensor of predicted covariance matrix
tensor : torch.Tensor of shape [batch_size, Y_dim, Y_dim]
- torch.tensor of shape [batch_size, Y_dim]
- the original tensor
-
unwhiten_back
(self, sample)¶ Scale and shift back to the unwhitened state
- pred : torch.Tensor
- network prediction of shape [batch_size, n_samples, self.Y_dim]
- torch.Tensor
- the unwhitened pred
-
sample_low_rank
(self, n_samples, mu, logvar, F)¶ Sample from a single Gaussian posterior with a full but low-rank plus diagonal covariance matrix
- n_samples : int
- how many samples to obtain
- mu : torch.Tensor of shape [self.batch_size, self.Y_dim]
- network prediction of the mu (mean parameter) of the BNN posterior
- logvar : torch.Tensor of shape [self.batch_size, self.Y_dim]
- network prediction of the log of the diagonal elements of the covariance matrix
- F : torch.Tensor of shape [self.batch_size, self.Y_dim, self.rank]
- network prediction of the low rank portion of the covariance matrix
- np.array of shape [self.batch_size, n_samples, self.Y_dim]
- samples
-
sample_full_rank
(self, n_samples, mu, tril_elements, as_numpy=True)¶ Sample from a single Gaussian posterior with a full-rank covariance matrix
- n_samples : int
- how many samples to obtain
- mu : torch.Tensor of shape [self.batch_size, self.Y_dim]
- network prediction of the mu (mean parameter) of the BNN posterior
- tril_elements : torch.Tensor of shape [self.batch_size, tril_len]
- network prediction of lower-triangular matrix in the log-Cholesky decomposition of the precision matrix
- np.array of shape [self.batch_size, n_samples, self.Y_dim]
- samples
-
-
class
h0rton.h0_inference.
DiagonalGaussianBNNPosterior
(Y_dim, device, Y_mean=None, Y_std=None)[source]¶ Bases:
h0rton.h0_inference.gaussian_bnn_posterior.BaseGaussianBNNPosterior
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix
BaseGaussianNLL.__init__ docstring for the parameter description.
-
set_sliced_pred
(self, pred)¶
-
sample
(self, n_samples, sample_seed)¶ Sample from a Gaussian posterior with diagonal covariance matrix
- n_samples : int
- how many samples to obtain
- sample_seed : int
- seed for the samples. Default: None
- np.array of shape [n_samples, self.Y_dim]
- samples
-
get_hpd_interval
(self)¶ Get the highest posterior density (HPD) interval
-
-
class
h0rton.h0_inference.
LowRankGaussianBNNPosterior
(Y_dim, device, Y_mean=None, Y_std=None)[source]¶ Bases:
h0rton.h0_inference.gaussian_bnn_posterior.BaseGaussianBNNPosterior
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix
BaseGaussianNLL.__init__ docstring for the parameter description.
-
set_sliced_pred
(self, pred)¶
-
sample
(self, n_samples, sample_seed)¶ Sample from the Gaussian posterior. Must be overridden by subclasses.
- n_samples : int
- how many samples to obtain
- sample_seed : int
- seed for the samples. Default: None
- np.array of shape [n_samples, self.Y_dim]
- samples
-
get_hpd_interval
(self)¶ Get the highest posterior density (HPD) interval
-
-
class
h0rton.h0_inference.
DoubleLowRankGaussianBNNPosterior
(Y_dim, device, Y_mean=None, Y_std=None)[source]¶ Bases:
h0rton.h0_inference.gaussian_bnn_posterior.BaseGaussianBNNPosterior
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix
BaseGaussianNLL.__init__ docstring for the parameter description.
-
set_sliced_pred
(self, pred)¶
-
sample
(self, n_samples, sample_seed)¶ Sample from a mixture of two Gaussians, each with a full but constrained as low-rank plus diagonal covariance
- n_samples : int
- how many samples to obtain
- sample_seed : int
- seed for the samples. Default: None
- np.array of shape [self.batch_size, n_samples, self.Y_dim]
- samples
-
get_hpd_interval
(self)¶ Get the highest posterior density (HPD) interval
-
-
class
h0rton.h0_inference.
FullRankGaussianBNNPosterior
(Y_dim, device, Y_mean=None, Y_std=None)[source]¶ Bases:
h0rton.h0_inference.gaussian_bnn_posterior.BaseGaussianBNNPosterior
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix
BaseGaussianNLL.__init__ docstring for the parameter description.
-
set_sliced_pred
(self, pred)¶
-
sample
(self, n_samples, sample_seed)¶ Sample from the Gaussian posterior. Must be overridden by subclasses.
- n_samples : int
- how many samples to obtain
- sample_seed : int
- seed for the samples. Default: None
- np.array of shape [n_samples, self.Y_dim]
- samples
-
get_hpd_interval
(self)¶ Get the highest posterior density (HPD) interval
-
-
class
h0rton.h0_inference.
DoubleGaussianBNNPosterior
(Y_dim, device, Y_mean=None, Y_std=None)[source]¶ Bases:
h0rton.h0_inference.gaussian_bnn_posterior.BaseGaussianBNNPosterior
The negative log likelihood (NLL) for a single Gaussian with diagonal covariance matrix
BaseGaussianNLL.__init__ docstring for the parameter description.
-
set_sliced_pred
(self, pred)¶
-
sample
(self, n_samples, sample_seed)¶ Sample from a mixture of two Gaussians, each with a full but constrained as low-rank plus diagonal covariance
- n_samples : int
- how many samples to obtain
- sample_seed : int
- seed for the samples. Default: None
- np.array of shape [self.batch_size, n_samples, self.Y_dim]
- samples
-
get_hpd_interval
(self)¶ Get the highest posterior density (HPD) interval
-
-
h0rton.h0_inference.
plot_weighted_h0_histogram
(all_samples, all_weights, lens_i=0, true_h0=None, include_fit_gaussian=True, save_dir='.')[source]¶ Plot the histogram of H0 samples, overlaid with a Gaussian fit and truth H0
- all_samples : np.array
- H0 samples
- all_weights : np.array
- H0 weights corresponding to all_samples, possibly including nan values
-
h0rton.h0_inference.
plot_h0_histogram
(samples, lens_i=0, true_h0=None, include_fit_gaussian=True, save_dir='.')[source]¶ Plot the histogram of H0 samples, overlaid with a Gaussian fit and truth H0
- all_samples : np.array
- H0 samples
- all_weights : np.array
- H0 weights corresponding to all_samples, possibly including nan values