【发布时间】:2021-06-10 11:26:33
【问题描述】:
假设我有一个带有 pdf 的二维高斯
我想画一个对应level-set(contour)的椭圆
按照here我知道我可以用它的特征分解替换精度矩阵以获得 伽马在哪里
然后要找到椭圆上点的坐标,我必须这样做
我尝试绘制此图,但它不起作用。
绘制轮廓
from scipy.stats import multivariate_normal
import numpy as np
from numpy.linalg import eigh
import math
import matplotlib.pyplot as plt
# Target distribution
sx2 = 1.0
sy2 = 2.0
rho = 0.6
Sigma = np.array([[sx2, rho*math.sqrt(sx2)*math.sqrt(sy2)], [rho*math.sqrt(sx2)*math.sqrt(sy2), sy2]])
target = multivariate_normal(mean=np.zeros(2), cov=Sigma)
# Two different contours
xy = target.rvs()
xy2 = target.rvs()
# Values where to plot the density
x, y = np.mgrid[-2:2:0.1, -2:2:0.1]
zz = target.pdf(np.dstack((x, y)))
fig, ax = plt.subplots()
ax.contour(x,y, zz, levels=np.sort([target.pdf(xy), target.pdf(xy2)]))
ax.set_aspect("equal")
plt.show()
绘制椭圆
# Find gamma and perform eigendecomposition
gamma = math.log(1 / (4*(np.pi**2)*sx2*sy2*(1 - rho**2)*(target.pdf(xy)**2)))
eigenvalues, P = eigh(np.linalg.inv(Sigma))
# Compute u and v as per link using thetas from 0 to 2pi
thetas = np.linspace(0, 2*np.pi, 100)
uv = (gamma / np.sqrt(eigenvalues)) * np.hstack((np.cos(thetas).reshape(-1,1), np.sin(thetas).reshape(-1, 1)))
# Plot
plt.scatter(uv[:, 0], uv[:, 1])
但是这显然行不通。
【问题讨论】:
-
要绘制 z 轮廓,调用轮廓绘图函数并告诉它您想要
(x - mu)^T . Sigma^(- 1) . (x - mu)的轮廓(即伽玛)。为此,您可以使用 Sigma,无需计算特征值/特征向量分解。
标签: python statistics gaussian normal-distribution