From c45af156315b2cc96a47a18af02712d1b2d244b7 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 14 Jan 2025 13:39:33 +0100 Subject: [PATCH 1/3] Fix `mode` of `LKJCholesky` and define `mean(::LKJCholesky)` --- Project.toml | 2 +- src/cholesky/lkjcholesky.jl | 11 ++++++++++- test/cholesky/lkjcholesky.jl | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index eba90ae18..50607efaa 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Distributions" uuid = "31c24e10-a181-5473-b8eb-7969acd0382f" authors = ["JuliaStats"] -version = "0.25.116" +version = "0.25.117" [deps] AliasTables = "66dad0bd-aa9a-41b7-9441-69ab47430ed8" diff --git a/src/cholesky/lkjcholesky.jl b/src/cholesky/lkjcholesky.jl index 7556620f6..fda164f0a 100644 --- a/src/cholesky/lkjcholesky.jl +++ b/src/cholesky/lkjcholesky.jl @@ -109,11 +109,20 @@ function insupport(d::LKJCholesky, R::LinearAlgebra.Cholesky) return true end -function StatsBase.mode(d::LKJCholesky) +function StatsBase.mean(d::LKJCholesky) factors = Matrix{eltype(d)}(LinearAlgebra.I, size(d)) return LinearAlgebra.Cholesky(factors, d.uplo, 0) end +function mode(d::LKJCholesky; check_args::Bool=true) + @check_args( + LKJCholesky, + @setup(η = d.η), + (η, η > 1, "mode is defined only when η > 1."), + ) + return mean(d) +end + StatsBase.params(d::LKJCholesky) = (d.d, d.η, d.uplo) @inline partype(::LKJCholesky{T}) where {T <: Real} = T diff --git a/test/cholesky/lkjcholesky.jl b/test/cholesky/lkjcholesky.jl index b9afb59be..b23cbd1de 100644 --- a/test/cholesky/lkjcholesky.jl +++ b/test/cholesky/lkjcholesky.jl @@ -124,14 +124,25 @@ using FiniteDifferences end @testset "properties" begin - @testset for p in (4, 5), η in (2, 3.5), uplo in ('L', 'U') + @testset for p in (4, 5), η in (0.5, 2, 3.5), uplo in ('L', 'U') d = LKJCholesky(p, η, uplo) @test d.d == p @test size(d) == (p, p) @test Distributions.params(d) == (d.d, d.η, d.uplo) @test partype(d) <: Float64 - m = mode(d) + if η > 1 + m = mode(d) + @test m isa Cholesky{eltype(d)} + @test Matrix(m) ≈ I + else + @test_throws DomainError(η, "LKJCholesky: mode is defined only when η > 1.") mode(d) + end + m = mode(d; check_args = false) + @test m isa Cholesky{eltype(d)} + @test Matrix(m) ≈ I + + m = mean(d) @test m isa Cholesky{eltype(d)} @test Matrix(m) ≈ I end From a50ff12b92ed56396437f7b0776e20e8435c2a7f Mon Sep 17 00:00:00 2001 From: David Widmann Date: Wed, 15 Jan 2025 20:31:41 +0100 Subject: [PATCH 2/3] Remove definition of `mean(::LKJCholesky)` --- src/cholesky/lkjcholesky.jl | 8 ++------ test/cholesky/lkjcholesky.jl | 4 ---- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/cholesky/lkjcholesky.jl b/src/cholesky/lkjcholesky.jl index fda164f0a..3455bf8bb 100644 --- a/src/cholesky/lkjcholesky.jl +++ b/src/cholesky/lkjcholesky.jl @@ -109,18 +109,14 @@ function insupport(d::LKJCholesky, R::LinearAlgebra.Cholesky) return true end -function StatsBase.mean(d::LKJCholesky) - factors = Matrix{eltype(d)}(LinearAlgebra.I, size(d)) - return LinearAlgebra.Cholesky(factors, d.uplo, 0) -end - function mode(d::LKJCholesky; check_args::Bool=true) @check_args( LKJCholesky, @setup(η = d.η), (η, η > 1, "mode is defined only when η > 1."), ) - return mean(d) + factors = Matrix{eltype(d)}(LinearAlgebra.I, size(d)) + return LinearAlgebra.Cholesky(factors, d.uplo, 0) end StatsBase.params(d::LKJCholesky) = (d.d, d.η, d.uplo) diff --git a/test/cholesky/lkjcholesky.jl b/test/cholesky/lkjcholesky.jl index b23cbd1de..d751d07c9 100644 --- a/test/cholesky/lkjcholesky.jl +++ b/test/cholesky/lkjcholesky.jl @@ -141,10 +141,6 @@ using FiniteDifferences m = mode(d; check_args = false) @test m isa Cholesky{eltype(d)} @test Matrix(m) ≈ I - - m = mean(d) - @test m isa Cholesky{eltype(d)} - @test Matrix(m) ≈ I end for (d, η) in ((2, 4), (2, 1), (3, 1)), T in (Float32, Float64) @test @inferred(partype(LKJCholesky(d, T(η)))) === T From ed0ab6d2a848738219fb7c08abd689876af56c04 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Thu, 16 Jan 2025 11:38:13 +0100 Subject: [PATCH 3/3] =?UTF-8?q?Also=20test=20boundary=20case=20=CE=B7=20?= =?UTF-8?q?=3D=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Seth Axen --- test/cholesky/lkjcholesky.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cholesky/lkjcholesky.jl b/test/cholesky/lkjcholesky.jl index d751d07c9..82e22f606 100644 --- a/test/cholesky/lkjcholesky.jl +++ b/test/cholesky/lkjcholesky.jl @@ -124,7 +124,7 @@ using FiniteDifferences end @testset "properties" begin - @testset for p in (4, 5), η in (0.5, 2, 3.5), uplo in ('L', 'U') + @testset for p in (4, 5), η in (0.5, 1, 2, 3.5), uplo in ('L', 'U') d = LKJCholesky(p, η, uplo) @test d.d == p @test size(d) == (p, p)