def responsibility(x, k, pi, mu, lam): K = len(pi) num = pi[k] * norm.pdf(x, mu[k], inv(lam[k])) den = 0 for j in range(K): den = den + pi[j] * norm.pdf(x, mu[j], inv(lam[j])) return num / den
def reward_func(self, action): R = np.eye(4) * 0.16 P_reduced = np.delete(self.P, 2, 0) P_reduced = np.delete(P_reduced, 2, 1) P_ = inv(P_reduced) S_ = inv(R) + P_ S = inv(S_) mu = np.append(self.x[:2], self.x[3:]) a = -0.5 * np.dot(mu.T.dot(P_ - np.dot(P_.dot(S), P_)), mu) return np.exp(a) * np.sqrt(np.linalg.det(S)/np.linalg.det(P_reduced)) - 1.
def reward_func(self, action): R = np.eye(4) * 1.25 P_reduced = np.delete(self.P, 2, 0) P_reduced = np.delete(P_reduced, 2, 1) #P_reduced = P_reduced + np.eye(4)*1e-4 P_ = inv(P_reduced) S_ = inv(R) + P_ S = inv(S_) mu = np.append(self.x[:2], self.x[3:]) a = -0.5 * np.dot(mu.T.dot(P_ - np.dot(P_.dot(S), P_)), mu) reward = np.exp(a) * np.sqrt(np.linalg.det(S)/np.linalg.det(P_reduced)) #- np.sum(mu[:2]**2) #reward -= 1. reward -= -0.1*action[0]**2 + 1*action[1]**2 + 1. return reward
def gaussian_init(mu_in, var_in): # single Gaussian mu, var = mu_in, var_in d = len(mu) if d == 1: def log_gaussian(x): log_p_const = -0.5 * np.log(2 * PI) - 0.5 * np.log(var) sub_mu = x - mu return log_p_const - 0.5 * sub_mu * sub_mu / var def generator(size): return npr.normal(mu, np.sqrt(var), size) else: var_det, var_inv = linalg.det(var), linalg.inv(var) log_p_const = -(d / 2.) * np.log(2 * PI) - 0.5 * np.log(var_det) def log_gaussian(x): sub_mu = x - mu #out = log_p_const - 0.5*np.sum(np.multiply(sub_mu,np.dot(var_inv,sub_mu.T).T ),1) out = log_p_const - 0.5 * np.sum( np.multiply(sub_mu, np.dot(sub_mu, var_inv.T)), 1) return out def generator(size): return npr.multivariate_normal(mu, var, size) return log_gaussian, generator
def plot_GMM(X, mu, lam, pi, centres, covs, K, title, savefigpath=False): plt.figure() plt.xlim(-5, 10) plt.ylim(-5, 15) plt.plot(X[:, 0], X[:, 1], 'kx', alpha=0.2) legend = ['Datapoints'] for k in range(K): plt.plot(mu[k][0], mu[k][1], 'ro') cov = inv(lam[k]) ell = draw_ellipse(mu[k], cov) ell.set_alpha(pi[k]) # ell.set_edgecolor('m') ell.set_fill(True) splot = plt.subplot(1, 1, 1) splot.add_artist(ell) # Plotting the ellipses for the GMM that generated the data for i in range(len(centres)): true_ell = draw_ellipse(centres[i], covs[i]) true_ell.set_edgecolor('g') true_ell.set_fill(False) splot.add_artist(true_ell) # legend.append('Data generation GMM 1, var1=%.2f, var2=%.2f, cov=%.2f' %(covs[0][0,0],covs[0][1,1],covs[0][1,0])) # legend.append('Data generation GMM 2, var1=%.2f, var2=%.2f, cov=%.2f' %(covs[1][0,0],covs[1][1,1],covs[1][1,0])) plt.title(title) if isinstance(savefigpath, str): plt.savefig(savefigpath) else: plt.legend(legend) plt.show()
def gamma_fisher(x1, x2, params={'alpha': 1., 'theta': 1.}): score1 = gamma_score(x1, params) score2 = gamma_score(x2, params) temp = 1. / params['theta'] fisher_info_mat = np.array([[polygamma(1, params['alpha']), temp], [temp, params['alpha'] / params['theta']**2]]) return np.dot(np.dot(score1, inv(fisher_info_mat)), score2.T)[0, 0]
def qa_posterior_moments(m, K_ff, y, noise): B = cholesky(K_ff + 1e-7 * np.eye(K_ff.shape[0])) Sigma = inv(np.dot(B.T, B) + noise * np.eye(B.shape[0])) / noise mu = np.dot(Sigma, np.dot(B.T, (y - m).T)) print(mu.shape, Sigma.shape, y.shape) return mu, Sigma
def fit(self, X, x, c=None, n=None, t=None, baseline='Fleming-Harrington', init=[]): x, c, n, t = surpyval.xcnt_handler(x, c, n, t, group_and_sort=False) if init == []: init = self.phi_init(X) else: init = np.array(init) if baseline == 'Breslow': fun = lambda params : self.neg_ll_cox(X, x, c, n, *params) else: self.baseline = nonparametric_dists[baseline].fit(x, c, n, t) fun = lambda params : self.neg_ll(X, x, c, n, *params) jac = jacobian(fun) hess = hessian(fun) with np.errstate(all='ignore'): res = minimize(fun, init, jac=jac) res = minimize(fun, res.x, jac=jac, method='TNC', tol=1e-20) params = res.x se = np.sqrt(np.diag(inv(hess(res.x)))) return {'params' : params, 'exp(param)' : np.exp(params),'se' : se}
def EKF(self, x, P, a, Y=None): Q = self.Q R = self.R Id = np.eye(5) x_ = self.dynamics(x, a) #x_ = np.dot(A(x), x) A = self.A(x, a) H = self.H(x_) P_ = np.dot(np.dot(A, P), A.T) + Q if not is_pos_def(P_): print("P_:", P_) print("P:", P) print("A:", A(x, a)) APA = np.dot(np.dot(A, P), A.T) print("APA:", APA) print("APA +:", is_pos_def(APA)) S = R + np.dot(np.dot(H, P_), H.T) K = np.dot(np.dot(P_, H.T), inv(S)) if Y is None: Y = self.obs(x_) x = x_ + np.dot( K, Y - self.obs(x_)) #x = x_ + np.dot(K, Y - np.dot(H, x_)) I_KH = (Id - np.dot(K, H)) P = np.dot(I_KH, P_) P = (P + P.T) / 2 # make symmetric to avoid computational overflows return x, P, K
def informative_priors(centres, covs, K): a = np.ones(2) * (10**0.5) # large alpha means pi values are ~= b = np.ones(2) * ( 1000**0.5) # large beta keeps Gaussian from which mu is drawn small V = [inv(cholesky(covs[k])) / (1000**0.5) for k in range(K)] m = centres u = np.ones(2) * (1000) - 2 return a, b, V, m, u
def gen_point_source_psf_image(pixel_grid, image, loc, psf_weights, psf_means, psf_covars): # use image PSF icovs = np.array([npla.inv(c) for c in psf_covars]) dets = np.array([npla.det(c) for c in psf_covars]) chols = np.array([npla.cholesky(c) for c in psf_covars]) return mog_like(pixel_grid, psf_means, icovs, dets, psf_weights)
def beta_fisher(x1, x2, params={'alpha': .5, 'beta': .5}): score1 = beta_score(x1, params) score2 = beta_score(x2, params) temp = polygamma(1, params['alpha'] + params['beta']) fisher_info_mat = np.array([[polygamma(1, params['alpha']) - temp, -temp], [-temp, polygamma(1, params['beta']) - temp]]) return np.dot(np.dot(score1, inv(fisher_info_mat)), score2.T)[0, 0]
def logPostFA(W, params): X = params['data'] N = X.shape[0] D, K = W.shape Q = np.dot(W, W.T) + np.eye(D) * .1 log_prior = D * K * params['a'] * np.log(params['b']) - D * K * np.log( gammaFn(params['a'])) + np.sum( (params['a'] - 1) * np.log(W)) - np.sum(params['b'] * W) return N / 2. * np.log(det(Q)) - .5 * trace(np.dot(np.dot(X.T, X), inv(Q))) + log_prior
def reparam_1d(w, var_ard, len_weight, s, t): ''' mean-GP mapping: w->z''' # assuming dim_w, dim_s = 1 s_ = s.reshape((1, len(s))) w_ = w.reshape((1, len(w))) s_sub_w = s_ - w_.T Kws = var_ard * np.exp(np.power((s_sub_w), 2) / -2. * len_weight) Kss = var_ard * np.exp(np.power((s_ - s_.T), 2) / -2. * len_weight) out = np.dot(np.dot(Kws, linalg.inv(Kss)), t) return out
def update_params(self, means, covs, pis): assert covs.shape[1] == covs.shape[2] == self.D assert self.K == covs.shape[0] == len(pis), "%d != %d != %d"%(self.K, covs.shape[0], len(pis)) #assert np.isclose(np.sum(pis), 1.) self.means = means self.covs = covs self.pis = pis self.dets = np.array([npla.det(c) for c in self.covs]) self.icovs = np.array([npla.inv(c) for c in self.covs]) self.chols = np.array([npla.cholesky(c) for c in self.covs])
def E_ln_p_mu_lam(beta, m, W, nu, m0, W0, nu0): sum1, sum2, sum3 = 0., 0., 0. for k in range(K): F = beta0 * E_ln_mu_k(k, beta, m, W, nu, m0) Eln_lam = E_ln_lam_k(k, nu, W) sum1 = sum1 + D * np.log(beta0 / (2 * np.pi)) + Eln_lam - F sum2 = sum2 + 0.5 * (nu0 - D - 1) * E_ln_lam_k(k, nu, W) sum3 = sum3 + nu[k] * np.trace(np.dot(inv(W0), W[k])) lnB = ln_B(W0, nu0) return 0.5 * sum1 + sum2 - 0.5 * sum3 + K * lnB
def gaussian_kl(w_mean, w_chol, prior_var): w_sigma = np.dot(w_chol.T, w_chol) w_sigma_inv = npalg.inv(w_sigma) log_det_pos = 2 * np.sum(np.log(np.diag(w_chol))) log_det_prior = num_weights * np.log(prior_var) trace_term = prior_var * np.sum(np.diag(w_sigma_inv)) # mean_term = np.dot(w_mean.T, np.dot(w_sigma_inv, w_mean)) mean_term = np.sum(w_sigma_inv * np.outer(w_mean, w_mean)) # print('%.3f, %.3f, %3.f, %.3f' % (log_det_pos, log_det_prior, trace_term, mean_term)) kl = 0.5 * (log_det_pos - log_det_prior - num_weights + trace_term + mean_term) return kl
def EKF(self, x, P, a, Y=None): Q = self.Q R = self.R A = self.A H = self.H x_ = self.dynamics(x, a) #x_ = np.dot(A(x), x) P_ = np.dot(np.dot(A(x,a), P), A(x,a).T) + Q S = R + np.dot(np.dot(H(x_), P_), H(x_).T) K = np.dot(np.dot(P_, H(x_).T), inv(S)) if Y is None: Y = self.obs(x_) x = x_ + np.dot(K, Y - self.obs(x_)) #x = x_ + np.dot(K, Y - np.dot(H, x_)) Id = np.eye(P.shape[0]) I_KH = (Id - np.dot(K, H(x_))) P = np.dot(I_KH, P_) return x, P, K
def plot_GMM_2(X, x, mu, lam, pi, centres, covs, K, title, cols=cols, savefigpath=False): plt.figure() plt.xlim(-8, 8) plt.ylim(-6, 10) plt.plot(X[:, 0], X[:, 1], 'kx', alpha=0.2) legend = ['Datapoints'] for k in range(K): cov = inv(lam[k]) ell = draw_ellipse_2(mu[k], cov) ell.set_alpha(0.5) ell.set_edgecolor('m') ell.set_fill(False) splot = plt.subplot(1, 1, 1) splot.add_artist(ell) # legend.append('pi=%.2f, var1=%.2f, var2=%.2f, cov=%.2f'%(pi[k],cov[0,0],cov[1,1],cov[1,0])) # for whatever reason lots of the ellipses are very long and narrow, why? # Plotting the ellipses for the GMM that generated the data for i in range(2): true_ell = draw_ellipse_2(centres[i], covs[i]) true_ell.set_edgecolor('g') true_ell.set_fill(False) splot.add_artist(true_ell) # legend.append('Data generation GMM 1, var1=%.2f, var2=%.2f, cov=%.2f' %(covs[0][0,0],covs[0][1,1],covs[0][1,0])) # legend.append('Data generation GMM 2, var1=%.2f, var2=%.2f, cov=%.2f' %(covs[1][0,0],covs[1][1,1],covs[1][1,0])) plt.title(title) plt.plot(x[0], x[1], 'ro') if isinstance(savefigpath, str): plt.savefig(savefigpath) else: plt.legend(legend) plt.show()
def plot_GMM(X, mu, lam, pi, centres, covs, K, title, savefigpath=False, xylims=[-5,10,-5,15], cols=cols): plt.figure() if xylims != None: plt.xlim(xylims[0],xylims[1]) plt.ylim(xylims[2],xylims[3]) plt.plot(X[:,0], X[:,1], 'kx', alpha=0.2) legend = ['Datapoints'] # label clusters if too many to have unique colours if K > len(cols): for k in range(K): plt.text(mu[k][0],mu[k][1], 'k=%d'%k) cols = 'r'*200 for k in range(K): if pi[k] > 1e-3: plt.plot(mu[k][0], mu[k][1], cols[k], marker='o', linestyle=None) else: plt.plot(mu[k][0], mu[k][1], cols[k], marker='X', linestyle=None) cov = inv(lam[k]) ell = draw_ellipse(mu[k], cov) ell.set_alpha(pi[k]) # ell.set_edgecolor('m') ell.set_fill(True) splot = plt.subplot(1, 1, 1) splot.add_artist(ell) # Plotting the ellipses for the GMM that generated the data if centres is not None and covs is not None: for i in range(len(centres)): true_ell = draw_ellipse(centres[i], covs[i]) true_ell.set_edgecolor('g') true_ell.set_fill(False) splot.add_artist(true_ell) plt.title(title) plt.legend(legend) if isinstance(savefigpath, str): plt.savefig(savefigpath) plt.close('all') else: plt.show()
def __init__(self, n=50, bounds=[-10, 10], rnd_seed=None, A=None, b=None, c=None, params=defaults, **kwargs): min_dists = { 10: 64, 100: 56, 1000: 48, } self.params = params self.rnd_seed = rnd_seed if rnd_seed else np.random.randint(0, 1e3) _A, _b, _c = self._generate_data(n) self.A = _A if A is None else A self.b = _b if b is None else b self.c = _c if c is None else c min_points = [np.asarray( -inv(self.A.T * self.A + self.c * np.identity(n)) * self.A.T @ self.b ).reshape(n, 1)] super().__init__(n, bounds, min_dists, params, min_points)
def logevidence(self, params): kern_params, wnoise, mean_params = self.unpack_params(params, fudge=self.fudge) Kxx = self.build_Kxx(self.xt, self.xt, params, prior=True) L = cholesky(Kxx) iL = inv(L) inv_Kxx = iL.T @ iL if self.mean: mu = self.mean(self.xt, params)[None] # D x T yc = (self.ykdt - mu).reshape([self.k, -1]) else: yc = self.ykt_ logdet = self.k * np.sum(np.log(np.diag(L))) * 2 ll = -1 / 2 * np.sum( (yc @ inv_Kxx) * yc) - 1 / 2 * logdet - self.k / 2 * np.log( 2 * np.pi) * self.t * self.d # lp = mvn.logpdf(yc, yc[0].squeeze()*0, Kxx).sum() # check marg-log-likelihood return ll.sum()
def myqr_vjp(g, ans, x): from autograd.numpy import matmul as m from autograd.numpy.linalg import inv gq = g[0] gr = g[1] q = ans[0] r = ans[1] rt = r.T rtinv = inv(rt) qt = q.T grt = gr.T gqt = gq.T mid = m(r,grt) - m(gr,rt)+ m(qt,gq)- m(gqt,q) n = mid.shape[0] indices = np.triu_indices(n, k = 0) tmp = np.ones([n,n]) tmp[indices] = 0 return m(q, gr + m(mid*tmp, rtinv)) + m((gq-m(q,m(qt,gq))),rtinv)
def gen_prof_mog_params(image, loc, gal_sig, gal_rho, gal_phi, psf_weights, psf_means, psf_covars, prof_amp, prof_sig): v_s = image.equa2pixel(loc) R = galaxies.gen_galaxy_transformation(gal_sig, gal_rho, gal_phi) W = np.dot(R, R.T) K_psf = psf_weights.shape[0] K_prof = prof_amp.shape[0] # compute MOG components num_components = K_psf * K_prof weights = np.zeros(num_components, dtype=np.float) means = np.zeros((num_components, 2), dtype=np.float) covars = np.zeros((num_components, 2, 2), dtype=np.float) cnt = 0 for k in range(K_psf): # num PSF Componenets for j in range(K_prof): # galaxy type components ## compute weights and component mean/variances weights[cnt] = psf_weights[k] * prof_amp[j] ## compute means means[cnt,0] = v_s[0] + psf_means[k, 0] means[cnt,1] = v_s[1] + psf_means[k, 1] ## compute covariance matrices for ii in range(2): for jj in range(2): covars[cnt, ii, jj] = psf_covars[k, ii, jj] + \ prof_sig[j] * W[ii, jj] # increment index cnt += 1 icovs = np.array([npla.inv(c) for c in covars]) dets = np.array([npla.det(c) for c in covars]) chols = np.array([npla.cholesky(c) for c in covars]) return means, covars, icovs, dets, chols, weights
def pred(self, xt, x1, ykdt, params): kern_params, wnoise, mean_params = self.unpack_params(params, fudge=self.fudge) k, d, t = ykdt.shape if self.mean: mu = self.mean(self.xt, params)[None] # D x T ### TO BE CHANGED yc = (ykdt - mu).reshape([self.k, -1]) else: yc = ykdt.reshape([k, -1]) KXX = self.build_Kxx(xt, xt, params, prior=True) # select points to condition on val_inds = np.argwhere(np.isnan(yc[0]) == False).squeeze() nval_inds = np.argwhere(np.isnan(yc[0]) == True).squeeze() KXX = KXX[:, val_inds] KXX = KXX[val_inds] yc = yc[:, val_inds] L = np.linalg.cholesky(KXX) iL = inv(L) Kinv = iL.T @ iL KXx = self.build_Kxx(xt, x1, params, prior=False) t0 = xt.shape[0] t1 = x1.shape[0] KXx = KXx.reshape([t0, d, t1, d]).reshape([t0 * d, -1]) noise = np.kron(np.diag(wnoise), np.eye(t0)) noise[nval_inds, nval_inds] = 0 KXx[:t0 * d, :t0 * d] += noise KXx = KXx.reshape([t0, d, t1, d]).reshape([d * t0, -1]) KXx = KXx[val_inds] Kxx = self.build_Kxx(x1, x1, params, prior=True) mu_pred = KXx.T.dot(Kinv).dot(yc.T).T cov_pred = Kxx - KXx.T.dot(Kinv).dot(KXx) mu_pred = mu_pred.reshape([k, d, -1]) return mu_pred, np.sqrt( np.diag(cov_pred).reshape([d, -1]) + self.fudge)
def plot_GMM(X, x, mu, lam, pi, centres, covs, K, title, cols=cols, savefigpath=False): plt.figure() plt.plot(X[:, 0], X[:, 1], 'kx', alpha=0.2) legend = ['Datapoints'] for k in range(K): cov = inv(lam[k]) x_ell, y_ell = draw_ellipse(mu[k], cov) plt.plot(x_ell, y_ell, cols[k], alpha=pi[k]) legend.append('pi=%.2f, var1=%.2f, var2=%.2f, cov=%.2f' % (pi[k], cov[0, 0], cov[1, 1], cov[1, 0])) # for whatever reason lots of the ellipses are very long and narrow, why? # Plotting the ellipses for the GMM that generated the data for i in range(2): x_true_ell, y_true_ell = draw_ellipse(centres[i], covs[i]) plt.plot(x_true_ell, y_true_ell, 'g--') legend.append('Data generation GMM 1, var1=%.2f, var2=%.2f, cov=%.2f' % (covs[0][0, 0], covs[0][1, 1], covs[0][1, 0])) legend.append('Data generation GMM 2, var1=%.2f, var2=%.2f, cov=%.2f' % (covs[1][0, 0], covs[1][1, 1], covs[1][1, 0])) plt.title(title) plt.plot(x[0], x[1], 'ro') if isinstance(savefigpath, str): plt.savefig(savefigpath) else: plt.legend(legend) plt.show()
def log_wishart(lam, W, nu, d=2): # default dimensionality = 2 lognum1 = ((nu - d - 1) / 2) * np.log(det(lam)) lognum2 = -np.trace(np.dot(inv(W), lam)) / 2 logden1 = (nu * d / 2) * np.log(2) + (nu / 2) * np.log(det(W)) return lognum1 + lognum2 - logden1 - multigammaln(nu / 2, d)
warnings.simplefilter('ignore') verbose = False ELBO, ELBO_M, ELBO_E = np.empty(2 * N_its), np.empty(N_its), np.empty(N_its) for i in tqdm(range(N_its)): # M step alpha, beta, m, W, nu, NK, xbar, S = M_step(r, X, alpha0, beta0, m0, W0, nu0) alphas[i, :] = alpha betas[i, :] = beta # m is shape K,D when cast to nparray ms[i, :, :] = np.array(m) # shape N_its*K*D varx[i, :] = np.array([inv(S[k])[0, 0] for k in range(K)]) vary[i, :] = np.array([inv(S[k])[1, 1] for k in range(K)]) covxy[i, :] = np.array([inv(S[k])[1, 0] for k in range(K)]) def E_pi(alpha, alpha0, N): return [(alpha[k]) / (K * alpha0 + N) for k in range(K)] Epi = E_pi(alpha, alpha0, N) ELBO[2 * i] = calculate_ELBO(r, alpha, beta, m, W, nu, NK, S, xbar, alpha0, m0, W0, nu0) ELBO_M[i] = ELBO[2 * i] if verbose: print('\n******************Iteration %d************************\n' % i) print('\nalpha', alpha, '\nbeta', beta, '\nnu', nu, '\nm', m, '\nW', W, '\nnu', nu)
def sample_mu(m, beta, lam): return multivariate_normal(m, inv(beta * lam))
def log_q_mu(mu, m, beta, lam): return norm.logpdf(mu, m, inv(beta * lam))
ELBO[2 * i] = calculate_ELBO(r, alpha, beta, m, W, nu, NK, S, xbar, alpha0, m0, W0, nu0) ELBO_M[i] = ELBO[2 * i] if verbose: print('\n******************Iteration %d************************\n' % i) print('\nalpha', alpha, '\nbeta', beta, '\nnu', nu, '\nm', m, '\nW', W, '\nnu', nu) print('E[pi] = ', Epi) print('ELBO = %f' % ELBO[i]) # Plot title = 'iteration %d' % i filename = 'plots/img%04d.png' % i # plot_GMM(X, mu, lam, pi, centres, covs, K, title) plot_GMM(X, m, inv(S), Epi, centres, covs, K, title, savefigpath=filename) # E step r = E_step(N, K, alpha, nu, W, beta, m, X) ELBO[2 * i + 1] = calculate_ELBO(r, alpha, beta, m, W, nu, NK, S, xbar, alpha0, m0, W0, nu0) ELBO_E[i] = ELBO[2 * i + 1] # Make and display gif filedir = 'plots' gifdir = 'gifs' gifname = make_gif(filedir, gifdir) # delete pngs for next run for file in os.listdir(filedir): os.remove(os.path.join(filedir, file))
def W_k(Nk, xkbar, Sk, m0, beta0, W0): inv_Wk = inv(W0) + Nk * Sk + ((beta0 * Nk) / (beta0 + Nk)) * np.dot( (xkbar - m0).T, (xkbar - m0)) return inv(inv_Wk)