def mult(M1, M2): assert (M1.m == M2.n) c = M1.m M = [[Interval(0) for _ in range(M2.m)] for _ in range(M1.n)] for i in range(M1.n): for j in range(M2.m): for k in range(c): p = Interval.mult(M1.matrix[i][k], M2.matrix[k][j]) M[i][j] = Interval.add(M[i][j], p) return InterMatrix(M)
def ik(x, y): s = In(0.0) assert (len(x) == len(y)), "Not same length" n = len(x) for i in range(n): diff = In.add(x[i], In.neg(y[i])) sq = square.image(diff) s = In.add(s, sq) s = In.mult(In(-0.5), s) return exp.image(s)
def test(n=50, noise=0.001, x=3, y=4): X = [[np.random.rand() * 10, np.random.rand() * 10] for i in range(n)] Y = [f(a) for a in X] gp = InterGP(k, ik) gp.fit(X, Y) xx = [In(x), In(y)] x = [In.fromNoise(x, noise), In.fromNoise(y, noise)] a = gp.predict(xx) b = gp.predict(x) print("\nRESULTS:") print(a) print(b)
def predict(self, x): interX = [[In(yy) for yy in y] for y in self.X] K_star = self.generateMatrixCov([x], interX, True) print("K star", K_star) k_double_star = self.ik(x, x) print("K double star", k_double_star) interInvK = InterMatrix.createFromMatrix(self.inv_K) interInvKf = InterMatrix.createFromMatrix(self.inv_K_f) print("interInvKf", interInvKf) mean = InterMatrix.mult(K_star, interInvKf) K_star_T = K_star.transpose() prod = InterMatrix.mult(interInvK, K_star_T) prod = InterMatrix.mult(K_star, prod) prod = prod.neg() var = In.add(k_double_star, prod.matrix[0][0]) # var = k_double_star - K_star * self.inv_K * (K_star.T) return (mean, var)
def image(self, inter): xs = [inter.a, inter.b] xs += [x for x in self.extrema if inter.contains(x)] ys = [self.f(x) for x in xs] m = min(ys) M = max(ys) return Interval(m, M)
def add(M1, M2): assert (M1.n == M2.n) assert (M1.m == M2.m) M = [[0 for _ in range(M1.m)] for _ in range(M1.n)] for i in range(M1.n): for j in range(M1.m): M[i][j] = Interval.add(M1.matrix[i][j], M2.matrix[i][j]) return InterMatrix(M)
def createFromMatrix(M): n = np.shape(M)[0] m = np.shape(M)[1] iM = [[Interval(M[i, j]) for j in range(m)] for i in range(n)] return InterMatrix(iM)
def neg(self): N = [[Interval.neg(self.matrix[i][j]) for j in range(self.m)] for i in range(self.n)] return InterMatrix(N)
def zeros(n, m): M = [[Interval(0.0) for j in range(m)] for i in range(n)] return InterMatrix(M)