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 test_special():
    sigma2 = peaks.sigma2_at_M(M, k, p, Omega_m)
    Mt = M * (1 - 1e-6 * 0.5)
    Mb = M * (1 + 1e-6 * 0.5)
    sigma2t = peaks.sigma2_at_M(Mt, k, p, Omega_m)
    sigma2b = peaks.sigma2_at_M(Mb, k, p, Omega_m)
    d, e, f, g = 2.16087369917, 1.18309392312, 0.133881834517, -0.0263615354323
    n = mf._dndM_sigma2_precomputed(M, sigma2, sigma2t, sigma2b, Omega_m, d, e,
                                    f, g)
    npt.assert_array_less(n[1:], n[:-1])
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
def test_special():
    sigma2 = peaks.sigma2_at_M(M, k, p, Omega_m)
    dsigma2dM = peaks.dsigma2dM_at_M(M, k, p, Omega_m)
    d, e, f, g = 2.16087369917, 1.18309392312, 0.133881834517, -0.0263615354323
    n = mf._dndM_sigma2_precomputed(M, sigma2, dsigma2dM, Omega_m, d, e, f, g)
    npt.assert_array_less(n[1:], n[:-1])