蒙特卡洛模拟python

用户名

我想模拟一个七场比赛的季后赛系列赛。假设我拥有系列赛每场比赛的获胜概率。我想知道每种可能的系列结果的概率。例如,TeamA参加4场比赛,TeamB参加4场比赛,TeamA参加5场比赛,等等。

这是我想出的,它似乎可以工作,但我认为可以做得更好。

winPercGM1 = .5
winPercGM2 = .56
winPercGM3 = .47
winPercGM4 = .55
winPercGM5 = .59
winPercGM6 = .59
winPercGM7 = .38
winPercs = [winPercGM1, winPercGM2,  winPercGM3, winPercGM4, winPercGM5, winPercGM6,     winPercGM7]

def WinSeries():   
    teamAwins = 0
    teamBwins = 0  
    for perc in winPercs:
        if teamAwins == 4:            
            break            
        elif teamBwins == 4:            
            break            
        elif perc > np.random.random():
            teamAwins += 1            
        else:
            teamBwins += 1            
    return teamAwins, teamBwins

def RunFun(n):
teamAWins = []
teamBWins = []   
for i in xrange(n):
    result = WinSeries()
    teamAWin = result[0]
    teamBWin = result[1]        
    teamAWins.append(teamAWin)
    teamBWins.append(teamBWin)       
return teamAWins, teamBWins


n = 500000
results = RunFun(n)

teamAwinSeries = results[0]
teamBwinSeries = results[1]

teamBin4 = teamAwinSeries.count(0)/n
teamBin5  = teamAwinSeries.count(1)/n
teamBin6 = teamAwinSeries.count(2)/n
teamBin7 = teamAwinSeries.count(3) / n
teamAin4 = teamBwinSeries.count(0)/n
teamAin5  = teamBwinSeries.count(1)/n
teamAin6 = teamBwinSeries.count(2)/n
teamAin7 = teamBwinSeries.count(3) / n
贾明疮

使用numpy(Python 2.7)可以轻松完成此操作

import numpy as np

probs = np.array([.5 ,.56 ,.47 ,.55 ,.59 ,.59 ,.38])
nsims = 500000

chance = np.random.uniform(size=(nsims, 7))

teamAWins = (chance > probs[None, :]).astype('i4')
teamBWins = 1 - teamAWins

teamAwincount = {}
teamBwincount = {}
for ngames in range(4, 8):
    afilt = teamAWins[:, :ngames].sum(axis=1) == 4
    bfilt = teamBWins[:, :ngames].sum(axis=1) == 4

    teamAwincount[ngames] = afilt.sum()
    teamBwincount[ngames] = bfilt.sum()

    teamAWins = teamAWins[~afilt]
    teamBWins = teamBWins[~bfilt]

teamAwinprops = {k : 1. * count/nsims for k, count in teamAwincount.iteritems()}
teamBwinprops = {k : 1. * count/nsims for k, count in teamBwincount.iteritems()}

输出:

>>> sum(teamAwinprops.values()) + sum(teamBwinprops.values())
1.0
>>> teamAwincount
{4: 26186, 5: 47062, 6: 59222, 7: 95381}
>>> teamBwincount
{4: 36187, 5: 79695, 6: 97802, 7: 58465}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

蒙特卡洛模拟python

来自分类Dev

R中的蒙特卡洛模拟

来自分类Dev

N骰子的蒙特卡洛模拟

来自分类Dev

R中的蒙特卡洛模拟

来自分类Dev

Python中的蒙特卡洛

来自分类Dev

MCNP蒙特卡洛模拟的开源替代方案

来自分类Dev

蒙特卡洛模拟无法识别连通图

来自分类Dev

从甲板上抽牌进行蒙特卡洛扑克模拟

来自分类Dev

使用降雪的R中并行蒙特卡洛模拟

来自分类Dev

在简单的Java中对Pi进行蒙特卡洛模拟?

来自分类Dev

R:蒙特卡洛模拟和正态分布问题

来自分类Dev

使用R中的蒙特卡洛模拟股价

来自分类Dev

使用蒙特卡洛模拟的误差传播数据帧

来自分类Dev

向量数据集的蒙特卡洛模拟

来自分类Dev

如何绘制随机行走/蒙特卡洛模拟Python的平均值

来自分类Dev

蒙特卡洛模拟在Python中连续两个头部的预期投掷

来自分类Dev

python,乌龟,pi和蒙特卡洛

来自分类Dev

使用GPU加速python中的蒙特卡洛仿真

来自分类Dev

python中的蒙特卡洛模拟:使用ipyparallel进行并行化要比序列化花费更长的时间

来自分类Dev

蒙特卡洛人口实施

来自分类Dev

了解蒙特卡洛树搜索

来自分类Dev

$ \ pi $的蒙特卡洛估计

来自分类Dev

检索蒙特卡洛模拟值以进行卡方检验

来自分类Dev

检索蒙特卡洛模拟值以进行卡方检验

来自分类Dev

Matlab代码将Java翻译为蒙特卡洛模拟

来自分类Dev

蒙特卡洛模拟的最佳种子MT19937_64的最佳方式

来自分类Dev

NSTableView:使用蒙特卡洛模拟找出列宽吗?

来自分类Dev

蒙特卡洛模拟,用于抛硬币以获得特定图案

来自分类Dev

是否有办法从使用R的大型样本蒙特卡洛模拟中确定精确分数?

Related 相关文章

热门标签

归档