python - 为什么即使协方差是半正定的,bivariate_normal 也会返回 NaN?

标签 python matplotlib scipy statistics

我有以下正态分布点:

import numpy as np
from matplotlib import pylab as plt
from matplotlib import mlab

mean_test = np.array([0,0])
cov_test = array([[ 0.6744121 , -0.16938146],
                  [-0.16938146,  0.21243464]])

协方差矩阵是半正定的,可以作为协方差

# Semi-positive definite if all eigenvalues are 0 or 
# if there exists a Cholesky decomposition 
print np.linalg.eigvals(cov_test)
print np.linalg.cholesky(cov_test)

[0.72985988 0.15698686]

[[ 0.82122597 0.] [-0.20625439 0.41218172]]

如果我产生一些积分,我会得到:

 data_test = np.random.multivariate_normal(mean_test, cov_test, 1000)
 plt.scatter(data_test[:,0],data_test[:,1])

data

问题:

当我尝试绘制协方差等高线时,为什么 bivariate_normal 方法会失败(返回 NaN)?

x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = mlab.bivariate_normal(X, Y, 
                      cov_test[0,0], cov_test[1,1],
                      0, 0, cov_test[0,1])
print Z
plt.contour(X, Y, Z)

输出:

 [[ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]
 ..., 
 [ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]
 [ nan  nan  nan ...,  nan  nan  nan]]

 ValueError: zero-size array to reduction operation minimum which has no identity

最佳答案

协方差矩阵的对角线是方差,但是mlab.bivariate_normal 的参数sigmaxsigmay平方方差的根。改变这个:

Z = mlab.bivariate_normal(X, Y, 
                      cov_test[0,0], cov_test[1,1],
                      0, 0, cov_test[0,1])

为此:

Z = mlab.bivariate_normal(X, Y, 
                      np.sqrt(cov_test[0,0]), np.sqrt(cov_test[1,1]),
                      0, 0, cov_test[0,1])

关于python - 为什么即使协方差是半正定的,bivariate_normal 也会返回 NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27645510/

相关文章:

python - 如何使类 JSON 可序列化以在 Django session 中使用

python - Spark 最快的创建 numpy 数组 RDD 的方法

python - matplotlib.pyplot.plotfile() - 如何将多个图形绘制到同一个图形中?

python - AttributeError: 'module'对象没有属性 'spectrogram'

python - 内置函数 scipy.signal.savgol_filter 返回错误

python - python 中的 ctypes 有多快?

python - 将时间戳转换为 Pandas 中的自定义周期之一

python-3.x - 在 Cartopy 中隐藏高纬度非矩形投影的右侧轴(纬度)标签

python - 仅显示选定的或特定的 matplotlib 图形

Python Pandas 发现两个分布之间的统计差异