From 6db383af471ca4bf9dd27badef003e25d4f95e4c Mon Sep 17 00:00:00 2001 From: Mykola Lukashchuk Date: Wed, 23 Oct 2024 10:34:12 +0200 Subject: [PATCH 1/4] update version to 1.6.0 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f7c738da..cd039ff4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ExponentialFamily" uuid = "62312e5e-252a-4322-ace9-a5f4bf9b357b" authors = ["Ismail Senoz ", "Dmitry Bagaev "] -version = "1.5.3" +version = "1.6.0" [deps] BayesBase = "b4ee3484-f114-42fe-b91c-797d54a0c67e" From 25283dc67dbd37e34569f1e781bb6f89cd9ed093 Mon Sep 17 00:00:00 2001 From: Mykola Lukashchuk Date: Thu, 24 Oct 2024 13:33:17 +0200 Subject: [PATCH 2/4] fix: add product between wishart and wishartfast --- src/distributions/wishart.jl | 6 ++++++ src/distributions/wishart_inverse.jl | 6 ++++++ test/distributions/wishart_tests.jl | 29 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/distributions/wishart.jl b/src/distributions/wishart.jl index 773166a4..1fcbb238 100644 --- a/src/distributions/wishart.jl +++ b/src/distributions/wishart.jl @@ -203,6 +203,12 @@ function BayesBase.prod(::PreserveTypeProd{Distribution}, left::WishartFast, rig return WishartFast(df, invV) end +BayesBase.default_prod_rule(::Type{<:Wishart}, ::Type{<:WishartFast}) = PreserveTypeProd(Distribution) + +function BayesBase.prod(::PreserveTypeProd{Distribution}, left::Wishart, right::WishartFast) + return prod(PreserveTypeProd(Distribution), convert(WishartFast, left), right) +end + function BayesBase.insupport(ef::ExponentialFamilyDistribution{WishartFast}, x::Matrix) return size(getindex(unpack_parameters(ef), 2)) == size(x) && isposdef(x) end diff --git a/src/distributions/wishart_inverse.jl b/src/distributions/wishart_inverse.jl index 4cb80207..676c9b0c 100644 --- a/src/distributions/wishart_inverse.jl +++ b/src/distributions/wishart_inverse.jl @@ -225,6 +225,12 @@ function BayesBase.prod(::PreserveTypeProd{Distribution}, left::InverseWishartFa return InverseWishartFast(df, V) end +BayesBase.default_prod_rule(::Type{<:InverseWishart}, ::Type{<:InverseWishartFast}) = PreserveTypeProd(Distribution) + +function BayesBase.prod(::PreserveTypeProd{Distribution}, left::InverseWishart, right::InverseWishartFast) + return prod(PreserveTypeProd(Distribution), convert(InverseWishartFast, left), right) +end + function BayesBase.insupport(ef::ExponentialFamilyDistribution{InverseWishartFast}, x::Matrix) return size(getindex(unpack_parameters(ef), 2)) == size(x) && isposdef(x) end diff --git a/test/distributions/wishart_tests.jl b/test/distributions/wishart_tests.jl index 33e90f71..efbcfce2 100644 --- a/test/distributions/wishart_tests.jl +++ b/test/distributions/wishart_tests.jl @@ -131,3 +131,32 @@ end end end end + +@testitem "Wishart: prod between Wishart and WishartFast" begin + include("distributions_setuptests.jl") + + import ExponentialFamily: WishartFast + import Distributions: Wishart + + for Sleft in rand(Wishart(10, Array(Eye(2))), 2), Sright in rand(Wishart(10, Array(Eye(2))), 2), νright in (6, 7), νleft in (4, 5) + let left = Wishart(νleft, Sleft), right = WishartFast(νright, Sright) + # Test commutativity of the product + prod_result1 = prod(PreserveTypeProd(Distribution), left, right) + prod_result2 = prod(PreserveTypeProd(Distribution), right, left) + + @test prod_result1.ν ≈ prod_result2.ν + @test prod_result1.invS ≈ prod_result2.invS + + # Test that the product preserves type + @test prod_result1 isa WishartFast + @test prod_result2 isa WishartFast + + # prod the same before conversion + left_fast = convert(WishartFast, left) + prod_fast = prod(ClosedProd(), left_fast, right) + + @test prod_fast.ν ≈ prod_result1.ν + @test prod_fast.invS ≈ prod_result2.invS + end + end +end \ No newline at end of file From 2c11b39a594a17a8c7ff43a776113fec10681ecd Mon Sep 17 00:00:00 2001 From: Mykola Lukashchuk Date: Thu, 24 Oct 2024 13:48:59 +0200 Subject: [PATCH 3/4] test: add inverse wishart wishart product --- test/distributions/wishart_inverse_tests.jl | 29 +++++++++++++++++++++ test/distributions/wishart_tests.jl | 5 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/test/distributions/wishart_inverse_tests.jl b/test/distributions/wishart_inverse_tests.jl index 9d76f41b..7f6ffcc6 100644 --- a/test/distributions/wishart_inverse_tests.jl +++ b/test/distributions/wishart_inverse_tests.jl @@ -228,3 +228,32 @@ end end end end + +@testitem "InverseWishart: prod between InverseWishart and InverseWishartFast" begin + include("distributions_setuptests.jl") + + import ExponentialFamily: InverseWishartFast + import Distributions: InverseWishart + + for Sleft in rand(InverseWishart(10, Array(Eye(2))), 2), Sright in rand(InverseWishart(10, Array(Eye(2))), 2), νright in (6, 7), νleft in (4, 5) + let left = InverseWishart(νleft, Sleft), right = InverseWishartFast(νright, Sright) + # Test commutativity of the product + prod_result1 = prod(PreserveTypeProd(Distribution), left, right) + prod_result2 = prod(PreserveTypeProd(Distribution), right, left) + + @test prod_result1.ν ≈ prod_result2.ν + @test prod_result1.S ≈ prod_result2.S + + # Test that the product preserves type + @test prod_result1 isa InverseWishartFast + @test prod_result2 isa InverseWishartFast + + # prod prod stays if we convert fisrt and then do product + left_fast = convert(InverseWishartFast, left) + prod_fast = prod(ClosedProd(), left_fast, right) + + @test prod_fast.ν ≈ prod_result1.ν + @test prod_fast.S ≈ prod_result2.S + end + end +end diff --git a/test/distributions/wishart_tests.jl b/test/distributions/wishart_tests.jl index efbcfce2..fca2acbc 100644 --- a/test/distributions/wishart_tests.jl +++ b/test/distributions/wishart_tests.jl @@ -151,7 +151,7 @@ end @test prod_result1 isa WishartFast @test prod_result2 isa WishartFast - # prod the same before conversion + # prod stays the same if we convert fisrt and then do product left_fast = convert(WishartFast, left) prod_fast = prod(ClosedProd(), left_fast, right) @@ -159,4 +159,5 @@ end @test prod_fast.invS ≈ prod_result2.invS end end -end \ No newline at end of file +end + From 9f311fd65b3f9bf8fbc23f669dbd07b6536d156d Mon Sep 17 00:00:00 2001 From: Mykola Lukashchuk Date: Thu, 24 Oct 2024 13:56:37 +0200 Subject: [PATCH 4/4] fix: add prod between inverse wisharts --- src/distributions/wishart_inverse.jl | 6 ++++++ test/distributions/wishart_inverse_tests.jl | 15 ++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/distributions/wishart_inverse.jl b/src/distributions/wishart_inverse.jl index 676c9b0c..6c308999 100644 --- a/src/distributions/wishart_inverse.jl +++ b/src/distributions/wishart_inverse.jl @@ -231,6 +231,12 @@ function BayesBase.prod(::PreserveTypeProd{Distribution}, left::InverseWishart, return prod(PreserveTypeProd(Distribution), convert(InverseWishartFast, left), right) end +BayesBase.default_prod_rule(::Type{<:InverseWishart}, ::Type{<:InverseWishart}) = PreserveTypeProd(Distribution) + +function BayesBase.prod(::PreserveTypeProd{Distribution}, left::InverseWishart, right::InverseWishart) + return prod(PreserveTypeProd(Distribution), convert(InverseWishartFast, left), convert(InverseWishartFast, right)) +end + function BayesBase.insupport(ef::ExponentialFamilyDistribution{InverseWishartFast}, x::Matrix) return size(getindex(unpack_parameters(ef), 2)) == size(x) && isposdef(x) end diff --git a/test/distributions/wishart_inverse_tests.jl b/test/distributions/wishart_inverse_tests.jl index 7f6ffcc6..3c586004 100644 --- a/test/distributions/wishart_inverse_tests.jl +++ b/test/distributions/wishart_inverse_tests.jl @@ -236,10 +236,10 @@ end import Distributions: InverseWishart for Sleft in rand(InverseWishart(10, Array(Eye(2))), 2), Sright in rand(InverseWishart(10, Array(Eye(2))), 2), νright in (6, 7), νleft in (4, 5) - let left = InverseWishart(νleft, Sleft), right = InverseWishartFast(νright, Sright) + let left = InverseWishart(νleft, Sleft), right = InverseWishart(νleft, Sleft), right_fast = convert(InverseWishartFast, right) # Test commutativity of the product - prod_result1 = prod(PreserveTypeProd(Distribution), left, right) - prod_result2 = prod(PreserveTypeProd(Distribution), right, left) + prod_result1 = prod(PreserveTypeProd(Distribution), left, right_fast) + prod_result2 = prod(PreserveTypeProd(Distribution), right_fast, left) @test prod_result1.ν ≈ prod_result2.ν @test prod_result1.S ≈ prod_result2.S @@ -248,12 +248,17 @@ end @test prod_result1 isa InverseWishartFast @test prod_result2 isa InverseWishartFast - # prod prod stays if we convert fisrt and then do product + # prod stays if we convert fisrt and then do product left_fast = convert(InverseWishartFast, left) - prod_fast = prod(ClosedProd(), left_fast, right) + prod_fast = prod(ClosedProd(), left_fast, right_fast) @test prod_fast.ν ≈ prod_result1.ν @test prod_fast.S ≈ prod_result2.S + + # prod for Inverse Wishart is defenied + prod_result_not_fast = prod(PreserveTypeProd(Distribution), left, right) + @test prod_result_not_fast.ν ≈ prod_result1.ν + @test prod_result_not_fast.S ≈ prod_result1.S end end end