为了计算多元法线的CDF,我遵循this示例(对于单变量情况)但不能解释scipy产生的输出:
from scipy.stats import norm
import numpy as np
mean = np.array([1,5])
covariance = np.matrix([[1, 0.3 ],[0.3, 1]])
distribution = norm(loc=mean,scale = covariance)
print distribution.cdf(np.array([2,4]))
产生的输出是:
[[ 8.41344746e-01 4.29060333e-04]
[ 9.99570940e-01 1.58655254e-01]]
如果联合CDF定义为:
P (X1 ≤ x1, . . . ,Xn ≤ xn)
然后预期输出应该是介于0和1之间的实数。
答案 0 :(得分:19)
经过大量搜索,我认为Noah H. Silbert撰写的this博客条目描述了标准库中唯一可用于计算Python中多变量法线的cdf的现成代码。 Scipy有办法做到这一点,但正如博客中所提到的,很难找到。该方法基于Alan Genz的论文。
从博客中,这是它的工作原理。
from scipy.stats import mvn
import numpy as np
low = np.array([-10, -10])
upp = np.array([.1, -.2])
mu = np.array([-.3, .17])
S = np.array([[1.2,.35],[.35,2.1]])
p,i = mvn.mvnun(low,upp,mu,S)
print p
0.2881578675080012
答案 1 :(得分:4)
The scipy multivariate_normal
from v1.1.0 has a cdf function built in now:
from scipy.stats import multivariate_normal as mvn
import numpy as np
mean = np.array([1,5])
covariance = np.array([[1, 0.3],[0.3, 1]])
dist = mvn(mean=mean, cov=covariance)
print("CDF:", dist.cdf(np.array([2,4])))
CDF: 0.14833820905742245
Documentation for v1.2.0 can be found here.
答案 2 :(得分:0)
如果您不关心性能(即仅偶尔执行),那么您可以使用multivariate_normal
创建多变量普通pdf,然后按integrate.nquad