def initial_pool(self): initial_wrapper = _Initialpoolsampling(self.eps0 , self.data, self.model, \ self.distance , self.prior_dict) #print wrapper #print wrapper(1) if self.N_threads == 1: results = [] for i in range(self.N_particles): results.append(initial_wrapper(i)) else: self.pool = Pool(self.N_threads) self.mapfn = self.pool.map results = self.mapfn(initial_wrapper , range(self.N_particles)) self.pool.close() #self.pool.terminate() #self.pool.join() results = np.array(results).T self.theta_t = results[1:self.n_params+1,:] self.w_t = results[self.n_params+1,:] self.rhos = results[self.n_params+2,:] self.sig_t = 2. * utils.covariance(self.theta_t , self.w_t) plot_thetas(self.theta_t, self.w_t, self.prior_dict, 0, basename=self.basename)
def initial_pool(self): initial_wrapper = _Initialpoolsampling(self.eps0 , self.data, self.model, \ self.distance , self.prior_dict) #print wrapper #print wrapper(1) if self.N_threads == 1: results = [] for i in range(self.N_particles): results.append(initial_wrapper(i)) else: self.pool = Pool(self.N_threads) self.mapfn = self.pool.map results = self.mapfn(initial_wrapper , range(self.N_particles)) self.pool.close() #self.pool.terminate() #self.pool.join() results = np.array(results).T self.theta_t = results[1:self.n_params+1,:] self.w_t = results[self.n_params+1,:] / self.N_particles self.rhos = results[self.n_params+2:,:] self.sig_t = 2. * utils.covariance(self.theta_t , self.w_t) plot_thetas(self.theta_t, self.w_t, self.prior_dict, 0, basename=self.basename)
def calculate_frechet_distance(text1, text2, verbose=False): first = [] for line in tqdm(text1, disable=not verbose): first.append(extract_features(line.strip())) first = torch.stack(first, dim=0) mu1 = torch.mean(first, dim=0) sigma1 = covariance(first, rowvar=False) second = [] for line in tqdm(text2, disable=not verbose): second.append(extract_features(line.strip())) second = torch.stack(second, dim=0) mu2 = torch.mean(second, dim=0) sigma2 = covariance(second, rowvar=False) return _frechet_distance(mu1, sigma1, mu2, sigma2)
def main(): #statistics = ['wp','xi','upf','mcf'] #statistics = ['wp', 'xi', 'upf'] statistics = ['xi'] #statistics = ['upf'] #statistics = ['xi2'] #errtag = '_hod3_test0' #errtags = ['_hod3_test0']*len(statistics) #statistics = ['upf'] #statistics = ['mcf'] #errtags = ['_hod3_test0'] #testtags = ['', '', '_fstar8.0_p1.0'] errtag = '_hod3_test0_check' hod = 3 # choose a middle-of-the-road hod nbins = 9 ncosmos = 7 nboxes = 5 ntests = 1 #eventually this should be 10 cosmos = list(range(ncosmos)) boxes = list(range(nboxes)) tests = list(range(ntests)) stat_str = '_'.join(statistics) #err_str = ''.join(errtags) #err_str = errtag res_dir = '../../clust/covariances/' devmean_arr = [] for i, statistic in enumerate(statistics): testing_dir = '../../clust/results_aemulus_test/results_{}/'.format( statistic) devmean_arr.append( calculate_devmeans(testing_dir, statistic, hod, cosmos, boxes, tests)) #compute covariance assuming the mean is zero, as that is the expectation value (should be unbiased) devmeans = np.concatenate(devmean_arr, axis=1) cov = utils.covariance(devmeans, zeromean=True) cov_fn = res_dir + "cov_aemulus_{}{}.dat".format(stat_str, errtag) print(f"Saving to {cov_fn}") np.savetxt(cov_fn, cov) # save std for gp, and percentiles # TODO: I think this should be diagonal error, slightly diff than std?? err = np.std(devmeans, axis=0) np.savetxt(res_dir + "error_aemulus_{}{}.dat".format(stat_str, errtag), err) print("err:", err) p16 = np.percentile(devmeans, 16, axis=0) p84 = np.percentile(devmeans, 84, axis=0) np.savetxt(res_dir + "p16_aemulus_{}{}.dat".format(stat_str, errtag), p16) np.savetxt(res_dir + "p84_aemulus_{}{}.dat".format(stat_str, errtag), p84)
def run_abc(self): """ Run PMC_ABC """ self.initial_pool() t = 1 while t < self.T: self.eps_t = np.percentile(self.rhos, 75) print "iteration= " , t , "threshold= " , self.eps_t importance_wrapper = _Importancepoolsampling(self.eps_t, self.data, self.model, \ self.distance, self.prior_dict, \ self.theta_t, self.w_t, self.sig_t) if self.N_threads == 1 : results = [] for i in range(self.N_particles): results.append(importance_wrapper(i)) else: self.pool = Pool(self.N_threads) self.mapfn = self.pool.map results = self.mapfn(importance_wrapper, range(self.N_particles)) self.pool.close() #self.pool.terminate() #self.pool.join() results = np.array(results).T self.theta_t = results[1:self.n_params+1,:] self.w_t = results[self.n_params+1,:] self.rhos = results[self.n_params+2,:] self.sig_t = 2. * utils.covariance(self.theta_t, self.w_t) plot_thetas(self.theta_t, self.w_t, self.prior_dict, t, basename=self.basename, fig_name="{0}_{1}.png".format(self.basename, str(t))) t += 1
def run_abc(self): """ Run PMC_ABC """ self.initial_pool() t = 1 while t < self.T: self.eps_t = np.percentile(np.atleast_2d(self.rhos), 75, axis=1) print "iteration= " , t , "threshold= " , self.eps_t importance_wrapper = _Importancepoolsampling(self.eps_t, self.data, self.model, \ self.distance, self.prior_dict, \ self.theta_t, self.w_t, self.sig_t) if self.N_threads == 1 : results = [] for i in range(self.N_particles): results.append(importance_wrapper(i)) else: self.pool = Pool(self.N_threads) self.mapfn = self.pool.map results = self.mapfn(importance_wrapper, range(self.N_particles)) self.pool.close() #self.pool.terminate() #self.pool.join() results = np.array(results).T self.theta_t = results[1:self.n_params+1,:] self.w_t = results[self.n_params+1,:] self.rhos = results[self.n_params+2:,:] self.sig_t = 2. * utils.covariance(self.theta_t, self.w_t) plot_thetas(self.theta_t, self.w_t, self.prior_dict, t, basename=self.basename, fig_name="{0}_{1}.png".format(self.basename, str(t))) t += 1
def main(): #statistics = ['wp'] #statistics = ['wp','upf','mcf'] #statistics = ['wp', 'mcf'] statistics = ['wp', 'xi', 'upf', 'mcf'] #savetag = '_fstar8.0_p1.0' #traintags = ['_nonolap', '_nonolap']#, f'{savetag}_nonolap'] traintag = '_nonolap' #testtag = '_mean_test0' testtag = '_glam4' errtag = '_hod3_test0' savetag = '_residuals' #savetag = '_signfix' #testtags = ['_mean_test0','_mean_test0']#,f'{savetag}_mean_test0'] #errtags = ['_hod3_test0','_hod3_test0']#, '_hod3_test0'] tags = [ '_log_kM32ExpConst2_100hod', '_log_kM32ExpConst_100hod', '_log_kM32ExpConst_100hod', '_log_kM32ExpConst_100hod' ] #tags = ['_log_kM32ExpConst2_100hod'] # statistics = ['mcf'] # savetag = '_fstar8.0_p2.0' # traintags = [f'{savetag}_nonolap'] # testtags = [f'{savetag}_mean_test0'] # errtags = ['_hod3_test0'] # tags = ['_log_kM32ExpConst_100hod'] nhod_test = 100 CC_test = range(0, 7) HH_test = range(0, nhod_test) stat_str = '_'.join(statistics) cov_dir = '../../clust/covariances'.format(stat_str) acctags = [] fracerr_arrs = [] for i, statistic in enumerate(statistics): #gptag = traintags[i] + errtags[i] + tags[i] #acctag = gptag + testtags[i] gptag = traintag + errtag + tags[i] acctag = gptag + testtag acctags.append(acctag) print("Computing emu error for", statistic, testtag, acctag) if 'glam4' in testtag: fracerrs = load_fracerrs_glam4(statistic, testtag, acctag) print("Using residual fractional errors!") fracerrs -= np.mean(fracerrs, axis=0) fracerr_arrs.append(fracerrs) elif 'glam' in testtag: fracerrs = load_fracerrs_glam(statistic, testtag, acctag) print("Using residual fractional errors!") fracerrs -= np.mean(fracerrs, axis=0) fracerr_arrs.append(fracerrs) else: fracerrs = load_fracerrs_aemulus(statistic, testtag, acctag, CC_test, HH_test) fracerr_arrs.append(fracerrs) fracerrs = np.concatenate(fracerr_arrs, axis=1) cov_perf = utils.covariance(fracerrs, zeromean=True) tag_str = traintag + errtag + testtag + savetag #tag_str += ''.join(tags) #tag_str = '' #acc_str = ''.join(acctags) save_fn_perf = f"{cov_dir}/cov_emuperf_{stat_str}{tag_str}.dat" print('Saving cov_perf to', save_fn_perf) np.savetxt(save_fn_perf, cov_perf) p16 = np.percentile(fracerrs, 16, axis=0) p84 = np.percentile(fracerrs, 84, axis=0) save_fn_p16_perf = f"{cov_dir}/p16_emuperf_{stat_str}{tag_str}.dat" save_fn_p84_perf = f"{cov_dir}/p84_emuperf_{stat_str}{tag_str}.dat" np.savetxt(save_fn_p16_perf, p16) np.savetxt(save_fn_p84_perf, p84) cov_perf_nonzeromean = utils.covariance(fracerrs) save_fn_perf_nonzeromean = f"{cov_dir}/cov_emuperf_nonzeromean_{stat_str}{tag_str}.dat" np.savetxt(save_fn_perf_nonzeromean, cov_perf_nonzeromean)
def plot_accuracy_fromparams(statistic, testtag, acctag, errtag, params_zipped, hod=None, nbins=9, nhods=100, xrsq=False, err_as_percentiles=False, sample_var='aemulus'): ncols = 3 fig, ax = plt.subplots(ncols, 1, figsize=(10, 15), gridspec_kw={'height_ratios': [1] * ncols}) plt.subplots_adjust(hspace=0.15) res_dir = '../../clust/results_{}/'.format(statistic) stat_mean = np.zeros(nbins) if sample_var == 'minerva': minerva_dir = f'../../clust/covariances' cov_minerva = np.loadtxt(f"{minerva_dir}/cov_minerva_{statistic}.dat") L_minerva = 1.5 #Gpc L_aemulus = 1.05 #Gpc cov_test = cov_minerva * (L_minerva / L_aemulus)**3 elif sample_var == 'aemulus': cov_test = np.loadtxt( "../../clust/covariances/cov_aemulus_{}{}.dat".format( statistic, errtag)) if 'mean' in testtag: cov_test *= 1. / 5. err_test = np.diag(cov_test) for (cosmo, hod) in params_zipped: hod = int(hod) if "mean" in acctag: idtag = '{}_cosmo_{}_HOD_{}_mean'.format(statistic, cosmo, hod) else: idtag = '{}_cosmo_{}_Box_0_HOD_{}_test_0'.format( statistic, cosmo, hod) fnt = '{}testing_{}{}/{}.dat'.format(res_dir, statistic, testtag, idtag) rtest, ptest = np.loadtxt(fnt) #rtest, ptest = np.loadtxt(fnt, delimiter=',', unpack=True) stat_mean += ptest[:nbins] stat_mean /= len(list(params_zipped)) #color_idx = np.linspace(0, 1, len(set(params_zipped[:,0]))) color_idx = np.linspace(0, 1, len(list(params_zipped))) i = 0 fracerrs = [] colidx = 0 for cosmo, hod in params_zipped: #colidx = cosmo #color=plt.cm.rainbow(color_idx[colidx]) color = plt.cm.rainbow(color_idx[colidx]) colidx += 1 if "mean" in acctag: idtag = '{}_cosmo_{}_HOD_{}_mean'.format(statistic, cosmo, hod) else: idtag = '{}_cosmo_{}_Box_0_HOD_{}_test_0'.format( statistic, cosmo, hod) lw = 1 alpha = 1 fnt = '{}testing_{}{}/{}.dat'.format(res_dir, statistic, testtag, idtag) rtest, ptest = np.loadtxt(fnt) if xrsq: ptplot = rtest[:nbins]**2 * ptest[:nbins] else: ptplot = ptest[:nbins] #rtest, ptest = np.loadtxt(fnt, delimiter=',', unpack=True) if i == 0: ax[0].plot(rtest[:nbins], ptplot, marker='o', ls='None', markerfacecolor='None', markeredgecolor=color, lw=lw, alpha=alpha, label='truth') else: ax[0].plot(rtest[:nbins], ptplot, marker='o', ls='None', markerfacecolor='None', markeredgecolor=color, lw=lw, alpha=alpha) fnp = '../testing_results/predictions_{}{}/{}.dat'.format( statistic, acctag, idtag) rpredic, ppredic = np.loadtxt(fnp, delimiter=',', unpack=True) if xrsq: ppplot = rpredic[:nbins]**2 * ppredic[:nbins] else: ppplot = ppredic[:nbins] if i == 0: ax[0].plot(rpredic[:nbins], ppplot, marker=None, ls='-', color=color, lw=lw, alpha=alpha, label='emulator prediction') else: ax[0].plot(rpredic[:nbins], ppplot, marker=None, ls='-', color=color, lw=lw, alpha=alpha) fracerr = (ppredic - ptest) / ptest fracerrs.append(fracerr) ax[1].plot(rtest[:nbins], fracerr[:nbins], color=color, lw=lw, alpha=alpha) i += 1 if fracerr[-2] > 10: print(cosmo, hod, fracerr) ax[2].axhline(0, color='k', ls=':') cov_fracerrs = utils.covariance(fracerrs, zeromean=True) err_fracerrs = np.diag(cov_fracerrs) ax[2].plot(rtest[:nbins], np.sqrt(err_fracerrs[:nbins]), color='b', lw=2, ls='-', label='error (RMS of fractional error)') ax[2].plot(rtest[:nbins], np.sqrt(err_test[:nbins]), color='r', lw=2, label='sample variance') ax[2].set_ylabel("error") ax[2].legend() ax[0].legend() ax[2].set_xlabel(r"r ($h^{-1}$Mpc)") stat_labels = { 'upf': r"P$_U$(r)", 'wp': r'$w_p$($r_p$)', 'mcf': "M(r)", 'xi': r"$\xi_0$(r)", 'xi2': r"$\xi_2$(r)" } if xrsq: ax[0].set_ylabel(r'r$^2$' + stat_labels[statistic]) else: ax[0].set_ylabel(stat_labels[statistic]) if statistic in ['wp', 'upf', 'xi']: ax[0].set_yscale('log') if statistic in ['wp', 'mcf', 'xi', 'xi2']: for nc in range(ncols): ax[nc].set_xscale('log') ax[1].set_ylabel("fractional error") return fracerrs
def test_covariance(self): cov = covariance( [0, 4], [8, None, None, None, 7], [7, None, 1, None, 6], 7.5, 7) self.assertEqual(cov, 0.5)