Created
October 21, 2019 12:37
-
-
Save g-leech/026a6f8846a21a00f744cec4a0959745 to your computer and use it in GitHub Desktop.
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
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