Skip to content

Commit

Permalink
Implement one-argument cov2cor (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Jul 26, 2022
1 parent 77590ce commit 4220596
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/cov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, dims::Int=1;
return m, cov(x, wv, dims; corrected=depcheck(:mean_and_cov, :corrected, corrected))
end


"""
cov2cor(C, s)
cov2cor(C::AbstractMatrix, [s::AbstractArray])
Compute the correlation matrix from the covariance matrix `C` and a vector of standard
deviations `s`. Use `StatsBase.cov2cor!` for an in-place version.
Compute the correlation matrix from the covariance matrix `C` and, optionally, a vector
of standard deviations `s`. Use `StatsBase.cov2cor!` for an in-place version.
"""
cov2cor(C::AbstractMatrix, s::AbstractArray) = cov2cor!(copy(C), s)
cov2cor(C::AbstractMatrix, s::AbstractArray=sqrt.(view(C, diagind(C)))) = cov2cor!(copy(C), s)

"""
cor2cov(C, s)
Expand Down
2 changes: 2 additions & 0 deletions test/cov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ weight_funcs = (weights, aweights, fweights, pweights)
@testset "cov2cor" begin
@test cov2cor(cov(X, dims = 1), std(X, dims = 1)) cor(X, dims = 1)
@test cov2cor(cov(X, dims = 2), std(X, dims = 2)) cor(X, dims = 2)
@test cov2cor(cov1) cor1
@test cov2cor(cov2) cor2
@test cov2cor(cov1, std1) cor1
@test cov2cor(cov2, std2) cor2
end
Expand Down

0 comments on commit 4220596

Please sign in to comment.