cosmodesi / desilike

BSD 3-Clause "New" or "Revised" License
19 stars 14 forks source link

Planck sampler wanders off? #12

Closed pmcdonal closed 1 year ago

pmcdonal commented 1 year ago

I have this code:

from desilike import setup_logging
from desilike.likelihoods.cmb import TTTEEEHighlPlanck2018PlikLiteLikelihood, \
                                     TTLowlPlanck2018ClikLikelihood,\
                                     EELowlPlanck2018ClikLikelihood
from desilike.likelihoods import SumLikelihood
from desilike.theories.primordial_cosmology import Cosmoprimo

setup_logging()
cosmo = Cosmoprimo(engine="camb")
likelihoods = [Likelihood(cosmo=cosmo) for Likelihood in [TTTEEEHighlPlanck2018PlikLiteLikelihood, TTLowlPlanck2018ClikLikelihood,\
                                                          EELowlPlanck2018ClikLikelihood]]
likelihood_clik = SumLikelihood(likelihoods=likelihoods)
from desilike.samplers import ZeusSampler

sampler = ZeusSampler(likelihood_clik, seed=42)
chains = sampler.run(check={'max_eigen_gr': 0.05})

and it does the following... I guess as I write this I realize I don't know what it uses for initial sampling choices, and if it is drawing from prior instead of ref this could happen for that reason... Is it possible to specify a starting point and covariance matrix for sampling? (e.g., with cobaya I would give it the max like point and Hessian covariance to start with)

[001886.50] [0/1] 02-03 11:24 ZeusSampler INFO Varied parameters: ['h', 'omega_cdm', 'omega_b', 'logA', 'n_s', 'tau_reio', 'A_planck']. [001894.60] [0/1] 02-03 11:24 root INFO Initialising ensemble of 18 walkers... zeus: Exception while calling your likelihood function: params: [[0.71845617 0.11316231 0.02500884 3.25690416 0.97226037 0.17722994 0.9907607 ]] args: [] kwargs: {} exception:

Traceback (most recent call last): File "/Users/pmcdonal/github/cosmodesi/desilike/desilike/base.py", line 710, in calculate self.calculator.calculate(**self.param_values) File "/Users/pmcdonal/github/cosmodesi/desilike/desilike/likelihoods/cmb/planck2018_clik.py", line 99, in calculate self.loglikelihood = self.clik(self.vector)[0] File "lkl.pyx", line 90, in clik.lkl.clik.call clik.lkl.CError: clik_compute(../src/clik.c:391)::ForwardError distribution_lkl(../src/minipmc/distribution.c:219)::ForwardError lklbs_lkl(../src/lklbs.c:219)::ForwardError simall_lkl(../src/simall/clik_simall.c:48)::Error -1233 (multipole EE 4 too large (got 0.352342 expected <0.3))

adematti commented 1 year ago

Thanks for the report; I guess I should catch clik errors then... (do not know why they just do not return -inf if unhappy). First point is indeed sampled from the reference distributions --- I'll clarify that. ref_scale can be used to rescale all reference distributions, so setting this to a very small value will achieve what you want. ZeusSampler doesn't need input proposal matrix (others do, e.g. MCMCSampler).

pmcdonal commented 1 year ago

Well, -inf is an implicit prior that you might not want... I'd do it, but, strictly, I can see why you might not hard code. But ok, I figured out how to see ref and prior numbers (after painfully much floundering) and there's no way this point is drawn from ref, and generally it should be ruled out by data, i.e., no way a chain will walk there from the ref starting point... so how/why does it go there? (i.e., whatever it is, it seems like it must be a flaw somewhere in the system)

adematti commented 1 year ago

The parameter vector which is thrown out is compatible with priors, right? Then it's up to Zeus

adematti commented 1 year ago

(I have just edited the Planck likelihoods to catch errors by default)

pmcdonal commented 1 year ago

Well, then if there is no way to fix through Zeus options I'd say the flaw in the system is Zeus... I.e., it isn't the priors' fault, since the problem is being so crazy-far from reasonably consistent with data, in spite of ref... (ok, this is somewhat a matter of judgement, but, in my opinion, a sampler that cannot be stopped from doing this is not one I'd want to use...)

Upon further thought, this is why I'd say the likelihood crash is really the correct response, if it can't return a legit number. I.e., I'd rather know if "something that shouldn't happen" happens, rather than quietly going on...

adematti commented 1 year ago

well, up to you but I'd give it a try again, maybe that was very very bad luck :) (and, if I thought something should not happen, then I would prevent it with priors --- especially as Zeus is just given a likelihood callable, without information on the typical parameter ranges)

pmcdonal commented 1 year ago

It should be possible to give it an idea of starting parameter range and have it use it... It's just about efficiency - whether the Planck likelihood crashed or not there, the call was a waste of time (or would have been if actually evaluated)... if it hadn't crashed we just wouldn't know... You shouldn't need to tailor priors to keep sampler from wasting time in areas ruled out by data. I'm not asking you to solve it though... just thinking out loud... (although I do plan to turn the crashing back on if I can). This sampling is unbearable... no wonder people get so little done... of course, I am doing it because I was also having trouble finding stable Hessians (I don't understand the reason though, for parameter space where chain results look Gaussian... basically nothing is making sense to me right now)