# The programs in Chapter 3 assume that the following are executed
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("seaborn-ticks")
def Hermite(j):
if j == 0:
return [1]
a = [0] * (j + 2)
b = [0] * (j + 2)
a[0] = 1
for i in range(1, j + 1):
b[0] = -a[1]
for k in range(i + 1):
b[k] = 2 * a[k - 1] - (k + 1) * a[k + 1]
for h in range(j + 2):
a[h] = b[h]
return b[:(j+1)]
Hermite(1) # Hermite polynomial of the first degree
Hermite(2) # Hermite polynomial of the second degree
Hermite(3) # Hermite polynomial of the third degree
def H(j, x):
coef = Hermite(j)
S = 0
for i in range(j + 1):
S = S + np.array(coef[i]) * (x ** i)
return S
cc = np.sqrt(5) / 4
a = 1/4
def phi(j, x):
return np.exp(-(cc - a) * x**2) * H(j, np.sqrt(2 * cc) * x)
color = ["b", "g", "r", "k"]
p = [[] for _ in range(4)]
x = np.linspace(-2, 2, 100)
for i in range(4):
for k in x:
p[i].append(phi(i, k))
plt.plot(x, p[i], c=color[i], label="j = %d" % i)
plt.ylim(-2, 8)
plt.ylabel("phi")
plt.title("Characteristic function of Gauss Kernel")
# Definition of Kernel
sigma = 1
def k(x, y):
return np.exp(-(x - y)**2 / sigma**2)
# Sample generation and gram matrix setup
m = 300
x = np.random.randn(m) - 2 * np.random.randn(m)**2 + 3 * np.random.randn(m)**3
# Eigenvalues and eigenvectors
K = np.zeros((m, m))
for i in range(m):
for j in range(m):
K[i, j] = k(x[i], x[j])
values, vectors = np.linalg.eig(K)
lam = values / m
alpha = np.zeros((m, m))
for i in range(m):
alpha[:, i] = vectors[i, :] * np.sqrt(m) / (values[i] + 10e-16)
# Display graph
def F(y, i):
S = 0
for j in range(m):
S = S + alpha[j, i] * k(x[j], y)
return S
i = 1 # Run with different i's
def G(y):
return F(y, i)
w = np.linspace(-2, 2, 100)
plt.plot(w, G(w))
plt.title("Eigen Values and their Eigen Functions")