我如何使用 scipy.stats 中的 multivariate_normal.cdf 函数始终获得相同的结果?

How can I always get the same result using the multivariate_normal.cdf function from scipy.stats?

在这个例子中,我使用函数 multivariate_normal.cdf 两次,我得到了两个不同的结果。结果很接近但仍然不同。是否可以进行任何调整以在每次使用该功能时获得完全相同的结果?

import numpy as np
from scipy.stats import multivariate_normal


def normal_cdf_3d(d_1, d_2, d_3, correl_1_2, correl_1_3, correl_2_3):
    mean = np.array([0, 0, 0])
    cov = np.array(
        [
            [1, correl_1_2, correl_1_3],
            [correl_1_2, 1, correl_2_3],
            [correl_1_3, correl_2_3, 1],
        ]
    )
    x = np.array([d_1, d_2, d_3])
    y = multivariate_normal(mean=mean, cov=cov).cdf(x=x)
    return y


d_1_ = -0.2304886114323222
d_2_ = -0.1479019945774904
d_3_ = 0.525
correl_1_2_ = 0.5500190982169267
correl_1_3_ = 0.9219544457292886
correl_2_3_ = 0.5916079783099616

a = normal_cdf_3d(
    d_1=d_1_,
    d_2=d_2_,
    d_3=d_3_,
    correl_1_2=correl_1_2_,
    correl_1_3=correl_1_3_,
    correl_2_3=correl_2_3_,
)

b = normal_cdf_3d(
    d_1=d_1_,
    d_2=d_2_,
    d_3=d_3_,
    correl_1_2=correl_1_2_,
    correl_1_3=correl_1_3_,
    correl_2_3=correl_2_3_,
)

print(a)
print(b)

if a == b:
    print(True)
else:
    print(False)

# Results
# 0.2698170436763295 (a)
# 0.2698184075101584 (b)
# False

因为 CDF 必须通过多维 space 中的数值积分来计算,所以 multivariate_normal 对象具有相对较低的相对和绝对收敛公差。此外,要考虑积分的点数默认为 1000000 * ndim,在本例中为 3000000.

import numpy as np
from scipy.stats import multivariate_normal

d_1 = -0.2304886114323222
d_2 = -0.1479019945774904
d_3 = 0.525
correl_1_2 = 0.5500190982169267
correl_1_3 = 0.9219544457292886
correl_2_3 = 0.5916079783099616

mean = np.array([0, 0, 0])
cov = np.array(
    [
        [1, correl_1_2, correl_1_3],
        [correl_1_2, 1, correl_2_3],
        [correl_1_3, correl_2_3, 1],
    ]
)
x = np.array([d_1, d_2, d_3])
mvn = multivariate_normal(mean=mean, cov=cov)
print(mvn.abseps, mvn.releps, mvn.maxpts)
# prints
1e-05 1e-05 3000000

增加点数和减少公差将以牺牲性能为代价获得更好的精度。请记住,Python 中的默认浮点数精度为 1e-17。

mvn.cdf(x) - mvn.cdf(x)
# returns:
-9.741263303830738e-06    # Wall time: 1.27 ms

mvn.abseps = mvn.releps = 1e-8
mvn.maxpts = 5_000_000
mvn.cdf(x) - mvn.cdf(x)
# returns:
-3.6016106763625544e-10   # Wall time: 409 ms

mvn.abseps = mvn.releps = 1e-10
mvn.maxpts = 8_000_000
mvn.cdf(x) - mvn.cdf(x)
# returns:
-2.0803747613484802e-11   # Wall time: 2.22 s

mvn.abseps = mvn.releps = 1e-12
mvn.maxpts = 10_000_000
mvn.cdf(x) - mvn.cdf(x)
# returns:
-4.4660386500083861e-12   # Wall time: 3.39 s

这没有很好的时间缩放。如果可能的话,再尝试几次增加点数,直到我们得到相同的输出。

mvn.abseps = mvn.releps = 1e-17
mvn.maxpts = 35_000_000
mvn.cdf(x) - mvn.cdf(x)
# returns:
-1.755373624234835e-12    # Wall time: 11.1 s

mvn.abseps = mvn.releps = 1e-19
mvn.maxpts = 70_000_000
mvn.cdf(x) - mvn.cdf(x)
# returns:
2.7454705175955496e-12    # Wall time: 30 s

mvn.abseps = mvn.releps = 1e-25
mvn.maxpts = 100_000_000
mvn.cdf(x) - mvn.cdf(x)
# returns:
-2.1491142199181468e-12   # Wall time: 39.3 s

根据最近 3 次运行,计算的可用精度存在限制。您可以只使用在某些 epsilon 内作为相同的数字是可以接受的,即使它小于 Python 的精度。

def approx_equal(x, y, tol=1e-5):
    return abs(x-y) < tol