Python中的不确定性包:使用给定的协方差矩阵获取数据不确定性

问题描述

我相信我的问题很容易理解,但是我想说得很清楚,因此这篇文章的长度。

我在下面总结的初始情况与本帖子(https://stats.stackexchange.com/questions/50830/can-i-convert-a-covariance-matrix-into-uncertainties-for-variables)中介绍的情况类似,但特别关注python程序包的不确定性如何处理此类情况。

这里是情况:

  • 我提出了一些测量值的标称值所对应的数据点数组(标称值是指没有考虑任何不确定性的裸值)。

  • 每个数据点都带有不确定性,这也提供给我。而且,由于测量中的某些系统性,不同的数据点不是独立的而是相关的。因此,给出了一个带有非零非对角线元素的整体协方差矩阵。

我想做的是对我的数据进行计算,以使不确定性适当地传播。最终,我希望在控制台中以标称值+/-不确定性的形式显示值。 python包“不确定性”似乎是正确的处理方法,但是我不确定它为我的初始数据点提供的不确定性数字的含义。

我期望的是,我的初始数据点的不确定性与“原始”标准偏差相对应,即我的协方差矩阵的对角元素的平方根。这会忽略数据中的相关性,但是以名义值+/-不确定性的形式显示相关结果始终无法显示相关性,只要正确地采用了相关性,这就不会成为问题。考虑到进一步的计算。

但是,程序包显示另一个数字表示不确定性,我不知道它来自哪里。软件包文档几乎没有帮助。我想知道我是否可能滥用该包裹。

有人可以帮助我了解情况吗?非常感谢!!

这是一个最小的可重现示例:

import uncertainties
import numpy as np

# To settle ideas,here are two different covariance matrices with same diagonals 
# -> I expect them to lead to the same std deviations below,but this is not the case:

Cov_matrix1 = np.array([[0.00,0.0,0.0],[0.0,1,0],4]],np.float64)
Cov_matrix2 = np.array([[0.00,0.5,3],[0.5,0.2],[3,0.2,np.float64)

# here are some initial nominal values:

data_nominal = np.array([1,2,np.float64)

print(" The nominal values of data,whithout covariance matrix is ",data_nominal)

# I impose correlations in my data,using the above covariance matrices

correlated_data1 = np.asarray(uncertainties.correlated_values(data_nominal,Cov_matrix1))
correlated_data2 = np.asarray(uncertainties.correlated_values(data_nominal,Cov_matrix2))

# I print my data in the console,and see that data points have different uncertainties in both cases,# even though the two covariance matrices have the same diagonals ... What is happening ? 

print("\n First covariance matrix is ")
print(Cov_matrix1)
print("\n Data values are ",correlated_data1)

print("\n 2nd covariance matrix is ")
print(Cov_matrix2)
print("\n Data values are now ",correlated_data2)

解决方法

我认为问题在于协方差矩阵之一在某种意义上是“非法的”

Cov_matrix2 = np.array([[0.00,0.5,3],[0.5,1,0.2],[3,0.2,4]],np.float64)

不是正半定值,它具有一个负特征值。因此,在数学上使用它是不可行的,这是程序包所没有注意到的。实际上,该程序包使用了该非法矩阵而没有警告或错误消息,并且当然,所产生的输出不能被认为是有意义的,因此是意外行为。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...