具有对称&的MvNormal错误正半定矩阵 [英] MvNormal Error with Symmetric & Positive Semi-Definite Matrix
问题描述
我的问题的摘要是我正在尝试复制Matlab函数:
The summary of my problem is that I am trying to replicate the Matlab function:
mvnrnd(mu', sigma, 200)
使用以下方法进入Julia:
into Julia using:
rand( MvNormal(mu, sigma), 200)'
,结果是一个200 x 7的矩阵,本质上生成了200个随机返回时间序列数据.
and the result is a 200 x 7 matrix, essentially generating 200 random return time series data.
Matlab可以工作,Julia不能.
Matlab works, Julia doesn't.
我的输入矩阵是:
mu = [0.15; 0.03; 0.06; 0.04; 0.1; 0.02; 0.12]
sigma = [0.0035 -0.0038 0.0020 0.0017 -0.0006 -0.0028 0.0009;
-0.0038 0.0046 -0.0011 0.0001 0.0003 0.0054 -0.0024;
0.0020 -0.0011 0.0041 0.0068 -0.0004 0.0047 -0.0036;
0.0017 0.0001 0.0068 0.0125 0.0002 0.0109 -0.0078;
-0.0006 0.0003 -0.0004 0.0002 0.0025 -0.0004 -0.0007;
-0.0028 0.0054 0.0047 0.0109 -0.0004 0.0159 -0.0093;
0.0009 -0.0024 -0.0036 -0.0078 -0.0007 -0.0093 0.0061]
使用Distributions.jl,运行以下行:
Using Distributions.jl, running the line:
MvNormal(sigma)
产生错误:
ERROR: LoadError: Base.LinAlg.PosDefException(4)
矩阵sigma是对称的,但仅是正半定的:
The matrix sigma is symmetrical but only positive semi-definite:
issym(sigma) #symmetrical
> true
isposdef(sigma) #positive definite
> false
using LinearOperators
check_positive_definite(sigma) #check for positive (semi-)definite
> true
对于这些测试,Matlab产生的结果相同,但是Matlab能够生成200x7的随机返回样本矩阵.
Matlab produces the same results for these tests however Matlab is able to generate the 200x7 random return sample matrix.
有人可以建议我做些什么来使其在Julia中工作吗?还是问题出在哪里?
Could someone advise as to what I could do to get it working in Julia? Or where the issue lies?
谢谢.
推荐答案
问题是协方差矩阵是不确定的.见
The issue is that the covariance matrix is indefinite. See
julia> eigvals(sigma)
7-element Array{Float64,1}:
-3.52259e-5
-2.42008e-5
2.35508e-7
7.08269e-5
0.00290538
0.0118957
0.0343873
所以它不是协方差矩阵.这可能是由于四舍五入而发生的,因此,如果您可以访问未四舍五入的数据,则可以尝试使用该方法.我刚刚尝试过,但在Matlab中也遇到了错误.但是,与Julia相比,Matlab确实允许矩阵为正的 semi 定的.
so it is not a covariance matrix. This might have happened because of rounding so if you have access to unrounded data you can try that instead. I just tried and I also got an error in Matlab. However, in contrast to Julia, Matlab does allow the matrix to be positive semidefinite.
进行此工作的一种方法是将对角矩阵添加到原始矩阵,然后将其输入到MvNormal
.即
A way to make this work is to add a diagonal matrix to the original matrix and then input that to MvNormal
. I.e.
julia> MvNormal(randn(7), sigma - minimum(eigvals(Symmetric(sigma)))*I)
Distributions.MvNormal{PDMats.PDMat{Float64,Array{Float64,2}},Array{Float64,1}}(
dim: 7
μ: [0.889004,-0.768551,1.78569,0.130445,0.589029,0.529418,-0.258474]
Σ: 7x7 Array{Float64,2}:
0.00353523 -0.0038 0.002 0.0017 -0.0006 -0.0028 0.0009
-0.0038 0.00463523 -0.0011 0.0001 0.0003 0.0054 -0.0024
0.002 -0.0011 0.00413523 0.0068 -0.0004 0.0047 -0.0036
0.0017 0.0001 0.0068 0.0125352 0.0002 0.0109 -0.0078
-0.0006 0.0003 -0.0004 0.0002 0.00253523 -0.0004 -0.0007
-0.0028 0.0054 0.0047 0.0109 -0.0004 0.0159352 -0.0093
0.0009 -0.0024 -0.0036 -0.0078 -0.0007 -0.0093 0.00613523
)
协方差"矩阵当然不再相同,但是非常接近.
The "covariance" matrix is of course not the same anymore, but it is very close.
这篇关于具有对称&的MvNormal错误正半定矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!