Advertisement
Guest User

Ridder Classic solution 21/02/2020

a guest
Feb 24th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. from scipy import special
  2. import math
  3. import numpy as np
  4. import matplotlib as mpl
  5. import matplotlib.pyplot as plt
  6.  
  7. #function to calculate log(n choose k)
  8. def logComb(n,k):
  9.     g = max(k, n-k)
  10.     num = sum([math.log(i) for i in range(g+1, n+1)])
  11.     denom = sum([math.log(i) for i in range(1, n-g+1)])
  12.     return(num-denom)
  13.  
  14. def oneTerm(x, x1, n, p):
  15.     if (x1+n)%2 != 0 or (x-x1-2*n+200)%4 != 0:
  16.         return (0)
  17.  
  18.     if (x-x1) < -2*(100-n) or (x-x1) > 2*(100-n):
  19.         return (0)
  20.  
  21.     comb1 = logComb(n, int((x1+n)/2))
  22.     comb2 = logComb(100-n, int((x-x1-2*n+200)/4))
  23.  
  24.     if p == 0:
  25.         if ((x1+n)/2) == 0:
  26.             ps = 0
  27.             qs = 0
  28.         else:
  29.             return (0)
  30.     elif p == 1:
  31.         if ((n-x1)/2) == 0:
  32.             ps = 0
  33.             qs = 0
  34.         else:
  35.             return (0)
  36.     else:
  37.         ps = ((x1+n)/2) * math.log(p)
  38.         qs = ((n-x1)/2) * math.log(1-p)
  39.        
  40.     twos = (n-100) * math.log(2)
  41.  
  42.     return(math.exp(comb1 + ps + qs + comb2 + twos))
  43.  
  44. def pWinFnc(x,n,p):
  45.     x1Range = range(-n, n+1, 2)
  46.     return(sum([oneTerm(x, x1, n, p) for x1 in x1Range]))
  47.  
  48. #determining optimal n
  49. xRange = range(1, 201)
  50.  
  51. pList = np.linspace(0, 1, 101)
  52. maxPList = []
  53. maxNList = []
  54.  
  55. for p in pList:
  56.     pWin = [(sum([pWinFnc(x,n,p) for x in xRange]), n) for n in range(0,101)]
  57.     z = max(pWin, key=lambda x:x[0])
  58.     maxPList.append(z[0])
  59.     maxNList.append(z[1])
  60.     print(p)
  61.  
  62. print(maxPList, maxNList)
  63.  
  64. fig, ax = plt.subplots()
  65. ax.scatter(pList, maxPList, color='b', s=10)
  66. ax.set(xlabel='p, heads probability of the 1-coin', ylabel='P(Win), Win Probability',
  67.        title='P(Win) vs p')
  68. ax.grid()
  69. ax.set_xticks(np.arange(0, 1.01, step=0.1))
  70. fig.savefig("pwin_vs_p.png")
  71.  
  72. fig, ax = plt.subplots()
  73. ax.scatter(pList, maxNList, color='r', s=10)
  74. ax.set(xlabel='p, heads probability of the 1-coin', ylabel='Optimal n',
  75.        title='Optimal n vs p')
  76. ax.grid()
  77. ax.set_xticks(np.arange(0, 1.01, step=0.1))
  78. fig.savefig("n_vs_p.png")
  79.  
  80.  
  81. #printing example PMFs
  82. p = 0.7
  83. n = 40
  84. pWin = [pWinFnc(x,n,p) for x in range(-200,201)]
  85.  
  86. fig, ax = plt.subplots()
  87. ax.bar(range(-200, 201), pWin, color='g')
  88. ax.set(xlabel='x, Total score', ylabel='Probability',
  89.        title=f'PMF f(x), n={n}, p={p}')
  90. plt.axvline(x=0, color = "b")
  91. ax.grid()
  92. fig.savefig("PMF.png")
  93.  
  94. p = 0.01
  95. n = 15
  96. pWin = [pWinFnc(x,n,p) for x in range(-200,201)]
  97.  
  98. fig, ax = plt.subplots()
  99. ax.bar(range(-200, 201), pWin, color='g')
  100. ax.set(xlabel='x, Total score', ylabel='Probability',
  101.        title=f'PMF f(x), n={n}, p={p}')
  102. plt.axvline(x=0, color = "b")
  103. ax.grid()
  104. fig.savefig("PMF2.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement