Can Turing differentiate a truncated Poisson

Domenic_Di_Francesco
Wed, 19 Feb 2025 16:45:40 +0000
General Usage

does Turing’s AD backend struggle with truncated Poisson distributions?

Turing.@model function bps_hurdle_model(bonus::Vector{Int}, bps::Vector{Int},
    μ_αbps = 0, σ_αbps = 1, μ_βbps = 0, σ_βbps = 1)

    α_pzero ~ Normal(-1, 1); β_pzero ~ Normal(1/3, 1)
    α_bps ~ Normal(μ_αbps, σ_αbps); β_bps ~ Normal(μ_βbps, σ_βbps)

    for n ∈ 1:length(bonus)
        p_zero = logistic(α_pzero + β_pzero * bps[n])
        λ = exp(α_bps + β_bps * bps[n])

        if bonus[n] == 0
            Turing.@addlogprob! log(p_zero)
        else
            Turing.@addlogprob! log(1 - p_zero)
            bonus[n] ~ truncated(Poisson(λ), 1, 3)
        end
    end
end

this model results in the following error:

julia> bps_hurdle_model(Int.(bonus_data.bonus), Int.(bonus_data.bps)) |>
           model -> sample(model, NUTS(), MCMCThreads(), n_draws, n_chains)
ERROR: TaskFailedException

    nested task error: TaskFailedException

        nested task error: MethodError: no method matching _gammalogccdf(::ForwardDiff.Dual{…}, ::ForwardDiff.Dual{…}, ::ForwardDiff.Dual{…})
        The function `_gammalogccdf` exists, but no method is defined for this combination of argument types.

        Closest candidates are:
          _gammalogccdf(::Float64, ::Float64, ::Float64)
           @ StatsFuns ~/.julia/packages/StatsFuns/NSRpu/src/distrs/gamma.jl:80
          _gammalogccdf(::T, ::T, ::T) where T<:Union{Float16, Float32}
           @ StatsFuns ~/.julia/packages/StatsFuns/NSRpu/src/distrs/gamma.jl:97

…despite there being no gamma distributions explictly defined in the model.

switching to non-gradient based samplers for the bonus parameters allows the model to run, but not very successfully.

I have an alternative parameterisation that seems to run using NUTS(), but I do not understand the original issue. If Stan can sample from an equivalent model where I use a truncated Poisson distribution, what is causing this error?

2 posts - 2 participants