Created
November 19, 2014 02:25
-
-
Save neubig/e859ef0cc1a63d1c2ea4 to your computer and use it in GitHub Desktop.
A simple program to sample functions from a Gaussian process and plot them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from math import exp | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def rbf_kernel(x1, x2, variance = 1): | |
return exp(-1 * ((x1-x2) ** 2) / (2*variance)) | |
def gram_matrix(xs): | |
return [[rbf_kernel(x1,x2) for x2 in xs] for x1 in xs] | |
xs = np.arange(-1, 1, 0.01) | |
mean = [0 for x in xs] | |
gram = gram_matrix(xs) | |
plt_vals = [] | |
for i in range(0, 5): | |
ys = np.random.multivariate_normal(mean, gram) | |
plt_vals.extend([xs, ys, "k"]) | |
plt.plot(*plt_vals) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment