使用随机模拟输出的差异

问题描述

我试图通过生成模拟来找到股价从25美元跌至18美元的预期时间步长。从分析上可以看出,所需的时间步长预期为129。

以下3个代码对我来说基本上是相同的,但给出的结果却不同。

一个返回平均100个时间步长:

target = 18
support_level = 20
I = []
n = 50000

for i in range(n):
    stock_price = 25
    t = 0
    
    while stock_price != target:

        t += 1

        if stock_price < support_level:
            stock_price += random.choices([-1,1],weights=[1/3,2/3])[0]

        if stock_price == support_level:
            stock_price += random.choices([-1,weights=[0.1,0.9])[0]

        if stock_price > support_level:
            stock_price += random.choices([-1,weights=[2/3,1/3])[0]
    
    I.append(t)
sum(I) / len(I)

第二个平均返回114:

for i in range(n):
    stock_price = 25
    t = 0
    
    while stock_price != target:
        x = random.uniform(0,1)
        t += 1

        if stock_price < support_level:
            if x <= 2/3:
               stock_price += 1
            else:
               stock_price -= 1

        if stock_price == support_level:
            if x <= 0.9:
               stock_price += 1
            else:
               stock_price -= 1

        if stock_price > support_level:
            if x <= 1/3:
               stock_price += 1
            else:
               stock_price -= 1
    
    I.append(t)

第三个返回平均值129:

for i in range(n):
    p=25
    t=0

    while p != 18:
        if(p==20):
            dist = [0.1,0.9]
        elif(p>20):
            dist = [2/3,1/3]
        else:
            dist = [1/3,2/3]

        p=p+random.choices(direction,dist)[0]
        t+=1
        
    I.append(t)
    
sum(I)/len(I)

有什么区别?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)