예제 #1
0
def gmf(Mr=20, Nmock=500): 
    '''
    Plot Group Multiplicty Function of fake observations 
    '''
    prettyplot() 
    pretty_colors = prettycolors() 
    
    # import fake obs GMF
    gmf, sig_gmf = Data.data_gmf(Mr=Mr, Nmock=Nmock)
    # group richness bins
    gmf_bin = Data.data_gmf_bins()

    fig = plt.figure(1) 
    sub = fig.add_subplot(111)

    sub.errorbar(
            0.5*(gmf_bin[:-1]+gmf_bin[1:]), gmf, yerr=sig_gmf,
            fmt="ok", capsize=1.0
            )
    sub.set_xlim([1, 60])
    sub.set_yscale('log')
    sub.set_ylabel(r"Group Multiplicity Function (h$^{3}$ Mpc$^{-3}$)", fontsize=20)
    sub.set_xlabel(r"$\mathtt{Group\;\;Richness}$", fontsize=20)
    # save to file 
    fig_file = ''.join([util.fig_dir(), 
        'gmf.Mr', str(Mr), '.Nmock', str(Nmock), '.png'])
    fig.savefig(fig_file, bbox_inches='tight')
    return None
예제 #2
0
def getObvs(observables, **data_dict): 
    ''' Given the list of observable strings return data vector and 
    covariance matrices 
    '''
    if observables == ['xi']:
        fake_obs = Data.data_xi(**data_dict)
        fake_obs_cov = Data.data_cov(inference='abc', **data_dict)[1:16 , 1:16]
        xi_Cii = np.diag(fake_obs_cov)
        Cii_list = [xi_Cii]

    elif observables == ['nbar','xi']:
        fake_obs = np.hstack([Data.data_nbar(**data_dict), Data.data_xi(**data_dict)])
        fake_obs_cov = Data.data_cov(inference='abc', **data_dict)[:16 , :16]
        Cii = np.diag(fake_obs_cov)
        xi_Cii = Cii[1:]
        nbar_Cii = Cii[0]
        Cii_list = [nbar_Cii, xi_Cii]

    elif observables == ['nbar','gmf']:
        ##### FIRST BIN OF GMF DROPPED ###############
        # CAUTION: hardcoded 
        fake_obs = np.hstack([Data.data_nbar(**data_dict), Data.data_gmf(**data_dict)[1:]])

        # CAUTION: Covariance matrix starts at 17 instead  
        fake_obs_cov = Data.data_cov(inference='abc', **data_dict)
        Cii = np.diag(fake_obs_cov)
        gmf_Cii = Cii[17:]
        nbar_Cii = Cii[0]
        Cii_list = [nbar_Cii, gmf_Cii]

    return fake_obs, Cii_list 
예제 #3
0
def TrueObservables(Mr=21, b_normal=0.25):
    ''' Plot xi and gmf for the data 
    '''
    # xi data
    xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc')
    data_xi_cov = cov_data[1:16, 1:16]
    xi_r_bin = Data.data_xi_bin(Mr=Mr)
    # gmf data
    data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc')
    data_gmf_cov = cov_data[16:, 16:]

    r_binedge = Data.data_gmf_bins()
    gmf_r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:])

    prettyplot()
    pretty_colors = prettycolors()

    fig = plt.figure(figsize=(12, 6))
    sub_xi = fig.add_subplot(121)
    sub_gmf = fig.add_subplot(122)

    #sub_xi.errorbar(xi_r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k',
    #        markersize=0, lw=0, capsize=3, elinewidth=1.5)
    #sub_xi.scatter(xi_r_bin, xi_data, c='k', s=10, lw=0)
    sub_xi.fill_between(xi_r_bin,
                        xi_data - np.sqrt(np.diag(data_xi_cov)),
                        xi_data + np.sqrt(np.diag(data_xi_cov)),
                        color=pretty_colors[1])
    sub_xi.set_yscale('log')
    sub_xi.set_xscale('log')
    sub_xi.set_xlim(0.1, 20)
    sub_xi.set_xlabel(r'$\mathtt{r}\; [\mathtt{Mpc}/h]$', fontsize=25)
    sub_xi.set_ylabel(r'$\xi(r)$', fontsize=25)

    #sub_gmf.errorbar(gmf_r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)),
    #        fmt="o", color='k',
    #        markersize=0, lw=0, capsize=4, elinewidth=2)
    #sub_gmf.scatter(gmf_r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation')
    sub_gmf.fill_between(gmf_r_bin,
                         data_gmf - np.sqrt(np.diag(data_gmf_cov)),
                         data_gmf + np.sqrt(np.diag(data_gmf_cov)),
                         color=pretty_colors[1])
    sub_gmf.set_xlim(1, 20)
    sub_gmf.set_xlabel(r'$\mathtt{N}$ (Group Richness)', fontsize=25)

    sub_gmf.yaxis.tick_right()
    sub_gmf.yaxis.set_ticks_position('both')
    sub_gmf.yaxis.set_label_position('right')
    sub_gmf.set_ylim([10**-7, 2.0 * 10**-4])
    sub_gmf.set_yscale('log')
    sub_gmf.set_ylabel(r'$\zeta(\mathtt{N})$', fontsize=25)

    fig.subplots_adjust(hspace=0.05)
    fig_name = ''.join([ut.fig_dir(), 'paper.data_observables', '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150)
    plt.close()
    return None
예제 #4
0
파일: paper.py 프로젝트: mjvakili/ccppabc
def TrueObservables(Mr=21, b_normal=0.25): 
    ''' Plot xi and gmf for the data 
    '''
    # xi data 
    xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') 
    data_xi_cov = cov_data[1:16, 1:16]
    xi_r_bin = Data.data_xi_bin(Mr=Mr)
    # gmf data 
    data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal)
    cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') 
    data_gmf_cov = cov_data[16:, 16:]

    r_binedge = Data.data_gmf_bins()
    gmf_r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:]) 
   
    prettyplot()
    pretty_colors = prettycolors() 

    fig = plt.figure(figsize=(12,6))
    sub_xi = fig.add_subplot(121) 
    sub_gmf = fig.add_subplot(122)
   
    #sub_xi.errorbar(xi_r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k', 
    #        markersize=0, lw=0, capsize=3, elinewidth=1.5)
    #sub_xi.scatter(xi_r_bin, xi_data, c='k', s=10, lw=0)
    sub_xi.fill_between(xi_r_bin, 
            xi_data-np.sqrt(np.diag(data_xi_cov)), xi_data+np.sqrt(np.diag(data_xi_cov)), 
            color=pretty_colors[1])
    sub_xi.set_yscale('log')
    sub_xi.set_xscale('log')
    sub_xi.set_xlim(0.1, 20)
    sub_xi.set_xlabel(r'$\mathtt{r}\; [\mathtt{Mpc}/h]$', fontsize=25)
    sub_xi.set_ylabel(r'$\xi(r)$', fontsize=25)

    #sub_gmf.errorbar(gmf_r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)), 
    #        fmt="o", color='k', 
    #        markersize=0, lw=0, capsize=4, elinewidth=2)
    #sub_gmf.scatter(gmf_r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation')
    sub_gmf.fill_between(gmf_r_bin, 
            data_gmf-np.sqrt(np.diag(data_gmf_cov)), data_gmf+np.sqrt(np.diag(data_gmf_cov)), 
            color=pretty_colors[1])
    sub_gmf.set_xlim(1, 20)
    sub_gmf.set_xlabel(r'$\mathtt{N}$ (Group Richness)', fontsize=25)

    sub_gmf.yaxis.tick_right()
    sub_gmf.yaxis.set_ticks_position('both')
    sub_gmf.yaxis.set_label_position('right') 
    sub_gmf.set_ylim([10**-7, 2.0*10**-4])
    sub_gmf.set_yscale('log')
    sub_gmf.set_ylabel(r'$\zeta(\mathtt{N})$', fontsize=25)

    fig.subplots_adjust(hspace=0.05)
    fig_name = ''.join([ut.fig_dir(), 
        'paper.data_observables', 
        '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight', dpi=150) 
    plt.close()
    return None 
예제 #5
0
def abc_hodsim(Mr=21, b_normal=0.25):

    prior_min, prior_max = PriorRange('first_try')
    prior_range = np.zeros((len(prior_min),2))
    prior_range[:,0] = prior_min
    prior_range[:,1] = prior_max

    abc_hod = ABC_HODsim() 

    data_hod_dict = Data.data_hod_param(Mr=21)
    data_hod = np.array([
        data_hod_dict['logM0'],                 # log M0 
        np.log(data_hod_dict['sigma_logM']),    # log(sigma)
        data_hod_dict['logMmin'],               # log Mmin
        data_hod_dict['alpha'],                 # alpha
        data_hod_dict['logM1']                  # log M1
        ])
    print Data.data_nbar(Mr=Mr, b_normal=b_normal), Data.data_gmf(Mr=Mr, b_normal=b_normal)
    print abc_hod(data_hod, prior_range=prior_range, observables=['nbar', 'gmf']) 
예제 #6
0
def abc_hodsim(Mr=21, b_normal=0.25):

    prior_min, prior_max = PriorRange('first_try')
    prior_range = np.zeros((len(prior_min), 2))
    prior_range[:, 0] = prior_min
    prior_range[:, 1] = prior_max

    abc_hod = ABC_HODsim()

    data_hod_dict = Data.data_hod_param(Mr=21)
    data_hod = np.array([
        data_hod_dict['logM0'],  # log M0 
        np.log(data_hod_dict['sigma_logM']),  # log(sigma)
        data_hod_dict['logMmin'],  # log Mmin
        data_hod_dict['alpha'],  # alpha
        data_hod_dict['logM1']  # log M1
    ])
    print Data.data_nbar(Mr=Mr,
                         b_normal=b_normal), Data.data_gmf(Mr=Mr,
                                                           b_normal=b_normal)
    print abc_hod(data_hod,
                  prior_range=prior_range,
                  observables=['nbar', 'gmf'])
예제 #7
0
def plot_data(Mr = 21 , output_dir = None):
    '''
    plot summary statistics of the data
    '''
    if output_dir is None:
        fig_dir = util.fig_dir()
    else:
        fig_dir = output_dir

    cov = Data.data_cov()
    xi_err = np.sqrt(np.diag(cov)[1:16])
    gmf_err = np.sqrt(np.diag(cov)[16:]) 
   
    fig , axes = plt.subplots(1, 2, figsize=(10, 5))
    fig.subplots_adjust(wspace=0.4, hspace=0.4)
    ax = axes[0]
    x = Data.xi_binedges()
    y = Data.data_xi() 
    ax.errorbar(0.5*(x[:-1]+x[1:]), y, yerr=xi_err, fmt=".k", capsize=0)
    ax.set_yscale('log')
    ax.set_xscale('log')
    ax.set_xlim(0.2, 22)
    ax.set_xlabel(r'$r[\mathrm{Mpc}/h]$')
    ax.set_ylabel(r'$\xi(r)$')
    
    ax = axes[1]
    x = Data.gmf_bins()
    y = Data.data_gmf() 
    ax.errorbar(0.5*(x[:-1]+x[1:]), y, yerr=gmf_err, fmt=".k", capsize=0)
    ax.set_xlim(1, 20)
    ax.set_yscale('log')
    ax.set_xlabel(r'Group Richness $N$')
    ax.set_ylabel(r'$\zeta(N)$')

    fig_file = ''.join([fig_dir, "data", '_Mr', str(Mr),'.pdf'])
    plt.savefig(fig_file)
    plt.close()
예제 #8
0
def plot_data(Mr=21, output_dir=None):
    '''
    plot summary statistics of the data
    '''
    if output_dir is None:
        fig_dir = util.fig_dir()
    else:
        fig_dir = output_dir

    cov = Data.data_cov()
    xi_err = np.sqrt(np.diag(cov)[1:16])
    gmf_err = np.sqrt(np.diag(cov)[16:])

    fig, axes = plt.subplots(1, 2, figsize=(10, 5))
    fig.subplots_adjust(wspace=0.4, hspace=0.4)
    ax = axes[0]
    x = Data.xi_binedges()
    y = Data.data_xi()
    ax.errorbar(0.5 * (x[:-1] + x[1:]), y, yerr=xi_err, fmt=".k", capsize=0)
    ax.set_yscale('log')
    ax.set_xscale('log')
    ax.set_xlim(0.2, 22)
    ax.set_xlabel(r'$r[\mathrm{Mpc}/h]$')
    ax.set_ylabel(r'$\xi(r)$')

    ax = axes[1]
    x = Data.gmf_bins()
    y = Data.data_gmf()
    ax.errorbar(0.5 * (x[:-1] + x[1:]), y, yerr=gmf_err, fmt=".k", capsize=0)
    ax.set_xlim(1, 20)
    ax.set_yscale('log')
    ax.set_xlabel(r'Group Richness $N$')
    ax.set_ylabel(r'$\zeta(N)$')

    fig_file = ''.join([fig_dir, "data", '_Mr', str(Mr), '.pdf'])
    plt.savefig(fig_file)
    plt.close()
예제 #9
0
import data as Data
from hod_sim import HODsim
from group_richness import richness

Nwalkers = 10
Nchains_burn = 1
Nchains_pro = 1

# data observables
fake_obs = []  # list of observables
for obv in observables:
    if obv == 'nbar':
        data_nbar, data_nbar_var = Data.data_nbar(**data_dict)
        fake_obs.append(data_nbar)
    if obv == 'gmf':
        data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict)
        fake_obs.append(data_gmf)
    if obv == 'xi':
        # import xir and full covariance matrix of xir
        data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict)
        # take the inverse of the covariance matrix
        data_xi_invcov = solve(np.eye(len(data_xi)), data_xi_cov)
        fake_obs.append(data_xi)

# 'True' HOD parameters
data_hod_dict = Data.data_hod_param(Mr=data_dict['Mr'])
data_hod = np.array([
    data_hod_dict['logM0'],  # log M0 
    np.log(data_hod_dict['sigma_logM']),  # log(sigma)
    data_hod_dict['logMmin'],  # log Mmin
    data_hod_dict['alpha'],  # alpha
예제 #10
0
from hod_sim import HODsim
from group_richness import richness


Nwalkers = 10
Nchains_burn = 1
Nchains_pro = 1
    
# data observables
fake_obs = []       # list of observables 
for obv in observables: 
    if obv == 'nbar': 
        data_nbar, data_nbar_var = Data.data_nbar(**data_dict)
        fake_obs.append(data_nbar)
    if obv == 'gmf': 
        data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict)
        fake_obs.append(data_gmf)
    if obv == 'xi': 
        # import xir and full covariance matrix of xir 
        data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict)           
        # take the inverse of the covariance matrix
        data_xi_invcov = solve(np.eye(len(data_xi)) , data_xi_cov)          
        fake_obs.append(data_xi)

# 'True' HOD parameters
data_hod_dict = Data.data_hod_param(Mr=data_dict['Mr'])
data_hod = np.array([
    data_hod_dict['logM0'],                 # log M0 
    np.log(data_hod_dict['sigma_logM']),    # log(sigma)
    data_hod_dict['logMmin'],               # log Mmin
    data_hod_dict['alpha'],                 # alpha
예제 #11
0
def PosteriorObservable(Mr=21, b_normal=0.25, clobber=False):
    ''' Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood
    '''
    prettyplot()
    pretty_colors = prettycolors()
    fig = plt.figure(1, figsize=(16, 12))
    gs = gridspec.GridSpec(2, 2, height_ratios=[2.5, 1], width_ratios=[1, 1])

    for obvs in ['nbargmf', 'nbarxi']:

        if obvs == 'nbargmf':
            result_dir = ''.join([
                ut.dat_dir(),
                'paper/ABC',
                obvs,
                '/run1/',
            ])
            theta_file = lambda tt: ''.join(
                [result_dir, 'nbar_gmf_theta_t',
                 str(tt), '.ABCnbargmf.dat'])
            tf = 8
            obvs_list = ['gmf']
        elif obvs == 'nbarxi':
            result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/'])
            theta_file = lambda tt: ''.join(
                [result_dir, 'nbar_xi_theta_t',
                 str(tt), '.abc.dat'])
            tf = 9
            obvs_list = ['xi']
        else:
            raise ValueError

        theta = np.loadtxt(theta_file(tf))  # import thetas
        #theta = theta[:10]

        obvs_file = ''.join(
            theta_file(tf).rsplit('.dat')[:-1] + ['.', obvs_list[0], '.p'])
        print obvs_file

        HODsimulator = ABC_HODsim(Mr=Mr, b_normal=b_normal)
        if not os.path.isfile(obvs_file) or clobber:
            model_obv = []
            for i in xrange(len(theta)):
                print i
                obv_i = HODsimulator(theta[i],
                                     prior_range=None,
                                     observables=obvs_list)
                model_obv.append(obv_i[0])
            model_obv = np.array(model_obv)
            pickle.dump(model_obv, open(obvs_file, 'wb'))
        else:
            model_obv = pickle.load(open(obvs_file, 'rb'))

        if 'xi' in obvs:
            r_bin = Data.data_xi_bin(Mr=Mr)
        elif 'gmf' in obvs:
            r_binedge = Data.data_gmf_bins()
            r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:])

        a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5],
                                      axis=0)

        # plotting
        if obvs == 'nbarxi':
            ax = plt.subplot(gs[0])
        elif obvs == 'nbargmf':
            ax = plt.subplot(gs[1])

        if 'xi' in obvs:  # 2PCF
            xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal)
            cov_data = Data.data_cov(Mr=Mr,
                                     b_normal=b_normal,
                                     inference='mcmc')
            data_xi_cov = cov_data[1:16, 1:16]

            ax.fill_between(r_bin,
                            a,
                            e,
                            color=pretty_colors[3],
                            alpha=0.3,
                            edgecolor="none")
            ax.fill_between(r_bin,
                            b,
                            d,
                            color=pretty_colors[3],
                            alpha=0.5,
                            edgecolor="none")
            ax.errorbar(r_bin,
                        xi_data,
                        yerr=np.sqrt(np.diag(data_xi_cov)),
                        fmt="o",
                        color='k',
                        markersize=0,
                        lw=0,
                        capsize=3,
                        elinewidth=1.5)
            ax.scatter(r_bin, xi_data, c='k', s=10, lw=0)
            ax.set_ylabel(r'$\xi_\mathtt{gg}(\mathtt{r})$', fontsize=27)
            ax.set_yscale('log')
            ax.set_xscale('log')
            ax.set_xticklabels([])
            ax.set_xlim([0.1, 20.])
            ax.set_ylim([0.09, 1000.])

            ax = plt.subplot(gs[2])
            ax.fill_between(r_bin,
                            a / xi_data,
                            e / xi_data,
                            color=pretty_colors[3],
                            alpha=0.3,
                            edgecolor="none")
            ax.fill_between(r_bin,
                            b / xi_data,
                            d / xi_data,
                            color=pretty_colors[3],
                            alpha=0.5,
                            edgecolor="none")
            ax.errorbar(r_bin,
                        np.repeat(1., len(xi_data)),
                        yerr=np.sqrt(np.diag(data_xi_cov)) / xi_data,
                        fmt="o",
                        color='k',
                        markersize=0,
                        lw=0,
                        capsize=3,
                        elinewidth=1.5)
            ax.plot(np.arange(0.1, 20., 0.1),
                    np.repeat(1., len(np.arange(0.1, 20, 0.1))),
                    c='k',
                    ls='--',
                    lw=2)
            ax.set_xlim([0.1, 20.])
            ax.set_xscale('log')
            ax.set_ylim([0.5, 1.5])
            ax.set_xlabel(r'$\mathtt{r}\;[\mathtt{Mpc}/h]$', fontsize=25)
            ax.set_ylabel(r'$\xi_\mathtt{gg}/\xi_\mathtt{gg}^\mathtt{obvs}$',
                          fontsize=25)

        elif 'gmf' in obvs:  # GMF
            data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal)
            cov_data = Data.data_cov(Mr=Mr,
                                     b_normal=b_normal,
                                     inference='mcmc')
            data_gmf_cov = cov_data[16:, 16:]

            ax.fill_between(r_bin,
                            a,
                            e,
                            color=pretty_colors[3],
                            alpha=0.3,
                            edgecolor="none")
            ax.fill_between(r_bin,
                            b,
                            d,
                            color=pretty_colors[3],
                            alpha=0.5,
                            edgecolor="none",
                            label='ABC Posterior')
            ax.errorbar(r_bin,
                        data_gmf,
                        yerr=np.sqrt(np.diag(data_gmf_cov)),
                        fmt="o",
                        color='k',
                        markersize=0,
                        lw=0,
                        capsize=4,
                        elinewidth=2)
            ax.scatter(r_bin,
                       data_gmf,
                       s=15,
                       lw=0,
                       c='k',
                       label='Mock Observation')
            ax.legend(loc='upper right',
                      scatterpoints=1,
                      prop={'size': 25},
                      borderpad=1.0)

            ax.yaxis.tick_right()
            ax.yaxis.set_ticks_position('both')
            ax.yaxis.set_label_position('right')
            ax.set_ylabel(r'$\zeta$ $[(\mathrm{h}/\mathtt{Mpc})^{3}]$',
                          fontsize=25)

            ax.set_yscale('log')
            ax.set_xlim([1., 20.])
            ax.set_xticklabels([])
            ax.set_ylim([10.**-7.2, 2 * 10**-4.])

            ax = plt.subplot(gs[3])
            ax.fill_between(r_bin,
                            a / data_gmf,
                            e / data_gmf,
                            color=pretty_colors[3],
                            alpha=0.3,
                            edgecolor="none")
            ax.fill_between(r_bin,
                            b / data_gmf,
                            d / data_gmf,
                            color=pretty_colors[3],
                            alpha=0.5,
                            edgecolor="none")

            ax.errorbar(r_bin,
                        np.repeat(1., len(data_gmf)),
                        yerr=np.sqrt(np.diag(data_gmf_cov)) / data_gmf,
                        fmt="o",
                        color='k',
                        markersize=0,
                        lw=0,
                        capsize=3,
                        elinewidth=1.5)
            ax.plot(np.arange(1., 20., 1),
                    np.repeat(1., len(np.arange(1., 20, 1))),
                    c='k',
                    ls='--',
                    lw=1.75)

            ax.yaxis.tick_right()
            ax.yaxis.set_label_position('right')
            ax.set_ylim([-0.1, 2.1])
            ax.set_ylabel(r'$\zeta/\zeta^\mathtt{obvs}$', fontsize=25)
            ax.set_xlim([1., 20.])
            ax.set_xlabel(r'$\mathtt{N}$ [Group Richness]', fontsize=25)

    fig.subplots_adjust(wspace=0.05, hspace=0.0)
    fig_name = ''.join([ut.fig_dir(), 'paper', '.ABCposterior', '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight')
    plt.close()
    return None
예제 #12
0
def plot_posterior_model(observable, abc_theta_file=None, data_dict={'Mr':20, 'b_normal': 0.25, 'Nmock':500},
        clobber=False):
    '''
    Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood

    Parameters
    ----------
    observable : string
        One of the following strings ['xi', 'scaledxi', 'gmf']

    '''
    # load the particles
    if abc_theta_file is None:
        raise ValueError("Please specify the theta output file from ABC-PMC run")
    theta = np.loadtxt(abc_theta_file)

    if observable == 'scaledxi':
        obvs_str = 'xi'
    else:
        obvs_str = observable

    obvs_file = ''.join(abc_theta_file.rsplit('.dat')[:-1] + ['.', observable, '.dat'])
    print obvs_file
    if not os.path.isfile(obvs_file) or clobber:
        for i in xrange(len(theta)):
            obv_i  = HODsimulator(
                    theta[i], prior_range=None,
                    observables=[obvs_str], Mr=data_dict['Mr'])
            try:
                model_obv.append(obv_i[0])
            except UnboundLocalError:
                model_obv = [obv_i[0]]
        model_obv = np.array(model_obv)
        np.savetxt(obvs_file, model_obv)
    else:
        model_obv = np.loadtxt(obvs_file)

    if 'xi' in observable:
        r_bin = Data.data_xi_bins(Mr=data_dict['Mr'])
    elif observable == 'gmf':
        r_bin = Data.data_gmf_bins()

    a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5], axis=0)

    # plotting
    fig = plt.figure(1)
    ax = fig.add_subplot(111)

    if observable == 'xi':  # 2PCF
        data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict) # data

        ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none")
        ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none")
        #ax.plot(r_bin, c, "k", lw=1)
        ax.errorbar(r_bin, data_xi, yerr = np.sqrt(np.diag(data_xi_cov)), fmt=".k",
                    capsize=0)
        ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20)
        ax.set_ylabel(r'$\xi_{\rm gg}$', fontsize=25)
        ax.set_xscale('log')
        ax.set_xlim([0.1, 20.])

    elif observable == 'scaledxi':  # Scaled 2PFC (r * xi)
        data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict) # data

        ax.fill_between(r_bin, r_bin*a, r_bin*e, color="k", alpha=0.1, edgecolor="none")
        ax.fill_between(r_bin, r_bin*b, r_bin*d, color="k", alpha=0.3, edgecolor="none")
        ax.errorbar(r_bin, r_bin*data_xi, yerr=r_bin*np.sqrt(np.diag(data_xi_cov)), fmt=".k",
                    capsize=0)
        ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20)
        ax.set_ylabel(r'$r \xi_{\rm gg}$', fontsize=25)
        ax.set_xscale('log')
        ax.set_xlim([0.1, 20.])

    elif observable == 'gmf':   # GMF
        data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict)
        ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none")
        ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none")
        ax.errorbar(r_bin, data_gmf, yerr = data_gmf_sigma, fmt=".k",
                    capsize=0)
        ax.set_xlabel(r'Group Richness', fontsize=25)
        ax.set_ylabel(r'GMF $[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25)

        ax.set_yscale('log')
        ax.set_xlim([1., 50.])

    fig.savefig(
            ''.join([util.fig_dir(),
                observable, '.posterior_prediction',
                '.Mr', str(data_dict['Mr']), '_Nmock', str(data_dict['Nmock']),
                '.pdf']),
            bbox_inches='tight')
예제 #13
0
def mcmc_ipython_par(Nwalkers,
                     Nchains_burn,
                     Nchains_pro,
                     observables=['nbar', 'xi'],
                     data_dict={
                         'Mr': 20,
                         'Nmock': 500
                     },
                     prior_name='first_try',
                     threads=1):
    '''
    Standard MCMC implementaion
    

    Parameters
    -----------
    - Nwalker : 
        Number of walkers
    - Nchains_burn : 
        Number of burn-in chains
    - Nchains_pro : 
        Number of production chains   
    - observables : 
        list of observables. Options are 'nbar', 'gmf', 'xi'
    - data_dict : dictionary that specifies the observation keywords
    '''
    # data observables
    fake_obs = []  # list of observables
    fake_obs_cov = []
    for obv in observables:
        if obv == 'nbar':
            data_nbar, data_nbar_var = Data.data_nbar(**data_dict)
            fake_obs.append(data_nbar)
            fake_obs_cov.append(data_nbar_var)
        if obv == 'gmf':
            data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict)
            fake_obs.append(data_gmf)
            fake_obs_cov.append(data_gmf)
        if obv == 'xi':
            # import xir and full covariance matrix of xir
            data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict)
            data_xi_invcov = Data.data_xi_inv_cov(**data_dict)
            fake_obs.append(data_xi)
            fake_obs_cov.append(data_xi_invcov)

    # True HOD parameters
    data_hod_dict = Data.data_hod_param(Mr=data_dict['Mr'])
    data_hod = np.array([
        data_hod_dict['logM0'],  # log M0 
        np.log(data_hod_dict['sigma_logM']),  # log(sigma)
        data_hod_dict['logMmin'],  # log Mmin
        data_hod_dict['alpha'],  # alpha
        data_hod_dict['logM1']  # log M1
    ])
    Ndim = len(data_hod)

    # Priors
    prior_min, prior_max = PriorRange(prior_name)
    prior_range = np.zeros((len(prior_min), 2))
    prior_range[:, 0] = prior_min
    prior_range[:, 1] = prior_max

    # Initializing Walkers
    random_guess = np.array([11., np.log(.4), 11.5, 1.0, 13.5])
    pos0 = np.repeat(random_guess, Nwalkers).reshape(Ndim, Nwalkers).T + \
            1e-1 * np.random.randn(Ndim * Nwalkers).reshape(Nwalkers, Ndim)

    # Initializing the emcee sampler
    hod_kwargs = {
        'prior_range': prior_range,
        'data': fake_obs,
        'data_cov': fake_obs_cov,
        'observables': observables,
        'Mr': data_dict['Mr']
    }
    # Set up the interface to the ipcluster.
    c = Client()
    view = c[:]
    view.push({"lnPost": lnPost})

    # Modules necessary in posterior calculation should be called here
    view.execute("import numpy as np")
    view.execute("from hod_sim import HODsimulator")

    # Setting up the Sampler
    sampler = emcee.EnsembleSampler(Nwalkers,
                                    Ndim,
                                    lnPost,
                                    kwargs=hod_kwargs,
                                    pool=view)

    # Setting up a file for saving the chains
    chain_file = ''.join([
        util.dat_dir(),
        util.observable_id_flag(observables), '_Mr',
        str(data_dict["Mr"]), '_theta.mcmc_chain.dat'
    ])
    f = open(chain_file, "w")
    f.close()

    # Running the Sampler and writing out the chains
    for result in sampler.sample(pos0,
                                 iterations=Nchains_burn + Nchains_pro,
                                 storechain=False):
        position = result[0]
        f = open(chain_file, "a")
        for k in range(position.shape[0]):
            output_str = '\t'.join(position[k].astype('str')) + '\n'
            f.write(output_str)
        f.close()
예제 #14
0
def plot_gmf_model(file_name="infered_hod_file_name", observables=['gmf'], 
                   Mr=20, data_dict={'Mr':20, 'Nmock':500}):
    
    """
    GMF model 1\sigma & 2\sigma model predictions 
    """    
    #load the data   
    theta = np.loadtxt(file_name+".dat")[:3] 
    
    for obv in observables: 
        if obv == 'gmf': 
            data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict)
    
    mod_gmf = []

    for i in xrange(len(theta)):
        
        mod_gmf.append(HODsimulator(theta[i], prior_range=None, observables=['xi'], Mr=20))
 
    mod_gmf = np.array(mod_gmf)
    bins = gmf_bins
    a, b, c, d, e = np.percentile(mod_gmf, [2.5, 16, 50, 84, 97.5], axis=0)

    
    fig1 = plt.figure()
    ax = fig1.add_subplot(111)

    
    ax.fill_between(bins, a, e, color="k", alpha=0.1, edgecolor="none")
    ax.fill_between(bins, b, d, color="k", alpha=0.3, edgecolor="none")
    #ax.plot(bins, c, "k", lw=1)
    ax.errorbar(bins, data_gmf, yerr = data_gmf_sigma, fmt=".k",
                capsize=0)    
    xlabel = ax.set_xlabel(r'Group richness', fontsize=20)
    ylabel = ax.set_ylabel(r'GMF$[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25)
    
    plt.yscale('log')
    plt.xlim(xmin = 1 , xmax = 50)
    fig1.savefig('../figs/gmf_posterior_prediction'+str(Mr)+'.pdf', 
	        bbox_extra_artists=[xlabel, ylabel], bbox_inches='tight') 


    
    fig2 = plt.figure()
    ax = fig2.add_subplot(111)

    ax.fill_between(bins, a - data_gmf, e-data_gmf, color="k", alpha=0.1, edgecolor="none")
    ax.fill_between(bins, b - data_gmf, d-data_gmf, color="k", alpha=0.3, edgecolor="none")
    #ax.plot(rr, c, "k", lw=1) we don't care about the best fit here
    ax.errorbar(bins, data_gmf - data_gmf, yerr = data_gmf_sigma, fmt=".k",capsize=0)

    xlabel = ax.set_xlabel(r'Group richness', fontsize=20)
    ylabel = ax.set_ylabel(r'$\Delta$GMF$[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25)
    plt.xlim(xmin = 1 , xmax = 50)
    fig2.savefig('../figs/gmf_residual_posterior_prediction'+str(Mr)+'.pdf',
                bbox_extra_artists=[xlabel, ylabel], bbox_inches='tight')


    fig3, axes = pl.subplots(2, 1, figsize=(10, 8) , sharex = True)
    fig3.subplots_adjust(wspace=0.0  , hspace=0.4)

    ax1 = axes[0,0]
    
    ax1.fill_between(bins, a, e, color="k", alpha=0.1, edgecolor="none")
    ax1.fill_between(bins, b, d, color="k", alpha=0.3, edgecolor="none")
    #ax1.plot(rr, c, "k", lw=1)
    ax1.errorbar(bins, data_xi, yerr = data_gmf_sigma, fmt=".k",
                capsize=0)    
    ylabel = ax.set_ylabel(r'$\Delta$GMF$[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25)
    ax1.set_yscale("log")
    ax1_set.xlim(xmin = 1 , xmax = 50)
    
    ax2 = axes[0,0]
    ax2.fill_between(rr, a - data_gmf, e- data_gmf, color="k", alpha=0.1, edgecolor="none")
    ax2.fill_between(rr, b- data_gmf, d- data_gmf, color="k", alpha=0.3, edgecolor="none")
    #ax2.plot(rr, c, "k", lw=1)
    ax2.errorbar(rr, data_gmf- data_gmf , yerr = data_gmf_sigma, fmt=".k",
                capsize=0)    
    ax2_set.xlim(xmin = 1 , xmax = 50)
    ylabel = ax.set_ylabel(r'$\Delta$GMF$[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25)

    fig3.savefig('../figs/gmf&residual_posterior_prediction'+str(Mr)+'.pdf', bbox_extra_artists=[xlabel, ylabel], bbox_inches='tight')
예제 #15
0
def mcmc_mpi(Nwalkers,
             Nchains,
             observables=['nbar', 'xi'],
             data_dict={
                 'Mr': 21,
                 'b_normal': 0.25
             },
             prior_name='first_try',
             mcmcrun=None):
    '''
    Standard MCMC implementaion
    
    Parameters
    -----------
    - Nwalker : 
        Number of walkers
    - Nchains : 
        Number of MCMC chains   
    - observables : 
        list of observables. Options are: ['nbar','xi'],['nbar','gmf'],['xi']
    - data_dict : dictionary that specifies the observation keywords
    '''
    #Initializing the vector of observables and inverse covariance matrix
    if observables == ['xi']:
        fake_obs = Data.data_xi(**data_dict)
        #fake_obs_icov = Data.data_inv_cov('xi', **data_dict)
        fake_obs_icov = Data.data_cov(inference='mcmc', **data_dict)[1:16,
                                                                     1:16]
    if observables == ['nbar', 'xi']:
        fake_obs = np.hstack(
            [Data.data_nbar(**data_dict),
             Data.data_xi(**data_dict)])
        fake_obs_icov = Data.data_cov(inference='mcmc', **data_dict)[:16, :16]
    if observables == ['nbar', 'gmf']:
        ##### FIRST BIN OF GMF DROPPED ###############
        # CAUTION: hardcoded
        fake_obs = np.hstack(
            [Data.data_nbar(**data_dict),
             Data.data_gmf(**data_dict)[1:]])
        fake_obs_icov = np.zeros((10, 10))
        #print Data.data_cov(**data_dict)[17: , 17:].shape

        # Covariance matrix being adjusted accordingly
        fake_obs_icov[1:, 1:] = Data.data_cov(inference='mcmc',
                                              **data_dict)[17:, 17:]
        fake_obs_icov[0, 1:] = Data.data_cov(inference='mcmc',
                                             **data_dict)[0, 17:]
        fake_obs_icov[1:, 0] = Data.data_cov(inference='mcmc',
                                             **data_dict)[17:, 0]
        fake_obs_icov[0, 0] = Data.data_cov(inference='mcmc', **data_dict)[0,
                                                                           0]

    # True HOD parameters
    data_hod_dict = Data.data_hod_param(Mr=data_dict['Mr'])
    data_hod = np.array([
        data_hod_dict['logM0'],  # log M0 
        np.log(data_hod_dict['sigma_logM']),  # log(sigma)
        data_hod_dict['logMmin'],  # log Mmin
        data_hod_dict['alpha'],  # alpha
        data_hod_dict['logM1']  # log M1
    ])
    Ndim = len(data_hod)

    # Priors
    prior_min, prior_max = PriorRange(prior_name)
    prior_range = np.zeros((len(prior_min), 2))
    prior_range[:, 0] = prior_min
    prior_range[:, 1] = prior_max

    # mcmc chain output file
    chain_file = ''.join([
        util.mcmc_dir(),
        util.observable_id_flag(observables), '.', mcmcrun, '.mcmc_chain.dat'
    ])
    #print chain_file

    if os.path.isfile(chain_file) and continue_chain:
        print 'Continuing previous MCMC chain!'
        sample = np.loadtxt(chain_file)
        Nchain = Niter - (len(sample) / Nwalkers
                          )  # Number of chains left to finish
        if Nchain > 0:
            pass
        else:
            raise ValueError
        print Nchain, ' iterations left to finish'

        # Initializing Walkers from the end of the chain
        pos0 = sample[-Nwalkers:]
    else:
        # new chain
        f = open(chain_file, 'w')
        f.close()
        Nchain = Niter

        # Initializing Walkers
        random_guess = data_hod
        pos0 = np.repeat(random_guess, Nwalkers).reshape(Ndim, Nwalkers).T + \
                         5.e-2 * np.random.randn(Ndim * Nwalkers).reshape(Nwalkers, Ndim)
        #print pos0.shape
    # Initializing MPIPool
    pool = MPIPool()
    if not pool.is_master():
        pool.wait()
        sys.exit(0)

    # Initializing the emcee sampler
    hod_kwargs = {
        'prior_range': prior_range,
        'data': fake_obs,
        'data_icov': fake_obs_icov,
        'observables': observables,
        'Mr': data_dict['Mr']
    }
    sampler = emcee.EnsembleSampler(Nwalkers,
                                    Ndim,
                                    lnPost,
                                    pool=pool,
                                    kwargs=hod_kwargs)

    # Initializing Walkers
    for result in sampler.sample(pos0, iterations=Nchain, storechain=False):
        position = result[0]
        #print position
        f = open(chain_file, 'a')
        for k in range(position.shape[0]):
            output_str = '\t'.join(position[k].astype('str')) + '\n'
            f.write(output_str)
        f.close()

    pool.close()
예제 #16
0
파일: paper.py 프로젝트: mjvakili/ccppabc
def PosteriorObservable(Mr=21, b_normal=0.25, clobber=False):
    ''' Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood
    '''
    prettyplot()
    pretty_colors=prettycolors()
    fig = plt.figure(1, figsize=(16,12))
    gs = gridspec.GridSpec(2, 2, height_ratios=[2.5, 1], width_ratios=[1,1]) 

    for obvs in ['nbargmf', 'nbarxi']: 

        if obvs == 'nbargmf':
            result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/run1/',])
            theta_file = lambda tt: ''.join([result_dir, 'nbar_gmf_theta_t', str(tt), '.ABCnbargmf.dat']) 
            tf = 8 
            obvs_list = ['gmf']
        elif obvs == 'nbarxi':
            result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/'])
            theta_file = lambda tt: ''.join([result_dir, 'nbar_xi_theta_t', str(tt), '.abc.dat']) 
            tf = 9
            obvs_list = ['xi']
        else:
            raise ValueError

        theta = np.loadtxt(theta_file(tf))  # import thetas
        #theta = theta[:10]
        
        obvs_file = ''.join(theta_file(tf).rsplit('.dat')[:-1] + ['.', obvs_list[0], '.p'])
        print obvs_file
        
        HODsimulator = ABC_HODsim(Mr=Mr, b_normal=b_normal)
        if not os.path.isfile(obvs_file) or clobber:
            model_obv = [] 
            for i in xrange(len(theta)):
                print i 
                obv_i = HODsimulator(
                        theta[i], 
                        prior_range=None,
                        observables=obvs_list)
                model_obv.append(obv_i[0])
            model_obv = np.array(model_obv)
            pickle.dump(model_obv, open(obvs_file, 'wb'))
        else:
            model_obv = pickle.load(open(obvs_file, 'rb'))

        if 'xi' in obvs:
            r_bin = Data.data_xi_bin(Mr=Mr)
        elif 'gmf' in obvs:
            r_binedge = Data.data_gmf_bins()
            r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:]) 

        a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5], axis=0)

        # plotting
        if obvs == 'nbarxi': 
            ax = plt.subplot(gs[0])
        elif obvs == 'nbargmf': 
            ax = plt.subplot(gs[1])

        if 'xi' in obvs:  # 2PCF
            xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal)
            cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') 
            data_xi_cov = cov_data[1:16, 1:16]

            ax.fill_between(r_bin, a, e, color=pretty_colors[3], alpha=0.3, edgecolor="none")
            ax.fill_between(r_bin, b, d, color=pretty_colors[3], alpha=0.5, edgecolor="none")
            ax.errorbar(r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k', 
                    markersize=0, lw=0, capsize=3, elinewidth=1.5)
            ax.scatter(r_bin, xi_data, c='k', s=10, lw=0)
            ax.set_ylabel(r'$\xi_\mathtt{gg}(\mathtt{r})$', fontsize=27)
            ax.set_yscale('log') 
            ax.set_xscale('log')
            ax.set_xticklabels([])
            ax.set_xlim([0.1, 20.])
            ax.set_ylim([0.09, 1000.])

            ax = plt.subplot(gs[2])
            ax.fill_between(r_bin, a/xi_data, e/xi_data, color=pretty_colors[3], alpha=0.3, edgecolor="none")
            ax.fill_between(r_bin, b/xi_data, d/xi_data, color=pretty_colors[3], alpha=0.5, edgecolor="none")
            ax.errorbar(r_bin, np.repeat(1., len(xi_data)), yerr=np.sqrt(np.diag(data_xi_cov))/xi_data, 
                    fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5)
            ax.plot(np.arange(0.1, 20., 0.1), np.repeat(1., len(np.arange(0.1, 20, 0.1))), c='k', ls='--', lw=2)
            ax.set_xlim([0.1, 20.])
            ax.set_xscale('log') 
            ax.set_ylim([0.5, 1.5]) 
            ax.set_xlabel(r'$\mathtt{r}\;[\mathtt{Mpc}/h]$', fontsize=25)
            ax.set_ylabel(r'$\xi_\mathtt{gg}/\xi_\mathtt{gg}^\mathtt{obvs}$', fontsize=25)

        elif 'gmf' in obvs:   # GMF
            data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal)
            cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') 
            data_gmf_cov = cov_data[16:, 16:]

            ax.fill_between(r_bin, a, e, color=pretty_colors[3], alpha=0.3, edgecolor="none")
            ax.fill_between(r_bin, b, d, color=pretty_colors[3], alpha=0.5, edgecolor="none", label='ABC Posterior')
            ax.errorbar(r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)), fmt="o", color='k', 
                    markersize=0, lw=0, capsize=4, elinewidth=2)
            ax.scatter(r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation')
            ax.legend(loc='upper right', scatterpoints=1, prop={'size': 25}, borderpad=1.0)

            ax.yaxis.tick_right()
            ax.yaxis.set_ticks_position('both')
            ax.yaxis.set_label_position('right') 
            ax.set_ylabel(r'$\zeta$ $[(\mathrm{h}/\mathtt{Mpc})^{3}]$', fontsize=25)

            ax.set_yscale('log')
            ax.set_xlim([1., 20.])
            ax.set_xticklabels([])
            ax.set_ylim([10.**-7.2, 2*10**-4.])

            ax = plt.subplot(gs[3])
            ax.fill_between(r_bin, a/data_gmf, e/data_gmf, color=pretty_colors[3], alpha=0.3, edgecolor="none")
            ax.fill_between(r_bin, b/data_gmf, d/data_gmf, color=pretty_colors[3], alpha=0.5, edgecolor="none")

            ax.errorbar(r_bin, np.repeat(1., len(data_gmf)), yerr=np.sqrt(np.diag(data_gmf_cov))/data_gmf, 
                    fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5)
            ax.plot(np.arange(1., 20., 1), np.repeat(1., len(np.arange(1., 20, 1))), c='k', ls='--', lw=1.75)

            ax.yaxis.tick_right()
            ax.yaxis.set_label_position('right') 
            ax.set_ylim([-0.1, 2.1])
            ax.set_ylabel(r'$\zeta/\zeta^\mathtt{obvs}$', fontsize=25)
            ax.set_xlim([1., 20.])
            ax.set_xlabel(r'$\mathtt{N}$ [Group Richness]', fontsize=25)

    fig.subplots_adjust(wspace=0.05, hspace=0.0)
    fig_name = ''.join([ut.fig_dir(), 
        'paper', 
        '.ABCposterior', 
        '.pdf'])
    fig.savefig(fig_name, bbox_inches='tight')
    plt.close()
    return None 
예제 #17
0
def mcmc_ipython_par(Nwalkers, Nchains_burn, Nchains_pro, observables=['nbar', 'xi'], 
        data_dict={'Mr':20, 'Nmock':500}, prior_name = 'first_try', threads=1): 
    '''
    Standard MCMC implementaion
    

    Parameters
    -----------
    - Nwalker : 
        Number of walkers
    - Nchains_burn : 
        Number of burn-in chains
    - Nchains_pro : 
        Number of production chains   
    - observables : 
        list of observables. Options are 'nbar', 'gmf', 'xi'
    - data_dict : dictionary that specifies the observation keywords
    '''
    # data observables
    fake_obs = []       # list of observables 
    fake_obs_cov = [] 
    for obv in observables: 
        if obv == 'nbar': 
            data_nbar, data_nbar_var = Data.data_nbar(**data_dict)
            fake_obs.append(data_nbar)
            fake_obs_cov.append(data_nbar_var)
        if obv == 'gmf': 
            data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict)
            fake_obs.append(data_gmf)
            fake_obs_cov.append(data_gmf)
        if obv == 'xi': 
            # import xir and full covariance matrix of xir
            data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict)   
            data_xi_invcov = Data.data_xi_inv_cov(**data_dict)
            fake_obs.append(data_xi)
            fake_obs_cov.append(data_xi_invcov)

    # True HOD parameters
    data_hod_dict = Data.data_hod_param(Mr=data_dict['Mr'])
    data_hod = np.array([
        data_hod_dict['logM0'],                 # log M0 
        np.log(data_hod_dict['sigma_logM']),    # log(sigma)
        data_hod_dict['logMmin'],               # log Mmin
        data_hod_dict['alpha'],                 # alpha
        data_hod_dict['logM1']                  # log M1
        ])
    Ndim = len(data_hod)
    
    # Priors
    prior_min, prior_max = PriorRange(prior_name)
    prior_range = np.zeros((len(prior_min),2))
    prior_range[:,0] = prior_min
    prior_range[:,1] = prior_max
    
    # Initializing Walkers 
    random_guess = np.array([11. , np.log(.4) , 11.5 , 1.0 , 13.5])
    pos0 = np.repeat(random_guess, Nwalkers).reshape(Ndim, Nwalkers).T + \
            1e-1 * np.random.randn(Ndim * Nwalkers).reshape(Nwalkers, Ndim)

    # Initializing the emcee sampler
    hod_kwargs = {
            'prior_range': prior_range, 
            'data': fake_obs, 
            'data_cov': fake_obs_cov, 
            'observables': observables, 
            'Mr': data_dict['Mr']
            }
    # Set up the interface to the ipcluster.
    c = Client()
    view = c[:]
    view.push({"lnPost": lnPost})
     
    # Modules necessary in posterior calculation should be called here
    view.execute("import numpy as np")
    view.execute("from hod_sim import HODsimulator")

    # Setting up the Sampler
    sampler = emcee.EnsembleSampler(Nwalkers, Ndim, lnPost, kwargs=hod_kwargs, pool = view)

    # Setting up a file for saving the chains
    chain_file = ''.join([util.dat_dir(), 
        util.observable_id_flag(observables), 
        '_Mr', str(data_dict["Mr"]), '_theta.mcmc_chain.dat'])
    f = open(chain_file, "w")
    f.close()

    # Running the Sampler and writing out the chains
    for result in sampler.sample(pos0, iterations=Nchains_burn + Nchains_pro, storechain=False):
        position = result[0]
        f = open(chain_file, "a")
        for k in range(position.shape[0]):
	    output_str = '\t'.join(position[k].astype('str')) + '\n'
            f.write(output_str)
        f.close()
예제 #18
0
def mcmc_mpi(
    Nwalkers,
    Nchains,
    observables=["nbar", "xi"],
    data_dict={"Mr": 21, "b_normal": 0.25},
    prior_name="first_try",
    mcmcrun=None,
):
    """
    Standard MCMC implementaion
    
    Parameters
    -----------
    - Nwalker : 
        Number of walkers
    - Nchains : 
        Number of MCMC chains   
    - observables : 
        list of observables. Options are: ['nbar','xi'],['nbar','gmf'],['xi']
    - data_dict : dictionary that specifies the observation keywords
    """
    # Initializing the vector of observables and inverse covariance matrix
    if observables == ["xi"]:
        fake_obs = Data.data_xi(**data_dict)
        # fake_obs_icov = Data.data_inv_cov('xi', **data_dict)
        fake_obs_icov = Data.data_cov(inference="mcmc", **data_dict)[1:16, 1:16]
    if observables == ["nbar", "xi"]:
        fake_obs = np.hstack([Data.data_nbar(**data_dict), Data.data_xi(**data_dict)])
        fake_obs_icov = Data.data_cov(inference="mcmc", **data_dict)[:16, :16]
    if observables == ["nbar", "gmf"]:
        ##### FIRST BIN OF GMF DROPPED ###############
        # CAUTION: hardcoded
        fake_obs = np.hstack([Data.data_nbar(**data_dict), Data.data_gmf(**data_dict)[1:]])
        fake_obs_icov = np.zeros((10, 10))
        # print Data.data_cov(**data_dict)[17: , 17:].shape

        # Covariance matrix being adjusted accordingly
        fake_obs_icov[1:, 1:] = Data.data_cov(inference="mcmc", **data_dict)[17:, 17:]
        fake_obs_icov[0, 1:] = Data.data_cov(inference="mcmc", **data_dict)[0, 17:]
        fake_obs_icov[1:, 0] = Data.data_cov(inference="mcmc", **data_dict)[17:, 0]
        fake_obs_icov[0, 0] = Data.data_cov(inference="mcmc", **data_dict)[0, 0]

    # True HOD parameters
    data_hod_dict = Data.data_hod_param(Mr=data_dict["Mr"])
    data_hod = np.array(
        [
            data_hod_dict["logM0"],  # log M0
            np.log(data_hod_dict["sigma_logM"]),  # log(sigma)
            data_hod_dict["logMmin"],  # log Mmin
            data_hod_dict["alpha"],  # alpha
            data_hod_dict["logM1"],  # log M1
        ]
    )
    Ndim = len(data_hod)

    # Priors
    prior_min, prior_max = PriorRange(prior_name)
    prior_range = np.zeros((len(prior_min), 2))
    prior_range[:, 0] = prior_min
    prior_range[:, 1] = prior_max

    # mcmc chain output file
    chain_file = "".join([util.mcmc_dir(), util.observable_id_flag(observables), ".", mcmcrun, ".mcmc_chain.dat"])
    # print chain_file

    if os.path.isfile(chain_file) and continue_chain:
        print "Continuing previous MCMC chain!"
        sample = np.loadtxt(chain_file)
        Nchain = Niter - (len(sample) / Nwalkers)  # Number of chains left to finish
        if Nchain > 0:
            pass
        else:
            raise ValueError
        print Nchain, " iterations left to finish"

        # Initializing Walkers from the end of the chain
        pos0 = sample[-Nwalkers:]
    else:
        # new chain
        f = open(chain_file, "w")
        f.close()
        Nchain = Niter

        # Initializing Walkers
        random_guess = data_hod
        pos0 = np.repeat(random_guess, Nwalkers).reshape(Ndim, Nwalkers).T + 5.0e-2 * np.random.randn(
            Ndim * Nwalkers
        ).reshape(Nwalkers, Ndim)
        # print pos0.shape
    # Initializing MPIPool
    pool = MPIPool()
    if not pool.is_master():
        pool.wait()
        sys.exit(0)

    # Initializing the emcee sampler
    hod_kwargs = {
        "prior_range": prior_range,
        "data": fake_obs,
        "data_icov": fake_obs_icov,
        "observables": observables,
        "Mr": data_dict["Mr"],
    }
    sampler = emcee.EnsembleSampler(Nwalkers, Ndim, lnPost, pool=pool, kwargs=hod_kwargs)

    # Initializing Walkers
    for result in sampler.sample(pos0, iterations=Nchain, storechain=False):
        position = result[0]
        # print position
        f = open(chain_file, "a")
        for k in range(position.shape[0]):
            output_str = "\t".join(position[k].astype("str")) + "\n"
            f.write(output_str)
        f.close()

    pool.close()
예제 #19
0
def plot_posterior_model(observable,
                         abc_theta_file=None,
                         data_dict={
                             'Mr': 20,
                             'b_normal': 0.25,
                             'Nmock': 500
                         },
                         clobber=False):
    '''
    Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood

    Parameters
    ----------
    observable : string
        One of the following strings ['xi', 'scaledxi', 'gmf']

    '''
    # load the particles
    if abc_theta_file is None:
        raise ValueError(
            "Please specify the theta output file from ABC-PMC run")
    theta = np.loadtxt(abc_theta_file)

    if observable == 'scaledxi':
        obvs_str = 'xi'
    else:
        obvs_str = observable

    obvs_file = ''.join(
        abc_theta_file.rsplit('.dat')[:-1] + ['.', observable, '.dat'])
    print obvs_file
    if not os.path.isfile(obvs_file) or clobber:
        for i in xrange(len(theta)):
            obv_i = HODsimulator(theta[i],
                                 prior_range=None,
                                 observables=[obvs_str],
                                 Mr=data_dict['Mr'])
            try:
                model_obv.append(obv_i[0])
            except UnboundLocalError:
                model_obv = [obv_i[0]]
        model_obv = np.array(model_obv)
        np.savetxt(obvs_file, model_obv)
    else:
        model_obv = np.loadtxt(obvs_file)

    if 'xi' in observable:
        r_bin = Data.data_xi_bins(Mr=data_dict['Mr'])
    elif observable == 'gmf':
        r_bin = Data.data_gmf_bins()

    a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5], axis=0)

    # plotting
    fig = plt.figure(1)
    ax = fig.add_subplot(111)

    if observable == 'xi':  # 2PCF
        data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict)  # data

        ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none")
        ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none")
        #ax.plot(r_bin, c, "k", lw=1)
        ax.errorbar(r_bin,
                    data_xi,
                    yerr=np.sqrt(np.diag(data_xi_cov)),
                    fmt=".k",
                    capsize=0)
        ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20)
        ax.set_ylabel(r'$\xi_{\rm gg}$', fontsize=25)
        ax.set_xscale('log')
        ax.set_xlim([0.1, 20.])

    elif observable == 'scaledxi':  # Scaled 2PFC (r * xi)
        data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict)  # data

        ax.fill_between(r_bin,
                        r_bin * a,
                        r_bin * e,
                        color="k",
                        alpha=0.1,
                        edgecolor="none")
        ax.fill_between(r_bin,
                        r_bin * b,
                        r_bin * d,
                        color="k",
                        alpha=0.3,
                        edgecolor="none")
        ax.errorbar(r_bin,
                    r_bin * data_xi,
                    yerr=r_bin * np.sqrt(np.diag(data_xi_cov)),
                    fmt=".k",
                    capsize=0)
        ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20)
        ax.set_ylabel(r'$r \xi_{\rm gg}$', fontsize=25)
        ax.set_xscale('log')
        ax.set_xlim([0.1, 20.])

    elif observable == 'gmf':  # GMF
        data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict)
        ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none")
        ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none")
        ax.errorbar(r_bin, data_gmf, yerr=data_gmf_sigma, fmt=".k", capsize=0)
        ax.set_xlabel(r'Group Richness', fontsize=25)
        ax.set_ylabel(r'GMF $[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25)

        ax.set_yscale('log')
        ax.set_xlim([1., 50.])

    fig.savefig(''.join([
        util.fig_dir(), observable, '.posterior_prediction', '.Mr',
        str(data_dict['Mr']), '_Nmock',
        str(data_dict['Nmock']), '.pdf'
    ]),
                bbox_inches='tight')