Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def f3(x, w):
- return w[0]+w[1]*x[0]+w[2]*x[1]+w[3]*x[2]+w[4]*x[3]+w[5]*x[4]
- def f1(x, w):
- return w[0]+w[1]*x[4] # beceauce its the 4' colum. only takes last year
- #vi får en vektor med nogle parametre, og af det regner vi
- # givet vi har 'højre siden af i' hvor sandsynligt er det så, at vores w er rigtigt
- def log_p(w, X_train, t_train, sigma, mu_0, f):
- D = w.size - 1 #det er fordi w er én større end x
- sumValue = 0
- for i in range(np.shape(X_train)[0]): #fordi jeg kun er intresset i antallet af rækker
- sumValue += t_train[i]*np.log(f(X_train[i,:], w)) - log_factorial_scalar(t_train[i])
- sumValue -= ((1/(2*sigma) * np.dot(w-mu_0, w-mu_0)) + np.log((np.sqrt(2*np.pi*sigma))**(D+1)))
- return sumValue
- w_tau = np.ones(6) #tau fordi det er den tidligere jævnfør side 37 L11
- mu_0 = np.ones(w_tau.size)
- sigma = 0.25
- cov_matrix = sigma*np.eye(w_tau.size)
- N = 1000 #arbitræt
- n = 0 #arbitræt
- #vi sampler med t_tau som den næste middelværdi. Den starter med at være sat til np.ones
- w_sample = np.random.multivariate_normal(w_tau, cov_matrix)
- u = np.random.uniform(low=0, high=1, size=1)
- 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))
- while n < N:
- w_sample = np.random.multivariate_normal(w_tau, cov_matrix)
- u = np.random.uniform(low=0, high=1, size=1)
- 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))
- #note: the bigger the number in w_sample & log_A, the bigger the chance that w is correct
- if log_A > np.log(u):
- w_tau = w_sample
- n += 1
- #print('for e5 (4)')
- W_manySampels = np.zeros([10,2])
- for i in range(10):
- print(i)
- w_tau = np.ones(2) #tau fordi det er den tidligere jævnfør side 37 L11
- mu_0 = np.ones(w_tau.size)
- sigma = 0.25
- cov_matrix = sigma*np.eye(w_tau.size)
- N = 1000 #arbitræt
- n = 0 #arbitræt
- #vi sampler med t_tau som den næste middelværdi. Den starter med at være sat til np.ones
- while n < N:
- w_sample = np.random.multivariate_normal(w_tau, cov_matrix)
- u = np.random.uniform(low=0, high=1, size=1)
- 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))
- #note: the bigger the number in w_sample & log_A, the bigger the chance that w is correct
- if log_A > np.log(u):
- w_tau = w_sample
- n += 1
- W_manySampels[i,:] = w_tau
- plt.figure()
- plt.plot(W_manySampels[:,0], W_manySampels[:,1], 'x')
- plt.title("Model 1")
- plt.xlabel('w0')
- plt.ylabel('w1')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement