Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discrepency qcinv and actual solution weiner filtering #2

Open
Gabriel-Ducrocq opened this issue Jul 28, 2020 · 2 comments
Open

Discrepency qcinv and actual solution weiner filtering #2

Gabriel-Ducrocq opened this issue Jul 28, 2020 · 2 comments

Comments

@Gabriel-Ducrocq
Copy link

Hi,

I have been trying to run qcinv and compare its output to the true solution when using a noise covariance matrix porportional to the identity matrix and no skymap - in this case the matrix A.T N^-1 A + C^-1 is diagonal.

Using a stopping criterion of an error lower than 1e-6, I am finding unexpected discrepencies between the solution found by qcinv and the one computed directly. Here you will find a minimal code:

import numpy as np
import healpy as hp
import qcinv
import utils
import matplotlib.pyplot as plt
from classy import Class
cosmo = Class()



## Setting params
LENSING = 'yes'
OUTPUT_CLASS = 'tCl pCl lCl'
observations = None
COSMO_PARAMS_NAMES = ["n_s", "omega_b", "omega_cdm", "100*theta_s", "ln10^{10}A_s", "tau_reio"]


nside=512
lmax=2*nside
npix = 12*nside**2

def generate_cls(theta, pol = False):
    params = {'output': OUTPUT_CLASS,
              "modes":"s,t",
              "r":0.001,
              'l_max_scalars': lmax,
              'lensing': LENSING}
    d = {name:val for name, val in zip(COSMO_PARAMS_NAMES, theta)}
    params.update(d)
    cosmo.set(params)
    cosmo.compute()
    cls = cosmo.lensed_cl(lmax)
    # 10^12 parce que les cls sont exprimés en kelvin carré, du coup ça donne une stdd en 10^6
    cls_tt = cls["tt"]*2.7255e6**2
    if not pol:
        cosmo.struct_cleanup()
        cosmo.empty()
        return cls_tt
    else:
        cls_ee = cls["ee"]*2.7255e6**2
        cls_bb = cls["bb"]*2.7255e6**2
        cls_te = cls["te"]*2.7255e6**2
        cosmo.struct_cleanup()
        cosmo.empty()
        return cls_tt, cls_ee, cls_bb, cls_te


COSMO_PARAMS_MEAN_PRIOR = np.array([0.9665, 0.02242, 0.11933, 1.04101, 3.047, 0.0561])
noise_covar_temp = 0.1**2*np.ones(npix)
inv_noise = (1/noise_covar_temp)

beam_fwhm = 0.35
fwhm_radians = (np.pi / 180) * 0.35
bl_gauss = hp.gauss_beam(fwhm=fwhm_radians, lmax=lmax)




### Generating skymap

theta_ = COSMO_PARAMS_MEAN_PRIOR
cls_ = generate_cls(theta_, False)
map_true = hp.synfast(cls_, nside=nside, lmax=lmax, fwhm=beam_fwhm, new=True)
d = map_true
d += np.random.normal(scale=np.sqrt(noise_covar_temp))



#### Setting solver's params
class cl(object):
    pass

s_cls = cl
s_cls.cltt = cls_


n_inv_filt = qcinv.opfilt_tt.alm_filter_ninv(inv_noise, bl_gauss)
chain_descr = [
    [0, ["diag_cl"], lmax, nside, np.inf, 1.0e-6, qcinv.cd_solve.tr_cg, qcinv.cd_solve.cache_mem()]]

chain = qcinv.multigrid.multigrid_chain(qcinv.opfilt_tt, chain_descr, s_cls, n_inv_filt,
                                        debug_log_prefix=('log_'))

soltn_complex = np.zeros(int(qcinv.util_alm.lmax2nlm(lmax)), dtype=np.complex)

#### Solving
chain.solve(soltn_complex,d)
hp.almxfl(soltn_complex, cls_, inplace=True)
weiner_map_pcg = soltn_complex



##### Computing the exact weiner map:


## comouting b part of the system
b_weiner = hp.almxfl(hp.map2alm(inv_noise * d, lmax=lmax)*(npix/(4*np.pi)), bl_gauss)

inv_cls = np.array([cl if cl != 0 else 0 for cl in cls_])

Sigma = 1 / (inv_cls + inv_noise[0] * (npix / (4 * np.pi)) * bl_gauss ** 2)

##### Solving:
weiner_map_diag = hp.almxfl(b_weiner, Sigma)


##Graphics


pix_map_pcg = hp.alm2map(weiner_map_pcg, lmax=lmax, nside=nside)
pix_map_diag = hp.alm2map(weiner_map_diag, lmax=lmax, nside=nside)


rel_error_pix = np.abs((pix_map_pcg-pix_map_diag)/pix_map_diag)
hp.mollview(rel_error_pix)
plt.show()


plt.boxplot(rel_error_pix, showfliers=True)
plt.show()
plt.boxplot(rel_error_pix, showfliers=False)
plt.show()

Note that with such a choice of beam, noise and resolution, the errors are acceptables on most of the pixels. Here are plots of the map and boxplot (with and without outliers) of relative differences:

Capture d’écran 2020-07-28 à 16 17 10

Capture d’écran 2020-07-28 à 16 17 46

Capture d’écran 2020-07-28 à 16 17 53

Note that keeping all values equal but reducing the resolution increases the errors. For example, with nside = 32, we get the following graphics:

Capture d’écran 2020-07-28 à 16 23 32

Capture d’écran 2020-07-28 à 16 23 44

Capture d’écran 2020-07-28 à 16 23 50

Now the fractional errors are about 1%, which is pretty big... Keeping a high resolution but increasing the noise level has the same effect.

Have you encountered such a behavior ? Am I missing something and using qcinv the wrong way ?

Thank you,
Gabriel.

@carronj
Copy link
Owner

carronj commented Jul 29, 2020

Hi Gabriel,

The matrix A.T N^-1 A matrix is never exactly diagonal (the pixels have finite size and the spherical harmonics are not exactly orthogonal on the pixelized sphere). One does expect some difference that goes away increasing the resolution.

Julien

@Gabriel-Ducrocq
Copy link
Author

Hi Julien,

Thank you for replying quickly ! Okay that makes sense.
The same behavior happens when keeping the same resolution with a greater noise. For example, keeping NSIDE = 512 but taking a noise level 10 times greater, we get:

Capture d’écran 2020-07-31 à 00 16 01

Capture d’écran 2020-07-31 à 00 16 15

Capture d’écran 2020-07-31 à 00 16 25

Now most of the errors are of the order of few percents. Do you have an idea why increasing the noise level increases the errors ?

Intuitively, I would say that increasing the noise also increases the conditionning number of the matrix A^T N^-1 A + C^-1. However, even if this matrix is not perfectly diagonal, the diagonal preconditioner should be good enough to cancel problems due to bad conditioning, don't you think ?

Thank you,
Gabriel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants