Skip to content

Instantly share code, notes, and snippets.

@g-leech

g-leech/fixed_rbf.py

Created Oct 21, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.