joshspeagle / dynesty

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

Using weighted samples #474

Closed ZcharlieZ closed 4 months ago

ZcharlieZ commented 5 months ago

Dynesty version 2.1.3

Hello,

my question regards weighted and unweighted samples. My doubt is whether, after running the sampler, when you access to results.samples, the latter contains the PDFs already weighted or still to be weighted with results.importance_weights(). I went through the documentation but I am still confused. From the Basic Post-Processing section of the documentation

image

it seems that results.samples need still to be weighted through the weights. However, then there are results.samples_equal() giving equal-weighted samples.

Moreover, the PDFs of results.samples_equal() are significantly tighter than those from results.samples. Here below, there are a portion of three cornerplots from the same sampling I did. From the top to the bottom: Dynesty default cornerplot function, my cornerplot function using results.samples, my cornerplot function using results.samples_equal(). The PDFs of the first and the third plots almost coincide.

image image image

Finally, after sampling, I plot the model over my data. To do that, I compute for instance, at fixed x, the 16, 50, and 84 percentiles of the whole sampling. When doing that, the uncertainty region from results.samples is way larger than the uncertainty using sampler_equal() (perhaps because I still have to weight the samples).

In conclusion which samples should be used?

  1. result.samples;
  2. result.samples but doing something with results.importance_weights() (in case, what);
  3. result.samples_equal().

From a previous post, I understood the importance of using weights and it makes complete sense to me. However, it is still not so clear what to use and how. I apologise if I have returned to the point already partially discussed.

Many thanks in advance for your help and time!

Carlo

segasai commented 5 months ago

Hi,

It is certainly possible the docs can be improved, but I will mostly repeat what I said earlier:

If you are doing something with the samples, and you don't know how to use weights, you should be using the second approach. I personally pretty much all only work with the equal weighted samples.

ZcharlieZ commented 5 months ago

Hi,

thanks for your reply. From the previous iteration, it was clear to me that results.samples have associated weights, but not that they were not used. Sorry for that. Now I understand why PDFs are so broad. In case one would use weights, does resample_equal(samples, weights) allow to easily weight results.samples?

Thanks again for the support

segasai commented 5 months ago

Yes you can use resample_equal() to manually get equally weigthed samples or just used results.samples_equal() from the start https://dynesty.readthedocs.io/en/latest/api.html#dynesty.results.Results.samples_equal