如何在 python 中模拟有偏差的模具?
我想模拟 N 面偏置芯片?
def roll(N,bias):
'''this function rolls N dimensional die with biasing provided'''
# do something
return result
>> N=6
>> bias=( 0.20,0.20,0.15,0.15,0.14,0.16,)
>> roll(N,bias)
2
I want to simulate N-sided biased die?
def roll(N,bias):
'''this function rolls N dimensional die with biasing provided'''
# do something
return result
>> N=6
>> bias=( 0.20,0.20,0.15,0.15,0.14,0.16,)
>> roll(N,bias)
2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这里有一点数学知识。
常规骰子会以相同的概率给出每个数字 1-6,即
1/6
。 这被称为均匀分布(它的离散版本,而不是连续版本)。 这意味着如果X
是描述单个角色结果的随机变量,则X~U[1,6]
- 意味着X
是分布式的同等地针对掷骰子的所有可能结果(1 到 6)。这相当于在
[0,1)
中选择一个数字,同时将其分为 6 个部分:[0,1/6 ),
[1/6,2/6)
,[2/6,3/6)
,[3/6,4/ 6)
、[4/6,5/6)
、[5/6,1)
。您要求不同的分布,这是有偏见的。
实现此目的的最简单方法是根据您想要的偏差将
[0,1)
部分分为 6 个部分。 因此,根据您的情况,您需要将其分为以下几部分:[0,0.2)
,[0.2,0.4)
,[0.4,0.55)
,0.55,0.7)
,[0.7,0.84)
,[0.84,1)
。如果您查看维基百科条目,您会发现在这种情况下,累积概率函数不会由 6 个等长部分组成,而是由 6 个根据您给定的偏差而长度不同的部分组成。 质量分布也是如此。
回到问题,根据您使用的语言,将其翻译回您的骰子。 在 Python 中,这是一个非常粗略但有效的示例:
A little bit of math here.
A regular die will give each number 1-6 with equal probability, namely
1/6
. This is referred to as uniform distribution (the discrete version of it, as opposed to the continuous version). Meaning that ifX
is a random variable describing the result of a single role thenX~U[1,6]
- meaningX
is distributed equally against all possible results of the die roll, 1 through 6.This is equal to choosing a number in
[0,1)
while dividing it into 6 sections:[0,1/6)
,[1/6,2/6)
,[2/6,3/6)
,[3/6,4/6)
,[4/6,5/6)
,[5/6,1)
.You are requesting a different distribution, which is biased.
The easiest way to achieve this is to divide the section
[0,1)
to 6 parts depending on the bias you want. So in your case you would want to divide it into the following:[0,0.2)
,[0.2,0.4)
,[0.4,0.55)
,0.55,0.7)
,[0.7,0.84)
,[0.84,1)
.If you take a look at the wikipedia entry, you will see that in this case, the cumulative probability function will not be composed of 6 equal-length parts but rather of 6 parts which differ in length according to the bias you gave them. Same goes for the mass distribution.
Back to the question, depending on the language you are using, translate this back to your die roll. In Python, here is a very sketchy, albeit working, example:
我已经为字典创建了一个代码,给出了事件和相应的概率,它返回了相应的键,即该概率的事件。
i have created a code for a dictionary giving a event and corresponding probability, it gives back the corresponding key ie the event of that probability.
我们也可以使用 numpy 的多项式分布
如果您想滚动有偏差的骰子(具有给定的偏差概率)n 次,使用以下函数
We could use
numpy
'smultinomial
distribution tooIf you want to roll the biased die (with the given
bias
probabilities) forn
times, use the following function对于 python 3.6 及更高版本,您可以使用 stdlib 中已经包含的 random's choice() 方法。
要模拟示例中的骰子,等效代码是:-
For python 3.6 and above, you can make use of random's choices() method already part of stdlib.
To simulate the die in your example, the equivalent code would be:-
更多与语言无关,但您可以使用查找表。
使用 0-1 范围内的随机数并在表中查找该值:
More language agnostic, but you could use a lookup table.
Use a random number in the range 0-1 and lookup the value in a table:
偏差将成比例。
也可以使用整数(更好):
The bias will be proportional.
Could use integers too (better):
有点令人惊讶的是
np.random .choice
此处尚未给出答案。p 选项给出“与a中每个条目相关的概率”,其中a对我们来说是
np.range(N)
。 “如果没有给出,则样本假定在a中的所有条目上均匀分布”。It is a little surprising that the
np.random.choice
answer hasn't been given here.The p option gives "the probabilities associated with each entry in a", where a is
np.range(N)
for us. "If not given the sample assumes a uniform distribution over all entries in a".请参阅Walker 别名方法的配方,了解具有不同概率的随机对象。
一个例子,字符串 ABC 或 D 的概率为 .1 .2 .3 .4 --
欢呼
——丹尼斯
See the recipe for Walker's alias method for random objects with different probablities.
An example, strings A B C or D with probabilities .1 .2 .3 .4 --
cheers
-- denis
只是为了建议一种更有效(和 pythonic3)的解决方案,可以使用 bisect< /a> 在累积值向量中进行搜索 - 此外,可以预先计算并存储该向量,希望对该函数的后续调用将引用相同的“偏差”(遵循问题用语)。
在没有 Python 3 累积的情况下,只需编写一个简单的循环来计算累积和。
Just to suggest a more efficient (and pythonic3) solution, one can use bisect to search in the vector of accumulated values — that can moreover be precomputed and stored in the hope that subsequent calls to the function will refer to the same "bias" (to follow the question parlance).
In absence of Python 3 accumulate, one can just write a simple loop to compute the cumulative sum.