undark-lab / swyft

A system for scientific simulation-based inference at scale.
Other
154 stars 13 forks source link

How to take samples from the posterior estimate #130

Closed GertKl closed 1 year ago

GertKl commented 1 year ago

Hi!

The following concerns swyft 0.3.2:

I would like to draw individual samples from my estimated posterior. I.e. I don't want to make a weighted histogram, but I would like to have a list of actual parameter-samples from the posterior-estimate. I suppose I would have to draw samples from the prior, and keep them with a probability that is equal to the likelihood ratio (weight)? However, what is the easiest way to retrieve the prior sample together with it's associated weight?

Suppose I have successfully made a WeightedMarginalSamples object, e.g. samples = swyft.MarginalPosterior.weighted_sample(nsamples, obs). I don't find it clear where to go from here.

cweniger commented 1 year ago

Hi GertKI,

we don't support Swyft 0.3.x anymore, and recommend migration to 0.4.x. If you need help with that, I'm happy to have a chat, let me know.

Regarding samples generated like you write above, I don't remember the details, but the object should contain both the weights and the parameters. If you use the weights to subsample the parameters with probabilities proportional to the weights, you should end up with posterior samples.

bkmi commented 1 year ago

Try this function.

https://github.com/undark-lab/swyft/blob/2c0ed514622a37e8ec4e406b99a8327ecafb7ab4/swyft/inference/marginalposterior.py#L154

GertKl commented 1 year ago

Thank you @bkmi ! I was clearly looking in the wrong place.