In [76]:
import math
In [77]:
# permutations - How many different ways to pick a 4 digits password? without replacement.
n = 10
k = 4
# 10*9*8*7*6*5*4*3*2*1 / 6*5*4*3*2*1 = 1040
permutations = math.factorial(n) / math.factorial(n - k)
print(permutations, '# of permutations.')
In [78]:
# combinations - How many different ways to pick 3 cards from the deck? with replacement.
n = 52
k = 3
# 52 * 51 * 50 ... / 49 * 48 * 47 ...
permutations = math.factorial(n) / math.factorial(n-k)
# permutations / 3 * 2 * 1
combinations = permutations / math.factorial(k)
print(combinations, '# of combinations.')
In [79]:
# cards
In [80]:
# number of cards in a deck
cards = 52
# number of suits.
suits = 4
# individual cards.
ind_cards = 1
# number of dups. ei. number of aces?
dup_cards = ind_cards * suits
# number of cards in a suit
cards_in_suit = cards / suits
In [81]:
# prob. of getting a King?
prob = (dup_cards / cards) * 100
print(str(round(prob, 2)) + "%")
In [82]:
# prob. of getting a face card (King, Queen, Jack)?
prob = (dup_cards * 3 / cards) * 100
print(str(round(prob, 2)) + "%")
In [83]:
# prob. of getting a 10 of clubs?
prob = (ind_cards / cards) * 100
print(str(round(prob, 2)) + "%")
In [84]:
# prob. of getting a club?
prob = (cards_in_suit / cards) * 100
print(str(round(prob, 2)) + "%")
In [85]:
# prob. of drawing a king when a King was already picked first?
prob1 = (4 / 52) * 100
prob2 = (3 / 51) * 100
print(str(round(prob1, 2)) + "%")
print(str(round(prob2, 2)) + "%")
In [86]:
# drawing a heart or a club?
prob1 = (13 / 52) * 100
prob2 = (13 / 52) * 100
mutually_exclusive = prob1 + prob2
print(str(round(mutually_exclusive, 2)) + "%")
In [87]:
# Expected Value
In [88]:
# Pay $2.00, you win 3 if the die is 1,2,3 and 1 if it is 4,5,6. Is the game even? Yes ...
exp = 3 * 3/6 + 1 * 3/6
print(str(round(exp, 2)))
In [89]:
# Pay $2.00, you win 3 if the die is 1,2 and 1 if it is 5,6. 0 if 3,4. Is the game even? No ...
exp = 3 * 2/6 + 0 * 2/6 + 1 * 2/6
print(str(round(exp, 2)))
In [90]:
# Urn - Bayes, Herding...
# each person guesses if an urn is maj. blue or maj. red
# Bayes Refresher...
# Pr[A | B] = Pr[A and B] / Pr[B] = Pr[A|B] * Pr[B] = Pr[A and B] = Pr[B|A] * Pr[A]
# Pr[B | A] = Pr[A | B] * Pr[B] / Pr[A]
In [91]:
# Pr[maj-blue] = 1/2
# Pr[maj-red] = 1/2
# Pr[Blue|maj-blue] = 2/3
# Pr[Red|maj-red] = 2/3
# Pr[Blue|maj-red] = 1/3
# Pr[Red|maj-blue] = 1/3
In [92]:
# pick 1 ...
print("Question: 1st person picks blue, what is the prob. that the maj of the urn is blue?")
In [93]:
# ... so Pr[maj-blue | Blue] = Pr[maj-blue] * Pr[Blue | maj-blue] / Pr[Blue]
print("(Pr[maj-blue] * Pr[Blue | maj-blue]) / Pr[Blue]")
print()
# Pr[maj-blue] (1/2) * Pr[Blue | maj-blue] (2/3)
pr_maj_blue___times___pr_blue__given__urn_maj_blue = 1/2 * 2/3
print("Pr[maj-blue] * Pr[Blue | maj-blue]:", pr_maj_blue___times___pr_blue__given__urn_maj_blue)
print()
# prob. of picking a blue given urn is maj. blue + prob. of picking blue given urn is maj. red.
pr_blue = (1/2 * 2/3) + (1/2 * 1/3)
print("Pr[Blue]:", pr_blue)
print()
answer = pr_maj_blue___times___pr_blue__given__urn_maj_blue / pr_blue
print("answer:", str(round(answer*100,2)) + "%")
In [99]:
# or pick 1 ...
print("Question: 1st person picks blue, what is the prob. that the maj of the urn is red?")
In [98]:
# ... so Pr[maj-red | Blue] = Pr[maj-red] * Pr[Blue | maj-red] / Pr[Blue]
print("(Pr[maj-red] * Pr[Blue | maj-red]) / Pr[Blue]")
print()
# Pr[maj-blue] (1/2) * Pr[Blue | maj-blue] (2/3)
pr_maj_red___times___pr_blue__given__urn_maj_red = 1/2 * 1/3
print("Pr[maj-red] * Pr[Blue | maj-red]:", pr_maj_blue___times___pr_blue__given__urn_maj_blue)
print()
# prob. of picking a blue given urn is maj. blue + prob. of picking blue given urn is maj. red.
pr_blue = (1/2 * 2/3) + (1/2 * 1/3)
print("Pr[Blue]:", pr_blue)
print()
# expected is 33% because 1 - .66 = .33
answer = pr_maj_red___times___pr_blue__given__urn_maj_red / pr_blue
print("answer:", str(round(answer*100,2)) + "%")
In [94]:
# pick 2 ...
print("Question: 2nd person picks blue, what is the prob. that the maj of the urn is blue?")
In [95]:
print("(Pr[maj-blue] * Pr[Blue Blue | maj-blue]) / Pr[Blue Blue]")
print()
prob = 1/2 * (2/3 * 2/3)
print("1.", "Pr[maj-blue] * Pr[Blue Blue | maj-blue]:", prob)
print()
pr_blue_blue = (1/2 * 2/3 * 2/3) + (1/2 * 1/3 * 1/3)
print("2.", "Pr[Blue Blue]:", pr_blue_blue)
print()
answer = prob / pr_blue_blue
print("answer:", str(round(answer*100,2)) + "%")
In [96]:
print("(Pr[maj-blue] * Pr[Blue Red | maj-blue]) / Pr[Blue Red]")
print()
prob = 1/2 * (2/3 * 1/3)
print("1.","Pr[maj-blue] * Pr[Blue Red | maj-blue]:", prob)
print()
pr_blue_red = (1/2 * 2/3 * 1/3) + (1/2 * 1/3 * 2/3)
print("2.","Pr[Blue Red]:", pr_blue_blue)
print()
answer = prob / pr_blue_red
print("answer:", str(round(answer*100,2)) + "%")
In [97]:
print("(Pr[maj-blue] * Pr[Red Red | maj-blue]) / Pr[Red Red]")
print()
prob = 1/2 * (1/3 * 1/3)
print("1.","Pr[maj-blue] * Pr[Red Red | maj-blue]:", prob)
print()
# prob Red Red so .. either red red given maj blue or red red given maj red
pr_red_red = (1/2 * 1/3 * 1/3) + (1/2 * 2/3 * 2/3)
print("2.","Pr[Red Red]:", pr_blue_blue)
print()
answer = prob / pr_red_red
print("answer:", str(round(answer*100,2)) + "%")
In [ ]:
# getting crazy ...
In [100]:
print("(Pr[maj-blue] * Pr[Red Blue Blue Blue | maj-blue]) / Pr[Red Red]")
print()
prob = 1/2 * (1/3 * 2/3 * 2/3 * 2/3)
print("1.","Pr[maj-blue] * Pr[Red Blue Blue Blue | maj-blue]:", prob)
print()
# prob Red Red so .. either red red given maj blue or red red given maj red
pr_red_blue_blue_blue = (1/2 * 1/3 * 2/3 * 2/3 * 2/3) + (1/2 * 2/3 * 1/3 * 1/3 * 1/3)
print("2.","Pr[Red Blue Blue Blue]:", pr_blue_blue)
print()
answer = prob / pr_red_blue_blue_blue
print("answer:", str(round(answer*100,2)) + "%")
In [102]:
print("(Pr[maj-blue] * Pr[Red Red Red Blue Blue Blue | maj-blue]) / Pr[Red Red]")
print()
prob = 1/2 * (1/3 * 1/3 * 1/3 * 2/3 * 2/3 * 2/3)
print("1.","Pr[maj-blue] * Pr[Red Red Red Blue Blue Blue | maj-blue]:", prob)
print()
# prob Red Red so .. either red red given maj blue or red red given maj red
pr_red_red_red_blue_blue_blue = (1/2 * 1/3 * 1/3 * 1/3 * 2/3 * 2/3 * 2/3) + (1/2 * 2/3 * 2/3 * 2/3 * 1/3 * 1/3 * 1/3)
print("2.","Pr[Red Red Red Blue Blue Blue]:", pr_blue_blue)
print()
answer = prob / pr_red_red_red_blue_blue_blue
print("answer:", str(round(answer*100,2)) + "%")
In [111]:
import numpy as np
import matplotlib.pyplot as plt
In [112]:
# maj-blue, #1 red, #2 blue, #3 blue, #4 blue
probs = []
# 1. red
probs.append( (1/2 * 1/3) / ((1/2 * 1/3) + (1/2 * 2/3)) )
# 2. red, blue
probs.append( (1/2 * 1/3 * 2/3) / ((1/2 * 1/3 * 2/3) + (1/2 * 2/3 * 1/3)) )
# 3. red, blue, blue
probs.append( (1/2 * 1/3 * 2/3 * 2/3) / ((1/2 * 1/3 * 2/3 * 2/3) + (1/2 * 2/3 * 1/3 * 1/3)) )
# 4. red, blue, blue, blue
probs.append( (1/2 * 1/3 * 2/3 * 2/3 * 2/3) / ((1/2 * 1/3 * 2/3 * 2/3 * 2/3) + (1/2 * 2/3 * 1/3 * 1/3 * 1/3)) )
# 5. red, blue, blue, blue, blue
probs.append( (1/2 * 1/3 * 2/3 * 2/3 * 2/3 * 2/3) / ((1/2 * 1/3 * 2/3 * 2/3 * 2/3 * 2/3) + (1/2 * 2/3 * 1/3 * 1/3 * 1/3 * 1/3)) )
_ = plt.plot(np.arange(1,len(probs) + 1,1), probs, marker='.', linestyle='none')
_ = plt.xlabel('students')
_ = plt.ylabel('prob. urn is maj-blue')
plt.show()
In [142]:
def translate(priors, maj):
prob = 1
func = np.vectorize(lambda x, maj: 2/3 if x == maj else 1/3)
for e in func(priors, maj):
prob = e * prob
return prob
def nom(priors, maj):
# calc: Pr[maj-blue] * Pr[prior | maj-blue]
return (1/2 * translate(priors, maj))
def denom(priors):
# calc: Pr[prior] | maj-blue
return (1/2 * translate(priors, 'b')) + (1/2 * translate(priors, 'r'))
def prob(priors, maj):
p1 = nom(priors, maj)
p2 = denom(priors)
return p1/p2
In [146]:
# tests
assert prob(['b'], 'b') == 2/3
print(prob(['b'], 'b'))
assert prob(['r','b'], 'b') == 1/2
print(prob(['r','b'], 'b'))
In [196]:
def simulation(draws=20,maj='b',neg_maj='r'):
n = 1
priors = [ ]
ans = [ ]
prev = 0
while n < number_of_draws:
# pull from urn
pick = maj if np.random.random() < .67 else neg_maj
# add pick to prior
priors.append(pick)
# prob.
score = prob(priors, maj)
# save
ans.append(score)
if (score > prev) & (prev > .5):
# chained past ... the point of no return.
break;
# save the current score as the prev. and update # of draws.
prev = score
n = n + 1
return priors, ans
priors, ans = simulation()
x = np.arange(1, len(priors) + 1, 1)
_ = plt.plot(x, ans, marker='.', linestyle='none')
_ = plt.xticks(x, priors)
_ = plt.xlabel('picks')
_ = plt.ylabel('prob of being maj. blue', )
plt.show()
In [191]:
n = 1
totals = [ ]
while n < 10000:
priors, ans = simulation()
totals.append(len(priors))
n = n + 1
In [193]:
_ = plt.hist(totals, normed=True)
_ = plt.xlabel('picks per round')
_ = plt.ylabel('prob.')
plt.show()
In [195]:
_ = plt.hist(totals, bins=50, normed=True, cumulative=True)
_ = plt.xlabel('picks per round')
_ = plt.ylabel('ecdf')
plt.show()
In [ ]: