Skip to content

Commit 4220596

Browse files
authored
Implement one-argument cov2cor (#816)
1 parent 77590ce commit 4220596

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/cov.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, dims::Int=1;
123123
return m, cov(x, wv, dims; corrected=depcheck(:mean_and_cov, :corrected, corrected))
124124
end
125125

126+
126127
"""
127-
cov2cor(C, s)
128+
cov2cor(C::AbstractMatrix, [s::AbstractArray])
128129
129-
Compute the correlation matrix from the covariance matrix `C` and a vector of standard
130-
deviations `s`. Use `StatsBase.cov2cor!` for an in-place version.
130+
Compute the correlation matrix from the covariance matrix `C` and, optionally, a vector
131+
of standard deviations `s`. Use `StatsBase.cov2cor!` for an in-place version.
131132
"""
132-
cov2cor(C::AbstractMatrix, s::AbstractArray) = cov2cor!(copy(C), s)
133+
cov2cor(C::AbstractMatrix, s::AbstractArray=sqrt.(view(C, diagind(C)))) = cov2cor!(copy(C), s)
133134

134135
"""
135136
cor2cov(C, s)

test/cov.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ weight_funcs = (weights, aweights, fweights, pweights)
122122
@testset "cov2cor" begin
123123
@test cov2cor(cov(X, dims = 1), std(X, dims = 1)) cor(X, dims = 1)
124124
@test cov2cor(cov(X, dims = 2), std(X, dims = 2)) cor(X, dims = 2)
125+
@test cov2cor(cov1) cor1
126+
@test cov2cor(cov2) cor2
125127
@test cov2cor(cov1, std1) cor1
126128
@test cov2cor(cov2, std2) cor2
127129
end

0 commit comments

Comments
 (0)