In [1]:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.stats import norm
xx = np.linspace(-3, 3, 200)
norm_xx = norm.pdf(xx)
fig, ax = plt.subplots()
ax.plot(xx, norm_xx, "r")
ax.set_ylim(0, 0.5)
ax.set_ylabel(r"$\mathcal{N}\left(x|\mu,\sigma^2\right)$", fontsize="xx-large")
ax.set_yticks([])
ax.set_yticklabels([])
ax.set_xticks([0])
ax.set_xticklabels([r"$\mu$"], fontsize="xx-large")
ax.text(-.1, 0.25, "$2\sigma$", fontsize="xx-large")
ax.annotate("",
xy=(-1, 0.24), xycoords='data',
xytext=(1, 0.24), textcoords='data',
arrowprops=dict(arrowstyle="<->",
connectionstyle="arc3"),
)
plt.show()
In [2]:
xx = np.linspace(-0.9, 0.9, 100)
yy = 4 * xx - np.sin(xx * np.pi)
fig, ax = plt.subplots()
ax.plot(xx, yy, color="red")
ax.set_xlim(-1, 1)
ax.set_ylim(-4, 4)
ax.set_xticks([0])
ax.set_xticklabels([r'$x_0$'], fontsize="xx-large")
ax.set_yticks([0])
ax.set_yticklabels([r'$y(x_0, \mathbf{w})$'], fontsize="xx-large")
xx = np.linspace(-4, 4, 100)
yy = norm.pdf(xx, scale=0.5) / 5
ax.plot([-1, 0], [0, 0], "g--")
ax.plot([0, 0], [-4, 4], "k")
ax.plot(yy, xx)
ax.annotate("",
xy=(0.75, -0.5), xycoords='data',
xytext=(0.75, 0.5), textcoords='data',
arrowprops=dict(arrowstyle="<->",
connectionstyle="arc3"),
)
ax.text(0.77, -0.2, r'$2\sigma$', fontsize="xx-large")
ax.text(0.15, -1, r'$p(t|x_0,\mathbf{w}, \beta)$', fontsize="xx-large")
ax.text(0.5, 3, r'$y(x, \mathbf{w})$', fontsize="xx-large")
plt.show()
In [3]:
def phi(x, M):
return x[:,None] ** np.arange(M + 1)
N = 10
# 生成 0,1 之间等距的 N 个 数
x_tr = np.linspace(0, 1, N)
# 计算 t
t_tr = np.sin(2 * np.pi * x_tr) + 0.25 * np.random.randn(N)
# 加正则项的解
M = 9
alpha = 5e-3
beta = 11.1
lam = alpha / beta
phi_x_tr = phi(x_tr, M)
A_0 = phi_x_tr.T.dot(phi_x_tr) + lam * np.eye(M+1)
y_0 = t_tr.dot(phi_x_tr)
# 求解 Aw=y
coeff = np.linalg.solve(A_0, y_0)[::-1]
f = np.poly1d(coeff)
# 绘图
xx = np.linspace(0, 1, 500)
# Bayes估计的均值和标准差
S = np.linalg.inv(A_0 * beta)
m_xx = beta * phi(xx, M).dot(S).dot(y_0)
s_xx = np.sqrt(1 / beta + phi(xx, M).dot(S).dot(phi(xx, M).T).diagonal())
fig, ax = plt.subplots()
ax.plot(x_tr, t_tr, 'co')
ax.plot(xx, np.sin(2 * np.pi * xx), 'g')
ax.plot(xx, f(xx), 'r')
ax.fill_between(xx, m_xx-s_xx, m_xx+s_xx, color="pink")
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-1.5, 1.5)
ax.set_xticks([0, 1])
ax.set_yticks([-1, 0, 1])
ax.set_xlabel("$x$", fontsize="x-large")
ax.set_ylabel("$t$", fontsize="x-large")
plt.show()