暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【Python的可视化分析】蒙特卡洛模拟(下)

数据信息化 2019-11-23
939

    上文最后几个“聪明的”的赌徒研究出一种理论上稳赚不赔的方法。

    关于赌徒们提到的这个方法在概率论中叫做‘’。

鞅的原名 martingale 原指一类于18世纪流行于法国的投注策略,称为加倍赌注法。在博弈中,赌徒会掷硬币,若硬币正面向上,赌徒会赢得赌本,若硬币反面向上,赌徒会输掉赌本。这一策略使赌徒在输钱后加倍赌金投注,为的是在初次赢钱时赢回之前输掉的所有钱,同时又能另外赢得与最初赌本等值的收益。当赌徒的财产和可用时间同时接近无穷时,他掷硬币后硬币正面向上的概率会接近1,由此看来,加倍赌注法似乎是一种必然能赢钱的策略
https://zh.wiki.6bu6.xyz/wiki/鞅_(概率论)


首先游戏基本规则不变,定义骰子的游戏规则:

import random


def give_the_number():
number=random.randint(1,6)


    if number <=3:
# print("骰子的大小为:",number," 你赢了!")
return True
else:
# print("骰子的大小为:",number," 你输了!")
return False

接下来讨论第一轮投掷的情况:

def smart_gambler(funds,initial_wager,game_count):    #本金,初始赌注,游戏次数
wager = initial_wager
x = []
y = []
current_game_count = 1




previous_game = 'win'
previous_game_wager = initial_wager




while current_game_count <= game_count:
if previous_game == 'win': #第一轮获胜的情况
print('The gambler win the last game,Play more')
if give_the_number():
funds += wager
print(funds)
x.append(current_game_count)
y.append(funds)
else: #第一轮输掉的情况
funds -= wager
previous_game = 'loss'
print(funds)
previous_game_wager = wager
x.append(current_game_count)
y.append(funds)
if funds < 0: #破产退出游戏
print ('The gambler went broke in'+
str(current_game_count)+'games')
break

继续该讨论第二轮以至后续的情况:

        

elif previous_game == 'loss':    #第一轮输掉了但是第二轮赢了
print("we lost the last one, but the gambler will double up˜")
if give_the_number():
                wager = previous_game_wager * 2  
print ('we won'+str(wager))
                funds += wager    #增加双倍赌注
print (funds)
wager = initial_wager
previous_game = 'win'
x.append(current_game_count)
y.append(funds)
else: #第一轮输掉了,第二轮又输了
wager = previous_game_wager * 2
print ('we lost'+str(wager))
                funds -= wager      #减少双倍赌注
if funds < 0:
print('The gambler went broke in'
+str(current_game_count)+'games')
break
print(funds)
previous_game = 'loss'
previous_game_wager = wager
x.append(current_game_count)
y.append(funds)
if funds < 0: #输破产,不让玩了
print('went broke after'+str(current_game_count)+'games')
                    break
current_game_count += 1
print(funds)


接着我们导入可视化包,同时导入蒙特卡洛模型:

import matplotlib.pyplot as plt


plt.plot(x,y)
n=0


while n<100:
smart_gambler(10000,1000,100)
n+=1


plt.xlabel("How many games does the fool played")


plt.ylabel("Funds")
plt.show()

参数不变,还是50个赌徒,本金为10000元,赌注为1000元,进行100次模拟游戏。让我们来看下结果:

这个结果是不是与想象中的不太一样?如果以10000元为基数,可以看到有很多人都赚了钱,难道是模拟次数太少了?我们把模拟次数更改为1000,继续输出结果:

可以看到还是有少数人走到了最后,但是这个数量相比于基数50也是比较多了,根本算不上十赌九输呀,那么问题出在哪里了呢?不知道大家注意到了没有,到了200次之后有很多赌徒一把输到破产,但是下一轮又赢了回来,问题就出在这里,本金都不够下一轮赌注了还怎么赌?所以我们要考虑当投注金额大于本金时,应当停止投注,否则的话,会出现负数,与实际情况不符,所以改进了一下代码:

import random
import matplotlib.pyplot as plt


def give_the_number():
number=random.randint(1,6)

if number <=3:
# print("骰子的大小为:",number," 你赢了!")
return True
else:
# print("骰子的大小为:",number," 你输了!")
return False


def smart_gambler(funds,initial_wager,game_count):
current_game_count =1


wager =initial_wager
previous_game_wager=initial_wager
previous_game =random.randint(0,1)
x=[]
y=[]


while current_game_count<=game_count:
if previous_game==1:
           print('The gambler win the last game,Play more')
if give_the_number():
funds +=wager
print(current_game_count,wager,funds)


x.append(current_game_count)
y.append(funds)


else:
funds -=wager
previous_game=0
print(current_game_count,wager,funds)
previous_game_wager=min(wager,funds) #考虑本金小于赌注的情况


x.append(current_game_count)
y.append(funds)


if funds <=0:
print('The fool went broke in '+
str(current_game_count)+'games')
break


elif previous_game==0:
print('we lost the last game,but the gambler will double up')
if give_the_number():
wager =min(previous_game_wager *2,funds) #考虑本金小于赌注的情况
print('we won'+str(wager))


funds +=wager
print(current_game_count,wager,funds)


previous_game=1


x.append(current_game_count)
y.append(funds)


else:
wager =min(previous_game_wager *2,funds) #考虑本金小于赌注的情况
print('we lost'+str(wager))


funds -=wager
if funds<=0:
print('The gambler went brok in '+
str(current_game_count)+'games')
break
print(current_game_count,funds)
previous_game=0
previous_game_wager=wager
x.append(current_game_count)
y.append(funds)
if funds<=0:
print('We went borke after'+
str(current_game_count)+'games')
current_game_count +=1

print(current_game_count,wager,funds)
plt.plot(x,y)
       
n=0
while n<100:
smart_gambler(10000,1000,100)
n+=1

plt.xlabel('how many games does the fool play')
plt.ylabel('funds')
plt.show()

这次我们可以看到只有两个人坚持到最后,但是已经他们没有足够的资金进行下一轮的赌注了。




文章转载自数据信息化,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论