# ノンパラメトリック・ベイズ実践編２　回帰

• まず、単純に線形回帰
```s <- rnorm(n,0,1)
m <- matrix(s,ncol=2)
lm_res <- lm(m[,2] ~ m[,1])
lm_res
plot(m)

abline(lm_res)
```
```x1 <- x2 <- seq(from=-2,to=2,length=100)

x12 <- expand.grid(x1,x2)

k <- function(p,l,x1,x2){
exp(-2 * (sin(pi / p * sqrt((x1-x2)^2) / l) ^ 2))
}
d <- function(x1,x2){
sqrt((x1-x2)^2)
}
krbf <- function(l,x1,x2){
exp(-1 / 2 *d(x1 / l, x2 / l)^2)
}
p <- 2
l <- 1

k.val <- rep(0,length(x12[,1]))
for(i in 1:length(k.val)){
k.val[i] <- krbf(l,x12[i,1],x12[i,2])
}

image(matrix(k.val,length(x1),length(x2)))

k(x_i, x_j) = exp(-1 / 2 d(x_i / length_scale, x_j / length_scale)^2)
```
• 参加メンバーの方のパイソンコード(ipynb)も貼らせていただきます
```import numpy as np
import matplotlib.pyplot as plt

x1 = np.linspace(-2,2,100)
x2 = np.linspace(-2,2,100)

x1_, x2_ = np.meshgrid(x1, x2)

def d(x,y):
return(np.sqrt((x-y)**2))

def sin_kernel(x,y,periodicity,length_scale):
return(np.exp(-2 * (np.sin(np.pi / periodicity * d(x, y)) / length_scale) ** 2))

out_sin = sin_kernel(x1_,x2_,1,1)
plt.pcolormesh(x1_, x2_, out_sin, cmap="spring")
plt.colorbar()

def rbf_kernel(x,y,l):
return(np.exp((-1/2) * d(x/l,y/l) **2 ))

out_rbf = rbf_kernel(x1_,x2_,1)

plt.pcolormesh(x1_,x2_,out_rbf,cmap="spring")
plt.colorbar()```