1

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.

kulsuri
  • 143
  • 8

1 Answers1

5

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.

amrods
  • 2,029
  • 1
  • 17
  • 28
Andreas Noack
  • 1,370
  • 6
  • 11
  • 1
    In case `sigma` happens to be positive semi-definite, no change is required. So, the "fixed" `sigma` could be expressed as `sigma+max(0,-minimum(eigvals(sigma)))*I` – Dan Getz Feb 24 '16 at 20:42
  • @AndreasNoack Thank you for your detailed explanation and answer, it was exactly what I was looking for. – kulsuri Feb 25 '16 at 09:11
  • @user3580870 My covariance matrices sigma are generated from user inputted data and are always either positive definite or positive semi-definite so that happens to be quite an effective solution. Thanks! – kulsuri Feb 25 '16 at 09:19