import scipy.stats import numpy as np # Python ranges are [x,y) dice = scipy.stats.rv_discrete( values=(range(1,7), np.repeat(1/6.0, 6))) twoDice = scipy.stats.rv_discrete( values=(range(2,13), [1/36.0,2/36.0, 3/36.0, 4/36.0, 5/36.0, 6/36.0, 5/36.0, 4/36.0, 3/36.0, 2/36.0, 1/36.0] )) def getExpect(r,dice_dist): # See https://stats.stackexchange.com/questions/220/how-is-the-minimum-of-a-set-of-random-variables-distributed # for reasoning of the following formula: minOfDistCdf = map(lambda x: 1-dice_dist.sf(x)**r, dice_dist.xk) # Convert the previous CDF to a PMF, by subtracing the next item in the list with the previous one. minOfDistPmf = [minOfDistCdf[0]]+[y - x for x,y in zip(minOfDistCdf,minOfDistCdf[1:])] new_dist = scipy.stats.rv_discrete( values=(dice_dist.xk, minOfDistPmf)) return new_dist.expect() def simulate(r,d,dice_dist, n): # r = rounds, d = amount of dice, dice_dist = ..., n = amount of simulation rounds. minima = list() for i in range(0,n): minSoFar = 1000000000 # This is really poor and lazy programming :D But it's for a lazy person anyway. for i in range(0,r): thisThrow = sum(np.random.choice(dice_dist.xk, d)) minSoFar = min([minSoFar,thisThrow]) minima.append(minSoFar) return sum(minima) / float(n) print simulate(4,2,dice,1000) print getExpect(4,twoDice)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question