Integrals.jl icon indicating copy to clipboard operation
Integrals.jl copied to clipboard

Differentiation for a limit of an integral

Open JanLeppert opened this issue 5 years ago • 10 comments

I have a function with an integral, where the variable is the upper limit of the intgral. Using different integration methods and different automatic differentiation methods for differentiation for the upper limit leads to a StackOverflowError. If I understand the error messages right, the problem is in the different integration methods and the dual-type.

I tried to define with DiffRules.jl a rule for this case (which is simply the integrand at the upper limit), but I still got the error message. Because of the simple rule for this case, it would not be necessary to use the integration method itself and problems with the dual-type could be avoided.

JanLeppert avatar Dec 11 '20 12:12 JanLeppert

The place to put the derivative rule would be https://github.com/SciML/Quadrature.jl/blob/master/src/Quadrature.jl#L577-L635 . Indeed right now it doesn't do the derivative w.r.t. the upper bound.

ChrisRackauckas avatar Dec 11 '20 12:12 ChrisRackauckas

Hi, I have got the same problem. Is there a simple way to fix it? I think having the variable in the lower bound leads similarly to a StackOverflowError. Here is an example of this type of problem

using Quadrature
using ForwardDiff
##
function ftest2(x)
    prob  = QuadratureProblem(  (I,p) -> x^2*I^2 , 0.0, x, [x] )  
    tmp2  = solve( prob, QuadGKJL(), reltol=1e-12, abstol=0 )[1]
    return  tmp2
end
ftest2(1.0)[1]
##
ForwardDiff.derivative(x -> ftest2(x)[1], 0.0)

rseydam avatar Jul 19 '21 20:07 rseydam

Someone probably just needs to do

https://github.com/SciML/Quadrature.jl/blob/master/src/Quadrature.jl#L170

T = promote_type(typeof(lb),typeof(ub)) and then convert both lb and ub to T.

ChrisRackauckas avatar Jul 19 '21 23:07 ChrisRackauckas

Thanks for the suggestion. I have tried it in the following way:

T  = promote_type(typeof(lb),typeof(ub))
lb = convert(T,lb)
ub = convert(T,ub)

I receive the following:

ERROR: LoadError: StackOverflowError: Stacktrace: [1] cachedrule(#unused#::Type{ForwardDiff.Dual{ForwardDiff.Tag{var"#85#86", Float64}, Float64, 1}}, n::Int64) (repeats 79984 times) @ QuadGK ~.julia\packages\QuadGK\czbUH\src\gausskronrod.jl:249

This is the same error as before. Using another method, e.g. HCubatureJL() gives a different error. In this case, the Stacktrace leads back to the GaussKronrod method in the HCubature package with the following error

ERROR: LoadError: MethodError: no method matching kronrod(::Type{ForwardDiff.Dual{ForwardDiff.Tag{var"#109#110", Float64}, Float64, 1}}, ::Int64) Closest candidates are: kronrod(::Type{T}, ::Integer) where T<:AbstractFloat

rseydam avatar Jul 22 '21 11:07 rseydam

Beginner here. I think I'm running into this sort of problem too (and I asked about it on discourse). I'm unclear: has this differentiation wrt limit of integration feature been added yet? thanks

alexboche avatar Mar 05 '22 19:03 alexboche

Any updates on this? I am also trying to get derivatives wrt. to differentiation boundaries using HCubatureJL() and get exactly the same error as @rseydam above. It would be absolutely great if this was addressed! I am also a Julia beginner, but maybe with some guide I could help?

flmuk avatar Jun 26 '23 09:06 flmuk

Any updates on this? I am also trying to get derivatives wrt. to differentiation boundaries using HCubatureJL() and get exactly the same error as @rseydam above. It would be absolutely great if this was addressed! I am also a Julia beginner, but maybe with some guide I could help?

Similar problem for me as well

fabioveneto avatar Jun 26 '23 09:06 fabioveneto

I am also a Julia beginner but interested in solving this problem. Any advice where to start? The links above seem outdated.

hannesbahannes avatar Nov 13 '23 15:11 hannesbahannes

Derivative overloads generally aren't the easiest project to get started with. I'd usually advise doing something easier first, like https://github.com/SciML/Integrals.jl/issues/194.

If you do want to help this effort though, I think the starting place is to help improve the tests, i.e. help us finish https://github.com/SciML/Integrals.jl/pull/196 which will highlight exactly what parts of the algorithms are not dealing with derivatives well. The actual derivative overloads are in https://github.com/SciML/Integrals.jl/blob/master/ext/IntegralsForwardDiffExt.jl and https://github.com/SciML/Integrals.jl/blob/master/ext/IntegralsZygoteExt.jl, the latter using ChainRules https://juliadiff.org/ChainRulesCore.jl/stable/

ChrisRackauckas avatar Nov 17 '23 11:11 ChrisRackauckas

as a quick solution: with e.g. FiniteDiff.finite_difference_jacobian() you can differentiate w.r.t. the integral boundaries. Maybe that's helpful as a workaround

anneaux avatar Apr 19 '24 11:04 anneaux