Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiation for a limit of an integral #56

Open
JanLeppert opened this issue Dec 11, 2020 · 10 comments
Open

Differentiation for a limit of an integral #56

JanLeppert opened this issue Dec 11, 2020 · 10 comments

Comments

@JanLeppert
Copy link

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.

@ChrisRackauckas
Copy link
Member

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.

@rseydam
Copy link

rseydam commented Jul 19, 2021

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)

@ChrisRackauckas
Copy link
Member

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.

@rseydam
Copy link

rseydam commented Jul 22, 2021

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

@alexboche
Copy link

alexboche commented Mar 5, 2022

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

@flmuk
Copy link

flmuk commented Jun 26, 2023

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?

@fabioveneto
Copy link

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

@hannesbahannes
Copy link

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

@ChrisRackauckas
Copy link
Member

Derivative overloads generally aren't the easiest project to get started with. I'd usually advise doing something easier first, like #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 #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/

@anneaux
Copy link

anneaux commented Apr 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants