Repository navigation
Closed
Description
The highest derivative of lgamma is currently limited to the first derivative of trigamma(x) = polygamma(1,x), probably because it has a special name. But of course the derivative of polygamma(n, x) is polygamma(n+1,x) and they are all available. I ran into this problem when computing the Hessian of a function involving trigamma. Here is a minimal example
julia> harr(x::Vector) = 1/2 * log(x[1]*trigamma(x[1])-1) - log(x[2])
harr (generic function with 1 method)
julia> ForwardDiff.hessian(harr, [1.5, 60.])
ERROR: MethodError: Cannot `convert` an object of type ForwardDiff.Dual{2,Float64} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
in harr(::Array{ForwardDiff.Dual{2,ForwardDiff.Dual{2,Float64}},1}) at ./REPL[15]:1
in vector_mode_gradient at /home/beaujean/.julia/v0.5/ForwardDiff/src/gradient.jl:60 [inlined]
in gradient(::#harr, ::Array{ForwardDiff.Dual{2,Float64},1}, ::ForwardDiff.GradientConfig{2,ForwardDiff.Dual{2,Float64},Array{ForwardDiff.Dual{2,ForwardDiff.Dual{2,Float64}},1}}) at /home/beaujean/.julia/v0.5/ForwardDiff/src/gradient.jl:7
in vector_mode_jacobian(::ForwardDiff.##33#34{#harr,ForwardDiff.HessianConfig{2,Float64,Array{ForwardDiff.Dual{2,Float64},1},ForwardDiff.Dual{2,Float64},Array{ForwardDiff.Dual{2,ForwardDiff.Dual{2,Float64}},1}}}, ::Array{Float64,1}, ::ForwardDiff.JacobianConfig{2,Float64,Array{ForwardDiff.Dual{2,Float64},1}}) at /home/beaujean/.julia/v0.5/ForwardDiff/src/jacobian.jl:75
in jacobian(::ForwardDiff.##33#34{#harr,ForwardDiff.HessianConfig{2,Float64,Array{ForwardDiff.Dual{2,Float64},1},ForwardDiff.Dual{2,Float64},Array{ForwardDiff.Dual{2,ForwardDiff.Dual{2,Float64}},1}}}, ::Array{Float64,1}, ::ForwardDiff.JacobianConfig{2,Float64,Array{ForwardDiff.Dual{2,Float64},1}}) at /home/beaujean/.julia/v0.5/ForwardDiff/src/jacobian.jl:7
in hessian(::#harr, ::Array{Float64,1}) at /home/beaujean/.julia/v0.5/ForwardDiff/src/hessian.jl:6I checked the source code of Calculus and will post an issue there to add this functionality. I'm wondering why the generic support for polygamma hasn't been added. Perhaps it can't pick up the first argument to polygamma?
Metadata
Metadata
Assignees
Labels
No labels
Activity
fredRos commentedon Apr 24, 2017
It seems @johnmyleswhite has been busy with other stuff, there was no response so I tried to implement directly in ForwardDiff's
dual.jlfollowing theatanexample but couldn't get it to work. Any help is appreciated.My first attempt was
Running the MWE from above, I get
Removing
@ambiguousdidn't change anything. Using anIntegerinstead of aRealleads to aStackOverflowErrorfredRos commentedon Apr 25, 2017
update: The
StackOverflowErroralso occurs for other special functions with two arguments declared inForwardDiff.SPECIAL_FUNCSpolygammais missing inSPECIAL_FUNCS. Why actually?jrevels commentedon Apr 25, 2017
Hi @fredRos, sorry for such a late reply. Thanks for tackling this.
Just narrowing it down to this function:
This definition is infinitely recursive, because you're defining
polygamma(::Integer, ::Dual)to callpolygamma(::Integer, ::Dual)with no base case. The definition should probably be something closer to:It'll likely be easier for us to add the definition via Calculus. I have commit access there, so I can help out in resolving JuliaMath/Calculus.jl#104 (apologies for not doing so earlier, that issue must've slipped through the cracks on my end).
Nice catch! It looks like ForwardDiff's auto-definition code for binary
SpecialFunctionsprimitives is incorrect, because it assumes that they're unary (like the other auto-defined primitives). I'm not sure how our tests didn't catch this, there might be a bug in Calculus...I'll play around with fixing this up. Thanks again for looking into it.
fredRos commentedon Apr 25, 2017
jrevels commentedon Apr 28, 2017
An update: I've add polygamma to a new rules system I've implemented for DiffBase: JuliaAttic/DiffBase.jl#5.
Follow-up work will be for me to switch ForwardDiff over to using DiffBase's rules instead of reaching into Calculus' internals to generate derivative definitions.
jacobcvt12 commentedon Aug 10, 2017
I can replicate the original problem with no issues
However, I am unable to take gradients of the log probability density function of a Gamma wrt the parameters
I don't have this problem with other distributions I have tried such as
Normal,InverseGaussian, andCauchy.