In a chess game, a player has a 40% chance of winning, 25% of a draw, 35% chance of losing. After 12 games, what is the likelihood of 7 wins, 3 draws and 2 losses?
In [1]:
import math
In [3]:
def choose(n, k):
return math.factorial(n) / math.factorial(k) / math.factorial(n-k)
In [4]:
choose(5,2)
Out[4]:
In [5]:
n = 12
In [6]:
pw = 0.4
pd = 0.25
pl = 0.35
In [7]:
nw = 7
nd = 3
Multinomial is calculated by considering each outcome as probability of it happening vs it not happening, then using conditional probability for each remaining outcome.
In [9]:
def multi(n, nw, nd, pw, pd):
conditional_pd = pd / (1-pw)
conditional_n = n-nw
return choose(n, nw) * math.pow(pw, nw) * math.pow( 1-pw, n-nw ) * choose(conditional_n, nd) * math.pow( conditional_pd, nd ) * math.pow( 1-conditional_pd, conditional_n-nd )
In [10]:
multi( 12, 7, 3, 0.4, 0.25 )
Out[10]:
In [11]:
choose(n, nw) * math.pow(pw, nw) * math.pow( 1-pw, n-nw )
Out[11]:
In [44]:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
In [13]:
w = np.arange(0, 12, 1)
d = np.arange(0, 12, 1)
X, Y = np.meshgrid(w, d)
In [47]:
multi( 12, 7, 3, pw, pd )
Out[47]:
In [24]:
import numpy as np
In [28]:
arr = np.zeros((12,12))
In [30]:
for x in xrange(0,12):
for y in xrange(0,12):
if x + y > 12:
arr[x,y] = 0
else:
arr[x,y] = multi(12, x, y, pw, pd)
In [49]:
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, arr, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
In [40]:
np.max(arr)
Out[40]:
In [42]:
np.unravel_index(arr.argmax(), arr.shape)
Out[42]:
In [50]:
np.sum(arr)
Out[50]:
In [ ]: