def invC(mock, ell=0, rebin=None): ''' ***TESTED*** Test inverting the covariance matrix. This is inspired by the fact that the original binning of Nseries P(k) is not invertible... ''' pkay = Data.Pk() n_mock = pkay._n_mock(mock) for i in range(1, n_mock+1): pkay.Read(mock, i, NorS='ngc') k, pk = pkay.k, pkay.pk if i == 1: pks = np.zeros((len(k), n_mock)) pks[:, i-1] = pk C_pk = np.cov(pks.T) # covariance matrix invC_pk = np.linalg.inv(C_pk) fig = plt.figure(figsize=(10,5)) sub = fig.add_subplot(1,2,1) im = sub.imshow(C_pk, interpolation='None') fig.colorbar(im, ax=sub) sub = fig.add_subplot(1,2,2) im = sub.imshow(invC_pk, interpolation='None') fig.colorbar(im, ax=sub) plt.show() return None
def lnPost(zbin=1): ''' *** TESTED *** Likelihood and posterior functions can reproduce the chi-squared from Florian's MCMC chain. Test that the ln(Posterior) reproduces the chi-squared values of Florian's MCMC chains ''' # read in Florian's chains chain_file = ''.join([ UT.dat_dir(), 'Beutler/public_full_shape/', 'Beutler_et_al_full_shape_analysis_z', str(zbin), '_chain', str(0), '.dat' ]) sample = np.loadtxt(chain_file, skiprows=1) chi2s = sample[:, -1] sample = sample[:, 1:-1] # read in BOSS P(k) NGC + SGC pkay = Dat.Pk() k0, p0k_ngc = pkay.Observation(0, zbin, 'ngc') k2, p2k_ngc = pkay.Observation(2, zbin, 'ngc') k4, p4k_ngc = pkay.Observation(4, zbin, 'ngc') k0, p0k_sgc = pkay.Observation(0, zbin, 'sgc') k2, p2k_sgc = pkay.Observation(2, zbin, 'sgc') k4, p4k_sgc = pkay.Observation(4, zbin, 'sgc') k_list = [k0, k2, k4] pk_ngc_list = [p0k_ngc, p2k_ngc, p4k_ngc] pk_sgc_list = [p0k_sgc, p2k_sgc, p4k_sgc] # read in Covariance matrix # currently for testing purposes, # implemented to read in Florian's covariance matrix _, _, C_pk_ngc = Dat.beutlerCov(zbin, NorS='ngc', ell='all') _, _, C_pk_sgc = Dat.beutlerCov(zbin, NorS='sgc', ell='all') # calculate precision matrices (including the hartlap factor) n_mocks_ngc = 2045 n_mocks_sgc = 2048 f_hartlap_ngc = (float(n_mocks_ngc) - float( len(np.concatenate(pk_ngc_list))) - 2.) / (float(n_mocks_ngc) - 1.) f_hartlap_sgc = (float(n_mocks_sgc) - float( len(np.concatenate(pk_sgc_list))) - 2.) / (float(n_mocks_sgc) - 1.) Cinv_ngc = np.linalg.inv(C_pk_ngc) Cinv_sgc = np.linalg.inv(C_pk_sgc) Cinv_ngc *= f_hartlap_ngc Cinv_sgc *= f_hartlap_sgc lnpost_args = (k_list, pk_ngc_list, pk_sgc_list, Cinv_ngc, Cinv_sgc) for i in range(10): print('Like', -2. * Inf.lnLike(sample[i, :], *lnpost_args)) print('Post', -2. * Inf.lnPost(sample[i, :], *lnpost_args)) print('chi2', chi2s[i]) return None
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
def X_pk(mock, ell=0, krange=None, NorS='ngc', sys='fc', k_arr=False): ''' Construct data matrix X from P(k) measures of mock catalogs. X_i = P(k)_i X has N_mock x N_k dimensions. ''' pkay = Data.Pk() # read in P(k) data n_mock = pkay._n_mock(mock) for i in range(1, n_mock + 1): pkay.Read(mock, i, ell=ell, NorS=NorS, sys=sys) pkay.krange(krange) k, pk = pkay.k, pkay.pk if i == 1: pks = np.zeros((n_mock, len(k))) pks[i - 1, :] = pk if k_arr: return pks, k return pks
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
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
def mcmc(tag=None, zbin=1, nwalkers=48, Nchains=4, minlength=600, likelihood='pseudo'): ''' Parameters ---------- Nchains : int Number of independent chains to run for the gelman rubin convergence test ''' if tag is None: raise ValueError("specify a tag, otherwise it's confusing") temperature = 2.e-3 # temperature # read in BOSS P(k) NGC pkay = Dat.Pk() k0, p0k_ngc = pkay.Observation(0, zbin, 'ngc') k2, p2k_ngc = pkay.Observation(2, zbin, 'ngc') k4, p4k_ngc = pkay.Observation(4, zbin, 'ngc') pk_ngc_list = [p0k_ngc, p2k_ngc, p4k_ngc] k_list = [k0, k2, k4] # read in BOSS P(k) SGC k0, p0k_sgc = pkay.Observation(0, zbin, 'sgc') k2, p2k_sgc = pkay.Observation(2, zbin, 'sgc') k4, p4k_sgc = pkay.Observation(4, zbin, 'sgc') pk_sgc_list = [p0k_sgc, p2k_sgc, p4k_sgc] if likelihood == 'psuedo': # standard pseudo Gaussian likelihood # read in Covariance matrix # currently for testing purposes, # implemented to read in Florian's covariance matrix _, _, C_pk_ngc = Dat.beutlerCov(zbin, NorS='ngc', ell='all') _, _, C_pk_sgc = Dat.beutlerCov(zbin, NorS='sgc', ell='all') # calculate precision matrices (including the hartlap factor) Cinv_ngc = np.linalg.inv(C_pk_ngc) Cinv_sgc = np.linalg.inv(C_pk_sgc) # hartlap factor n_mocks_ngc = 2045 n_mocks_sgc = 2048 f_hartlap_ngc = (float(n_mocks_ngc) - float( len(np.concatenate(pk_ngc_list))) - 2.) / (float(n_mocks_ngc) - 1.) f_hartlap_sgc = (float(n_mocks_sgc) - float( len(np.concatenate(pk_sgc_list))) - 2.) / (float(n_mocks_sgc) - 1.) Cinv_ngc *= f_hartlap_ngc Cinv_sgc *= f_hartlap_sgc # ln Posterior function lnPost = lnPost_pseudo # args for ln Posterior function # data ks, BOSS NGC P_l(k), BOSS SGC P_l(k), NGC precision matrix, SGC precision matrix lnpost_args = (k_list, pk_ngc_list, pk_sgc_list, Cinv_ngc, Cinv_sgc) elif likelihood in ['pca', 'ica']: # read in patchy mock P(k)s for ngc and sgc pk_ngc_list, pk_sgc_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.z' + str(kwargs['zbin']), krange=[0.01, kmax], ell=ell, NorS='ngc', sys='fc')) pk_sgc_list.append( NG.X_pk('patchy.z' + str(kwargs['zbin']), krange=[0.01, kmax], ell=ell, NorS='sgc', sys='fc')) pk_ngc_mock = np.concatenate(pk_ngc_list, axis=1) pk_sgc_mock = np.concatenate(pk_sgc_list, axis=1) else: raise NotImplementedError if zbin == 1: # 0.2 < z < 0.5 # maximum likelihood value start = np.array([ 1.008, 1.001, 0.478, 1.339, 1.337, 1.16, 0.32, -1580., -930., 6.1, 6.8 ]) ndim = len(start) # initialize MPI pool try: pool = MPIPool() if not pool.is_master(): pool.wait() sys.exit(0) except ValueError: pool = None print("initializing ", Nchains, " independent emcee chains") pos, samplers = [], [] for ichain in range(Nchains): pos.append([ start + temperature * start * (2. * np.random.random_sample(ndim) - 1.) for i in range(nwalkers) ]) samplers.append( emcee.EnsembleSampler(nwalkers, ndim, lnPost, args=lnpost_args, pool=pool)) # Start MCMC print("Running MCMC...") withinchainvar = np.zeros((Nchains, ndim)) meanchain = np.zeros((Nchains, ndim)) scalereduction = np.repeat(2., ndim) # bunch of numbers for the mcmc run itercounter = 0 chainstep = minlength loop = 1 epsilon = 0.02 #0.02 ichaincheck = 100 rstate = np.random.get_state() while loop: itercounter += chainstep print("chain length =", itercounter) for jj in range(Nchains): for result in samplers[jj].sample(pos[jj], iterations=chainstep, rstate0=rstate, storechain=True): pos[jj] = result[0] chainchi2 = -2. * result[1] rstate = result[2] # append chain outputs to chain file chain_file = ''.join([ UT.dat_dir(), 'mcmc/', tag, '.chain', str(jj), '.zbin', str(zbin), '.dat' ]) f = open(chain_file, 'a') for k in range(pos[jj].shape[0]): output_str = '\t'.join( pos[jj][k].astype('str')) + '\t' + str( chainchi2[k]) + '\n' f.write(output_str) f.close() # we do the convergence test on the second half of the current chain (itercounter/2) chainsamples = samplers[jj].chain[:, itercounter / 2:, :].reshape( (-1, ndim)) withinchainvar[jj] = np.var(chainsamples, axis=0) meanchain[jj] = np.mean(chainsamples, axis=0) scalereduction = gelman_rubin_convergence(withinchainvar, meanchain, itercounter / 2, Nchains, ndim) print("scalereduction = ", scalereduction) loop = 0 for jj in range(ndim): if np.abs(1 - scalereduction[jj]) > epsilon: loopcriteria = 1 chainstep = ichaincheck if pool is not None: pool.close() return None
def W_importance(tag, chain, ica_algorithm=None, density_method='kde', n_comp_max=20, info_crit='bic', njobs=1, **kwargs): ''' Given a dictionary with the MCMC chain, evaluate the likelihood ratio ''' if 'RSD' in tag: # Florian's RSD analysis if 'zbin' not in kwargs.keys(): raise ValueError('specify zbin in kwargs') # read in BOSS P(k) (data D) k_list, pk_ngc_data, pk_sgc_data = [], [], [] pkay = Dat.Pk() for ell in [0, 2, 4]: k, plk_ngc = pkay.Observation(ell, kwargs['zbin'], 'ngc') _, plk_sgc = pkay.Observation(ell, kwargs['zbin'], 'sgc') k_list.append(k) pk_ngc_data.append(plk_ngc) pk_sgc_data.append(plk_sgc) pk_ngc_data = np.concatenate(pk_ngc_data) pk_sgc_data = np.concatenate(pk_sgc_data) 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) # calculate D - m(theta) for all the mcmc chain delta_ngc = chain['pk_ngc'] - pk_ngc_data delta_sgc = chain['pk_sgc'] - pk_sgc_data # import PATCHY mocks pk_ngc_list, pk_sgc_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.z' + str(kwargs['zbin']), krange=[0.01, kmax], ell=ell, NorS='ngc', sys='fc')) pk_sgc_list.append( NG.X_pk('patchy.z' + str(kwargs['zbin']), krange=[0.01, kmax], ell=ell, NorS='sgc', sys='fc')) pk_ngc_mock = np.concatenate(pk_ngc_list, axis=1) pk_sgc_mock = np.concatenate(pk_sgc_list, axis=1) if tag == 'RSD_pXiICA_gauss': # P_ICA(D - m(theta)) / P_PCA,Gauss(D - m(theta)) lnP_ica_ngc = NG.lnL_pXi_ICA(delta_ngc, pk_ngc_mock, ica_algorithm=ica_algorithm, density_method=density_method, n_comp_max=n_comp_max, info_crit=info_crit, njobs=njobs) lnP_ica_sgc = NG.lnL_pXi_ICA(delta_sgc, pk_sgc_mock, ica_algorithm=ica_algorithm, density_method=density_method, n_comp_max=n_comp_max, info_crit=info_crit, njobs=njobs) lnP_gauss_ngc = NG.lnL_pca_gauss(delta_ngc, pk_ngc_mock) lnP_gauss_sgc = NG.lnL_pca_gauss(delta_sgc, pk_sgc_mock) lnP_num = lnP_ica_ngc + lnP_ica_sgc lnP_den = lnP_gauss_ngc + lnP_gauss_sgc elif tag == 'RSD_ica_chi2': # this should be consistent with above! lnP_ica_ngc = NG.lnL_pXi_ICA(delta_ngc, pk_ngc_mock, ica_algorithm=ica_algorithm, density_method=density_method, n_comp_max=n_comp_max, info_crit=info_crit, njobs=njobs) lnP_ica_sgc = NG.lnL_pXi_ICA(delta_sgc, pk_sgc_mock, ica_algorithm=ica_algorithm, density_method=density_method, n_comp_max=n_comp_max, info_crit=info_crit, njobs=njobs) lnP_num = lnP_ica_ngc + lnP_ica_sgc lnP_den = -0.5 * chain['chi2'] elif 'gmf' in tag: # GMF geemf = Dat.Gmf() # read in SDSS GMF (data D) nbins, gmf_data = geemf.Observation() # calculate D - m(theta) for all the mcmc chain dgmf = gmf_data - chain['gmf'] # read mock gmfs (all mocks from 100 differnet HOD parameter points) gmf_mock = NG.X_gmf_all( ) #gmf_mock = NG.X_gmf('manodeep.run'+str(kwargs['run']))# # old likelihood derived from chi-squared of MCMC chain lnP_den = -0.5 * chain['chi2'] # -0.5 chi-squared from MCMC chain if tag == 'gmf_all_chi2': # importance weight determined by the ratio of # the chi^2 from the chain and the chi^2 calculated # using the covariance matrix from the entire catalog # we note that Sinha et al.(2017) does not include the # hartlap factor Cgmf = np.cov(gmf_mock.T) # covariance matrix Cinv = np.linalg.inv(Cgmf) # precision matrix lnP_num = np.empty(dgmf.shape[0]) for i in range(dgmf.shape[0]): # updated chi-square lnP_num[i] = -0.5 * np.dot(dgmf[i, :], np.dot(Cinv, dgmf[i, :].T)) if tag == 'gmf_pXiICA_chi2': # updated likelihood is calculated using # ln( PI_i p( delta_X_ICA_i | X_ICA_i^(gmm/kde)) ) lnP_num = NG.lnL_pXi_ICA(dgmf, gmf_mock, ica_algorithm=ica_algorithm, density_method=density_method, n_comp_max=n_comp_max, info_crit=info_crit, njobs=njobs) elif tag == 'gmf_pX_chi2': # updated likelihood is calculated using # ln( p( delta_X | X^(gmm/kde) ) ) lnP_num = NG.lnL_pX(dgmf, gmf_mock, density_method=density_method, n_comp_max=n_comp_max, info_crit=info_crit, njobs=njobs) elif tag == 'gmf_lowN_chi2': # importance weight determined by the ratio of # the chi^2 from the chain and the chi^2 calculated # using the covariance matrix from the entire catalog # and *excluding the highest N bin* Cgmf = np.cov(gmf_mock.T) # covariance matrix Cinv = np.linalg.inv(Cgmf) # precision matrix Nlim = Cinv.shape[0] - 1 lnP_num = np.empty(dgmf.shape[0]) for i in range(dgmf.shape[0]): # updated chi-square lnP_num[i] = -0.5 * np.dot( dgmf[i, :Nlim], np.dot(Cinv[:Nlim, :Nlim], dgmf[i, :Nlim].T)) else: raise NotImplementedError else: raise ValueError ws = np.exp(lnP_num - lnP_den) return [lnP_den, lnP_num, ws]