pythonpython-2.7python-3.xmcmcemcee

use emcee to sample probability distribution functions?


I have two arrays (pdf_#), each containing the probability for a parameter (val_#). How can I sample from these distributions to create a joint posterior distribution (i.e. like in the corner plot here)? For emcee it looks like I can only pass functions to be sampled from, not arrays.

Here is what I have:

pdf_1 = [.1, .1, .25, .3, .15]
pdf_2 = [.25, .3, .2, .1, .4]

val_1 = [2, 3, 4, 5, 6]
val_2 = [1, 2, 3 ,4 ,5]

(in reality the pdfs are sampled more finely, and sum to 1)

I would like to generate N numbers of samples (with x,y from val_1,val_2) following the pdfs.


Solution

  • Is there a reason you want to use mcmc?

    numpy.random.choice(a=val_1, size=N, p=pdf_1)
    

    will chose a value from a with the associated probabilities p. For N samples in one calladd set the size attribute. Is this not enough?

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html#numpy.random.choice

    Ah, and if you have the distributions and want to generate the plot like in your link, I would use matplotlib.pyplot.hist(array)