Last active
October 29, 2018 10:00
-
-
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, …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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