Skip to content

Commit

Permalink
Fix mode of LKJCholesky and define mean(::LKJCholesky)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion committed Jan 14, 2025
1 parent 957f0c0 commit bc5dbac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 10 additions & 1 deletion src/cholesky/lkjcholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions test/cholesky/lkjcholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 with :\nLKJCholesky: 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
Expand Down

0 comments on commit bc5dbac

Please sign in to comment.