问题描述
如何使用Pymc3模拟6面骰子掷骰?另外,我知道骰子的不同面具有不同的分布吗?
How do I simulate a 6-side Dice roll using Pymc3? Also, what is I know that different sides of the dice have different distributions?
推荐答案
在PyMC3
中模拟1000卷6面公平骰子的最简单方法是
The easiest way to simulate 1000 rolls of a fair 6-sided die in PyMC3
is
import pymc3 as pm
with pm.Model():
rolls = pm.DiscreteUniform('rolls', lower=1, upper=6)
trace = pm.sample(1000)
trace['rolls'] # shows you the result of 1000 rolls
请注意,这比调用np.random.randint(1, 7, size=1000)
慢,但等效.
Note that this is slower, but equivalent, to just calling np.random.randint(1, 7, size=1000)
.
对于1000卷不公平的死亡
For 1000 rolls of an unfair die
probs = np.array([0.1, 0.2, 0.3, 0.2, 0.1, 0.1])
with pm.Model():
rolls = pm.Multinomial('rolls', n=1000, p=probs, shape=6)
trace = pm.sample(1)
与np.random.multinomial(1000, pval=probs)
相同,但速度较慢.
想要 使用PyMC3
的情况是,例如,如果您观察到50卷不公平的骰子,则有 prior 期望一个公平的死者,并且想要评估那个期望的后验.这是一个示例:
The situtation in which you would want to use PyMC3
is if you observe, say, 50 rolls of an unfair die, have some prior expectation that it is a fair die, and want to evaluate the posterior of that expectation. Here's an example of that:
observations = np.array([20, 6, 6, 6, 6, 6])
with pm.Model():
probs = pm.Dirichlet('probs', a=np.ones(6)) # flat prior
rolls = pm.Multinomial('rolls', n=50, p=probs, observed=observations)
trace = pm.sample(1000)
trace['probs'] # posterior samples of how fair the die are
您可以使用内置的traceplot
来查看示例的外观:
You can use the built-in traceplot
to see how the samples look:
请注意,我们正确地计算出其中一方比另一方更经常出现!
Note that we correctly work out that one of the sides comes up more often than the others!
这篇关于如何使用Pymc3模拟偏置的6面骰子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!