Skip to content

Commit 7707f4a

Browse files
committed
fix test on correlations
the parameter specified for the cholesky-decompositoin is only a transformation of the correlation
1 parent 0cc569d commit 7707f4a

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/cholesky.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ end
176176
# U = _create_blockdiag(v[first(keys(v))], blocks) # v only for dispatch: plain matrix for gpu
177177
# end
178178

179+
"""
180+
Compute the cholesky-factor parameter for a given single
181+
correlation in a 2x2 matrix.
182+
Invert the transformation of cholesky-factor parameterization.
183+
"""
184+
function compute_cholcor_coefficient_single(ρ)
185+
# invert ρ = a / sqrt(a^2 + 1)
186+
sign(ρ) * sqrt^2/(1 - ρ^2))
187+
end
188+
189+
190+
191+
179192
"""
180193
get_ca_starts(vc::ComponentVector)
181194

src/elbo.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ function sample_ζresid_norm(urandn::AbstractMatrix, ζP::TP, ζMs::TM,
411411
ρsP = isempty(ϕuncc.ρsP) ? similar(ϕuncc.ρsP) : ϕuncc.ρsP # required by zygote
412412
UP = transformU_block_cholesky1(ρsP, cor_ends.P)
413413
ρsM = isempty(ϕuncc.ρsM) ? similar(ϕuncc.ρsM) : ϕuncc.ρsM # required by zygote
414+
# cholesky factor of the correlation: diag(UM' * UM) .== 1
415+
# coefficients ρsM can be larger than 1, still yielding correlations <1 in UM' * UM
414416
UM = transformU_block_cholesky1(ρsM, cor_ends.M)
415417
cf = ϕuncc.coef_logσ2_ζMs
416418
logσ2_logMs = vec(cf[1, :] .+ cf[2, :] .* ζMs)

test/test_elbo.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,13 @@ test_scenario = (scenario) -> begin
130130
ϕunc_true.logσ2_ζP = (log abs2).(sd_ζP_true)
131131
ϕunc_true.coef_logσ2_ζMs[1,:] = (log abs2).(sd_ζMs_a_true)
132132
ϕunc_true.coef_logσ2_ζMs[2,:] = logσ2_ζMs_b_true
133-
ϕunc_true.ρsP = ρsP_true
134-
ϕunc_true.ρsM = ρsM_true
133+
# note that the parameterization contains a transformation that
134+
# here only inverted for the single correlation case
135+
ϕunc_true.ρsP = CP.compute_cholcor_coefficient_single.(ρsP_true)
136+
ϕunc_true.ρsM = CP.compute_cholcor_coefficient_single.(ρsM_true)
137+
# check that ρsM_true = -0.6 recovered with params ϕunc_true.ρsM = -0.75
138+
UC = CP.transformU_cholesky1(ϕunc_true.ρsM); Σ = UC' * UC
139+
@test Σ[1,2] ρsM_true[1]
135140

136141
probd = CP.update(probc; ϕunc=ϕunc_true);
137142
= vcat(ϕ_ini.μP, probc.ϕg, probd.ϕunc)
@@ -189,12 +194,12 @@ test_scenario = (scenario) -> begin
189194
residPMst = vcat(residP,
190195
reshape(residMst, size(residMst,1)*size(residMst,2), size(residMst,3)))
191196
cor_PMs = cor(residPMst')
192-
@test cor_PMs[1,2] ρsP_true[1] atol=0.2
193-
@test all(.≈(cor_PMs[1:2,3:end], 0.0, atol=0.2)) # no correlations P,M
194-
@test cor_PMs[3,4] ρsM_true[1] atol=0.2
195-
@test all(.≈(cor_PMs[3:4,5:end], 0.0, atol=0.2)) # no correlations M1, M2
196-
@test cor_PMs[5,6] ρsM_true[1] atol=0.2
197-
@test all(.≈(cor_PMs[5:6,7:end], 0.0, atol=0.2)) # no correlations M1, M2
197+
@test cor_PMs[1,2] ρsP_true[1] atol=0.02
198+
@test all(.≈(cor_PMs[1:2,3:end], 0.0, atol=0.02)) # no correlations P,M
199+
@test cor_PMs[3,4] ρsM_true[1] atol=0.02
200+
@test all(.≈(cor_PMs[3:4,5:end], 0.0, atol=0.02)) # no correlations M1, M2
201+
@test cor_PMs[5,6] ρsM_true[1] atol=0.02
202+
@test all(.≈(cor_PMs[5:6,7:end], 0.0, atol=0.02)) # no correlations M1, M2
198203
end
199204
test_distζ(_ζsP, _ζsMs, ϕunc_true, ζMs_g)
200205
@testset "predict_hvi check sd" begin

0 commit comments

Comments
 (0)