Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1.  
  2. def f3(x, w):
  3. return w[0]+w[1]*x[0]+w[2]*x[1]+w[3]*x[2]+w[4]*x[3]+w[5]*x[4]
  4.  
  5.  
  6. def f1(x, w):
  7. return w[0]+w[1]*x[4] # beceauce its the 4' colum. only takes last year
  8.  
  9. #vi får en vektor med nogle parametre, og af det regner vi
  10.  
  11. # givet vi har 'højre siden af i' hvor sandsynligt er det så, at vores w er rigtigt
  12. def log_p(w, X_train, t_train, sigma, mu_0, f):
  13. D = w.size - 1 #det er fordi w er én større end x
  14. sumValue = 0
  15. for i in range(np.shape(X_train)[0]): #fordi jeg kun er intresset i antallet af rækker
  16. sumValue += t_train[i]*np.log(f(X_train[i,:], w)) - log_factorial_scalar(t_train[i])
  17. sumValue -= ((1/(2*sigma) * np.dot(w-mu_0, w-mu_0)) + np.log((np.sqrt(2*np.pi*sigma))**(D+1)))
  18.  
  19. return sumValue
  20.  
  21.  
  22.  
  23.  
  24. w_tau = np.ones(6) #tau fordi det er den tidligere jævnfør side 37 L11
  25.  
  26. mu_0 = np.ones(w_tau.size)
  27. sigma = 0.25
  28. cov_matrix = sigma*np.eye(w_tau.size)
  29. N = 1000 #arbitræt
  30. n = 0 #arbitræt
  31.  
  32. #vi sampler med t_tau som den næste middelværdi. Den starter med at være sat til np.ones
  33.  
  34.  
  35. w_sample = np.random.multivariate_normal(w_tau, cov_matrix)
  36. u = np.random.uniform(low=0, high=1, size=1)
  37. log_A = min(0, log_p(w_sample, X_train, t_train, sigma, mu_0, f3 ) - log_p(w_tau, X_train, t_train, sigma, mu_0, f3))
  38.  
  39. while n < N:
  40. w_sample = np.random.multivariate_normal(w_tau, cov_matrix)
  41. u = np.random.uniform(low=0, high=1, size=1)
  42. log_A = min(0, log_p(w_sample, X_train, t_train, sigma, mu_0, f3 ) - log_p(w_tau, X_train, t_train, sigma, mu_0, f3))
  43.  
  44. #note: the bigger the number in w_sample & log_A, the bigger the chance that w is correct
  45.  
  46. if log_A > np.log(u):
  47. w_tau = w_sample
  48. n += 1
  49.  
  50. #print('for e5 (4)')
  51.  
  52. W_manySampels = np.zeros([10,2])
  53.  
  54. for i in range(10):
  55. print(i)
  56. w_tau = np.ones(2) #tau fordi det er den tidligere jævnfør side 37 L11
  57.  
  58. mu_0 = np.ones(w_tau.size)
  59. sigma = 0.25
  60. cov_matrix = sigma*np.eye(w_tau.size)
  61. N = 1000 #arbitræt
  62. n = 0 #arbitræt
  63.  
  64. #vi sampler med t_tau som den næste middelværdi. Den starter med at være sat til np.ones
  65.  
  66. while n < N:
  67. w_sample = np.random.multivariate_normal(w_tau, cov_matrix)
  68. u = np.random.uniform(low=0, high=1, size=1)
  69. log_A = min(0, log_p(w_sample, X_train, t_train, sigma, mu_0, f1 ) - log_p(w_tau, X_train, t_train, sigma, mu_0, f1))
  70.  
  71. #note: the bigger the number in w_sample & log_A, the bigger the chance that w is correct
  72.  
  73. if log_A > np.log(u):
  74. w_tau = w_sample
  75. n += 1
  76. W_manySampels[i,:] = w_tau
  77.  
  78.  
  79.  
  80. plt.figure()
  81. plt.plot(W_manySampels[:,0], W_manySampels[:,1], 'x')
  82. plt.title("Model 1")
  83. plt.xlabel('w0')
  84. plt.ylabel('w1')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement