GM

import numpy as np
from matplotlib.pyplot import figure, show, plot, scatter, gca


def k(xs, ys, sigma=1, l=1):
    dx = np.expand_dims(xs, 1) - np.expand_dims(ys, 0)
    return (sigma ** 2) * np.exp(-((dx / l) ** 2) / 2)


def m(x):
    """The mean function. As discussed, we can let the mean always be zero."""
    return np.zeros_like(x)


def f(x):
    # coefs[i] is the coefficient of x^i
    coefs = [6, -2.5, -2.4, -0.1, 0.2, 0.03]

    total = 0

    for i, coef in enumerate(coefs):
        total += coef * (x ** i)
    return total


x_obs = np.array([-4, -1.5, 0, 1.5, 2.5, 2.7])
y_obs = f(x_obs)

x_s = np.linspace(-8, 7, 80)

K = k(x_obs, x_obs)
K_s = k(x_obs, x_s)
K_ss = k(x_s, x_s)

K_sTKinv = np.matmul(K_s.T, np.linalg.pinv(K))

mu_s = m(x_s) + np.matmul(K_sTKinv, y_obs - m(x_obs))
Sigma_s = K_ss - np.matmul(K_sTKinv, K_s)

p = figure(figsize=(8, 6))
axes = gca()
axes.set_ylim([-7, 8])

# real line
y_true = f(x_s)
plot(x_s, y_true, linewidth=3, color='black', linestyle="-", alpha=0.4)

scatter(x_obs, y_obs, color='red')

stds = np.sqrt(Sigma_s.diagonal())
err_xs = np.concatenate((x_s, np.flip(x_s, 0)))
err_ys = np.concatenate((mu_s + 2 * stds, np.flip(mu_s - 2 * stds, 0)))
# patch(err_xs, err_ys, alpha=0.2, line_width=0, color='grey')

for color in ['yellow', 'green']:
    y_s = np.random.multivariate_normal(mu_s, Sigma_s)
    plot(x_s, y_s, linewidth=1, color=color)

plot(x_s, mu_s, linewidth=3, color='blue', alpha=0.4)
show(p)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值