Skip to content

Instantly share code, notes, and snippets.

@chipkent
Created July 19, 2016 03:48
Show Gist options
  • Save chipkent/bdf96e592b26297eb0b37684c4fb70d6 to your computer and use it in GitHub Desktop.
Save chipkent/bdf96e592b26297eb0b37684c4fb70d6 to your computer and use it in GitHub Desktop.
Covariance Bug reproducer
using NamedArrays
function compute_covariance{T <: AbstractFloat}( M::AbstractArray{T,2}, I::AbstractArray{T,2})
return make_positive_definite( (M'*M) ./ (I'*I) )
end
make_positive_definite{T <: AbstractFloat}( m::AbstractArray{T,2}, epsilon::Real=1e-8 ) = nearPSD(m,epsilon)
nearPSD{T <: AbstractFloat}( A::NamedArrays.NamedArray{T,2}, epsilon::Real=1e-12 ) =
NamedArrays.NamedArray( nearPSD(A.array), (names(A,1), names(A,2)), A.dimnames )
function nearPSD{T <: AbstractFloat}( A::Array{T,2}, epsilon::Real=1e-12 )
n = size(A,1)
@assert size(A,1) == size(A,2)
A = 0.5(A+A')
λ, S = eig(A)
λ = max(λ,epsilon)
r = S * diagm(λ) * S'
return 0.5(r+r')
end
open("/tmp/r.ser") do f
global r = deserialize(f)
end
open("/tmp/I.ser") do f
global I = deserialize(f)
end
result = compute_covariance(r',I')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment