Ejemplo n.º 1
0
    def __init__(self,
                 z_lens,
                 z_source,
                 ddt_samples,
                 sigma_v_measurement,
                 j_model,
                 error_cov_measurement,
                 error_cov_j_sqrt,
                 ddt_weights=None,
                 kde_kernel='gaussian',
                 bandwidth=20,
                 nbins_hist=200,
                 sigma_sys_error_include=False,
                 normalized=True):
        """

        :param z_lens: lens redshift
        :param z_source: source redshift
        :param ddt_samples: numpy array of Ddt values
        :param ddt_weights: optional weights for the samples in Ddt
        :param kde_kernel: string of KDE kernel type
        :param bandwidth: bandwith of kernel
        :param nbins_hist: number of bins in the histogram
        :param sigma_v_measurement: numpy array, velocity dispersion measured
        :param j_model: numpy array of the predicted dimensionless dispersion on the IFU's
        :param error_cov_measurement: covariance matrix of the measured velocity dispersions in the IFU's
        :param error_cov_j_sqrt: covariance matrix of sqrt(J) of the model predicted dimensionless dispersion on the IFU's
        :param sigma_sys_error_include: bool, if True will include a systematic error in the velocity dispersion
         measurement (if sampled from), otherwise this sampled value is ignored.
        :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
        """
        self._tdLikelihood = DdtHistKDELikelihood(z_lens,
                                                  z_source,
                                                  ddt_samples,
                                                  ddt_weights=ddt_weights,
                                                  kde_kernel=kde_kernel,
                                                  bandwidth=bandwidth,
                                                  nbins_hist=nbins_hist)
        self._kinlikelihood = KinLikelihood(
            z_lens,
            z_source,
            sigma_v_measurement,
            j_model,
            error_cov_measurement,
            error_cov_j_sqrt,
            sigma_sys_error_include=sigma_sys_error_include,
            normalized=normalized)
        self.num_data = self._tdLikelihood.num_data + self._kinlikelihood.num_data
    def setup(self):
        self.z_lens = 0.8
        self.z_source = 3.0

        self.ddt_mean = 10
        self.ddt_sigma = 0.1

        self.dd_mean = 1.

        kwargs_ddt_gauss = {
            'z_lens': self.z_lens,
            'z_source': self.z_source,
            'ddt_mean': self.ddt_mean,
            'ddt_sigma': self.ddt_sigma
        }

        ds_dds = self.ddt_mean / self.dd_mean / (1 + self.z_lens)
        j_mean_list = np.ones(1)
        scaling_ifu = 1
        sigma_v_measurement = np.sqrt(
            ds_dds * scaling_ifu * j_mean_list) * const.c / 1000
        sigma_v_sigma = sigma_v_measurement / 10.
        error_cov_measurement = np.diag(sigma_v_sigma**2)
        error_cov_j_sqrt = np.diag(np.zeros_like(sigma_v_measurement))
        self.kin_likelihood = KinLikelihood(self.z_lens,
                                            self.z_source,
                                            sigma_v_measurement,
                                            j_mean_list,
                                            error_cov_measurement,
                                            error_cov_j_sqrt,
                                            normalized=True)
        self.ddt_gauss_likelihood = DdtGaussianLikelihood(**kwargs_ddt_gauss)

        self.ddt_gauss_kin_likelihood = DdtGaussKinLikelihood(
            self.z_lens,
            self.z_source,
            self.ddt_mean,
            self.ddt_sigma,
            sigma_v_measurement,
            j_mean_list,
            error_cov_measurement,
            error_cov_j_sqrt,
            sigma_sys_error_include=False)
        self.sigma_v_measurement = sigma_v_measurement
        self.error_cov_measurement = error_cov_measurement
Ejemplo n.º 3
0
    def __init__(self,
                 z_lens,
                 z_source,
                 ddt_mean,
                 ddt_sigma,
                 sigma_v_measurement,
                 j_model,
                 error_cov_measurement,
                 error_cov_j_sqrt,
                 sigma_sys_error_include=False,
                 normalized=True):
        """

        :param z_lens: lens redshift
        :param z_source: source redshift
        :param ddt_mean: mean Ddt [Mpc] of time-delay and lens model likelihood
        :param ddt_sigma: 1-sigma uncertainty in Ddt
        :param sigma_v_measurement: numpy array, velocity dispersion measured
        :param j_model: numpy array of the predicted dimensionless dispersion on the IFU's
        :param error_cov_measurement: covariance matrix of the measured velocity dispersions in the IFU's
        :param error_cov_j_sqrt: covariance matrix of sqrt(J) of the model predicted dimensionless dispersion on the IFU's
        :param sigma_sys_error_include: bool, if True will include a systematic error in the velocity dispersion
         measurement (if sampled from), otherwise this sampled value is ignored.
        :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
        """
        self._ddt_gauss_likelihood = DdtGaussianLikelihood(z_lens,
                                                           z_source,
                                                           ddt_mean=ddt_mean,
                                                           ddt_sigma=ddt_sigma)
        self._kinlikelihood = KinLikelihood(
            z_lens,
            z_source,
            sigma_v_measurement,
            j_model,
            error_cov_measurement,
            error_cov_j_sqrt,
            sigma_sys_error_include=sigma_sys_error_include,
            normalized=normalized)
        self.num_data = self._ddt_gauss_likelihood.num_data + self._kinlikelihood.num_data
Ejemplo n.º 4
0
    def __init__(self, z_lens, z_source, likelihood_type, name='name', **kwargs_likelihood):
        """

        :param z_lens: lens redshift
        :param z_source: source redshift
        :param name: string (optional) to name the specific lens
        :param likelihood_type: string to specify the likelihood type
        :param kwargs_likelihood: keyword arguments specifying the likelihood function,
        see individual classes for their use
        """
        self._name = name
        self._z_lens = z_lens
        self._z_source = z_source
        self.likelihood_type = likelihood_type
        if likelihood_type in ['DdtGaussian']:
            from hierarc.Likelihood.LensLikelihood.ddt_gauss_likelihood import DdtGaussianLikelihood
            self._lens_type = DdtGaussianLikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type in ['DdtDdKDE']:
            from hierarc.Likelihood.LensLikelihood.ddt_dd_kde_likelihood import DdtDdKDELikelihood
            self._lens_type = DdtDdKDELikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'DdtDdGaussian':
            from hierarc.Likelihood.LensLikelihood.ddt_dd_gauss_likelihood import DdtDdGaussian
            self._lens_type = DdtDdGaussian(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type in ['DsDdsGaussian']:
            from hierarc.Likelihood.LensLikelihood.ds_dds_gauss_likelihood import DsDdsGaussianLikelihood
            self._lens_type = DsDdsGaussianLikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'DdtLogNorm':
            from hierarc.Likelihood.LensLikelihood.ddt_lognorm_likelihood import DdtLogNormLikelihood
            self._lens_type = DdtLogNormLikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'IFUKinCov':
            from hierarc.Likelihood.LensLikelihood.kin_likelihood import KinLikelihood
            self._lens_type = KinLikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'DdtHist':
            from hierarc.Likelihood.LensLikelihood.ddt_hist_likelihood import DdtHistLikelihood
            self._lens_type = DdtHistLikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'DdtHistKDE':
            from hierarc.Likelihood.LensLikelihood.ddt_hist_likelihood import DdtHistKDELikelihood
            self._lens_type = DdtHistKDELikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'DdtHistKin':
            from hierarc.Likelihood.LensLikelihood.ddt_hist_kin_likelihood import DdtHistKinLikelihood
            self._lens_type = DdtHistKinLikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'DdtGaussKin':
            from hierarc.Likelihood.LensLikelihood.ddt_gauss_kin_likelihood import DdtGaussKinLikelihood
            self._lens_type = DdtGaussKinLikelihood(z_lens, z_source, **kwargs_likelihood)
        elif likelihood_type == 'Mag':
            from hierarc.Likelihood.LensLikelihood.mag_likelihood import MagnificationLikelihood
            self._lens_type = MagnificationLikelihood(**kwargs_likelihood)
        elif likelihood_type == 'TDMag':
            from hierarc.Likelihood.LensLikelihood.td_mag_likelihood import TDMagLikelihood
            self._lens_type = TDMagLikelihood(**kwargs_likelihood)
        else:
            raise ValueError('likelihood_type %s not supported! Supported are %s.' % (likelihood_type, LIKELIHOOD_TYPES))
Ejemplo n.º 5
0
    def setup(self):
        self._ddt, self._dd = 2000., 1000.

        self._sigma = 0.1
        ddt_samples = np.random.normal(loc=self._ddt,
                                       scale=self._ddt * self._sigma,
                                       size=1000000)
        ddt_weights = None  # np.random.uniform(low=0, high=1, size=100000)

        self._ddthist = DdtHistLikelihood(z_lens=None,
                                          z_source=None,
                                          ddt_samples=ddt_samples,
                                          ddt_weights=ddt_weights,
                                          nbins_hist=400)

        self._num_ifu = 10
        z_lens = 0.5
        z_source = 2
        ds_dds = self._ddt / self._dd / (1 + z_lens)
        j_model = np.ones(self._num_ifu)
        scaling_ifu = 1
        sigma_v_measurement = np.sqrt(
            ds_dds * scaling_ifu * j_model) * const.c / 1000
        sigma_v_sigma = sigma_v_measurement * self._sigma
        error_cov_measurement = np.diag(sigma_v_sigma**2)
        error_cov_j_sqrt = np.diag(np.zeros_like(sigma_v_measurement))
        self._kin_likelihood = KinLikelihood(z_lens, z_source,
                                             sigma_v_measurement, j_model,
                                             error_cov_measurement,
                                             error_cov_j_sqrt)
        self._ddt_kin_likelihood = DdtHistKinLikelihood(
            z_lens,
            z_source,
            ddt_samples,
            sigma_v_measurement,
            j_model,
            error_cov_measurement,
            error_cov_j_sqrt,
            ddt_weights=ddt_weights,
            kde_kernel='gaussian',
            bandwidth=20,
            nbins_hist=400)
        self.sigma_v_measurement = sigma_v_measurement
        self.error_cov_measurement = error_cov_measurement
class TestDdtGaussKinLikelihood(object):
    def setup(self):
        self.z_lens = 0.8
        self.z_source = 3.0

        self.ddt_mean = 10
        self.ddt_sigma = 0.1

        self.dd_mean = 1.

        kwargs_ddt_gauss = {
            'z_lens': self.z_lens,
            'z_source': self.z_source,
            'ddt_mean': self.ddt_mean,
            'ddt_sigma': self.ddt_sigma
        }

        ds_dds = self.ddt_mean / self.dd_mean / (1 + self.z_lens)
        j_mean_list = np.ones(1)
        scaling_ifu = 1
        sigma_v_measurement = np.sqrt(
            ds_dds * scaling_ifu * j_mean_list) * const.c / 1000
        sigma_v_sigma = sigma_v_measurement / 10.
        error_cov_measurement = np.diag(sigma_v_sigma**2)
        error_cov_j_sqrt = np.diag(np.zeros_like(sigma_v_measurement))
        self.kin_likelihood = KinLikelihood(self.z_lens,
                                            self.z_source,
                                            sigma_v_measurement,
                                            j_mean_list,
                                            error_cov_measurement,
                                            error_cov_j_sqrt,
                                            normalized=True)
        self.ddt_gauss_likelihood = DdtGaussianLikelihood(**kwargs_ddt_gauss)

        self.ddt_gauss_kin_likelihood = DdtGaussKinLikelihood(
            self.z_lens,
            self.z_source,
            self.ddt_mean,
            self.ddt_sigma,
            sigma_v_measurement,
            j_mean_list,
            error_cov_measurement,
            error_cov_j_sqrt,
            sigma_sys_error_include=False)
        self.sigma_v_measurement = sigma_v_measurement
        self.error_cov_measurement = error_cov_measurement

    def test_log_likelihood(self):
        ddt, dd = 9, 0.9
        lnlog_tot = self.ddt_gauss_kin_likelihood.log_likelihood(
            ddt, dd, aniso_scaling=None, sigma_v_sys_error=None)
        lnlog_ddt = self.ddt_gauss_likelihood.log_likelihood(ddt, dd)
        lnlog_kin = self.kin_likelihood.log_likelihood(ddt,
                                                       dd,
                                                       aniso_scaling=None,
                                                       sigma_v_sys_error=None)
        npt.assert_almost_equal(lnlog_tot, lnlog_ddt + lnlog_kin, decimal=5)

    def test_ddt_measurement(self):
        ddt_mean, ddt_sigma = self.ddt_gauss_kin_likelihood.ddt_measurement()
        assert ddt_mean == self.ddt_mean
        assert ddt_sigma == self.ddt_sigma

    def test_sigma_v_measurement(self):

        sigma_v_measurement_, error_cov_measurement_ = self.ddt_gauss_kin_likelihood.sigma_v_measurement(
        )
        assert sigma_v_measurement_[0] == self.sigma_v_measurement[0]
        assert error_cov_measurement_[0, 0] == self.error_cov_measurement[0, 0]

        sigma_v_predict, error_cov_predict = self.ddt_gauss_kin_likelihood.sigma_v_prediction(
            self.ddt_mean, self.dd_mean, aniso_scaling=1)
        assert sigma_v_predict[0] == self.sigma_v_measurement[0]
        assert error_cov_predict[0, 0] == 0
Ejemplo n.º 7
0
class DdtGaussKinLikelihood(object):
    """
    class for joint kinematics and time delay likelihood assuming that they are independent
    Uses KinLikelihood and DdtHistLikelihood combined
    """
    def __init__(self,
                 z_lens,
                 z_source,
                 ddt_mean,
                 ddt_sigma,
                 sigma_v_measurement,
                 j_model,
                 error_cov_measurement,
                 error_cov_j_sqrt,
                 sigma_sys_error_include=False,
                 normalized=True):
        """

        :param z_lens: lens redshift
        :param z_source: source redshift
        :param ddt_mean: mean Ddt [Mpc] of time-delay and lens model likelihood
        :param ddt_sigma: 1-sigma uncertainty in Ddt
        :param sigma_v_measurement: numpy array, velocity dispersion measured
        :param j_model: numpy array of the predicted dimensionless dispersion on the IFU's
        :param error_cov_measurement: covariance matrix of the measured velocity dispersions in the IFU's
        :param error_cov_j_sqrt: covariance matrix of sqrt(J) of the model predicted dimensionless dispersion on the IFU's
        :param sigma_sys_error_include: bool, if True will include a systematic error in the velocity dispersion
         measurement (if sampled from), otherwise this sampled value is ignored.
        :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
        """
        self._ddt_gauss_likelihood = DdtGaussianLikelihood(z_lens,
                                                           z_source,
                                                           ddt_mean=ddt_mean,
                                                           ddt_sigma=ddt_sigma)
        self._kinlikelihood = KinLikelihood(
            z_lens,
            z_source,
            sigma_v_measurement,
            j_model,
            error_cov_measurement,
            error_cov_j_sqrt,
            sigma_sys_error_include=sigma_sys_error_include,
            normalized=normalized)
        self.num_data = self._ddt_gauss_likelihood.num_data + self._kinlikelihood.num_data

    def log_likelihood(self,
                       ddt,
                       dd,
                       aniso_scaling=None,
                       sigma_v_sys_error=None,
                       sigma_v_sys_offset=None):
        """

        :param ddt: time-delay distance
        :param dd: angular diameter distance to the deflector
        :param aniso_scaling: array of size of the velocity dispersion measurement or None, scaling of the predicted
         dimensionless quantity J (proportional to sigma_v^2) of the anisotropy model in the sampling relative to the
         anisotropy model used to derive the prediction and covariance matrix in the init of this class.
        :param sigma_v_sys_error: float (optional) added error on the velocity dispersion measurement in quadrature
        :param sigma_v_sys_offset: float (optional) for a fractional systematic offset in the kinematic measurement
         such that sigma_v = sigma_v_measured * (1 + sigma_v_sys_offset)
        :return: log likelihood given the single lens analysis
        """
        lnlikelihood = self._ddt_gauss_likelihood.log_likelihood(
            ddt) + self._kinlikelihood.log_likelihood(
                ddt,
                dd,
                aniso_scaling,
                sigma_v_sys_error=sigma_v_sys_error,
                sigma_v_sys_offset=sigma_v_sys_offset)
        return lnlikelihood

    def sigma_v_prediction(self, ddt, dd, aniso_scaling=1):
        """
        model prediction mean velocity dispersion vector and model prediction covariance matrix

        :param ddt: time-delay distance
        :param dd: angular diameter distance to the deflector
        :param aniso_scaling: array of size of the velocity dispersion measurement or None, scaling of the predicted
         dimensionless quantity J (proportional to sigma_v^2) of the anisotropy model in the sampling relative to the
         anisotropy model used to derive the prediction and covariance matrix in the init of this class.
        :return: model prediction mean velocity dispersion vector and model prediction covariance matrix
        """
        return self._kinlikelihood.sigma_v_prediction(ddt, dd, aniso_scaling)

    def sigma_v_measurement(self,
                            sigma_v_sys_error=None,
                            sigma_v_sys_offset=None):
        """

        :param sigma_v_sys_error: float (optional) added error on the velocity dispersion measurement in quadrature
        :param sigma_v_sys_offset: float (optional) for a fractional systematic offset in the kinematic measurement
         such that sigma_v = sigma_v_measured * (1 + sigma_v_sys_offset)
        :return: measurement mean (vector), measurement covariance matrix
        """
        return self._kinlikelihood.sigma_v_measurement(
            sigma_v_sys_error=sigma_v_sys_error,
            sigma_v_sys_offset=sigma_v_sys_offset)

    def ddt_measurement(self):
        """

        :return: mean, 1-sigma of the ddt inference/model measurement
        """
        return self._ddt_gauss_likelihood.ddt_measurement()