Skip to content

Commit

Permalink
new line of sight distribution handling for multiple distributions an…
Browse files Browse the repository at this point in the history
…d separate list of dictionary, including generalized extreme value distribution
  • Loading branch information
sibirrer committed Apr 28, 2024
1 parent 6c65666 commit 6f59c3b
Show file tree
Hide file tree
Showing 17 changed files with 524 additions and 146 deletions.
18 changes: 12 additions & 6 deletions hierarc/Diagnostics/goodness_of_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def plot_ddt_fit(
cosmo,
kwargs_lens,
kwargs_kin,
kwargs_los,
color_measurement=None,
color_prediction=None,
redshift_trend=False,
Expand Down Expand Up @@ -64,7 +65,7 @@ def plot_ddt_fit(
ddt_model_sigma,
dd_model_mean,
dd_model_sigma,
) = likelihood.ddt_dd_model_prediction(cosmo, kwargs_lens=kwargs_lens)
) = likelihood.ddt_dd_model_prediction(cosmo, kwargs_lens=kwargs_lens, kwargs_los=kwargs_los)

ddt_name_list.append(name)
ddt_model_mean_list.append(ddt_model_mean)
Expand Down Expand Up @@ -134,13 +135,14 @@ def plot_ddt_fit(
ax.legend()
return f, ax

def kin_fit(self, cosmo, kwargs_lens, kwargs_kin):
def kin_fit(self, cosmo, kwargs_lens, kwargs_kin, kwargs_los):
"""Plots the prediction and the uncorrelated error bars on the individual lenses
currently works for likelihood classes 'TDKinGaussian', 'KinGaussian'.
:param cosmo: astropy.cosmology instance
:param kwargs_lens: lens model parameter keyword arguments
:param kwargs_kin: kinematics model keyword arguments
:param kwargs_los: line of sight list of dictionaries
:return: list of name, measurement, measurement errors, model prediction, model
prediction error
"""
Expand All @@ -160,7 +162,7 @@ def kin_fit(self, cosmo, kwargs_lens, kwargs_kin):
sigma_v_predict_mean,
cov_error_predict,
) = likelihood.sigma_v_measured_vs_predict(
cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin
cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin, kwargs_los=kwargs_los
)

if sigma_v_measurement is not None:
Expand Down Expand Up @@ -188,6 +190,7 @@ def plot_kin_fit(
cosmo,
kwargs_lens,
kwargs_kin,
kwargs_los,
color_measurement=None,
color_prediction=None,
):
Expand All @@ -197,19 +200,20 @@ def plot_kin_fit(
:param cosmo: astropy.cosmology instance
:param kwargs_lens: lens model parameter keyword arguments
:param kwargs_kin: kinematics model keyword arguments
:param kwargs_los: line of sight list of dictionaries
:param color_measurement: color of measurement
:param color_prediction: color of model prediction
:return: fig, axes of matplotlib instance
"""
logL = self._sample_likelihood.log_likelihood(cosmo, kwargs_lens, kwargs_kin)
logL = self._sample_likelihood.log_likelihood(cosmo, kwargs_lens, kwargs_kin, kwargs_los=kwargs_los)
print(logL, "log likelihood")
(
sigma_v_name_list,
sigma_v_measurement_list,
sigma_v_measurement_error_list,
sigma_v_model_list,
sigma_v_model_error_list,
) = self.kin_fit(cosmo, kwargs_lens, kwargs_kin)
) = self.kin_fit(cosmo, kwargs_lens, kwargs_kin, kwargs_los)

f, ax = plt.subplots(1, 1, figsize=(int(len(sigma_v_name_list) / 2), 4))
ax.errorbar(
Expand Down Expand Up @@ -256,6 +260,7 @@ def plot_ifu_fit(
cosmo,
kwargs_lens,
kwargs_kin,
kwargs_los,
lens_index,
bin_edges,
show_legend=True,
Expand All @@ -268,6 +273,7 @@ def plot_ifu_fit(
:param cosmo: astropy.cosmology instance
:param kwargs_lens: lens model parameter keyword arguments
:param kwargs_kin: kinematics model keyword arguments
:param kwargs_los: line of sight list of dictionaries
:param lens_index: int, index in kwargs_lens to be plotted (needs to be of type
'IFUKinCov')
:param bin_edges: radial bin edges in arc seconds. If number, then uniform
Expand All @@ -293,7 +299,7 @@ def plot_ifu_fit(
sigma_v_predict_mean,
cov_error_predict,
) = likelihood.sigma_v_measured_vs_predict(
cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin
cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin, kwargs_los=kwargs_los
)

if len(np.atleast_1d(bin_edges)) < 2:
Expand Down
4 changes: 2 additions & 2 deletions hierarc/Likelihood/KDELikelihood/kde_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def init_loglikelihood(self):
)
self.loglikelihood = self.kdelikelihood()
else:
raise ValueError(
raise NameError(
"likelihood_type %s not supported! Supported are %s."
% (likelihood_type, LIKELIHOOD_TYPES)
% (self.loglikelihood_type, LIKELIHOOD_TYPES)
)

def kdelikelihood(self):
Expand Down
3 changes: 2 additions & 1 deletion hierarc/Likelihood/LensLikelihood/double_source_plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ def __init__(
self._normalized = normalized

def lens_log_likelihood(
self, cosmo, kwargs_lens=None, kwargs_kin=None, kwargs_source=None
self, cosmo, kwargs_lens=None, kwargs_kin=None, kwargs_source=None, kwargs_los=None
):
"""Log likelihood of the data given a model.
:param cosmo: astropy.cosmology instance
:param kwargs_lens: keyword arguments of lens
:param kwargs_kin: keyword arguments of kinematics
:param kwargs_source: keyword argument of source
:param kwargs_los: keyword argument list of line of sight
:return: log likelihood of data given model
"""
beta_model = self._beta_model(cosmo)
Expand Down
20 changes: 12 additions & 8 deletions hierarc/Likelihood/cosmo_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def __init__(
gamma_in_distribution="NONE",
log_m2l_sampling=False,
log_m2l_distribution="NONE",
kappa_ext_sampling=False,
kappa_ext_distribution="NONE",
los_sampling=False,
los_distributions=None,
alpha_lambda_sampling=False,
beta_lambda_sampling=False,
alpha_gamma_in_sampling=False,
Expand Down Expand Up @@ -73,8 +73,10 @@ def __init__(
according to the lens posterior kwargs 'lambda_scaling_property'
:param beta_lambda_sampling: bool, if True samples a parameter beta_lambda, which scales lambda_mst linearly
according to the lens posterior kwargs 'lambda_scaling_property_beta'
:param kappa_ext_sampling: bool, if True samples a global external convergence parameter
:param kappa_ext_distribution: string, distribution function of the kappa_ext parameter
:param los_sampling: if sampling of the parameters should be done
:type los_sampling: bool
:param los_distributions: what distribution to be sampled
:type los_distributions: list of str
:param anisotropy_sampling: bool, if True adds a global stellar anisotropy parameter that alters the single lens
kinematic prediction
:param anisotropy_model: string, specifies the stellar anisotropy model
Expand Down Expand Up @@ -106,7 +108,8 @@ def __init__(
if sigma_v_systematics is True:
normalized = True
self._likelihoodLensSample = LensSampleLikelihood(
kwargs_likelihood_list, normalized=normalized
kwargs_likelihood_list, normalized=normalized,
los_distributions=los_distributions
)
self.param = ParamManager(
cosmology,
Expand All @@ -127,8 +130,8 @@ def __init__(
sne_distribution=sne_distribution,
z_apparent_m_anchor=z_apparent_m_anchor,
sigma_v_systematics=sigma_v_systematics,
kappa_ext_sampling=kappa_ext_sampling,
kappa_ext_distribution=kappa_ext_distribution,
los_sampling=los_sampling,
los_distributions=los_distributions,
anisotropy_sampling=anisotropy_sampling,
anisotropy_model=anisotropy_model,
anisotropy_distribution=anisotropy_distribution,
Expand Down Expand Up @@ -188,7 +191,7 @@ def likelihood(self, args, kwargs_cosmo_interp=None):
if args[i] < self._lower_limit[i] or args[i] > self._upper_limit[i]:
return -np.inf

kwargs_cosmo, kwargs_lens, kwargs_kin, kwargs_source = self.param.args2kwargs(
kwargs_cosmo, kwargs_lens, kwargs_kin, kwargs_source, kwargs_los = self.param.args2kwargs(
args
)
if self._cosmology == "oLCDM":
Expand All @@ -215,6 +218,7 @@ def likelihood(self, args, kwargs_cosmo_interp=None):
kwargs_lens=kwargs_lens,
kwargs_kin=kwargs_kin,
kwargs_source=kwargs_source,
kwargs_los=kwargs_los,
)

if self._sne_evaluate is True:
Expand Down
Loading

0 comments on commit 6f59c3b

Please sign in to comment.