joshspeagle / dynesty

Dynamic Nested Sampling package for computing Bayesian posteriors and evidences
https://dynesty.readthedocs.io/
MIT License
346 stars 76 forks source link

Doubt on sampling methods #472

Open ZcharlieZ opened 6 months ago

ZcharlieZ commented 6 months ago

Hi all!

I do have a doubt about how to plot the results after sampling. Let's suppose I would like to fit a linear relation. After the sampling, I have the posterior of all the parameters of the model. This results in having N curves. Thus at a fixed x value, I can compute for instance the median and the 16% and 84% around the curves. In the case of dynesty, however, weights are used. The posterior should be weighted and, if not wrong, the weight is logwt-logz[-1] for the PDFs in dyplot traceplots or cornerplots. If everything above is correct, how can I plot the median and the uncertainties of the model?

Many thanks in advance

ZcharlieZ commented 6 months ago

Hi again!

I think I got the solution, but I would like to ask for your kind confirmation. By using results.samples_equal() I assign the weight to the posterior of each parameter. In this way, I can use it to sample the 16, 50, and 84% of my relation at fixed x.

Could you confirm it, please?

Many thanks in advance

segasai commented 6 months ago

Hi,

The default results.samples have weigths associated with them, thus any statistic that needs to be calculated need to use weights. Dynesty contains the code to calcluate the mean and covariance from weighted samples, but if you want to compute things like median, I think it easier to just work with the unweighted samples from samples_equal which provide standard unweighted samples that you can work with like with a regular MCMC samples.

joshspeagle commented 6 months ago

You can also use the quantile function in utils to do exactly this, which computes quantiles following a weighted CDF using the ordered samples.

ZcharlieZ commented 6 months ago

Hi all!

Thanks for your replies. Let me go a bit more into the details of what I'm doing. I'm sampling the PDFs using the Dynamic Nested Sampler. The configuration is the following:

dsampler = dynesty.DynamicNestedSampler(log_likelihood, prior_transform, ndim=len(parameter_labels),
                                    bound='multi', sample='rslice', rstate=rstate, pool=pool, queue_size=250, nlive=1000)
dsampler.run_nested(dlogz_init=0.01, print_progress=True)

The first thing I noticed is that, keeping everything the same and using the same random state (rstate=np.random.default_rng(18)), but changing the sample method from rslice to rwalk the PDFs change significantly. Albeit the number of parameters used is lower than 10, for which unif is suggested, I'm using one of the two methods above because sampling with unif is very slow. Here below you find the traceplots using rslice and rwalk, respectively. In both cases, I also noticed that, after starting the run, the dlogz value in output from print_progress has a starting value, then for a while it becomes a way larger, and finally it reduces to reach the convergence below the threshold (dlogz_init=0.01).

rslice image

rwalk da517e7a-a469-4110-9562-e5c14de1699c

The second sampling should be more in line with what I should expect. In addition, I tried to sample the PDFs using an MCMC algorithm. In the latter, the PDFs differ from the "rwalk" case, but they look a bit more Gaussian-like distributed.

Finally, coming to the use of weights, the PDFs of the parameters differ considerably wider when I get the weighted samples. Indeed, when I plot the final relation, the 1sigma uncertainty around the curve is very large. Here below I attach the corner plots and the relations for the unweighted (blue colours) and weighted (red colours) samples.

e67972a5-da9e-4eb5-aeb1-b8644025d11b

5d6a7450-33d1-4fa2-a367-583adb1dd723

To summarise, I list my questions in the following.

  1. What is the reason why using a different sample method dramatically changes the results? Given that, which is the method one should use?
  2. Using an MCMC algorithm, similar to using a different sample method in Dynesty, leads to different results and perhaps more regular (looking both at the chains and corner plots). What might be the reason?
  3. If I understand correctly the scope of weights, I would expect the use of weighted samples to give a more "accurate" estimate of the posterior thus reducing the uncertainty around my relation, but this is not my case. Maybe I'm wrong about it. Could you explain me better this aspect, please?

Thank you very much for your help, very appreciated!

Carlo

segasai commented 5 months ago

Hi,

ZcharlieZ commented 5 months ago

Hi @segasai,

thanks for your previous reply. I reply to your previous points here below.

As clearly visible, all the PDFs are dramatically different from each other, regardless of the number of live points or the sampling method. Do you think I still need to increase even more the number of live points? Apparently, I am not seeing any improvement. For completeness, I list here below the print_progress for each case:

The version of dynesty used is always the latest one, i.e. v2.1.3.

Many thanks in advance for the help.

Carlo