예제 #1
0
def lnLike_pca(theta, k_list, pk_ngc_list, pk_sgc_list, pk_ngc_mock,
               pk_sgc_mock):
    ''' wrapper for nongauss.lnL_pca . lnL_pca decomposes  P_data - P_model into
    PCA componenets, then measures the likelihood by calculating the probability
    of each of the components p(x_pca,i), which is estimated using a nonparametric
    density estimation method (e.g. KDE) from the mock catalogs 

    parameters
    ----------
    theta : array
        cosmological parameters : alpha_perp, alpha_para, fsig8 and 
        nuisance parameters : b1NGCsig8, b1SGCsig8, b2NGCsig8, b2SGCsig8, 
        NNGC, NSGC, sigmavNGC, sigmavSGC

    k_list : list
        list of k values for the mono, quadru, and hexadecaopoles -- [k0, k2, k4]

    pk_ngc_list : list 
   
    pk_sgc_list : list 

    mocks_ngc : np.ndarray (N_mock x N_k)
        Array of the mock catalog P(k)s for NGC
    
    mocks_sgc : np.ndarray (N_mock x N_k)
        Array of the mock catalog P(k)s for SGC
    '''
    if 0.8 < theta[0] < 1.4 and 0.8 < theta[1] < 1.4:
        binrange1, binrange2, binrange3 = len(k_list[0]), len(k_list[1]), len(
            k_list[2])
        maxbin1 = len(k_list[0]) + 1

        k = np.concatenate(k_list)
        pk_ngc = np.concatenate(pk_ngc_list)
        pk_sgc = np.concatenate(pk_sgc_list)

        modelX = Mod.taruya_model(100, binrange1, binrange2, binrange3,
                                  maxbin1, k, theta[0], theta[1], theta[2],
                                  theta[3], theta[4], theta[5], theta[6],
                                  theta[7], theta[8], theta[9], theta[10])

        model_ngc = modelX[0]
        model_sgc = modelX[1]

        diff_ngc = model_ngc - pk_ngc
        diff_sgc = model_sgc - pk_sgc
        lnP_pca_ngc = NG.lnL_pca(delta_ngc, pk_ngc_mock)
        lnP_pca_sgc = NG.lnL_pca(delta_sgc, pk_sgc_mock)
        return lnP_pca_ngc + ln_pca_sgc  #-0.5*(chi2_ngc + chi2_sgc)
    else:
        return -0.5 * (10000.)
예제 #2
0
def importance_weight():
    '''
    '''
    # read in BOSS P(k) (read in data D)
    pkay = Dat.Pk()
    k_list, pk_ngc_data = [], []
    for ell in [0, 2, 4]:
        k, plk_ngc = pkay.Observation(ell, 1, 'ngc')
        k_list.append(k)
        pk_ngc_data.append(plk_ngc)
    binrange1, binrange2, binrange3 = len(k_list[0]), len(k_list[1]), len(
        k_list[2])
    maxbin1 = len(k_list[0]) + 1
    k = np.concatenate(k_list)

    chain = Inf.mcmc_chains('beutler_z1')

    # calculate D - m(theta) for all the mcmc chain
    delta_ngc = []
    for i in range(10):  #len(chain['chi2'])):
        model_i = Mod.taruya_model(
            100, binrange1, binrange2, binrange3, maxbin1, k,
            chain['alpha_perp'][i], chain['alpha_para'][i], chain['fsig8'][i],
            chain['b1sig8_NGC'][i], chain['b1sig8_SGC'][i],
            chain['b2sig8_NGC'][i], chain['b2sig8_SGC'][i], chain['N_NGC'][i],
            chain['N_SGC'][i], chain['sigmav_NGC'][i], chain['sigmav_SGC'][i])
        delta_ngc.append(model_i[0] - np.concatenate(pk_ngc_data))

    # import PATCHY mocks
    pk_ngc_list = []
    for ell in [0, 2, 4]:
        if ell == 4: kmax = 0.1
        else: kmax = 0.15
        pk_ngc_list.append(
            NG.X_pk('patchy.ngc.z1', krange=[0.01, kmax], ell=ell, sys='fc'))
    pk_ngc_mock = np.concatenate(pk_ngc_list, axis=1)

    lnP_ica_ngc = NG.lnL_ica(np.array(delta_ngc), pk_ngc_mock)
    lnP_pca_ngc = NG.lnL_pca(np.array(delta_ngc), pk_ngc_mock)

    lnw_ngc = lnP_ica_ngc - lnP_pca_ngc
    print(np.exp(lnw_ngc))
    return None
예제 #3
0
def lnLike_pseudo(theta, k_list, pk_ngc_list, pk_sgc_list, Cinv_ngc, Cinv_sgc):
    ''' log of the pseudo Gaussian likelihood function. This is identical to 
    Florian's implementation. 

    parameters
    ----------
    theta : array
        cosmological parameters : alpha_perp, alpha_para, fsig8 and 
        nuisance parameters : b1NGCsig8, b1SGCsig8, b2NGCsig8, b2SGCsig8, 
        NNGC, NSGC, sigmavNGC, sigmavSGC

    k_list : list
        list of k values for the mono, quadru, and hexadecaopoles -- [k0, k2, k4]

    pk_ngc_list : list 
    '''
    if 0.8 < theta[0] < 1.4 and 0.8 < theta[1] < 1.4:
        binrange1, binrange2, binrange3 = len(k_list[0]), len(k_list[1]), len(
            k_list[2])
        maxbin1 = len(k_list[0]) + 1

        k = np.concatenate(k_list)
        pk_ngc = np.concatenate(pk_ngc_list)
        pk_sgc = np.concatenate(pk_sgc_list)

        modelX = Mod.taruya_model(100, binrange1, binrange2, binrange3,
                                  maxbin1, k, theta[0], theta[1], theta[2],
                                  theta[3], theta[4], theta[5], theta[6],
                                  theta[7], theta[8], theta[9], theta[10])

        model_ngc = modelX[0]
        model_sgc = modelX[1]

        diff_ngc = model_ngc - pk_ngc
        diff_sgc = model_sgc - pk_sgc
        chi2_ngc = np.dot(diff_ngc, np.dot(Cinv_ngc, diff_ngc))
        chi2_sgc = np.dot(diff_sgc, np.dot(Cinv_sgc, diff_sgc))
        return -0.5 * (chi2_ngc + chi2_sgc)
    else:
        return -0.5 * (10000.)
예제 #4
0
def Pk_model_mcmc(tag, **kwargs):
    ''' Evalulate the P(k) model for the `tag` MCMC chain 
    '''
    if tag == 'beutler_z1':
        # read in BOSS P(k) (read in data D)
        pkay = Dat.Pk()
        k_list, pk_ngc_data = [], []
        for ell in [0, 2, 4]:
            k, plk_ngc = pkay.Observation(ell, 1, 'ngc')
            k_list.append(k)
            pk_ngc_data.append(plk_ngc)
        binrange1, binrange2, binrange3 = len(k_list[0]), len(k_list[1]), len(
            k_list[2])
        maxbin1 = len(k_list[0]) + 1
        k = np.concatenate(k_list)

        if 'ichain' in kwargs.keys():
            nchains = [kwargs['ichain']]
        else:
            nchains = range(4)

        # read in Florian's RSD MCMC chains
        for ichain in nchains:
            chain_file = ''.join([
                UT.dat_dir(), 'Beutler/public_full_shape/',
                'Beutler_et_al_full_shape_analysis_z1_chain',
                str(ichain), '.dat'
            ])
            sample = np.loadtxt(chain_file, skiprows=1)
            chain = sample[:, 1:]

            fname_ngc = ''.join([
                UT.dat_dir(), 'Beutler/public_full_shape/',
                'Beutler_et_al_full_shape_analysis_z1_chain',
                str(ichain), '.model.ngc.dat'
            ])
            fname_sgc = ''.join([
                UT.dat_dir(), 'Beutler/public_full_shape/',
                'Beutler_et_al_full_shape_analysis_z1_chain',
                str(ichain), '.model.sgc.dat'
            ])

            if not os.path.isfile(fname_ngc):
                f_ngc = open(fname_ngc, 'w')
                f_sgc = open(fname_sgc, 'w')
                mocks = range(sample.shape[0])
            else:
                ns_ngc = np.loadtxt(fname_ngc, unpack=True, usecols=[0])
                ns_sgc = np.loadtxt(fname_sgc, unpack=True, usecols=[0])
                f_ngc = open(fname_ngc, 'a')
                f_sgc = open(fname_sgc, 'a')
                if ns_ngc.max() != ns_sgc.max():
                    raise ValueError
                mocks = range(int(ns_ngc.max()) + 1, sample.shape[0])

            for i in mocks:
                model_i = Mod.taruya_model(100, binrange1, binrange2,
                                           binrange3, maxbin1, k, chain[i, 0],
                                           chain[i, 1], chain[i, 2],
                                           chain[i, 3], chain[i, 4],
                                           chain[i, 5], chain[i, 6], chain[i,
                                                                           7],
                                           chain[i, 8], chain[i, 9], chain[i,
                                                                           10])
                f_ngc.write('\t'.join(
                    [str(i)] +
                    [str(model_i[0][ii]) for ii in range(len(model_i[0]))]))
                f_sgc.write('\t'.join(
                    [str(i)] +
                    [str(model_i[1][ii]) for ii in range(len(model_i[1]))]))
                f_ngc.write('\n')
                f_sgc.write('\n')
    else:
        raise ValueError
    return None
예제 #5
0
def model():
    kbins = 60
    binsize = 120 / kbins
    minbin1 = 2
    minbin2 = 2
    minbin3 = 2
    maxbin1 = 30
    maxbin2 = 30
    maxbin3 = 20
    binrange1 = int(0.5 * (maxbin1 - minbin1))
    binrange2 = int(0.5 * (maxbin2 - minbin2))
    binrange3 = int(0.5 * (maxbin3 - minbin3))
    totbinrange = binrange1 + binrange2 + binrange3

    pkay = Dat.Pk()
    k0, p0k = pkay.Observation(0, 1, 'ngc')
    k2, p2k = pkay.Observation(2, 1, 'ngc')
    k4, p4k = pkay.Observation(4, 1, 'ngc')
    k = np.concatenate([k0, k2, k4])

    # alpha_perp, alpha_para, fsig8, b1NGCsig8, b1SGCsig8, b2NGCsig8, b2SGCsig8, NNGC, NSGC, sigmavNGC, sigmavSGC
    #value_array = [1.008, 1.001, 0.478, 1.339, 1.337, 1.16, 1.16, -1580., -1580., 6.1, 6.1]
    value_array = [
        1.008, 1.001, 0.478, 1.339, 1.337, 1.16, 0.32, -1580., -930., 6.1, 6.8
    ]
    #value_array = [1.00830111426, 1.0007368972, 0.478098423689, 1.33908539185, 1.33663505549, 1.15627984704, 0.31657562682, -1580.01689181, -928.488535962, 6.14815801563, 6.79551199595] #z1 max likelihood
    t_start = time.time()
    modelX = Mod.taruya_model(100, binrange1, binrange2, binrange3,
                              maxbin1 / binsize, k, value_array[0],
                              value_array[1], value_array[2], value_array[3],
                              value_array[4], value_array[5], value_array[6],
                              value_array[7], value_array[8], value_array[9],
                              value_array[10])
    print(time.time() - t_start, ' seconds')
    model_ngc = modelX[0]
    model_sgc = modelX[1]

    # read in pre-window convlution
    k_nw, p0k_ngc_nw, p2k_ngc_nw, p4k_ngc_nw, p0k_sgc_nw, p2k_sgc_nw, p4k_sgc_nw = np.loadtxt(
        ''.join([UT.dat_dir(), 'boss/test.nowindow.dat']), unpack=True)

    # now plot
    pretty_colors = prettycolors()
    fig = plt.figure(figsize=(11, 5))
    sub = fig.add_subplot(121)
    sub.scatter(k0, k0 * p0k, c=pretty_colors[1], lw=0)
    sub.plot(k0, k0 * model_ngc[:binrange1], c=pretty_colors[1])
    sub.plot(k_nw, k_nw * p0k_ngc_nw, c=pretty_colors[1], lw=1, ls='--')
    sub.scatter(k2, k2 * p2k, c=pretty_colors[3], lw=0)
    sub.plot(k_nw, k_nw * p2k_ngc_nw, c=pretty_colors[3], lw=1, ls='--')
    sub.plot(k2,
             k2 * model_ngc[binrange1:binrange1 + binrange2],
             c=pretty_colors[3])
    sub.scatter(k4, k4 * p4k, c=pretty_colors[5], lw=0)
    sub.plot(k_nw, k_nw * p4k_ngc_nw, c=pretty_colors[5], lw=1, ls='--')
    sub.plot(k4,
             k4 * model_ngc[binrange1 + binrange2:totbinrange],
             c=pretty_colors[5])
    sub.text(0.9,
             0.05,
             'NGC',
             ha='right',
             va='bottom',
             transform=sub.transAxes,
             fontsize=20)

    sub.set_xlim([0.01, 0.15])
    sub.set_ylim([-750, 2250])
    sub.set_xlabel('$k$', fontsize=25)
    sub.set_ylabel('$k \, P_{\ell}(k)$', fontsize=25)

    k0, p0k = pkay.Observation(0, 1, 'sgc')
    k2, p2k = pkay.Observation(2, 1, 'sgc')
    k4, p4k = pkay.Observation(4, 1, 'sgc')
    k = np.concatenate([k0, k2, k4])

    sub = fig.add_subplot(122)
    sub.scatter(k0, k0 * p0k, c=pretty_colors[1], lw=0)
    sub.plot(k0, k0 * model_sgc[:binrange1], c=pretty_colors[1])
    sub.plot(k_nw, k_nw * p0k_sgc_nw, c=pretty_colors[1], lw=1, ls='--')
    sub.scatter(k2, k2 * p2k, c=pretty_colors[3], lw=0)
    sub.plot(k2,
             k2 * model_sgc[binrange1:binrange1 + binrange2],
             c=pretty_colors[3])
    sub.plot(k_nw, k_nw * p2k_sgc_nw, c=pretty_colors[3], lw=1, ls='--')
    sub.scatter(k4, k4 * p4k, c=pretty_colors[5], lw=0)
    sub.plot(k4,
             k4 * model_sgc[binrange1 + binrange2:totbinrange],
             c=pretty_colors[5])
    sub.plot(k_nw, k_nw * p4k_sgc_nw, c=pretty_colors[5], lw=1, ls='--')
    sub.text(0.9,
             0.05,
             'SGC',
             ha='right',
             va='bottom',
             transform=sub.transAxes,
             fontsize=20)

    sub.set_xlim([0.01, 0.15])
    sub.set_ylim([-750, 2250])
    sub.set_xlabel('$k$', fontsize=25)
    fig.savefig(''.join([UT.fig_dir(), 'tests/test.model.plk.png']),
                bbox_inches='tight')
    return None