函数returns某特定值的概率

Probability that the function returns a specific value

我无法理解为所请求的以下概率值找到数学表达式的方法。我得到了一个 Python 函数,定义为:

def randomFunction(n):
    return sum([random.choice([-1,1] for _ in range(n)])

然后要求找到概率值:

  1. randomFunction(25) 将 return 4
  2. randomFunction(25) 将 return 3
  3. randomFunction(6000) 将 return 0(准确的 "analytic estimate," 不是基于模拟结果)

我为所有三个概率创建了一个 monte carlo 模拟,以便能够仔细检查我的工作;但是,我不确定如何从数学上解决这个问题。

我认为第三个问题将需要斯特林近似值,因为它被表述为 "analytic estimate." 我认为脱节与我如何找到成功次数以确定概率有关。最后,我能够解决第一个问题,通过推理,它的概率为 0.0,因为在添加 and/or 减去奇数的 ±1 时,偶数不能相加次。

我觉得这是一个很初级的问题;但是,我似乎缺少如何处理这个问题。任何 help/direction 将不胜感激!

我的模拟如下,以防有人感兴趣:

import random

# Initialize Counters
count4 = 0
count3 = 0
count0 = 0

# Function that 
def randomFunction(n):
    return sum([random.choice([-1,1]) for _ in range(n)])

### Simulation for Sum 4 ###
for x in range(10**4):
    if randomFunction(25) == 4:
        count4 += 1

print "Sum 4 Count: ",count4
print "P(Sum 4): ",float(count4)/10**4
############################

### Simulation for Sum 3 ###
for x in range(10**4):
    if randomFunction(25) == 3:
        count3+=1

print "Sum 3 Count: ",count3
print "P(Sum 3): ",float(count3)/10**4
############################

### Simulation for Sum 0 ###
for x in range(10**4):
    if randomFunction(6000) == 0:
        count0+=1
    if x%1000==0: # Progress update every 10%
        print str((float(x)/10**4)*100)+"%"

print "Sum 0 Count: ",count0
print "P(Sum 0): ",float(count0)/10**4
############################

(1) 为了randomFunction(25)到return 4,除了相等数量的1和[=13之外,它还必须生成四个1 =]s.

如果它总共要生成 25 个数字,那么它需要从 25-4=21 个剩余数字中获得相等数量的 1-1。这在数学上是不可能的。因此,答案是这是0

(2) 让我们使用相同的逻辑:您将需要十四个 1 和十一个 -1。现在,有 2^25 种可能的方法可以在 randomFunction 中生成 25 1-1 的列表;并且您需要这 25 个中的 11 个是特定数字(尽管这 11 个数字并不重要)。因此,有 25choose11 种方法可以获取这 11 个数字。极好的!所以现在,您只需要计算 25choose11 / 2^25

(3) 与之前相同的逻辑:您现在需要 3000 1s 和 3000 -1s。因此,计算 6000choose3000 / 2^6000