Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend CovarianceEstimation.cov #120

Closed
glennmoy opened this issue Jul 28, 2020 · 0 comments · Fixed by #174
Closed

Extend CovarianceEstimation.cov #120

glennmoy opened this issue Jul 28, 2020 · 0 comments · Fixed by #174

Comments

@glennmoy
Copy link
Member

glennmoy commented Jul 28, 2020

The current covariance (and correlation) methods overload Statistics.cov (Statistics.cor).

When using CovarianceEstimation.jl another cov method is exported that takes a CovarianceEstimator type.

The issue is that as of 0.2.4, cov returns a Symmetric matrix which incorrectly wraps the NamedDimsArray if it gets given one.

julia> using CovarianceEstimation, LinearAlgebra, NamedDims

julia> x = NamedDimsArray{(:a, :b)}(rand(5, 5));

julia> estimator = LinearShrinkage(DiagonalCommonVariance(); corrected=true)

julia> cov(estimator, x)
5×5 Symmetric{Float64,NamedDimsArray{(:b, :b),Float64,2,Array{Float64,2}}}:
  0.0668947  -0.0         0.0         0.0        -0.0
 -0.0         0.0668947  -0.0         0.0         0.0
  0.0        -0.0         0.0668947  -0.0         0.0
  0.0         0.0        -0.0         0.0668947  -0.0
 -0.0         0.0         0.0        -0.0         0.0668947

We need to extend the function properly with:

function CovarianceEstimation.cov(
    estimator::LinearShrinkage, a::NamedDimsArray{L, T, 2};
    dims=1,
) where {L, T}
    numerical_dims = dim(a, dims)
    data = cov(estimator, parent(a); dims=numerical_dims)
    names = symmetric_names(L, numerical_dims)
    return NamedDimsArray{names}(data)
end

then we get

julia> cov(LinearShrinkage(DiagonalCommonVariance(); corrected=true), x)
5×5 NamedDimsArray{(:b, :b),Float64,2,Symmetric{Float64,Array{Float64,2}}}:
  0.0668947  -0.0         0.0         0.0        -0.0
 -0.0         0.0668947  -0.0         0.0         0.0
  0.0        -0.0         0.0668947  -0.0         0.0
  0.0         0.0        -0.0         0.0668947  -0.0
 -0.0         0.0         0.0        -0.0         0.0668947
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant