2

I want to know the parameters of Gamma Distribution of Awareness. Awareness is 0.336 at 1 week, 0.554 at 4 week , and 0.64 at 13 week. data set is builed here. data is cdf of Gamma dist.

 xs = [ 1 0.336 ; 4 0.554 ; 13 0.64 ] 

i coded this in Julia and using Turing

@model function gmodel(ds)
    m,n = size(ds)
    k ~ InverseGamma(3,2)
    a ~ InverseGamma(3,2)
    for i in 1:m
        cdf_g = cdf(Gamma(k,a),ds[i,1])
        ds[i,2] ~ Normal(cdf_g, 3)
    end
end
c = sample(gmodel(xs),NUTS(),1000) 

but, i get an error of this.

MethodError: no method matching gammacdf(::ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.VarInfo{NamedTuple{(:k, :a),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:k,Tuple{}},Int64},Array{InverseGamma{Float64},1},Array{DynamicPPL.VarName{:k,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:a,Tuple{}},Int64},Array{InverseGamma{Float64},1},Array{DynamicPPL.VarName{:a,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"#33#34",(:ds,),(),(),Tuple{Array{Float64,2}},Tuple{}},DynamicPPL.Sampler{NUTS{Turing.Core.ForwardDiffAD{40},(),AdvancedHMC.DiagEuclideanMetric}},DynamicPPL.DefaultContext},Float64},Float64,2}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.VarInfo{NamedTuple{(:k, :a),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:k,Tuple{}},Int64},Array{InverseGamma{Float64},1},Array{DynamicPPL.VarName{:k,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:a,Tuple{}},Int64},Array{InverseGamma{Float64},1},Array{DynamicPPL.VarName{:a,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"#33#34",(:ds,),(),(),Tuple{Array{Float64,2}},Tuple{}},DynamicPPL.Sampler{NUTS{Turing.Core.ForwardDiffAD{40},(),AdvancedHMC.DiagEuclideanMetric}},DynamicPPL.DefaultContext},Float64},Float64,2}, ::Float64)
Closest candidates are:
  gammacdf(!Matched::Union{Float64, Int64}, !Matched::Union{Float64, Int64}, ::Union{Float64, Int64}) at C:\Users\kimse\.julia\packages\StatsFuns\zJ1EI\src\rmath.jl:77

How implement and use the Function of CDF or PDF functions in Turing ?

phipsgabler
  • 20,535
  • 4
  • 40
  • 60

1 Answers1

0

That's not really a Turing problem -- NUTS, as a HMC sampler, uses automatic differentiation to obtain the gradient of the logpdf. gammacdf is from StatsFuns, and not registered for AD with ForwardDiff. Also, the implementation is probably a ccall in Rmath.

Possible resolutions:

  • Use another sampler, not based on AD.
  • Try a different AD backend with Turing.setadbackend.
  • Express gammacdf in pure Julia, composed of primitively differentiable functions.
  • Add the necessary pullback or pushforward (i.e., an implementation of the gradient suitable for forward or backward mode AD), possibly with a different backend.

And I recommend asking on the #autodiff channel on the Julia slack, there's a load of really helpful people there.

phipsgabler
  • 20,535
  • 4
  • 40
  • 60