Skip to content

Commit

Permalink
refactored LOSDistribution
Browse files Browse the repository at this point in the history
  • Loading branch information
sibirrer committed Jul 29, 2024
1 parent ec09916 commit bee9c59
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 51 deletions.
20 changes: 13 additions & 7 deletions hierarc/Likelihood/hierarchy_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def __init__(
num_distribution_draws=50,
normalized=True,
# kappa quantities
kappa_pdf=None,
kappa_bin_edges=None,
los_distribution_individual=None,
kwargs_los_individual=None,
# priors
prior_list=None,
# specifics for each lens
Expand All @@ -74,9 +74,16 @@ def __init__(
:param global_los_distribution: if integer, will draw from the global kappa distribution specified in that
integer. If False, will instead draw from the distribution specified in kappa_pdf.
:type global_los_distribution: bool or integer
:param kappa_pdf: array of probability density function of the external convergence distribution
:param los_distribution_individual: name of the individual distribution ["GEV" and "PDF"]
:type los_distribution_individual: str or None
:param kwargs_los_individual: dictionary of the parameters of the individual distribution
If individual_distribution is "PDF":
"pdf_array": array of probability density function of the external convergence distribution
binned according to kappa_bin_edges
:param kappa_bin_edges: array of length (len(kappa_pdf)+1), bin edges of the kappa PDF
"bin_edges": array of length (len(kappa_pdf)+1), bin edges of the kappa PDF
If individual_distribution is "GEV":
"xi", "mean", "sigma"
:type kwargs_los_individual: dict or None
:param mst_ifu: bool, if True replaces the lambda_mst parameter by the lambda_ifu parameter (and distribution)
in sampling this lens.
:param lambda_scaling_property: float (optional), scaling of
Expand Down Expand Up @@ -119,10 +126,10 @@ def __init__(
self._num_distribution_draws = int(num_distribution_draws)

self._los = LOSDistribution(
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
global_los_distribution=global_los_distribution,
los_distributions=los_distributions,
individual_distribution=los_distribution_individual,
kwargs_individual=kwargs_los_individual
)
kwargs_min, kwargs_max = self.param_bounds_interpol()
self._lens_distribution = LensDistribution(
Expand Down Expand Up @@ -306,7 +313,6 @@ def log_likelihood_single(
kwargs_kin_draw = self._aniso_distribution.draw_anisotropy(**kwargs_kin)
kwargs_param = {**kwargs_lens_draw, **kwargs_kin_draw}
kin_scaling = self.kin_scaling(kwargs_param)
print(kwargs_lens_draw, "test kwargs_lens_draw")
lnlikelihood = self.log_likelihood(
ddt_,
dd_,
Expand Down
62 changes: 48 additions & 14 deletions hierarc/Sampling/Distributions/los_distributions.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import numpy as np
from hierarc.Util.distribution_util import PDFSampling
from scipy.stats import genextreme


class LOSDistribution(object):
"""Line of sight distribution drawing."""

def __init__(
self,
kappa_pdf=None,
kappa_bin_edges=None,
global_los_distribution=False,
los_distributions=None,
individual_distribution=None,
kwargs_individual=None,
):
"""
:param global_los_distribution: if integer, will draw from the global kappa distribution specified in that
integer. If False, will instead draw from the distribution specified in kappa_pdf.
:type global_los_distribution: bool or int
:param kappa_pdf: array of probability density function of the external convergence distribution
binned according to kappa_bin_edges
:param kappa_bin_edges: array of length (len(kappa_pdf)+1), bin edges of the kappa PDF
:param los_distributions: list of all line of sight distributions parameterized
:type los_distributions: list of str or None
:param individual_distribution: name of the individual distribution ["GEV" and "PDF"]
:type individual_distribution: str or None
:param kwargs_individual: dictionary of the parameters of the individual distribution
If individual_distribution is "PDF":
"pdf_array": array of probability density function of the external convergence distribution
binned according to kappa_bin_edges
"bin_edges": array of length (len(kappa_pdf)+1), bin edges of the kappa PDF
If individual_distribution is "GEV":
"xi", "mean", "sigma"
:type kwargs_individual: dict or None
"""

self._global_los_distribution = global_los_distribution
Expand All @@ -33,14 +41,14 @@ def __init__(
self._los_distribution = los_distributions[global_los_distribution]
else:
self._draw_kappa_global = False
if (
kappa_pdf is not None
and kappa_bin_edges is not None
and not self._draw_kappa_global
if (not self._draw_kappa_global and individual_distribution is not None
):
self._kappa_dist = PDFSampling(
bin_edges=kappa_bin_edges, pdf_array=kappa_pdf
)
if individual_distribution == "PDF":
self._kappa_dist = PDFSampling(**kwargs_individual)
elif individual_distribution == "GEV":
self._kappa_dist = GEV(**kwargs_individual)
else:
raise ValueError("individual_distribution %s not supported. Chose among 'GEV' and 'PDF'")
self._draw_kappa_individual = True
else:
self._draw_kappa_individual = False
Expand All @@ -54,6 +62,7 @@ def draw_los(self, kwargs_los, size=1):
:type size: int>0
:return: external convergence draw
"""

if self._draw_kappa_individual is True:
kappa_ext_draw = self._kappa_dist.draw(n=size)
elif self._draw_kappa_global:
Expand All @@ -66,8 +75,6 @@ def draw_los(self, kwargs_los, size=1):
mean = kwargs_los_i["mean"]
sigma = kwargs_los_i["sigma"]
xi = kwargs_los_i["xi"]
from scipy.stats import genextreme

kappa_ext_draw = genextreme.rvs(c=xi, loc=mean, scale=sigma, size=size)
else:
raise ValueError(
Expand All @@ -89,3 +96,30 @@ def draw_bool(self, kwargs_los):
if kwargs_los[self._global_los_distribution]["sigma"] != 0:
return True
return False


class GEV(object):
"""
draw from General Extreme Value distribution
"""
def __init__(self, xi, mean, sigma):
"""
:param xi: Xi value of GEV
:param mean: mean of GEV
:param sigma: sigma of GEV
"""
self._xi = xi
self._mean = mean
self._sigma = sigma

def draw(self, n=1):
"""
draws from the PDF of the GEV distribution
:param n: number of draws from distribution
:type n: int
:return: draws according to the PDF of the distribution
"""
kappa_ext_draw = genextreme.rvs(c=self._xi, loc=self._mean, scale=self._sigma, size=n)
return kappa_ext_draw
4 changes: 2 additions & 2 deletions test/test_Diagnostics/test_goodness_of_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def setup_method(self):
},
{
"ddt_samples": ddt_samples,
"kappa_pdf": kappa_pdf,
"kappa_bin_edges": kappa_bin_edges,
"los_distribution_individual": "PDF",
"kwargs_los_individual": {"bin_edges": kappa_bin_edges, "pdf_array": kappa_pdf},
},
{"ddt_samples": ddt_samples},
{
Expand Down
22 changes: 8 additions & 14 deletions test/test_Likelihood/test_hierarchy_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def setup_method(self):
num_distribution_draws=200,
los_distributions=["GAUSSIAN"],
global_los_distribution=0,
kappa_pdf=None,
kappa_bin_edges=None,
los_distribution_individual=None,
kwargs_los_individual=None,
mst_ifu=True,
**kwargs_likelihood,
**kwargs_model
Expand All @@ -63,8 +63,8 @@ def setup_method(self):
num_distribution_draws=200,
los_distributions=["GAUSSIAN"],
global_los_distribution=0, # testing previously set to =False
kappa_pdf=None,
kappa_bin_edges=None,
los_distribution_individual=None,
kwargs_los_individual=None,
mst_ifu=False,
**kwargs_likelihood,
**kwargs_model
Expand All @@ -80,8 +80,8 @@ def setup_method(self):
num_distribution_draws=0,
los_distributions=["GAUSSIAN"],
global_los_distribution=0,
kappa_pdf=None,
kappa_bin_edges=None,
los_distribution_individual=None,
kwargs_los_individual=None,
mst_ifu=True,
**kwargs_likelihood,
**kwargs_model
Expand All @@ -100,8 +100,8 @@ def setup_method(self):
num_distribution_draws=200,
# los_distributions=["GAUSSIAN"],
global_los_distribution=False,
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
los_distribution_individual="PDF",
kwargs_los_individual={"bin_edges": kappa_bin_edges, "pdf_array": kappa_pdf},
mst_ifu=False,
**kwargs_likelihood,
**kwargs_model
Expand All @@ -123,8 +123,6 @@ def setup_method(self):
j_kin_scaling_param_axes=j_kin_scaling_param_axes,
j_kin_scaling_grid_list=[param_scaling_array],
num_distribution_draws=200,
kappa_pdf=None,
kappa_bin_edges=None,
mst_ifu=False,
gamma_in_sampling=False,
gamma_in_distribution="GAUSSIAN",
Expand All @@ -149,8 +147,6 @@ def setup_method(self):
j_kin_scaling_param_axes=j_kin_scaling_param_axes,
j_kin_scaling_grid_list=[param_scaling_array],
num_distribution_draws=200,
kappa_pdf=None,
kappa_bin_edges=None,
mst_ifu=False,
gamma_in_sampling=True,
gamma_in_distribution="GAUSSIAN",
Expand All @@ -169,8 +165,6 @@ def setup_method(self):
j_kin_scaling_param_axes=ani_param_array,
j_kin_scaling_grid_list=[ani_scaling_array],
num_distribution_draws=200,
kappa_pdf=None,
kappa_bin_edges=None,
mst_ifu=False,
lambda_scaling_property=100,
**kwargs_likelihood,
Expand Down
36 changes: 22 additions & 14 deletions test/test_Likelihood/test_los_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def test_gev(self):

# here we draw from the scipy function
dist_gev = LOSDistribution(
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
global_los_distribution=1,
los_distributions=los_distribution,
individual_distribution="PDF",
kwargs_individual={"pdf_array": kappa_pdf, "bin_edges": kappa_bin_edges}
)

kappa_dist_drawn = dist_gev.draw_los(kwargs_los, size=10000)
Expand All @@ -47,10 +47,10 @@ def test_gev(self):

# here we draw from the distribution
dist_gev = LOSDistribution(
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
global_los_distribution=False,
los_distributions=los_distribution,
individual_distribution="PDF",
kwargs_individual={"pdf_array": kappa_pdf, "bin_edges": kappa_bin_edges}
)

kappa_dist_drawn = dist_gev.draw_los(kwargs_los, size=10000)
Expand All @@ -59,10 +59,10 @@ def test_gev(self):

# draw from Gaussian
dist_gev = LOSDistribution(
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
global_los_distribution=0,
los_distributions=los_distribution,
individual_distribution="PDF",
kwargs_individual={"pdf_array": kappa_pdf, "bin_edges": kappa_bin_edges}
)

kappa_dist_drawn = dist_gev.draw_los(kwargs_los, size=10000)
Expand Down Expand Up @@ -92,28 +92,28 @@ def test_draw_bool(self):
]

dist = LOSDistribution(
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
global_los_distribution=1,
los_distributions=los_distribution,
individual_distribution="PDF",
kwargs_individual={"pdf_array": kappa_pdf, "bin_edges": kappa_bin_edges}
)
bool_draw = dist.draw_bool(kwargs_los)
assert bool_draw is True

dist = LOSDistribution(
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
global_los_distribution=0,
los_distributions=los_distribution,
individual_distribution="PDF",
kwargs_individual={"pdf_array": kappa_pdf, "bin_edges": kappa_bin_edges}
)
bool_draw = dist.draw_bool(kwargs_los)
assert bool_draw is False

dist = LOSDistribution(
kappa_pdf=kappa_pdf,
kappa_bin_edges=kappa_bin_edges,
global_los_distribution=False,
los_distributions=los_distribution,
individual_distribution="PDF",
kwargs_individual={"pdf_array": kappa_pdf, "bin_edges": kappa_bin_edges}
)
bool_draw = dist.draw_bool(kwargs_los)
assert bool_draw is True
Expand All @@ -123,9 +123,17 @@ class TestRaise(unittest.TestCase):
def test_raise(self):
with self.assertRaises(ValueError):
los = LOSDistribution(
kappa_pdf=None,
kappa_bin_edges=None,
individual_distribution=None,
kwargs_individual=None,
global_los_distribution=0,
los_distributions=["BAD"],
)
los.draw_los(kwargs_los=[{}])

with self.assertRaises(ValueError):
los = LOSDistribution(
individual_distribution="BAD",
kwargs_individual=None,
global_los_distribution=False,
los_distributions=None,
)

0 comments on commit bee9c59

Please sign in to comment.