samedi 24 janvier 2015

Generating numbers with unequal probability

Here is the thing.


I want to generate two values, 1 and -1, with different probability. When I run this script, I get the message " choice() got an unexpected keyword argument 'p'"


Can anyone tell me why this happens and how to fix it? Thanks.



from scipy import *
import matplotlib.pyplot as plt
import random as r

def ruin_demo():


tmax = 1000 #The number of game rounds
Xi = 10 #The initial money the gambler has
T = []
M = []
t = 1
for N in linspace(0.3, 0.49, 100): #The probability changing region

meandeltaX = 1.0*N + (1-N)*(-1.0) #The expected value for each game round
M.append(meandeltaX)
while (t < tmax and Xi > 0):

deltaX = r.choice((1, -1), p=[N, 1-N]) # The change of money during each game round
#print deltaX
Xi = Xi + deltaX
t = t + 1
T.append(t)
plt.plot(M, T)
plt.show()

ruin_demo()




Aucun commentaire:

Enregistrer un commentaire