Example #1
0
 def full(self, X, Xs=None):
     X, Xc, Xs = self._common(X, Xs)
     if Xs is None:
         return at.dot(Xc, at.transpose(Xc))
     else:
         Xsc = at.sub(Xs, self.c)
         return at.dot(Xc, at.transpose(Xsc))
Example #2
0
 def _build_conditional(self, Xnew, pred_noise, diag, X, Xu, y, sigma,
                        cov_total, mean_total):
     sigma2 = at.square(sigma)
     Kuu = cov_total(Xu)
     Kuf = cov_total(Xu, X)
     Luu = cholesky(stabilize(Kuu))
     A = solve_lower(Luu, Kuf)
     Qffd = at.sum(A * A, 0)
     if self.approx == "FITC":
         Kffd = cov_total(X, diag=True)
         Lamd = at.clip(Kffd - Qffd, 0.0, np.inf) + sigma2
     else:  # VFE or DTC
         Lamd = at.ones_like(Qffd) * sigma2
     A_l = A / Lamd
     L_B = cholesky(at.eye(Xu.shape[0]) + at.dot(A_l, at.transpose(A)))
     r = y - mean_total(X)
     r_l = r / Lamd
     c = solve_lower(L_B, at.dot(A, r_l))
     Kus = self.cov_func(Xu, Xnew)
     As = solve_lower(Luu, Kus)
     mu = self.mean_func(Xnew) + at.dot(at.transpose(As),
                                        solve_upper(at.transpose(L_B), c))
     C = solve_lower(L_B, As)
     if diag:
         Kss = self.cov_func(Xnew, diag=True)
         var = Kss - at.sum(at.square(As), 0) + at.sum(at.square(C), 0)
         if pred_noise:
             var += sigma2
         return mu, var
     else:
         cov = self.cov_func(Xnew) - at.dot(at.transpose(As), As) + at.dot(
             at.transpose(C), C)
         if pred_noise:
             cov += sigma2 * at.identity_like(cov)
         return mu, cov if pred_noise else stabilize(cov)
Example #3
0
 def _build_conditional(self, Xnew, X, f, cov_total, mean_total):
     Kxx = cov_total(X)
     Kxs = self.cov_func(X, Xnew)
     L = cholesky(stabilize(Kxx))
     A = solve_lower(L, Kxs)
     v = solve_lower(L, f - mean_total(X))
     mu = self.mean_func(Xnew) + at.dot(at.transpose(A), v)
     Kss = self.cov_func(Xnew)
     cov = Kss - at.dot(at.transpose(A), A)
     return mu, cov
Example #4
0
 def square_dist(self, X, Xs=None):
     X2 = aet.sum(aet.square(X), 1)
     if Xs is None:
         sqd = -2.0 * aet.dot(X, aet.transpose(X)) + (
             aet.reshape(X2, (-1, 1)) + aet.reshape(X2, (1, -1)))
     else:
         Xs2 = aet.sum(aet.square(Xs), 1)
         sqd = -2.0 * aet.dot(X, aet.transpose(Xs)) + (
             aet.reshape(X2, (-1, 1)) + aet.reshape(Xs2, (1, -1)))
     return aet.clip(sqd, 0.0, np.inf)
Example #5
0
 def _build_conditional(self, Xnew, X, f):
     Kxx = self.cov_func(X)
     Kxs = self.cov_func(X, Xnew)
     Kss = self.cov_func(Xnew)
     L = cholesky(stabilize(Kxx))
     A = solve_lower(L, Kxs)
     cov = Kss - at.dot(at.transpose(A), A)
     v = solve_lower(L, f - self.mean_func(X))
     mu = self.mean_func(Xnew) + at.dot(at.transpose(A), v)
     beta = at.dot(v, v)
     nu2 = self.nu + X.shape[0]
     covT = (self.nu + beta - 2) / (nu2 - 2) * cov
     return nu2, mu, covT
Example #6
0
 def square_dist(self, X, Xs):
     X = at.mul(X, 1.0 / self.ls)
     X2 = at.sum(at.square(X), 1)
     if Xs is None:
         sqd = -2.0 * at.dot(X, at.transpose(X)) + (at.reshape(X2,
                                                               (-1, 1)) +
                                                    at.reshape(X2, (1, -1)))
     else:
         Xs = at.mul(Xs, 1.0 / self.ls)
         Xs2 = at.sum(at.square(Xs), 1)
         sqd = -2.0 * at.dot(X, at.transpose(Xs)) + (
             at.reshape(X2, (-1, 1)) + at.reshape(Xs2, (1, -1)))
     return at.clip(sqd, 0.0, np.inf)
Example #7
0
 def _build_conditional(self, Xnew):
     Xs, f = self.Xs, self.f
     X = cartesian(*Xs)
     delta = f - self.mean_func(X)
     covs = [stabilize(cov(Xi)) for cov, Xi in zip(self.cov_funcs, Xs)]
     chols = [cholesky(cov) for cov in covs]
     cholTs = [at.transpose(chol) for chol in chols]
     Kss = self.cov_func(Xnew)
     Kxs = self.cov_func(X, Xnew)
     Ksx = at.transpose(Kxs)
     alpha = kron_solve_lower(chols, delta)
     alpha = kron_solve_upper(cholTs, alpha)
     mu = at.dot(Ksx, alpha).ravel() + self.mean_func(Xnew)
     A = kron_solve_lower(chols, Kxs)
     cov = stabilize(Kss - at.dot(at.transpose(A), A))
     return mu, cov
Example #8
0
 def _build_marginal_likelihood_logp(self, y, X, Xu, sigma):
     sigma2 = at.square(sigma)
     Kuu = self.cov_func(Xu)
     Kuf = self.cov_func(Xu, X)
     Luu = cholesky(stabilize(Kuu))
     A = solve_lower(Luu, Kuf)
     Qffd = at.sum(A * A, 0)
     if self.approx == "FITC":
         Kffd = self.cov_func(X, diag=True)
         Lamd = at.clip(Kffd - Qffd, 0.0, np.inf) + sigma2
         trace = 0.0
     elif self.approx == "VFE":
         Lamd = at.ones_like(Qffd) * sigma2
         trace = (1.0 /
                  (2.0 * sigma2)) * (at.sum(self.cov_func(X, diag=True)) -
                                     at.sum(at.sum(A * A, 0)))
     else:  # DTC
         Lamd = at.ones_like(Qffd) * sigma2
         trace = 0.0
     A_l = A / Lamd
     L_B = cholesky(at.eye(Xu.shape[0]) + at.dot(A_l, at.transpose(A)))
     r = y - self.mean_func(X)
     r_l = r / Lamd
     c = solve_lower(L_B, at.dot(A, r_l))
     constant = 0.5 * X.shape[0] * at.log(2.0 * np.pi)
     logdet = 0.5 * at.sum(at.log(Lamd)) + at.sum(at.log(at.diag(L_B)))
     quadratic = 0.5 * (at.dot(r, r_l) - at.dot(c, c))
     return -1.0 * (constant + logdet + quadratic + trace)
Example #9
0
 def _build_conditional(self, Xnew, pred_noise, diag, X, y, noise, cov_total, mean_total):
     Kxx = cov_total(X)
     Kxs = self.cov_func(X, Xnew)
     Knx = noise(X)
     rxx = y - mean_total(X)
     L = cholesky(stabilize(Kxx) + Knx)
     A = solve_lower(L, Kxs)
     v = solve_lower(L, rxx)
     mu = self.mean_func(Xnew) + at.dot(at.transpose(A), v)
     if diag:
         Kss = self.cov_func(Xnew, diag=True)
         var = Kss - at.sum(at.square(A), 0)
         if pred_noise:
             var += noise(Xnew, diag=True)
         return mu, var
     else:
         Kss = self.cov_func(Xnew)
         cov = Kss - at.dot(at.transpose(A), A)
         if pred_noise:
             cov += noise(Xnew)
         return mu, cov if pred_noise else stabilize(cov)
Example #10
0
 def dist(self, X, Xs):
     if Xs is None:
         Xs = at.transpose(X)
     else:
         Xs = at.transpose(Xs)
     return at.abs_((X - Xs + self.c) % (self.c * 2) - self.c)