def plot_bf(i, args, bfpath, savepath=None):
    cosmo, h, Omega_m = get_cosmo(i)
    M = args['M']
    params = np.loadtxt(bfpath)
    colors = [
        plt.get_cmap("seismic")(ci) for ci in np.linspace(1.0, 0.0, len(zs))
    ]
    fig, ax = plt.subplots(2, sharex=True)
    for j in range(len(zs)):
        Mlo, Mhi, N, Mave = AD.building_box_binned_mass_function(i, j).T
        edge = 10**np.concatenate((Mlo, Mhi[-1:]))
        cov = AD.building_box_binned_mass_function_covariance(i, j)
        err = np.sqrt(cov.diagonal())
        s2 = args['sigma2s'][j]
        s2t = args['sigma2tops'][j]
        s2b = args['sigma2bots'][j]
        d, e, f, g = model_swap(params, args, x[j])
        dndM = massfunction._dndM_sigma2_precomputed(M, s2, s2t, s2b, Omega_m,
                                                     d, e, f, g)
        Nmodel = massfunction.n_in_bins(edge, M, dndM) * volume
        chi2 = np.dot(N - Nmodel, np.dot(np.linalg.inv(cov), N - Nmodel))
        print "Box%d snap%d chi2 = %.2f" % (i, j, chi2)
        ax[0].errorbar(Mave, N, err, c=colors[j], marker='.', ls='')
        ax[0].loglog(Mave, Nmodel, ls='-', c=colors[j])
        pd = (Nmodel - N) / Nmodel
        pde = err / Nmodel
        ax[1].errorbar(Mave, pd, pde, c=colors[j])
    ax[0].set_ylim(1, 1e6)
    yl = .11
    ax[1].set_ylim(-yl, yl)
    plt.subplots_adjust(hspace=0)
    if savepath:
        plt.gcf().savefig(savepath)
    else:
        plt.show()
def get_box_resids(i):
    cosmo, h, Omega_m = get_cosmo(i)
    M = np.logspace(12, 16, num=200)  #Msun/h
    k = np.logspace(-5, 1, num=1000)  #Mpc^-1
    bfpath = "bf_%s_box%d.txt" % (name, i)
    print i, name
    params = np.loadtxt(bfpath)
    zb = np.array([])
    Mb = np.array([])
    nub = np.array([])
    R = np.array([])
    Re = np.array([])
    for j in range(0, 10):  #snap
        Mlo, Mhi, N, Mtot = AD.building_box_binned_mass_function(i, j).T
        Mj = Mtot / N
        Mb = np.concatenate((Mb, Mj))
        zb = np.concatenate((zb, np.ones_like(N) * zs[j]))
        edge = 10**np.concatenate((Mlo, Mhi[-1:]))
        cov = AD.building_box_binned_mass_function_covariance(i, j)
        icov = np.linalg.inv(cov)
        err = np.sqrt(np.diag(cov))
        p = np.array([cosmo.pk_lin(ki, zs[j]) for ki in k]) * h**3
        nub = np.concatenate((nub, bias.nu_at_M(Mj, k / h, p, Omega_m)))
        d, e, f, g = model_swap(params, name, x[j])
        dndM = massfunction.dndM_at_M(M, k / h, p, Omega_m, d, e, f, g)
        Nmodel = massfunction.n_in_bins(edge, M, dndM) * volume
        R = np.concatenate((R, (Nmodel - N) / Nmodel))
        Re = np.concatenate((Re, err / Nmodel))
    return Mb, nub, zb, R, Re
def test_mf_binned():
    M2 = np.logspace(12, 16, num=100)
    dn = mf.dndM_at_M(M2, k / h, p, Omega_m)
    edges = np.logspace(12, 16, 11)
    n = mf.n_in_bins(edges, M2, dn)
    npt.assert_array_less(n[1:], n[:-1])
    n2 = np.array([
        mf.n_in_bin(edges[i], edges[i + 1], M2, dn)
        for i in range(len(edges) - 1)
    ])
    npt.assert_array_less(n2[1:], n2[:-1])
    npt.assert_array_equal(n, n2)
Example #4
0
def mass_func(pars, args):
    x = args['x']
    Omega_m = args['Omega_m']
    M = args['M']
    k = args['k']
    ps = args['ps']
    edges = args['edges']  #Msun/h
    Nout = []
    for i in range(len(x)):
        d, e, f, g = model_swap(pars, args['name'], x[i])
        dndM = massfunction.dndM_at_M(M, k, ps[i], Omega_m, d, e, f, g)
        Nout.append(massfunction.n_in_bins(edges[i], M, dndM) * volume)
    return Nout
def lnlike(params, args):
    x = args['x']  # a - 0.5
    Om = args['Omega_m']
    M = args['M']
    s2s = args['sigma2s']
    s2ts = args['sigma2tops']
    s2bs = args['sigma2bots']
    Ns = args['Ns']  #Number of halos in the sim
    edges = args['edges']  #Msun/h
    icovs = args['icovs']
    LL = 0
    for i in range(len(x)):
        d, e, f, g = model_swap(params, args, x[i])
        dndM = massfunction._dndM_sigma2_precomputed(M, s2s[i], s2ts[i],
                                                     s2bs[i], Om, d, e, f, g)
        N = massfunction.n_in_bins(edges[i], M, dndM) * volume
        X = Ns[i] - N
        LL += np.dot(X, np.dot(icovs[i], X))
    return -0.5 * LL