def test_likelihoods(): """ The output should be samples of a chi squared distribution with lik_FT.nz_modes d.o.f. """ cls = [cl_pp, cl_noise, cl_unl] labels = ["pp", "noise", "unl"] sim_libs = [lib_pp, lib_noise, lib_cmb_unl] ress = [HD_res, LD_res, HD_res] histos = [] def is_ok(sig): if abs(sig) < 0.01: return "** deviation suspiciously small **" if abs(sig) >= 0.01 and abs(sig) < 3.0: return " Test OK" if abs(sig) >= 3.0: return "** deviation suspiciously large **" for cl, label, sims, res in zip(cls, labels, sim_libs, ress): histo = [] lik_FT = likelihoods.circulant_Gauss_pdf_fourierspace(cl, (res, res), lside) for i, idx in enumerate_progress(np.arange(nsims), "+ test_likelihoods::collecting chi2 " + label + " sims"): sim = sims.get_sim(idx) Npix = lik_FT.dof histo.append((lik_FT.eval_log(sim) + lik_FT.lnZ) / Npix) histo = np.array(histo) histos.append(histo) # A chi sqd distribution with k d.o.f. has mean k and variance 2k # The loglikelihood is - 1/2 chi sqd where d.o.f. is the number of modes with nonzero spectrum. dev = (1.0 + 2 * np.mean(histo)) / np.sqrt(2.0 / lik_FT.dof / nsims) print "+++++++++++++++++++++++++++++++++++++++++++++++++++" print "+ Number of sims :", str(nsims) print "+ d.o.f. / Npix :", np.round(float(lik_FT.dof) / (2 ** res) ** 2, 2) print "+ Mean chi2, " + label + " sims, dev. from theory", np.round(dev, 3), " sig. " print "+ ---->", is_ok((1.0 + 2 * np.mean(histo)) / np.sqrt(2.0 / lik_FT.dof / nsims)) print "+++++++++++++++++++++++++++++++++++++++++++++++++++" return histos
def plot_gradients(number_of_sims=1): """ Plot the total curvature (F. info) for phi and Omega for different volumes. """ import pylab as pl pl.ioff() FT_lik = likelihoods.circulant_Gauss_pdf_fourierspace(cl_pp, (HD_res, HD_res), lside) curvp, curvO, curvpO = Fisher_lib.get_curvature_matrix_PhiOmega_ell() curvp = (1j + 1.0) * curvp # curvp += FT_lik.eval_curv() grad = curvp * 0 print "+++++++++++++++++++++++++++++++++++++++++++++++++++" for i, idx in enumerate_progress(np.arange(number_of_sims), label="plot_gradients::collecting sims"): dat = sim_lib_fixedphi.get_sim(idx) umap = len_cov.apply_cg_inverse(dat)[0].reshape(LD_shape) grad += len_cov.likelihood_gradient_phiOmega_ell(umap, nodisplacement=True)[0] # Estimate of phi nz = np.where(curvp != 0.0) phik_est = grad * 0 phik_est[nz] = -grad[nz] / curvp[nz] phi_est = np.fft.irfft2(phik_est, HD_shape) * np.prod(HD_shape) / np.sqrt(len_cov.HD_cub.vol()) counts, Cl_raw = rfft2_utils.rfft2cl(np.fft.rfft2(phi_est), lside) ell = np.arange(len(counts)) nz = np.where(counts > 0.0) N0 = np.zeros(len(counts)) ell_N0 = rfft2_utils.k2ell(Fisher_lib.kx_from_index(np.arange(1, Fisher_lib.N_2p1 - 1))) N0[ell_N0] = 1.0 / (0.5 * curvp.real[0, 1 : Fisher_lib.N_2p1 - 1]) N0[10:] = np.exp(np.interp(np.log(ell[10:]), np.log(ell[ell_N0]), np.log(N0[ell_N0]))) pl.figure() pl.title("Reconstruction") pl.loglog(binner.bin_centers(), binner.bin_that(ell[nz], (Cl_raw * ell ** 4 / np.pi / 2.0)[nz]), label="Clraw") pl.plot( binner.bin_centers(), binner.bin_that(ell[nz], ((Cl_raw - number_of_sims * N0) * ell ** 4 / np.pi / 2.0)[nz]), label="Clraw - N0", ) pl.plot(np.arange(len(cl_pp)), cl_pp * np.arange(len(cl_pp)) ** 4 / np.pi / 2.0) nz0 = np.where(N0 > 0.0) pl.plot(ell[nz0], number_of_sims * (N0 * ell ** 4 / 2.0 / np.pi)[nz0], label="N0") pl.legend(frameon=False) pl.xlabel("L") pl.ylabel("$C_L L^2 (L+1)^2 /2 \pi$") pl.ylim(1e-10, 2e-7) pl.savefig(path_to_figs + "/reconstruction.pdf") pl.close() print "+++++++++++++++++++++++++++++++++++++++++++++++++++"
def plot_curvature(): """ Plot the total curvature (F. info) for phi and Omega for different volumes. """ import pylab as pl pl.ioff() pl.figure() d_ress = [0, 2] print "+++++++++++++++++++++++++++++++++++++++++++++++++++" for d_res in d_ress: if d_res == d_ress[0]: print "+plot_curvature:: doing res", HD_res + d_res else: print "+ doing res", HD_res + d_res res = HD_res + d_res lsides = lside * 2 ** d_res Fisher = likelihoods.flatsky_Fisher_lib(cl_unl, (res, res), lsides, sN_uKamin, beam_FWHM_amin, verbose=False) FT_p_lik = likelihoods.circulant_Gauss_pdf_fourierspace(cl_pp, (res, res), lsides) idcs = np.arange(1, 2 ** (res - 1)) Fpp, FOO, FpO = Fisher.get_curvature_matrix_PhiOmega_ell() Fpp = Fpp[0, idcs] FOO = FOO[0, idcs] Fpp += FT_p_lik.eval_curv().real[0, idcs] kx = Fisher.kx_from_index(idcs) Nlev_pp = 1.0 / Fpp * 2 # On complex modes Nlev_OO = 1.0 / FOO * 2 # On complex modes pl.loglog(kx, Nlev_pp * kx ** 2 * (kx + 1) ** 2 / 2.0 / np.pi * 1e7, label="$\phi$, res =" + str(res)) pl.loglog(kx, Nlev_OO * kx ** 2 * (kx + 1) ** 2 / 2.0 / np.pi * 1e7, label="$\Omega$, res =" + str(res)) pl.plot( np.arange(len(cl_pp)), cl_pp * np.arange(len(cl_pp)) ** 2 * (np.arange(len(cl_pp)) + 1) ** 2 / 2.0 / np.pi * 1e7, label="$C^{\phi\phi}$", ) pl.legend(frameon=False) pl.title("Total inverse curvature") pl.xlabel("$\ell$") pl.ylabel("$C_\ell \ell^2 (\ell + 1)^2 /2\pi $") print "+plot_curvature:: saving fig curvatures.pdf" pl.savefig(path_to_figs + "/curvatures.pdf") pl.close() print "+++++++++++++++++++++++++++++++++++++++++++++++++++"