Multinomial distribution in PyMC

2019-06-04 06:54发布

I am a newbie to pymc. I have read the required stuff on github and was doing fine till I was stuck with this problem. I want to make a collection of multinomial random variables which I can later sample using mcmc. But the best I can do is

rv = [ Multinomial("rv", count[i], p_d[i]) for i in xrange(0, len(count)) ]

for i in rv:

  print i.value
  i.random()

for i in rv:

  print i.value

But it is of no good since I want to be able to call rv.value and rv.random(), otherwise I won't be able to sample from it.

count is a list of non-ve integers each denoting value of n for that distribution eg. a possible count can be [26, 39, 20, 10]

p_d is a list of lists denting probabilities. eg, a possible p_d can be [[0.7, 0.3], [0.5, 0.1, 0.4], [0.4, 0.6], [0.8, 0.2]]

The for loops are of no use. They just shows that the components are multinomial random variables but I think I can't use the components with mcmc to get the posterior distribution. I need some way to use mcmc with rv.

It will be perfectly fine if someone can apprise me of some function like numpy.array() (which converts list to numpy arrays) in pymc which can convert a list to something that I want. (I am sorry I am not able to express it in scientific terms but I have tried to make myself as clear as possible) Tell me if someone needs more information.

EDIT 1

I have data from several games, eg [8, 8, 10] . It denotes when this game was played 26 times, P1(Player 1) took A1 (Action 1) 8 times, A2 8 times, A3 10 times.

(There can be different no. of actions in different games. In this example where the outcome is [8, 8, 10], there are 3 actions )

I have around 200 such data (in the form of lists / numpy arrays)

I believe Multinomial distribution best describes the data.

So I wrote a deterministic function which, given a uniformly distributed random variable tau generates probability distribution over these actions eg, in this case , say [0.16, 0.28, 0.56]

You see, I have 200 such lists ,each denoting probability distribution over actions in that game. I also have a list containing 200 integers (possibly different) each denoting number of times a game was played (which I obtain by summing over the data eg the game with data [8, 8, 10,] was played 26 times).

Now given the observed data (a list of 200 lists eg, [[8, 8, 10], [0, 0, 0], ....., [12, 3]]), I want to draw posterior probability distribution of tau (which I assumed initially to be uniform)

1条回答
Luminary・发光体
2楼-- · 2019-06-04 07:32

Think you want something like this:

from pymc import *
p_d = [[0.7, 0.3], [0.5, 0.1, 0.4], [0.4, 0.6], [0.8, 0.2]]
count =[26, 39, 20, 10]

rv = [ Multinomial("rv"+str(i), count[i], p_d[i]) for i in xrange(0, len(count)) ]

m = MCMC(rv)

m.sample(100)

print m.trace('rv0')[:]

Also make sure you have pymc2.3 installed not 3.

查看更多
登录 后发表回答