JuliaDiff / JuliaDiff/ForwardDiff.jl

Implement gammalogccdf for ForwardDiff

Open
#666 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

I am modeling in Turing.jl some process in which I use Gamma prior to parametrize Truncated Poisson and I use NUTs sampler.

However, my model has nested dependence and I cannot fully vectorize the loop, so Zygote would be slow. The default ForwardDiff throws at me an error

ERROR: MethodError: no method matching _gammalogccdf(::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8})
Stacktrace:
  [1] gammalogccdf(k::Int64, θ::Int64, x::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8})
    @ StatsFuns ~/.julia/packages/StatsFuns/atA5S/src/distrs/gamma.jl:71
  [2] poislogcdf(λ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}, x::Int64)
    @ StatsFuns ~/.julia/packages/StatsFuns/atA5S/src/distrs/pois.jl:30
  [3] logcdf(d::Poisson{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}}, x::Int64)
    @ Distributions ~/.julia/packages/Distributions/Ufrz2/src/univariates.jl:638
  [4] _logcdf_noninclusive(d::Poisson{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}}, x::Int64)
    @ Distributions ~/.julia/packages/Distributions/Ufrz2/src/censored.jl:441
  [5] truncated(d::Poisson{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}}, l::Int64, u::Int64)
    @ Distributions ~/.julia/packages/Distributions/Ufrz2/src/truncate.jl:67
  [6] Truncated(d::Poisson{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}}, l::Int64, u::Int64)
    @ Distributions ./deprecated.jl:105
  [7] macro expansion

To my understanding this error suggests that the model is encountering issues with automatic differentiation, specifically when evaluating the complementary cumulative distribution function (CCDF) of the Gamma distribution. Seems like the error might be related to the Truncated Poisson distribution, as the truncation operation might involve the CCDF of the underlying distribution.

Can that be solved in ForwardDiff AD ?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the shown MethodError and inspect the gammalogccdf entry point in StatsFuns/src/distrs/gamma.jl, along with the ForwardDiff.Dual arguments in the stack trace. Trace how the truncated Poisson path reaches this call; done means that path no longer raises the reported method error for ForwardDiff inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.