def test_posterior_std(): np.random.seed(1) N, n = 10, 50 f = lambda x: np.sin(0.9 * x).flatten() X = np.random.uniform(-5, 5, size=(N, 1)) Xtest = np.linspace(-5, 5, n).reshape(-1, 1) y = f(X) gg = GP(X, y, SquaredExp) means, stds = gg.draw_posterior(Xtest) truth = np.array([ 0.04202604, 0.06074646, 0.06442741, 0.06198905, 0.05028453, 0.01173271, 0.04384755, 0.03729233, 0.0337959, 0.04233938, 0.05163432, 0.06106133, 0.06421379, 0.05957212, 0.05028201, 0.04232989, 0.04012184, 0.0419569, 0.04305928, 0.04062566, 0.03890225, 0.05161809, 0.07836714, 0.10348942, 0.11188181, 0.09352313, 0.05289924, 0.07014139, 0.15883308, 0.25247954, 0.32610151, 0.3625603, 0.35170763, 0.29152607, 0.18832523, 0.06334274, 0.11994324, 0.27386883, 0.42075257, 0.54795666, 0.64986834, 0.72557935, 0.77767936, 0.81080593, 0.83020954, 0.84065061, 0.84580095, 0.84812688, 0.84908808, 0.8494516 ]) assert (np.allclose(stds, truth, atol=1e-5))
def test_gp_posterior_mean(): np.random.seed(1) N, n = 10, 50 f = lambda x: np.sin(0.9 * x).flatten() X = np.random.uniform(-5, 5, size=(N, 1)) Xtest = np.linspace(-5, 5, n).reshape(-1, 1) y = f(X) gg = GP(X, y, SquaredExp) means, stds = gg.draw_posterior(Xtest) # Truth truth = [ 0.97406338, 0.93351725, 0.8504679, 0.73040922, 0.589338, 0.42420947, 0.24978696, 0.07058869, -0.11020691, -0.28851528, -0.45856577, -0.61317727, -0.74742986, -0.8562826, -0.93591749, -0.98394361, -0.99921925, -0.98145889, -0.93090595, -0.84831721, -0.73533425, -0.59509, -0.4327257, -0.25546679, -0.07205324, 0.10841341, 0.27787463, 0.43049268, 0.56317516, 0.67525843, 0.76748952, 0.84072345, 0.89486151, 0.92845102, 0.93909474, 0.92449526, 0.88371745, 0.81818343, 0.73203338, 0.6317308, 0.52505586, 0.4198154, 0.3226457, 0.23820076, 0.1688575, 0.11490075, 0.07503359, 0.04701673, 0.02826614, 0.01630293 ] assert (np.allclose(means, truth))