Skip to content

Commit

Permalink
bug fix for scaling of gamma_pl pre-processing
Browse files Browse the repository at this point in the history
  • Loading branch information
sibirrer committed Sep 16, 2024
1 parent f482350 commit a73eb84
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
12 changes: 9 additions & 3 deletions hierarc/Diagnostics/goodness_of_fit.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import matplotlib.pyplot as plt
import numpy as np
from hierarc.Likelihood.lens_sample_likelihood import LensSampleLikelihood
from hierarc.Likelihood.cosmo_likelihood import CosmoLikelihood


class GoodnessOfFit(object):
"""Class to manage goodness of fit diagnostics."""

def __init__(self, kwargs_likelihood_list):
def __init__(self, kwargs_likelihood_list, kwargs_model):
"""
:param kwargs_likelihood_list: list of likelihood kwargs of individual lenses consistent with the
LensLikelihood module
:param kwargs_model: model settings for ParamManager() class
:type kwargs_model: dict
"""
self._kwargs_likelihood_list = kwargs_likelihood_list
self._sample_likelihood = LensSampleLikelihood(
kwargs_likelihood_list, normalized=False
kwargs_likelihood_list,
normalized=False,
kwargs_global_model=kwargs_model,
)

def reduced_chi2(self, cosmo, kwargs_lens, kwargs_kin):
"""Reduced chi^2 of fit."""
logL = self._sample_likelihood.log_likelihood(cosmo, kwargs_lens, kwargs_kin)
logL = self._sample_likelihood.log_likelihood(cosmo=cosmo, kwargs_lens=kwargs_lens, kwargs_kin=kwargs_kin,
verbose=False)
num_data = self._sample_likelihood.num_data()
return -logL * 2 / num_data

Expand Down
4 changes: 3 additions & 1 deletion hierarc/LensPosterior/imaging_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def draw_lens(self, gamma_pl=None, no_error=False):
:return: theta_E, gamma, r_eff, delta_r_eff
"""
if no_error is True:
return self._theta_E, self._gamma, self._r_eff, 1
if gamma_pl is None:
gamma_pl = self._gamma
return self._theta_E, gamma_pl, self._r_eff, 1
theta_E_draw = np.maximum(
np.random.normal(loc=self._theta_E, scale=self._theta_E_error), 0
)
Expand Down
2 changes: 1 addition & 1 deletion hierarc/Likelihood/cosmo_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
log likelihood value (e.g. prior)
:param interpolate_cosmo: bool, if True, uses interpolated comoving distance in the calculation for speed-up
:param num_redshift_interp: int, number of redshift interpolation steps
:param cosmo_fixed: astropy.cosmology instance to be used and held fixed throughout the sampling
:param cosmo_fixed: ~astropy.cosmology instance to be used and held fixed throughout the sampling
:param normalized: bool, if True, returns the normalized likelihood, if False, separates the constant prefactor
(in case of a Gaussian 1/(sigma sqrt(2 pi)) ) to compute the reduced chi2 statistics
"""
Expand Down
3 changes: 1 addition & 2 deletions hierarc/Likelihood/kin_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self, param_grid_axes, j_kin_scaling_grid):

if self._dim_scaling == 1:
self._f_ani = interp1d(
param_grid_axes[0], j_kin_scaling_grid, kind="linear"
param_grid_axes[0], j_kin_scaling_grid, kind="linear", fill_value="extrapolate"
)
elif self._dim_scaling == 2:
self._f_ani = interp2d(
Expand All @@ -103,7 +103,6 @@ def j_scaling(self, param_array):
:param param_array: sorted list of parameters for the interpolation function
:return: scaling J(a_ani) for single slit
"""

if self._evalute_scaling is not True or len(param_array) == 0:
return 1
if self._dim_scaling == 1:
Expand Down
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 @@ -100,7 +100,7 @@ def setup_method(self):
self.kwargs_likelihood_list[i]["likelihood_type"] = likelihood_type

self.goodnessofFit = GoodnessOfFit(
kwargs_likelihood_list=self.kwargs_likelihood_list
kwargs_likelihood_list=self.kwargs_likelihood_list, kwargs_model={}
)

def test_plot_ddt_fit(self):
Expand Down Expand Up @@ -153,7 +153,7 @@ def test_raise(self):
}
]
goodness_of_fit = GoodnessOfFit(
kwargs_likelihood_list=kwargs_likelihood_list
kwargs_likelihood_list=kwargs_likelihood_list, kwargs_model={}
)
f, ax = plt.subplots(1, 1, figsize=(4, 4))
kwargs_lens = {"lambda_mst": 1}
Expand Down

0 comments on commit a73eb84

Please sign in to comment.