Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve ambiguity in the prod method for ProductOf #15

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/prod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,6 @@ function Base.prod(
return push!(product, item)
end

function Base.prod(
::GenericProd, ::UnspecifiedProd, ::UnspecifiedProd, left::ProductOf{F,F}, right::F
) where {F}
return LinearizedProductOf(F[getleft(left), getright(left), right], 3)
end

function Base.prod(
::GenericProd, ::UnspecifiedProd, ::UnspecifiedProd, left::ProductOf{L,R}, right::R
) where {L,R}
Expand All @@ -417,6 +411,18 @@ function Base.prod(
return ProductOf(getleft(right), LinearizedProductOf(R[left, getright(right)], 2))
end

function Base.prod(
::GenericProd, ::UnspecifiedProd, ::UnspecifiedProd, left::T, right::ProductOf{T,T}
) where {T}
return LinearizedProductOf(T[left, getleft(right), getright(right)], 3)
end

function Base.prod(
::GenericProd, ::UnspecifiedProd, ::UnspecifiedProd, left::ProductOf{T,T}, right::T
) where {T}
return LinearizedProductOf(T[getleft(left), getright(left), right], 3)
end

function Base.prod(
::GenericProd,
::UnspecifiedProd,
Expand Down
32 changes: 29 additions & 3 deletions test/prod_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ end
end

@testitem "`ProductOf` should support distributions that do not have explicitly defined `support`" begin

struct SomeComplexDistribution end

BayesBase.support(::SomeComplexDistribution) = error("not defined")
Expand All @@ -309,11 +308,9 @@ end
@test !insupport(prod, -1)
@test logpdf(prod, 1) === 2
@test logpdf(prod, 2) === 4

end

@testitem "`LinearizedProductOf` should support distributions that do not have explicitly defined `support`" begin

struct SomeComplexDistribution end

BayesBase.support(::SomeComplexDistribution) = error("not defined")
Expand All @@ -327,5 +324,34 @@ end
@test !insupport(prod, -1)
@test logpdf(prod, 1) === 2
@test logpdf(prod, 2) === 4
end

@testitem "There should be no ambiguity in `prod` for `ProductOf`" begin
struct SomeArbitraryDistribution end

@test prod(
GenericProd(),
SomeArbitraryDistribution(),
ProductOf(SomeArbitraryDistribution(), SomeArbitraryDistribution()),
Comment on lines +334 to +335
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not wrong PR tries to fix this as well?

julia> prod(GenericProd(), ProductOf(Normal(0, 1), Normal(0, 1)), ProductOf(Normal(0, 1), Normal(0, 1)))
ERROR: MethodError: prod(::GenericProd, ::ProductOf{Normal{Float64}, Normal{Float64}}, ::ProductOf{Normal{Float64}, Normal{Float64}}) is ambiguous.

Candidates:
  prod(::GenericProd, left::ProductOf{L, R}, right::T) where {L, R, T}
    @ BayesBase ~/.julia/packages/BayesBase/MnjV6/src/prod.jl:252
  prod(::GenericProd, left::T, right::ProductOf{L, R}) where {L, R, T}
    @ BayesBase ~/.julia/packages/BayesBase/MnjV6/src/prod.jl:285

Possible fix, define
  prod(::GenericProd, ::T, ::T) where {L, R, L, R, T<:ProductOf{L, R}, T<:ProductOf{L, R}}

Stacktrace:
 [1] top-level scope
   @ REPL[34]:1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it still fails on this branch for me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR in particular is not aimed to solve this particular case, I think this one can be a separate issue, as it is can be quite complex in general

) == LinearizedProductOf(
[
SomeArbitraryDistribution(),
SomeArbitraryDistribution(),
SomeArbitraryDistribution(),
],
3,
)

@test prod(
GenericProd(),
ProductOf(SomeArbitraryDistribution(), SomeArbitraryDistribution()),
SomeArbitraryDistribution(),
) == LinearizedProductOf(
[
SomeArbitraryDistribution(),
SomeArbitraryDistribution(),
SomeArbitraryDistribution(),
],
3,
)
end
Loading