Skip to content

Instantly share code, notes, and snippets.

@patricoferris
Last active October 2, 2019 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patricoferris/93dd9d1701458a0f656ac1d8f2da4dda to your computer and use it in GitHub Desktop.
Save patricoferris/93dd9d1701458a0f656ac1d8f2da4dda to your computer and use it in GitHub Desktop.
# First we calculate mu_s
alpha = 2.
K_xy = rbf_kernel(xs_train, xs_test, alpha)
K_yy = rbf_kernel(xs_test, xs_test, alpha)
K_xx = rbf_kernel(xs_train, xs_train, alpha)
np.random.seed(20)
mu_s = np.matmul(K_xy.T, np.matmul(np.linalg.pinv(K_xx), (ys_train)))
# Second we calculate sigma_s
sigma_s = K_yy - np.matmul(K_xy.T, np.matmul(np.linalg.pinv(K_xx), K_xy))
# Now we draw from the prior by using the method for creating normally distributed points
L = np.linalg.cholesky(sigma_s + 0.00001*np.eye(len(xs_test)))
f_post = mu_s + np.dot(L, np.random.normal(size=(len(xs_test), 1)))
f_post = np.linalg.norm(og_temps) * f_post
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(xs, og_temps, 'b.', markersize=10, label=u'True Points')
ax.plot(training_points, og_temps[training_points], 'r.', markersize=10, label=u'Observations')
ax.plot(xs_test, f_post)
ax.fill(np.concatenate([xs_test, xs_test[::-1]]),
np.concatenate([f_post - np.linalg.norm(og_temps) * 1.9600 * sigma_s,
(f_post + np.linalg.norm(og_temps) * 1.9600 * sigma_s)[::-1]]),
alpha=.5, fc='0.8', ec='None', label='95% confidence interval')
ax.set_title('Predicting New York Temperatures')
plt.show()
fig.savefig("nyc_temp_predictions")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment