Closed Tuebel closed 2 years ago
Thanks for letting me know about this! This is a high priority to fix, obviously. I think (hope?) it should be a quick fix, then we should add tests to be sure to catch this going forward. I'll spend some time on it today.
Here's a MWE, removed from Soss:
julia> using Soss, MeasureTheory, GeneralizedGenerated
julia> g = GG.mk_function(:(x -> Normal(x,1)))
function = (x;) -> begin
begin
(GeneralizedGenerated).Normal(x, 1)
end
end
julia> For(g, 3)
ERROR: MethodError: no method matching productmeasure(::ggfunc-function, ::UnitRange{Int64})
Closest candidates are:
productmeasure(::Any, ::Any, ::Any) at /home/chad/git/MeasureBase/src/combinators/smart-constructors.jl:69
productmeasure(::Kernel, ::Any) at /home/chad/git/MeasureBase/src/combinators/smart-constructors.jl:75
productmeasure(::Returns, ::Any, ::Any) at /home/chad/git/MeasureBase/src/combinators/smart-constructors.jl:82
...
Stacktrace:
[1] For(f::ggfunc-function, n::Int64)
@ MeasureBase ~/git/MeasureBase/src/combinators/for.jl:82
[2] top-level scope
@ REPL[53]:1
mk_function
calls GeneralizedGenerated.closure_conv
, which produces
julia> typeof(g)
ggfunc-function
julia> typeof(g) |> supertype
Any
So the issue is that MeasureBase.productMeasure
doesn't have a generic method, but wants a <:Function
, and a ggfun-function
doesn't meet this criteria.
The simplest fix for this is to add a method
productmeasure(f, pars) = productmeasure(f, identity, pars)
For example, this works:
julia> MeasureBase.productmeasure(f, pars) = MeasureBase.productmeasure(f, identity, pars)
julia> m = @model begin
x ~ For(3) do j Normal(j,1) end
end;
julia> rand(m())
(x = [1.9296440232404293, 3.1574221730525336, 4.1352330633979255],)
But it turns out this just kicks the can down the road:
julia> logpdf(m(), rand(m()))
ERROR: MethodError: no method matching basekernel(::ggfunc-function)
Closest candidates are:
basekernel(::Kernel) at /home/chad/git/MeasureBase/src/kernel.jl:73
basekernel(::Returns) at /home/chad/git/MeasureBase/src/kernel.jl:74
basekernel(::Function) at /home/chad/git/MeasureBase/src/kernel.jl:71
To be continued...
Hi @Tuebel , thanks again for this issue. There's a new release with tests that include
@testset "https://github.com/cscherrer/Soss.jl/issues/305" begin
m = @model begin
x ~ For(3) do j Normal(μ=j) end
end;
@test logpdf(m(), rand(m())) isa Float64
end
Thanks for the quick fix!
I'm not sure if this issue is best suited for this repo or MeasureTheory or MeasureBase.
This issue only occurs when using MeasureTheory.jl@v0.11.3 or later which bumped MeasureBase.jl to v0.3. Using MeasureTheory.jl@v0.11.2 works fine. I think, that is also about the time where ProductMeasure was ported to MeasureBase.
When using the
For(...) do i ...
syntax to create a ProductMeasure in a Soss model I get an error:However, calling rand on the ProductMeasure is successful so no tests would fail in MeasureTheory.jl
Thanks for this library by the way :)