Skip to content

Instantly share code, notes, and snippets.

@g-leech
Created October 21, 2019 12:37
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 g-leech/026a6f8846a21a00f744cec4a0959745 to your computer and use it in GitHub Desktop.
Save g-leech/026a6f8846a21a00f744cec4a0959745 to your computer and use it in GitHub Desktop.
from scipy.spatial.distance import cdist
import numpy as np
import matplotlib.pyplot as plt
# First write a covariance function. e.g. rbf
def radial_basis_kernel(x1, x2, varSigma, lengthScale):
if x2 is None:
d = cdist(x1, x1)
else:
d = cdist(x1, x2)
s = -np.power(d, 2) / lengthScale
return varSigma * np.exp(s)
# Then sample GP prior
# first, functions with one-dimensional inputs, so the infinite process indexed by reals
# choose index set for the marginal
N = 200
x = np.linspace(-6, 6, N).reshape(-1, 1)
# compute covariance matrix
sigmaSq = 2.0
scale = 1.0
K = rbf_kernel(x, None, sigmaSq, scale)
# create mean vector
mu = np.zeros(x.shape[0])#.reshape(1, 200)
# draw samples 20 from Gaussian distribution
sampleN = 20
f = np.random.multivariate_normal(mu, K, sampleN)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, f.T)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment