MvNormal Error with Symmetric & Positive Semi-Definite Matrix

丶灬走出姿态 提交于 2019-12-13 01:43:32

问题


The summary of my problem is that I am trying to replicate the Matlab function:

mvnrnd(mu', sigma, 200)

into Julia using:

rand( MvNormal(mu, sigma), 200)'

and the result is a 200 x 7 matrix, essentially generating 200 random return time series data.

Matlab works, Julia doesn't.

My input matrices are:

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]

Using Distributions.jl, running the line:

MvNormal(sigma)

Produces the error:

ERROR: LoadError: Base.LinAlg.PosDefException(4)

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 produces the same results for these tests however Matlab is able to generate the 200x7 random return sample matrix.

Could someone advise as to what I could do to get it working in Julia? Or where the issue lies?

Thanks.


回答1:


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 

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.

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.



来源:https://stackoverflow.com/questions/35600815/mvnormal-error-with-symmetric-positive-semi-definite-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!