# PRML Figure6.5を再現するコード

これです。

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import multivariate_normal

params = [(1,4,0,0), (9,4,0,0), (1,64,0,0),
(1,0.25,0,0), (1,4,10,0), (1,4,0,5)]

fig = plt.figure()
for n in range(len(params)):
(p0, p1, p2, p3) = params[n]

linex = np.linspace(-1,1,999)
kern = np.zeros([len(linex),len(linex)])
for i, x0 in enumerate(linex):
for j, x1 in enumerate(linex):
kern[i][j] = p0*np.exp(-p1*0.5*(x0-x1)*(x0-x1)) + p2 + p3*x0*x1
#      kern[i][j] = np.exp(-p1*0.5*np.abs(x0-x1))

liney = multivariate_normal(np.zeros(len(linex)),kern,5)
subplot.set_title("(%1.2f,%1.2f,%1.2f,%1.2f)" % (p0,p1,p2,p3))
for c in range(5):
subplot.plot(linex, liney[c])

plt.show()


#### 説明

その上で、さまざまな に対してサンプルを取得したのが上記のグラフになります。

#### おまけ

まったく同様に、Figure6.4は次のコードで再現できます。

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import multivariate_normal

fig = plt.figure()

# Gaussian Kernel
linex = np.linspace(-1,1,999)
kern = np.zeros([len(linex),len(linex)])
for i, x0 in enumerate(linex):
for j, x1 in enumerate(linex):
kern[i][j] = np.exp(-0.5*(x0-x1)*(x0-x1))
liney = multivariate_normal(np.zeros(len(linex)),kern,5)
for c in range(5):
subplot.plot(linex, liney[c])

# Exponential Kernel
linex = np.linspace(-1,1,999)
kern = np.zeros([len(linex),len(linex)])
for i, x0 in enumerate(linex):
for j, x1 in enumerate(linex):
kern[i][j] = np.exp(-0.5*np.abs(x0-x1))

liney = multivariate_normal(np.zeros(len(linex)),kern,5)