def lnlike(H0, z, zerr, pb_gal, distmu, diststd, distnorm, H0_min, H0_max,
           z_min, z_max, zerr_use, cosmo_use):
    if ((zerr_use == False) & (cosmo_use == False)):
        distgal = (c / 1000.) * z / H0
        like_gals = pb_gal * distnorm * norm(distmu,
                                             diststd).pdf(distgal) * z**2
        normalization = H0**3
    elif ((zerr_use == False) & (cosmo_use == True)):
        cosmo = FlatLambdaCDM(H0=H0, Om0=0.3, Tcmb0=2.725)
        distgal = cosmo.luminosity_distance(z).value  #in Mpc
        #like_gals = pb_gal * distnorm * norm(distmu, diststd).pdf(distgal)*distgal**2/cosmo.H(z).value
        like_gals = pb_gal * distnorm * norm(distmu, diststd).pdf(
            distgal) * distgal * distgal * (cosmo.comoving_distance(z).value +
                                            (1. + z) / cosmo.H(z).value)
        normalization = 1.
    elif ((zerr_use == True) & (cosmo_use == False)):
        ngals = z.shape[0]
        like_gals = np.zeros(ngals)
        z_s = np.arange(z_min, z_max, step=0.02)
        const = (c / 1000.) / H0
        normalization = H0**3
        for i in range(ngals):
            # Multiply the 2 Gaussians (redshift pdf and GW likelihood) with a change of variables into one Gaussian with mean Mu_new and std sigma_new
            #mu_new = (z[i]*diststd[i]*diststd[i]/(const*const)+ distmu[i]/const*zerr[i])/(diststd[i]*diststd[i]/(const*const)+zerr[i]*zerr[i])
            #sigma_new = (diststd[i]*diststd[i]*zerr[i]*zerr[i]/(const*const)/(diststd[i]*diststd[i]/(const*const)+ zerr[i]*zerr[i]))**0.5
            #like_gals[i]= pb_gal[i] * distnorm[i] * romb(gauss(mu_new, sigma_new, z_s) * z[i]*z[i], dx=0.02)
            like_gals[i] = pb_gal[i] * distnorm[i] * romb(
                gauss(z[i], zerr[i], z_s) *
                gauss(distmu[i], diststd[i], const * z_s) * z[i] * z[i],
                dx=0.02)
    else:
        ngals = z.shape[0]
        cosmo = FlatLambdaCDM(H0=H0, Om0=0.3, Tcmb0=2.725)
        like_gals = np.zeros(ngals)
        z_s = np.arange(z_min, z_max, step=0.02)
        normalization = 1.
        for i in range(ngals):
            dist_gal = cosmo.luminosity_distance(z[i]).value
            like_gals[i] = pb_gal[i] * distnorm[i] * romb(
                gauss(z[i], zerr[i], z_s) *
                gauss(distmu[i], diststd[i], dist_gal) * dist_gal * dist_gal /
                cosmo.H(z[i]).value,
                dx=0.02)

    lnlike_sum = np.log(np.sum(like_gals) / normalization)
    return lnlike_sum
Exemplo n.º 2
0
def diff_comoving_volume(z):
    cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Tcmb0=2.725)
    H0 = cosmo.H(0).value  # km / s / Mpc
    dl = cosmo.luminosity_distance(z).value
    lightspeed = c * 0.001  # convert from m/s to km/s
    ez = np.sqrt(cosmo.Om(0) * ((1 + z)**3) + (1 - cosmo.Om(0)))
    dv = 4 * np.pi * (lightspeed / H0) * (dl**2) / ((1 + z)**2) / ez
    dv = dv * ((0.001)**3)  # Mpc^3 to Gpc^3
    # Test case
    #dv_new = cosmo.differential_comoving_volume(3)
    #print (4 * np.pi * dv_new.value)
    return dv
Exemplo n.º 3
0
def calc_ps2d(cube, fmin=154, fmax=162, fov=2):
    f21cm = 1420.405751  # [MHz]
    cosmo = FlatLambdaCDM(H0=71, Om0=0.27)
    nf, ny, nx = cube.shape
    fc = (fmin + fmax) / 2
    zc = f21cm / fc - 1
    DM = cosmo.comoving_transverse_distance(zc).value  # [Mpc]
    
    pixelsize = fov / nx  # [deg]
    d_xy = DM * np.deg2rad(pixelsize)  # [Mpc]
    fs_xy = 1 / d_xy  # [Mpc^-1]
    
    dfreq = (fmax - fmin) / (nf-1)  # [MHz]
    c = ac.c.to("km/s").value
    H = cosmo.H(zc).value  # [km/s/Mpc]
    d_z = c * (1+zc)**2 * dfreq / H / f21cm  # [Mpc]
    fs_z = 1 / d_z  # [Mpc^-1]
    
    cubefft = fftpack.fftshift(fftpack.fftn(cube))
    ps3d = np.abs(cubefft) ** 2  # [K^2]
    norm1 = 1 / (nx * ny * nf)
    norm2 = 1 / (fs_xy**2 * fs_z)  # [Mpc^3]
    norm3 = 1 / (2*np.pi)**3
    ps3d *= (norm1 * norm2 * norm3)  # [K^2 Mpc^3]
    
    k_xy = 2*np.pi * fftpack.fftshift(fftpack.fftfreq(nx, d=d_xy))
    k_z  = 2*np.pi * fftpack.fftshift(fftpack.fftfreq(nf, d=d_z))
    k_perp = k_xy[k_xy >= 0]
    k_los  = k_z [k_z  >= 0]
    n_k_perp = len(k_perp)
    n_k_los  = len(k_los)
    ps2d = np.zeros(shape=(n_k_los, n_k_perp))

    eps = 1e-8
    ic_xy = (np.abs(k_xy) < eps).nonzero()[0][0]
    ic_z  = (np.abs(k_z)  < eps).nonzero()[0][0]
    p_xy = np.arange(nx) - ic_xy
    p_z  = np.abs(np.arange(nf) - ic_z)
    mx, my = np.meshgrid(p_xy, p_xy)
    rho = np.sqrt(mx**2 + my**2)
    rho = np.around(rho).astype(int)

    for r in range(n_k_perp):
        ix, iy = (rho == r).nonzero()
        for s in range(n_k_los):
            iz = (p_z == s).nonzero()[0]
            cells = np.concatenate([ps3d[z, iy, ix] for z in iz])
            ps2d[s, r] = cells.mean()
            
    return (ps2d, k_perp, k_los)
Exemplo n.º 4
0
class cosmology(object):  #{{{
    # some fundamental constants
    c0 = 2.99792458e5  # km/s
    GN = 4.30091e-9  # Mpc/Msun*(km/s)**2
    delta_c = 1.686
    hPl = 6.62607004e-34  # SI
    kBoltzmann = 1.38064852e-23  # SI

    def __init__(self, cosmo_dict={None}):  #{{{

        print(cosmo_dict)
        # basic cosmology parameters
        self.h = _set_param(cosmo_dict, 'h', 0.7)
        self.Om = _set_param(cosmo_dict, 'Om0', 0.3)
        self.Ob = _set_param(cosmo_dict, 'Ob0', 0.046)
        self.As = _set_param(cosmo_dict, 'As', 2.1e-9)
        self.pivot_scalar = _set_param(cosmo_dict, 'pivot_scalar',
                                       0.05 / self.h)
        self.w = _set_param(cosmo_dict, 'w', -1)
        self.ns = _set_param(cosmo_dict, 'ns', 0.97)
        self.Mnu = _set_param(cosmo_dict, 'Mnu', 0.)
        self.Neff = _set_param(cosmo_dict, 'Neff', 0.)
        self.TCMB = _set_param(cosmo_dict, 'TCMB', 2.726)
        self.P0 = _set_param(cosmo_dict, 'pressure_profile_P0', 18.1)

        # various definitions, should hopefully not need much change
        self.mass_def_initial = _set_param(cosmo_dict, 'mass_def_initial',
                                           '200m')
        self.mass_def_kappa_profile = _set_param(cosmo_dict,
                                                 'mass_def_profile', 'vir')
        self.mass_def_Tinker = _set_param(cosmo_dict, 'mass_def_Tinker',
                                          '200m')
        self.mass_def_Batt = _set_param(cosmo_dict, 'mass_def_Batt', '200c')
        self.r_out_def = _set_param(cosmo_dict, 'r_out_def', 'vir')
        self.r_out_scale = _set_param(cosmo_dict, 'r_out_scale', 2.5)
        self.concentration_model = _set_param(cosmo_dict,
                                              'concentration_model', 'duffy08')
        self.halo_profile = _set_param(cosmo_dict, 'halo_profile', 'nfw')
        self.HMF_fuction = _set_param(cosmo_dict, 'HMF_function', 'Tinker10')

        # derived quantities
        self.OL = 1. - self.Om  # dark energy
        self.Oc = self.Om - self.Ob  # (cold) dark matter
        self.H0 = 100. * self.h
        self.rhoM = self.Om * 2.7753e11

        self.bias_arr = None
        self.hmf_arr = None
        self.my_angular_diameter_distance = None
        self.my_angular_diameter_distance_z1z2 = None
        self.my_H = None
        self.my_comoving_distance = None
        self.astropy_cosmo = None

        self.initialized_colossus = False

        self.my_D = None
        self.my_k_integral = None
        self.my_PK = None
        self.my_PK_psi = None
        self.sigma8 = None

    #}}}
    def __initialize_astropy(self):  #{{{
        # adds an astropy object
        print 'Initializing astropy.'
        self.astropy_cosmo = FlatLambdaCDM(H0=self.H0 * u.km / u.s / u.Mpc,
                                           Tcmb0=self.TCMB * u.K,
                                           Om0=self.Om,
                                           Neff=self.Neff,
                                           m_nu=self.Mnu * u.eV,
                                           Ob0=self.Ob,
                                           name='my cosmology')

    @property
    def angular_diameter_distance(self):
        if self.astropy_cosmo is None:
            self.__initialize_astropy()
        if self.my_angular_diameter_distance is None:
            self.my_angular_diameter_distance = lambda z: (
                self.astropy_cosmo.angular_diameter_distance(z)).value * self.h
        return self.my_angular_diameter_distance

    @property
    def angular_diameter_distance_z1z2(self):
        if self.astropy_cosmo is None:
            self.__initialize_astropy()
        if self.my_angular_diameter_distance_z1z2 is None:
            self.my_angular_diameter_distance_z1z2 = lambda z1, z2: (
                self.astropy_cosmo.angular_diameter_distance_z1z2(
                    z1, z2)).value * self.h
        return self.my_angular_diameter_distance_z1z2

    @property
    def H(self):
        if self.astropy_cosmo is None:
            self.__initialize_astropy()
        if self.my_H is None:
            self.my_H = lambda z: (self.astropy_cosmo.H(z)).value / self.h
        return self.my_H

    @property
    def comoving_distance(self):
        if self.astropy_cosmo is None:
            self.__initialize_astropy()
        if self.my_comoving_distance is None:
            self.my_comoving_distance = lambda z: (
                self.astropy_cosmo.comoving_distance(z)).value * self.h
        return self.my_comoving_distance

    #}}}
    def __initialize_CAMB(self):  #{{{
        # adds linear matter power interpolator
        print 'Initializing CAMB.'
        pars = camb.CAMBparams()
        pars.set_cosmology(H0=self.H0,
                           ombh2=self.Ob * self.h**2.,
                           omch2=self.Oc * self.h**2.,
                           omk=0.,
                           mnu=self.Mnu,
                           standard_neutrino_neff=self.Neff,
                           nnu=self.Neff,
                           TCMB=self.TCMB)
        pars.InitPower.set_params(ns=self.ns,
                                  As=self.As,
                                  pivot_scalar=self.pivot_scalar)
        pars.NonLinear = model.NonLinear_none
        #        print(pars)
        self.my_PK = get_matter_power_interpolator(pars,
                                                   zmin=0.,
                                                   zmax=6.,
                                                   nz_step=150,
                                                   kmax=101.,
                                                   nonlinear=False,
                                                   hubble_units=True,
                                                   k_hunit=True).P
        pars.set_matter_power(redshifts=[0.], kmax=20.)
        results = camb.get_results(pars)
        self.sigma8 = results.get_sigma8()[0]
        print 'sigma8 = ' + str(self.sigma8)

    def PK(self, z, k):
        if self.my_PK is None:
            self.__initialize_CAMB()
        return self.my_PK(z, k)

    def __initialize_nonlin_CAMB(self):
        print 'Initializing nonlinear CAMB.'
        parsnl = camb.CAMBparams()
        parsnl.set_cosmology(H0=self.H0,
                             ombh2=self.Ob * self.h**2.,
                             omch2=self.Oc * self.h**2.,
                             omk=0.,
                             mnu=self.Mnu,
                             standard_neutrino_neff=self.Neff,
                             nnu=self.Neff,
                             TCMB=self.TCMB)
        parsnl.InitPower.set_params(ns=self.ns,
                                    As=self.As,
                                    pivot_scalar=self.pivot_scalar)
        #        parsnl.NonLinear = model.NonLinear_both
        parsnl.set_matter_power(redshifts=[0.], kmax=101.)
        self.my_PK_psi = get_matter_power_interpolator(
            parsnl,
            zmin=0.,
            zmax=1100.,
            nz_step=150,
            kmax=101.,
            nonlinear=True,
            hubble_units=True,
            k_hunit=True,
            var1=model.Transfer_Weyl,
            var2=model.Transfer_Weyl).P

    def PK_psi(self, z, k):
        if self.my_PK_psi is None:
            self.__initialize_nonlin_CAMB()
        return self.my_PK_psi(z, k)

    def D(self, z):
        if self.my_PK is None:
            self.__initialize_CAMB()
        if self.my_D is None:
            self.my_D = lambda z: np.sqrt(
                self.PK(z, 0.01) / self.PK(0., 0.01))  # growth function
        return self.my_D(z)

    def k_integral(self):
        if self.my_PK is None:
            self.__initialize_CAMB()
        if self.my_k_integral is None:
            integrand = lambda k: (1. / (2. * np.pi)) * k * self.PK(0., k)
            self.my_k_integral, _ = quad(integrand, 1.e-10, 100., limit=100)
        return self.my_k_integral

    #}}}
    def _initialize_colossus(self):  #{{{
        # adds colossus object
        print 'Initializing Colossus.'
        if self.sigma8 is None:
            self.__initialize_CAMB()
        colossus_cosmology.setCosmology(
            'my cosmology', {
                'flat': True,
                'H0': self.H0,
                'Om0': self.Om,
                'Ob0': self.Ob,
                'sigma8': self.sigma8,
                'ns': self.ns,
                'de_model': 'w0',
                'w0': self.w,
                'Tcmb0': self.TCMB,
                'Neff': self.Neff
            })
        self.initialized_colossus = True

    #}}}
    def virial_radius(self, M, z, massdef):  #{{{
        if not self.initialized_colossus:
            self._initialize_colossus()
        _, rvir, _ = mass_adv.changeMassDefinitionCModel(
            M,
            z,
            massdef,
            'vir',
            profile=self.halo_profile,
            c_model=self.concentration_model)
        return rvir * 1e-3  # Mpc/h

    #}}}
    def virial_radius_small_masses(self, M, z, massdef):  #{{{
        if not self.initialized_colossus:
            self._initialize_colossus()
        _, rvir, _ = mass_adv.changeMassDefinitionCModel(
            M,
            z,
            massdef,
            'vir',
            profile=self.halo_profile,
            c_model=self.small_mass_concentration_model)
        return rvir * 1e-3  # Mpc/h

    #}}}
    def convert_mass(self, M, z, massdefin, massdefout):  #{{{
        if massdefin == massdefout:
            return M
        if not self.initialized_colossus:
            self._initialize_colossus()
        Mout, _, _ = mass_adv.changeMassDefinitionCModel(
            M,
            z,
            massdefin,
            massdefout,
            profile=self.halo_profile,
            c_model=self.concentration_model)
        return Mout

    #}}}
    @staticmethod
    @jit(nopython=True)
    def __hmf_Tinker2010(nu, z):  #{{{
        # Eq 8 of Tinker10, parameters from Table 4
        z1 = z
        if z1 > 3.: z1 = 3
        # HMF only calibrated below z = 3, use the value for z = 3 at higher redshifts
        beta = 0.589 * (1. + z1)**(0.20)
        phi = -0.729 * (1. + z1)**(-0.08)
        eta = -0.243 * (1. + z1)**(0.27)
        gamma = 0.864 * (1. + z1)**(-0.01)
        alpha = 0.368
        return alpha * (1. +
                        (beta * nu)**(-2. * phi)) * nu**(2. * eta) * np.exp(
                            -0.5 * gamma * nu**2)

    #}}}
    @staticmethod
    @jit(nopython=True)
    def __hmf_Tinker2008(sigma):  #{{{
        B = 0.482
        d = 1.97
        e = 1.
        f = 0.51
        g = 1.228
        return B * ((sigma / e)**(-d) + sigma**(-f)) * np.exp(-g / sigma**2.)

    #}}}
    @staticmethod
    @jit(nopython=True)
    def __hmf_ShethTormen(nu):  #{{{
        A = 0.3222
        a = 0.707
        p = 0.3
        return A * (2. * a / np.pi)**0.5 * nu * (1. +
                                                 (nu**2. / a)**p) * np.exp(
                                                     -a * nu**2. / 2.)

    #}}}
    @staticmethod
    @jit(nopython=True)
    def __bz_Tinker2010(nu):  #{{{
        # Eq 6 of Tinker10, with parameters from Table 2
        Delta = 200.
        y = np.log10(Delta)
        A = 1.0 + 0.24 * y * np.exp(-(4. / y)**4)
        a = 0.44 * y - 0.88
        B = 0.183
        b = 1.5
        C = 0.019 + 0.107 * y + 0.19 * np.exp(-(4. / y)**4)
        c = 2.4
        return 1. - A * nu**a / (nu**a + 1.686**a) + B * nu**b + C * nu**c

    #}}}
    @staticmethod
    @jit(nopython=True)
    def __window_function_FT(x):  #{{{
        return 3. * (np.sin(x) - x * np.cos(x)) / x**3.

    #}}}
    @staticmethod
    def __chi(x):  #{{{
        return ((x**2 - 3.) * np.sin(x) + 3. * x * np.cos(x)) / x**3

    #}}}
    def __sigma(self, M, z):  #{{{
        RM = (3. * M / (4. * np.pi * self.rhoM))**(1. / 3.)
        integrand = lambda k: (1. / (2. * np.pi**2)) * (k**2 * self.PK(
            z, k) * (cosmology.__window_function_FT(k * RM))**2)
        sigmasq, _ = quad(integrand, 1e-10, 100., limit=100)
        return np.sqrt(sigmasq)

    #}}}
    def __chi_integral(self, M, z):  #{{{
        # computes the chi-integral [which is close to the derivative of sigma]
        RM = (3. * M / (4. * np.pi * self.rhoM))**(1. / 3.)
        integrand = lambda lk: (1. * np.log(10.) / np.pi**2) * (
            (10.**
             (lk))**3 * self.PK(z, (10.**lk)) * cosmology.__window_function_FT(
                 (10.**lk) * RM) * cosmology.__chi((10.**lk) * RM))
        integral, _ = quad(integrand, -10., 2., limit=100)
        return integral

    #}}}
    def dndM(self, M, z, s, chi_int, HMF_fuction):  #{{{
        if HMF_fuction is 'Tinker10':
            f = cosmology.__hmf_Tinker2010(cosmology.delta_c / s, z)
            return -cosmology.delta_c * self.rhoM * f * chi_int / (2. * s**3. *
                                                                   M**2.)
        elif HMF_fuction is 'Tinker08':
            g = cosmology.__hmf_Tinker2008(s)
            return -self.rhoM * g * chi_int / (2. * s**2. * M**2.)
        elif HMF_fuction is 'ShethTormen':
            g = cosmology.__hmf_ShethTormen(cosmology.delta_c / s)
            return -self.rhoM * g * chi_int / (2. * s**2. * M**2.)
        else:
            raise RuntimeError('Unknown HMF function in cosmology.dndM')

    #}}}
    def create_HMF_and_bias(self, path, numerics, pardict={None}):  #{{{
        if os.path.isfile(path +
                          'HMF_and_bias.npz') and not numerics.debugging:
            raise RuntimeError('HMF_and_bias have already been computed.')
        Npoints_M = numerics.Npoints_M
        Npoints_z = numerics.Npoints_z
        logM_grid = numerics.logM_grid
        z_grid = numerics.z_grid
        self.hmf_arr = np.empty((Npoints_M, Npoints_z))
        self.bias_arr = np.empty((Npoints_M, Npoints_z))
        for ii in xrange(Npoints_M):
            start = time()
            for jj in xrange(Npoints_z):
                z = z_grid[jj]
                M = self.convert_mass(10.**logM_grid[ii], z,
                                      self.mass_def_initial,
                                      self.mass_def_Tinker)
                sigma = self.__sigma(M, z)
                chi_int = self.__chi_integral(M, z)
                self.hmf_arr[ii, jj] = self.dndM(M, z, sigma, chi_int,
                                                 self.HMF_fuction)
                self.bias_arr[ii, jj] = cosmology.__bz_Tinker2010(
                    cosmology.delta_c / sigma)
            end = time()
            if (ii % 4 == 0) and numerics.verbose:
                print str((end - start) / 60. *
                          (Npoints_M -
                           ii)) + ' minutes remaining in create_HMF_and_bias.'
        np.savez(
            path + 'HMF_and_bias.npz',
            hmf=self.hmf_arr,
            bias=self.bias_arr,
        )
Exemplo n.º 5
0
    '''
    Modified GW luminosity distance
    '''
    cosmo = FlatLambdaCDM(H0=H0, Om0=Om0GLOB)
    return (cosmo.luminosity_distance(z).value) * Xi(z, Xi0, n=n)


def Xi(z, Xi0, n):

    return Xi0 + (1 - Xi0) / (1 + z)**n


zGridGLOB = np.logspace(start=-10, stop=5, base=10, num=1000)
dLGridGLOB = cosmo70GLOB.luminosity_distance(zGridGLOB).value
dcomGridGLOB = cosmo70GLOB.comoving_distance(zGridGLOB).value
HGridGLOB = cosmo70GLOB.H(zGridGLOB).value
from scipy import interpolate
dcom70fast = interpolate.interp1d(zGridGLOB,
                                  dcomGridGLOB,
                                  kind='cubic',
                                  bounds_error=False,
                                  fill_value=(0, np.NaN),
                                  assume_sorted=True)
dL70fast = interpolate.interp1d(zGridGLOB,
                                dLGridGLOB,
                                kind='cubic',
                                bounds_error=False,
                                fill_value=(0, np.NaN),
                                assume_sorted=True)

H70fast = interpolate.interp1d(zGridGLOB,
Exemplo n.º 6
0
                                                 DictFormatter)
import matplotlib.pyplot as plt
from matplotlib import pyplot, transforms

font = {'family': 'STIXGeneral', 'size': 22}

matplotlib.rc('font', **font)

redshift = 2.0
galfile = 'gal_iz30_atlascat_vel.cat'
x, y, z, vy, loglum = np.loadtxt(galfile, unpack=True)

zc = z < 30

aexp = 1. / (1 + redshift)
Hz = cosmo.H(redshift).value

sz = 1e-2
PlotName = 'sigmax'  # zspace # sigmax # real

y_ = y if PlotName == 'real' else y + vy / (aexp * Hz)  # zspace

ppname = PlotName

if PlotName == 'sigmax':
    ppname = 'zspace_sigmaz%.4f' % sz

pname = '%s_andrea2' % ppname

ym = np.mean(y_[zc])
xm = np.mean(x[zc])
class Cosmos_flat:

    def __init__(self, Omega_m0, H0):
        self.Omega_m0 = Omega_m0
        self.H0 = H0
        self.h0 = H0/100
        self.H_z = 0
        self.cosmos = FlatLambdaCDM(H0, Om0=Omega_m0)
        self.nfw_galsim = None
        self.nfw_com_dist = None
        self.nfw_z = None
        self.delta_sigma_coeff_1 = 1662916.5401756007
        self.delta_sigma_coeff_2 = 0

        self.NFW_rho_profile_delta_c = 0
        self.NFW_rho_profile_concentration = 0
        self.uni_crit_dens_z = 0
        self.density_radius_scale = 0
        self.NFW_radius = 0


    def com_distance(self, z, h_unit=True):
        if h_unit:
            hu = self.h0
        else:
            hu = 1
        return self.cosmos.comoving_distance(z).value * hu


    def NFW_profile_galsim(self, halo_position, Mass, conc, z, h_unit=True):
        """

        :param halo_position: (ra, dec) arcsec
        :param Mass:
        :param conc:
        :param z:
        :param h_unit:
        :return:
        """
        self.nfw_galsim = galsim.NFWHalo(Mass, conc, z, halo_position, self.Omega_m0, 1 - self.Omega_m0)
        self.nfw_com_dist = self.com_distance(z, h_unit=h_unit)
        self.nfw_z = z
        self.delta_sigma_coeff_2 = self.delta_sigma_coeff_1/self.nfw_com_dist/(1 + self.nfw_z)
        self.H_z = self.cosmos.H(z)

    def NFW_rho_profile(self, log_10_Mass, conc, z, density_radius_scale=200, h_unit=True):
        # rho(r) = delta_c*rho_c(z)/(r/r_s)/(1+r/r_s)^2

        # H(z)
        self.H_z = self.cosmos.H(z)
        # to define the halo radius above n*critical_mass_density(z)
        self.density_radius_scale = density_radius_scale
        # concentration
        self.NFW_rho_profile_concentration = conc
        # delta_c
        self.NFW_rho_profile_delta_c = density_radius_scale*conc**3/3/(numpy.log(1+conc) - conc/(1+conc))
        # h_0^2 * M_sum / Mpc^{-3}
        self.uni_crit_dens_z = 3*3.085677581/8/numpy.pi/6.67408/1.9885*self.H_z/100

        self.nfw_com_dist = self.com_distance(z, h_unit=h_unit)


    def get_shear(self, src_ra, src_dec, src_z, reduced=False):

        com_dist_src = self.com_distance(src_z)
        crit_coeff = self.delta_sigma_coeff_2*com_dist_src / (com_dist_src - self.nfw_com_dist)
        gamma1, gamma2 = self.nfw_galsim.getShear((src_ra, src_dec), src_z, reduced=reduced)
        delta_sigma = numpy.sqrt(gamma1 ** 2 + gamma2 ** 2) * crit_coeff
        return gamma1, gamma2, delta_sigma


    def get_kappa(self, ra, dec, z):
        return self.nfw_galsim.getConvergence((ra, dec), z)

    def get_sigma_crit(self, src_z, h_unit=True):
        com_dist_src = self.com_distance(src_z, h_unit)
        return self.delta_sigma_coeff_2*com_dist_src / (com_dist_src - self.nfw_com_dist)
Exemplo n.º 8
0
    def load(self, zMax=0.1, zErrFac=1, comovingNumberDensityGoal=0.1):

        if self.verbose:
            print('Sampling homogenously distributed galaxies...')
        from astropy.cosmology import FlatLambdaCDM
        fiducialcosmo = FlatLambdaCDM(H0=70.0, Om0=0.3)

        vol = 4 * np.pi * fiducialcosmo.comoving_distance(zMax).value**3 / 3
        nGals = np.int(vol * comovingNumberDensityGoal)

        dg = pd.DataFrame(columns=[
            '', 'theta', 'phi', 'z', 'z_err', 'z_lowerbound', 'z_lower',
            'z_upper', 'z_upperbound'
        ])

        dg.loc[:, "theta"] = np.arccos(1 - 2 * np.random.uniform(size=nGals))
        dg.loc[:, "phi"] = 2 * np.pi * np.random.uniform(size=nGals)

        # the following calculation of redshifts is independent of H0
        dmax = fiducialcosmo.comoving_distance(zMax).value
        # sample d_com^2 dd_com from 0 to dmax. CDF is p = d^3/dmax^3, quantile func is dmax*(p**(1/3))
        ds = dmax * np.random.uniform(size=nGals)**(1. / 3)

        z_table = np.linspace(0, zMax, 1000)
        d_table = fiducialcosmo.comoving_distance(z_table).value
        from scipy import interpolate
        redshFromdcom = interpolate.interp1d(d_table, z_table, kind='cubic')

        dg.loc[:, "z"] = redshFromdcom(ds)

        # remove some galaxies

        def compl_of_z(z, steep=5 / zMax, zstar=0.4 * zMax):
            return 0.5 * (1 - np.tanh((z - zstar) * steep))

        n = 100
        zedges = np.linspace(0, zMax, n)

        if self.verbose:
            print(
                'Removing galaxies according to prescribed incompleteness function...'
            )
        for i, z in enumerate(zedges):
            if i == n - 1:
                z2 = zMax + 0.0001
            else:
                z2 = zedges[i + 1]

            mask = (dg.theta.to_numpy() < np.pi / 2) & (
                dg.z.to_numpy() <= z2) & (dg.z.to_numpy() > z)

            nRemove = np.int(
                np.sum(mask) * (1 - compl_of_z(z + 0.5 * zMax / n)))

            # indices of all relevant galaxies in this volume
            idx = np.nonzero(mask)[0]

            if nRemove > 0:

                # sample nRemove *unique* elements from idx
                #import random
                #idxidx = random.sample(range(len(idx)), nRemove)
                #idxDrop = idx[idxidx]
                np.random.shuffle(idx)
                idxDrop = set(idx[:nRemove])

                # remove them - anecdocially, taking is faster than dropping

                idxKeep = set(range(dg.shape[0])) - idxDrop
                dg = dg.take(list(idxKeep))

        dg.loc[:, "w"] = 1

        if self.verbose:
            print("Sample observations from truncated gaussian likelihood...")
        zerr = np.ones(len(dg.z))
        #zerr = dg.loc[:,"z"].to_numpy()/4
        dg.loc[:, "z_err"] = zErrFac * np.clip(zerr, a_min=None, a_max=0.01)
        dg.loc[:, "z"] = sample_trunc_gaussian(mu=dg.loc[:, "z"],
                                               sigma=dg.loc[:, "z_err"],
                                               lower=0,
                                               size=1)

        ####
        from astropy.cosmology import FlatLambdaCDM
        fiducialcosmo = FlatLambdaCDM(H0=70.0, Om0=0.3)
        area = 2 * np.pi
        zmax1 = 1.0001 * np.max(
            dg[(dg.theta.to_numpy() < np.pi / 2)].z.to_numpy())
        zmax2 = 1.0001 * np.max(
            dg[(dg.theta.to_numpy() > np.pi / 2)].z.to_numpy())
        self.zedges1 = np.linspace(0, zmax1, 90 + 1)
        self.zedges2 = np.linspace(0, zmax2, 90 + 1)
        z1 = self.zedges1[:-1]
        z2 = self.zedges1[1:]
        vol1 = area * (fiducialcosmo.comoving_distance(z2).value**3 -
                       fiducialcosmo.comoving_distance(z1).value**3) / 3
        self.zcenters1 = 0.5 * (z1 + z2)
        z1 = self.zedges2[:-1]
        z2 = self.zedges2[1:]
        self.zcenters2 = 0.5 * (z1 + z2)
        vol2 = area * (fiducialcosmo.comoving_distance(z2).value**3 -
                       fiducialcosmo.comoving_distance(z1).value**3) / 3
        coarseden1 = np.zeros(vol1.shape)
        coarseden2 = np.zeros(vol2.shape)

        ###

        if not self._useDirac:

            dg.loc[:, "z_lower"] = dg.loc[:, "z"]
            dg.loc[:, "z_lowerbound"] = dg.loc[:, "z"]
            dg.loc[:, "z_upper"] = dg.loc[:, "z"]
            dg.loc[:, "z_upperbound"] = dg.loc[:, "z"]

            L = 0
            block = 10000

            if self.verbose:
                print("Computing galaxy posteriors...")

            while True:

                R = L + block

                # evaluate likelihood at fixed observations on sensible grids in mu
                # note that sigma depends on mu as well.

                if R >= len(dg):
                    lowerbound = dg.z.to_numpy(
                    )[L:] - 7 * dg.z_err.to_numpy()[L:]
                    lowerbound[lowerbound < 0] = 0
                    upperbound = dg.z.to_numpy(
                    )[L:] + 7 * dg.z_err.to_numpy()[L:]
                else:
                    lowerbound = dg.z.to_numpy(
                    )[L:R] - 7 * dg.z_err.to_numpy()[L:R]
                    lowerbound[lowerbound < 0] = 0
                    upperbound = dg.z.to_numpy(
                    )[L:R] + 7 * dg.z_err.to_numpy()[L:R]

                #lowerbound = np.zeros(lowerbound.shape)
                #upperbound = np.ones(upperbound.shape)*0.1
                mugrid = np.linspace(lowerbound, upperbound, 400).T
                # remove leading zeros (if lowerbound = 0), sigma=0 would follow and is ill-defined
                mugrid = mugrid[:, 1:]
                #  copy the algorithm to compute the error from what used to be mu
                # (despite being called z), the true redshift
                #sigmagrid = mugrid/4
                sigmagrid = np.ones(mugrid.shape)
                sigmagrid = zErrFac * np.clip(
                    sigmagrid, a_min=None, a_max=0.01)

                # fix observation to dg.z, eval as function of truth mu

                if R >= len(dg):
                    pdfs = trunc_gaussian_pdf(dg.loc[:, "z"].to_numpy()[L:],
                                              mu=mugrid,
                                              sigma=sigmagrid,
                                              lower=0)
                else:
                    pdfs = trunc_gaussian_pdf(dg.loc[:, "z"].to_numpy()[L:R],
                                              mu=mugrid,
                                              sigma=sigmagrid,
                                              lower=0)

                # multiply by prior
                #pdfs *= mugrid**2

                rsqdrdz = fiducialcosmo.comoving_distance(
                    mugrid).value**2 / fiducialcosmo.H(mugrid).value
                pdfs *= rsqdrdz
                #  pdfs *= mugrid**2
                # fit keelin pdfs to this posterior. normalization is not necessary

                ####
                if R >= len(dg):
                    mask1 = (dg.theta.to_numpy()[L:] < np.pi / 2)
                    mask2 = (dg.theta.to_numpy()[L:] > np.pi / 2)
                else:
                    mask1 = (dg.theta.to_numpy()[L:R] < np.pi / 2)
                    mask2 = (dg.theta.to_numpy()[L:R] > np.pi / 2)

                # actually prior is not constant in comoving for the part that drops!
                pdfs[mask1, :] = pdfs[mask1, :] * compl_of_z(mugrid[mask1, :])

                cdfs = np.cumsum(pdfs, axis=1)
                pdfs = pdfs / ((cdfs[:, -1])[:, np.newaxis])
                cdfs = cdfs / ((cdfs[:, -1])[:, np.newaxis])

                fits = fit_bounded_keelin_3(0.16, grids=mugrid, pdfs=pdfs)

                for i in range(len(self.zcenters2)):
                    maskz = (self.zedges1[i] < mugrid) & (mugrid <
                                                          self.zedges1[i + 1])
                    mask = mask1[:, np.newaxis] & maskz
                    coarseden1[i] += np.sum(pdfs[mask]) / vol1[i]
                    maskz = (self.zedges2[i] < mugrid) & (mugrid <
                                                          self.zedges2[i + 1])
                    mask = mask2[:, np.newaxis] & maskz
                    coarseden2[i] += np.sum(pdfs[mask]) / vol2[i]
                ###

                if R >= len(dg):

                    dg.iloc[L:, dg.columns.get_loc("z_lowerbound")] = fits[:,
                                                                           0]
                    dg.iloc[L:, dg.columns.get_loc("z_lower")] = fits[:, 1]
                    dg.iloc[L:, dg.columns.get_loc("z")] = fits[:, 2]
                    dg.iloc[L:, dg.columns.get_loc("z_upper")] = fits[:, 3]
                    dg.iloc[L:, dg.columns.get_loc("z_upperbound")] = fits[:,
                                                                           4]
                    break
                else:
                    dg.iloc[L:R, dg.columns.get_loc("z_lowerbound")] = fits[:,
                                                                            0]
                    dg.iloc[L:R, dg.columns.get_loc("z_lower")] = fits[:, 1]
                    dg.iloc[L:R, dg.columns.get_loc("z")] = fits[:, 2]
                    dg.iloc[L:R, dg.columns.get_loc("z_upper")] = fits[:, 3]
                    dg.iloc[L:R, dg.columns.get_loc("z_upperbound")] = fits[:,
                                                                            4]
#                dg.z[L:R] = fits[:, 2]
#                dg.z_lower[L:R]   = fits[:,1]
#                dg.z_upper[L:R]   = fits[:,3]
#                dg.z_lowerbound[L:R]   = fits[:,0]
#                dg.z_upperbound[L:R]   = fits[:,4]

#        mask = dg.z_lower < 1e-5
#        dg.loc[mask, "z_lower"] = dg.z[mask]*0.5
#        dg.loc[mask, "z_lowerbound"] = 0.0

                L += block

        self.compl1 = coarseden1 / 0.05
        self.compl2 = coarseden2 / 0.05

        dg.loc[:,
               "pix" + str(self._nside)] = hp.ang2pix(self._nside, dg.theta,
                                                      dg.phi)

        self.data = self.data.append(dg, ignore_index=True)
                  sigma_8=sigma_val,
                  z=boxRedshift,
                  delta_h=DeltaVir_bn98(boxRedshift),
                  delta_wrt='mean',
                  Mmin=7,
                  Mmax=16.5)

f_BH = lambda sigma, A, a, p, q: A * (2. / n.pi)**(
    0.5) * (1 + (sigma**2. / (a**delta_c * 2.))**
            (p)) * (delta_c * a**0.5 / sigma)**(q) * n.e**(-a * delta_c**2. /
                                                           (2. * sigma**2.))

X = n.arange(-0.6, 0.5, 0.01)  #n.log10(1./sigma)
sigma = 10**-X

hz = cosmo.H(boxRedshift).value / 100.
# m sigma relation using the sigma8 corrected power spectrum
m2sigma = interp1d(hf.M, hf.sigma)
# m nu relation: nu = (delta_c / sigma_m)**2
m2nu = interp1d(hf.M, hf.nu)
# jacobian
toderive = interp1d(n.log(hf.M), n.log(hf.sigma))
mass = hf.M[100:-100]
dlnsigmadlnm = derivative(toderive, n.log(mass))
rhom_units = cosmo.Om(boxRedshift) * cosmo.critical_density(boxRedshift).to(
    u.solMass / (u.Mpc)**3.)  #/(cosmo.h)**2.
# in units (Msun/h) / (Mpc/h)**3
rhom = rhom_units.value  # hf.mean_density#/(hz)**2.

ftC16 = f_BH(hf.sigma[100:-100], 0.279, 0.908, 0.671, 1.737)
Exemplo n.º 10
0
#plt.xlabel("Separation "+"({0.unit:s})".format(1.*SepUnit))
#plt.show()

closematch_zD = d2d < (1*u.arcsec)
notmatch_zD = np.invert(closematch_zD)

print('%i zDeep galaxies are also measured by MOSDEF' % sum(1 for i in closematch_zD if i))

cosmos = cosmos[notmatch_zD]
zDeepCoord = zDeepCoord[notmatch_zD]
print('This leaves %i zDeep galaxies in our sample' % len(cosmos))

fig, ax = plt.subplots()
binwidth=0.025
ax.hist(cosmos['z_spec'],bins=np.arange(min(cosmos['z_spec']),
                                               max(cosmos['z_spec']) + binwidth, binwidth))
ax.set_xlabel(r'$z_{spec}$')
ax.set_ylabel('N')
plt.show()'''

comdist0 = cosmo.comoving_distance(2.15)
binfac = 2.
dcomdist_dz = 2997. / cosmo.H(2.35)
xpos = comdist0 * (cosmos['ra'] - ra0) / np.cos(
    0.5 * np.abs(cosmos['dec'] + dec0) * np.pi / 180.) * binfac * np.pi / 180.
ypos = comdist0 * (cosmos['dec'] - dec0) * binfac * np.pi / 180.
zpos = (cosmos['z_spec'] - zmin) * dcomdist_dz * binfac

in_vol = np.where((xpos.value > 1) & (xpos.value < 35) & (ypos.value > 1)
                  & (ypos.value < 47) & (zpos.value > 1) & (zpos.value < 679))
print np.shape(in_vol)
Exemplo n.º 11
0
    def include_vol_prior(self, df):
        batchSize = 10000
        nBatches = max(int(len(df) / batchSize), 1)

        if self.verbose:
            print("Computing galaxy posteriors...")

        from keelin import convolve_bounded_keelin_3
        from astropy.cosmology import FlatLambdaCDM
        fiducialcosmo = FlatLambdaCDM(H0=70.0, Om0=0.3)
        zGrid = np.linspace(0, 1.4 * np.max(df.z_upperbound), 500)
        jac = fiducialcosmo.comoving_distance(
            zGrid).value**2 / fiducialcosmo.H(zGrid).value

        from scipy import interpolate
        func = interpolate.interp1d(zGrid, jac, kind='cubic')

        def convolve_batch(df, func, batchId, nBatches):
            N = len(df)
            # actual batch size, different from batchSize only due to integer rounding
            n = int(N / nBatches)
            start = n * batchId
            stop = n * (batchId + 1)
            if batchId == nBatches - 1:
                stop = N

            batch = df.iloc[start:stop]

            if self.verbose:
                if batchId % 100 == 0:
                    print("Batch " + str(batchId) + " of " + str(nBatches))

            ll = batch.z_lowerbound.to_numpy()
            l = batch.z_lower.to_numpy()
            m = batch.z.to_numpy()
            u = batch.z_upper.to_numpy()
            uu = batch.z_upperbound.to_numpy()

            return convolve_bounded_keelin_3(func,
                                             0.16,
                                             l,
                                             m,
                                             u,
                                             ll,
                                             uu,
                                             N=1000)

        res = np.vstack(
            parmap(lambda b: convolve_batch(df, func, b, nBatches),
                   range(nBatches)))

        mask = (res[:, 0] >= res[:, 1]) | (res[:, 1] >= res[:, 2]) | (
            res[:, 2] >= res[:, 3]) | (res[:, 3] >= res[:, 4]) | (res[:, 0] <
                                                                  0)

        if self.verbose:
            print(
                'Removing ' + str(np.sum(mask)) +
                ' galaxies with unfeasible redshift pdf after r-squared prior correction.'
            )

        df.z_lowerbound = res[:, 0]
        df.z_lower = res[:, 1]
        df.z = res[:, 2]
        df.z_upper = res[:, 3]
        df.z_upperbound = res[:, 4]

        df = df[~mask]

        return df
Exemplo n.º 12
0
    2.78, 1.87, 1.45, 1.19, 1.01, 0.87, 0.77, 0.76, 0.88, 0.91, 0.91, 0.91,
    1.00, 1.17, 1.50, 2.36, 3.62, 4.79
])
sigma_H_s_rel_ary = .01 * np.array([
    5.34, 3.51, 2.69, 2.20, 1.85, 1.60, 1.41, 1.35, 1.42, 1.41, 1.38, 1.36,
    1.46, 1.66, 2.04, 3.15, 4.87, 6.55
])

cosmo = FlatLambdaCDM(H0=69, Om0=0.3, m_nu=20 * u.meV)

s = 150 * u.Mpc  # BAO scale

# Fiducial values for dA/s and Hs
dA_div_s_ary = np.array([(cosmo.angular_diameter_distance(z) / s).value
                         for z in z_ary])
H_s_ary = np.array([(cosmo.H(z) * s / const.c * 1000).value for z in z_ary])

# Absolute errors on dA/s and Hs
sigma_dA_div_s_ary = sigma_dA_div_s_rel_ary * dA_div_s_ary
sigma_H_s_ary = sigma_H_s_rel_ary * H_s_ary

# Defines dA and H arrays
da = dA_div_s_ary * s.value
hh = H_s_ary / s.value
zz = z_ary

# Defines absolute error arrays
err_da = sigma_dA_div_s_ary * s.value
err_hh = sigma_H_s_ary / s.value
err_zz = np.zeros(len(z_ary))
Exemplo n.º 13
0
ax1.set_yticks(rc[::5])
ax1.set_yticklabels(n.round(rc[::5], 2))
ax1.grid()
ax2 = ax1.twinx()
ax2.plot(zs, rc, 'k')
ax2.set_ylabel('log10(M collapsing)')
ax2.set_yticks(rc[::5])
ax2.set_yticklabels(n.round(n.log10(mc[::5]), 2))
p.savefig(join(dir, "spherical-collapse-r.png"))
p.clf()

n.savetxt(join("..", "data", "sc-relation.txt"),
          n.transpose([zs, dc, mc, rc]),
          header="z delta_c mvir_star rvir_star")

hrr = (cosmo.H(data['redshift']).value / 100. / cosmo.h)**3.
#h2.mean_density0

# x coordinates definition
logsig = n.log10(data['sigmaM'])  #
lognu = n.log10(data['nuM'])
log_mvir = (data["log_" + qty + "_min"] + data["log_" + qty + "_max"]) / 2.
mvir = 10**log_mvir

# y coordinates
log_MF = n.log10(mvir * data["dNdVdlnM_" + cos] / data["rhom"])
log_MF_c = n.log10(data["dNdVdlnM_" + cos + "_c"])

ff = mvir * data["dNdVdlnM_" + cos] / data["rhom"] / abs(
    data["dlnsigmaM1_o_dlnM"]) * hrr
ff_nu = data['nuM'] * ff