JuliaMath / MeasureBase.jl

Other
28 stars 4 forks source link

no method matching iterate(::NoLogAbsDetJacobian) #124

Open cscherrer opened 1 year ago

cscherrer commented 1 year ago

This doesn't work:

julia> using ChainRules, MeasureBase, InverseFunctions

julia> f(x) = 3x + 1
f (generic function with 1 method)

julia> finv(y) = (y - 1) / 3
finv (generic function with 1 method)

julia> d = pushfwd(setinverse(f, finv), StdNormal())
PushforwardMeasure(
    InverseFunctions.FunctionWithInverse{typeof(f), typeof(finv)}(f, finv),
    StdNormal())

julia> logdensity_def(d, 0.2)
ERROR: MethodError: no method matching iterate(::NoLogAbsDetJacobian{typeof(finv), Float64})

Closest candidates are:
  iterate(::Base.EnvDict)
   @ Base env.jl:186
  iterate(::Base.EnvDict, ::Any)
   @ Base env.jl:186
  iterate(::Base.AsyncGenerator, ::Base.AsyncGeneratorState)
   @ Base asyncmap.jl:362

The problem is a line that reads

x_orig, inv_ladj = with_logabsdet_jacobian(unwrap(finv), y)

The rhs is a NoLogAbsDetJacobian{typeof(finv), Float64}, which the lhs tries to deconstruct as a pair.

It would be better to assign this a temporary variable first, so we could add a line that gives an informative error if it's not a pair. After that we could deconstruct it safely.

oschulz commented 1 year ago

I'll add it to the ToDo-list for #122 ...