@@ -6,6 +6,27 @@ struct EmptyCovarianceEstimator <: CovarianceEstimator end
66@testset " StatsBase.Covariance" begin
77weight_funcs = (weights, aweights, fweights, pweights)
88
9+ function test_isapprox_preserves_symherm_structure (f:: F , x:: AbstractMatrix , y:: AbstractMatrix , args... ) where F
10+ for wrapper in (identity, x -> Symmetric (x, :U ), x -> Symmetric (x, :L ), x -> Hermitian (x, :U ), x -> Hermitian (x, :L ))
11+ A = wrapper (copy (x))
12+ fA = @inferred (f (A, args... ))
13+ @test fA ≈ y
14+ if f === StatsBase. cov2cor! || f === StatsBase. cor2cov!
15+ @test fA === A
16+ if A isa Union{Symmetric,Hermitian}
17+ @test parent (fA) != fA # only active triangle is written to
18+ end
19+ else
20+ @test fA != = A
21+ if A isa Union{Symmetric,Hermitian}
22+ @test fA isa (A isa Symmetric ? Symmetric : Hermitian)
23+ @test fA. uplo == A. uplo
24+ @test parent (fA) != fA # only active triangle is written to
25+ end
26+ end
27+ end
28+ end
29+
930@testset " $f " for f in weight_funcs
1031 X = randn (3 , 8 )
1132
@@ -120,18 +141,32 @@ weight_funcs = (weights, aweights, fweights, pweights)
120141 cor2 = cor (X, wv2, 2 )
121142
122143 @testset " cov2cor" begin
123- @test cov2cor (cov (X, dims = 1 ), std (X, dims = 1 )) ≈ cor (X, dims = 1 )
124- @test cov2cor (cov (X, dims = 2 ), std (X, dims = 2 )) ≈ cor (X, dims = 2 )
125- @test cov2cor (cov1) ≈ cor1
126- @test cov2cor (cov2) ≈ cor2
127- @test cov2cor (cov1, std1) ≈ cor1
128- @test cov2cor (cov2, std2) ≈ cor2
144+ test_isapprox_preserves_symherm_structure (cov2cor, cov (X, dims = 1 ), cor (X, dims = 1 ), std (X, dims = 1 ))
145+ test_isapprox_preserves_symherm_structure (cov2cor, cov (X, dims = 2 ), cor (X, dims = 2 ), std (X, dims = 2 ))
146+ test_isapprox_preserves_symherm_structure (cov2cor, cov1, cor1)
147+ test_isapprox_preserves_symherm_structure (cov2cor, cov2, cor2)
148+ test_isapprox_preserves_symherm_structure (cov2cor, cov1, cor1, std1)
149+ test_isapprox_preserves_symherm_structure (cov2cor, cov2, cor2, std2)
150+ end
151+ @testset " StatsBase.cov2cor!" begin
152+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov (X, dims = 1 ), cor (X, dims = 1 ), std (X, dims = 1 ))
153+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov (X, dims = 2 ), cor (X, dims = 2 ), std (X, dims = 2 ))
154+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov1, cor1)
155+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov2, cor2)
156+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov1, cor1, std1)
157+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov2, cor2, std2)
129158 end
130159 @testset " cor2cov" begin
131- @test cor2cov (cor (X, dims = 1 ), std (X, dims = 1 )) ≈ cov (X, dims = 1 )
132- @test cor2cov (cor (X, dims = 2 ), std (X, dims = 2 )) ≈ cov (X, dims = 2 )
133- @test cor2cov (cor1, std1) ≈ cov1
134- @test cor2cov (cor2, std2) ≈ cov2
160+ test_isapprox_preserves_symherm_structure (cor2cov, cor (X, dims = 1 ), cov (X, dims = 1 ), std (X, dims = 1 ))
161+ test_isapprox_preserves_symherm_structure (cor2cov, cor (X, dims = 2 ), cov (X, dims = 2 ), std (X, dims = 2 ))
162+ test_isapprox_preserves_symherm_structure (cor2cov, cor1, cov1, std1)
163+ test_isapprox_preserves_symherm_structure (cor2cov, cor2, cov2, std2)
164+ end
165+ @testset " StatsBase.cor2cov!" begin
166+ test_isapprox_preserves_symherm_structure (StatsBase. cor2cov!, cor (X, dims = 1 ), cov (X, dims = 1 ), std (X, dims = 1 ))
167+ test_isapprox_preserves_symherm_structure (StatsBase. cor2cov!, cor (X, dims = 2 ), cov (X, dims = 2 ), std (X, dims = 2 ))
168+ test_isapprox_preserves_symherm_structure (StatsBase. cor2cov!, cor1, cov1, std1)
169+ test_isapprox_preserves_symherm_structure (StatsBase. cor2cov!, cor2, cov2, std2)
135170 end
136171 end
137172 end
@@ -198,41 +233,24 @@ weight_funcs = (weights, aweights, fweights, pweights)
198233 cor2 = cor (X, wv2, 2 )
199234
200235 @testset " cov2cor" begin
201- @test cov2cor ( cov (X, dims = 1 ), std (X, dims = 1 )) ≈ cor (X, dims = 1 )
202- @test cov2cor ( cov (X, dims = 2 ), std (X, dims = 2 )) ≈ cor (X, dims = 2 )
203- @test cov2cor ( cov1, std1) ≈ cor1
204- @test cov2cor ( cov2, std2) ≈ cor2
236+ test_isapprox_preserves_symherm_structure (cov2cor, cov1, cor1 )
237+ test_isapprox_preserves_symherm_structure (cov2cor, cov2, cor2 )
238+ test_isapprox_preserves_symherm_structure (cov2cor, cov1, cor1, std1)
239+ test_isapprox_preserves_symherm_structure (cov2cor, cov2, cor2, std2)
205240 end
206-
207- @testset " cov2cor!" begin
208- tmp_cov1 = copy (cov1)
209- @test ! (tmp_cov1 ≈ cor1)
210- StatsBase. cov2cor! (tmp_cov1, std1)
211- @test tmp_cov1 ≈ cor1
212-
213- tmp_cov2 = copy (cov2)
214- @test ! (tmp_cov2 ≈ cor2)
215- StatsBase. cov2cor! (tmp_cov2, std2)
216- @test tmp_cov2 ≈ cor2
241+ @testset " StatsBase.cov2cor!" begin
242+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov1, cor1)
243+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov2, cor2)
244+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov1, cor1, std1)
245+ test_isapprox_preserves_symherm_structure (StatsBase. cov2cor!, cov2, cor2, std2)
217246 end
218-
219247 @testset " cor2cov" begin
220- @test cor2cov (cor (X, dims = 1 ), std (X, dims = 1 )) ≈ cov (X, dims = 1 )
221- @test cor2cov (cor (X, dims = 2 ), std (X, dims = 2 )) ≈ cov (X, dims = 2 )
222- @test cor2cov (cor1, std1) ≈ cov1
223- @test cor2cov (cor2, std2) ≈ cov2
248+ test_isapprox_preserves_symherm_structure (cor2cov, cor1, cov1, std1)
249+ test_isapprox_preserves_symherm_structure (cor2cov, cor2, cov2, std2)
224250 end
225-
226- @testset " cor2cov!" begin
227- tmp_cor1 = copy (cor1)
228- @test ! (tmp_cor1 ≈ cov1)
229- StatsBase. cor2cov! (tmp_cor1, std1)
230- @test tmp_cor1 ≈ cov1
231-
232- tmp_cor2 = copy (cor2)
233- @test ! (tmp_cor2 ≈ cov2)
234- StatsBase. cor2cov! (tmp_cor2, std2)
235- @test tmp_cor2 ≈ cov2
251+ @testset " StatsBase.cor2cov!" begin
252+ test_isapprox_preserves_symherm_structure (StatsBase. cor2cov!, cor1, cov1, std1)
253+ test_isapprox_preserves_symherm_structure (StatsBase. cor2cov!, cor2, cov2, std2)
236254 end
237255 end
238256 end
0 commit comments