()
| 194 | |
| 195 | |
| 196 | def plot_gp_dist(): |
| 197 | np.random.seed(12345) |
| 198 | sns.set_context("paper", font_scale=0.95) |
| 199 | |
| 200 | X_test = np.linspace(-10, 10, 100) |
| 201 | X_train = np.array([-3, 0, 7, 1, -9]) |
| 202 | y_train = np.sin(X_train) |
| 203 | |
| 204 | fig, axes = plt.subplots(1, 3) |
| 205 | G = GPRegression(kernel="RBFKernel", alpha=0) |
| 206 | G.fit(X_train, y_train) |
| 207 | |
| 208 | y_pred_prior = G.sample(X_test, 3, "prior") |
| 209 | y_pred_posterior = G.sample(X_test, 3, "posterior_predictive") |
| 210 | |
| 211 | for prior_sample in y_pred_prior: |
| 212 | axes[0].plot(X_test, prior_sample.ravel(), lw=1) |
| 213 | axes[0].set_title("Prior samples") |
| 214 | axes[0].set_xticks([]) |
| 215 | axes[0].set_yticks([]) |
| 216 | |
| 217 | for post_sample in y_pred_posterior: |
| 218 | axes[1].plot(X_test, post_sample.ravel(), lw=1) |
| 219 | axes[1].plot(X_train, y_train, "ko", ms=1.2) |
| 220 | axes[1].set_title("Posterior samples") |
| 221 | axes[1].set_xticks([]) |
| 222 | axes[1].set_yticks([]) |
| 223 | |
| 224 | y_pred, conf = G.predict(X_test) |
| 225 | |
| 226 | axes[2].plot(X_test, np.sin(X_test), lw=1, label="true function") |
| 227 | axes[2].plot(X_test, y_pred, lw=1, label="MAP estimate") |
| 228 | axes[2].fill_between(X_test, y_pred + conf, y_pred - conf, alpha=0.1) |
| 229 | axes[2].plot(X_train, y_train, "ko", ms=1.2, label="observed") |
| 230 | axes[2].legend(fontsize="x-small") |
| 231 | axes[2].set_title("Posterior mean") |
| 232 | axes[2].set_xticks([]) |
| 233 | axes[2].set_yticks([]) |
| 234 | |
| 235 | fig.set_size_inches(6, 2) |
| 236 | plt.tight_layout() |
| 237 | plt.savefig("img/gp_dist.png", dpi=300) |
| 238 | plt.close("all") |
nothing calls this directly
no test coverage detected