Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Apr 28 15:50:45 2017
  5.  
  6. @author: CrystalOng
  7. """
  8.  
  9. import numpy as np
  10. import math as m
  11. import matplotlib.pyplot as plt
  12.  
  13. def UCB(mu1,mu2,T):
  14. l = [0,0]
  15. u = [1,1]
  16. Ti = [0,0]
  17. X = [[0],[0]]
  18. v=0
  19. for i in range(1,T+1):
  20. X[0].append(np.random.binomial(1,mu1))
  21. X[1].append(np.random.binomial(1,mu2))
  22. ad = 0
  23. if(u[0]>u[1]):
  24. ad = 0
  25. elif(u[0]<u[1]):
  26. ad = 1
  27. else:
  28. ad = np.random.binomial(1,0.5)
  29. X[1-ad][i] = 0
  30. v = v+X[ad][i]
  31. Ti[ad] =Ti[ad]+1
  32.  
  33. mub = sum(X[ad])/Ti[ad]
  34.  
  35. u[ad] = mub + m.sqrt(2*(m.log(T,10))/Ti[ad])
  36. l[ad] = mub - m.sqrt(2*(m.log(T,10))/Ti[ad])
  37. return v
  38.  
  39. def thomson(mu1,mu2,T):
  40. pi=0
  41. a = [1,1]
  42. b = [1,1]
  43. X = [0,0]
  44. for i in range(1,T+1):
  45. th1 = np.random.beta(a[0],b[0])
  46. th2 = np.random.beta(a[1],b[1])
  47. X = [np.random.binomial(1,mu1),np.random.binomial(1,mu2)]
  48. ad =0
  49. if(th1>th2):
  50. ad = 0
  51. elif(th1<th2):
  52. ad = 1
  53. else:
  54. ad = np.random.binomial(1,0.5)
  55. X[1-ad] = 0
  56. pi = pi+X[ad]
  57. a[ad] = a[ad]+X[ad]
  58. b[ad] = b[ad]+1-X[ad]
  59. return pi
  60.  
  61.  
  62.  
  63. x = np.arange(0.01,0.31,0.01)
  64. mu1=0.5
  65. T=1000
  66. avreg = []
  67. avreg2 =[]
  68. #TEST ALL DELTAS
  69. for delta in x:
  70. reg = []
  71. reg2 = []
  72. #SIMULATE AND GET THE AVERAGE 1000 TIMES
  73. for i in range(500000):
  74. if(i%250000):
  75. print(i)
  76. v=UCB(mu1,mu1+delta,T)
  77. pi=thomson(mu1,mu1+delta,T)
  78. reg.append((T*(mu1+delta)-v))
  79. reg2.append(T*(mu1+delta)- pi)
  80. avreg.append(np.mean(reg))
  81. avreg2.append(np.mean(reg2))
  82. plt.plot(x,avreg,'red',x,avreg2,'blue')
  83. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement