PyMC中的多项式分布 [英] Multinomial distribution in PyMC

查看:110
本文介绍了PyMC中的多项式分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pymc的新手.我已经在github上阅读了必需的内容,并且在我被这个问题困扰之前一切都很好. 我想收集多项随机变量,以后可以使用mcmc对其进行采样.但是我能做的最好的是

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

但这不好,因为我希望能够调用rv.valuerv.random(),否则我将无法从中进行采样.

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是非ve整数的列表,每个整数表示该分布的n值,例如.可能的计数可以是[26, 39, 20, 10]

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是列出概率列表的列表.例如,可能的p_d可以为[[0.7, 0.3], [0.5, 0.1, 0.4], [0.4, 0.6], [0.8, 0.2]]

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]]

for循环没有用.他们只是表明组件是多项式随机变量,但我想我无法将组件与mcmc一起使用以获取后验分布.我需要一些将mcmc与rv结合使用的方法.

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.

如果有人可以告诉我pymc中的一些函数(如numpy.array()(将列表转换为numpy数组),可以将列表转换为我想要的东西,那将是非常好的. (很抱歉,我无法用科学的术语来表达它,但是我试图使自己尽可能清晰) 告诉我是否有人需要更多信息.

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.

编辑1

我有一些游戏的数据,例如[8, 8, 10].它表示该游戏进行了26次时,P1(玩家1)获得A1(动作1)8次,A2 8次,A3 10次.

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.

(不同游戏中的动作数可以不同.在此示例中,结果为[8, 8, 10]的情况下,有3个动作)

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

我大约有200个这样的数据(以列表/numpy数组的形式)

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

我相信多项式分布最能描述数据.

I believe Multinomial distribution best describes the data.

所以我写了一个确定性函数,给定一个均匀分布的随机变量tau可以在这些动作上生成概率分布,例如,在这种情况下,例如[0.16, 0.28, 0.56]

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]

您看到,我有200个这样的列表,每个列表表示该游戏中各个动作的概率分布.我还有一个列表,其中包含200个整数(可能有所不同),每个整数表示游戏的播放次数(我通过对数据进行求和获得的值,例如,数据为[8, 8, 10,]的游戏播放了26次).

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).

现在给定观察到的数据(200个列表,例如[[8, 8, 10], [0, 0, 0], ....., [12, 3]]的列表),我想绘制tau的后验概率分布(我最初假设是均匀的)

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)

推荐答案

认为您想要这样的东西:

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')[:]

还要确保您没有安装pymc2.3.

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

这篇关于PyMC中的多项式分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆