Skip to content

Instantly share code, notes, and snippets.

@Bojne
Last active October 29, 2018 10:00
Show Gist options
  • Save Bojne/406898ec7e8dbceb85b7d668d7d9b4f8 to your computer and use it in GitHub Desktop.
Save Bojne/406898ec7e8dbceb85b7d668d7d9b4f8 to your computer and use it in GitHub Desktop.
“In raquetball, a player continues to serve as long as she is winning; a point is scored only when a player is serving and wins the volley. The first player to win 21 points wins the game. Assume that you serve first and have probability .6 of winning a volley when you serve and probability .5 when your opponent serves. Estimate, by simulation, …
import numpy as np
# Parameters - First game and winning prob.
initial = np.array([0.6,0.4]) #first game
prob_matrix = np.array([[0.6,0.4],[0.5,0.5]])
# Calculate the n product of a vector and prob_matrix
def multiple_dot(vector, n):
if (n == 0):
return vector
else:
vector = vector.dot(prob_matrix)
return multiple_dot(vector, n-1)
# multiple_dot(initial,20) -> array([0.55555556, 0.44444444])
# Save vectos into list.
def generate_21_list():
prob_list = []
prob_list.append(multiple_dot(initial,21)) # considering 21 wins in 21 games
for i in range(1,21): # consideration winning 21 scores in 21+i games
next_vector = prob_list[i-1].dot(prob_matrix)
prob_list.append(next_vector)
return prob_list
# Mean and get result
problist = generate_21_list()
(sum(problist) / len(problist))[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment