stochastics-uni-luebeck / LevyArea.jl

Iterated stochastic integrals in Julia.
https://stochastics-uni-luebeck.github.io/LevyArea.jl/stable
MIT License
9 stars 0 forks source link

Can the Levy area computations be made compatible with AD? #6

Closed frankschae closed 2 years ago

frankschae commented 2 years ago

(requires https://github.com/SciML/StochasticDiffEq.jl/pull/459 or the overload from https://github.com/SciML/StochasticDiffEq.jl/issues/458)

using StochasticDiffEq, LinearAlgebra, Random, Test
using Zygote, DiffEqSensitivity, ForwardDiff
using LevyArea

u₀ = [0.0, 0.0, 0.0]
f(u, p, t) = [0.0, 0.0, 0.0]
g(u, p, t) = [p[1] 0.0; 0.0 p[1]; 0.0 p[1]*u[1]]
dt = 1 // 2^10
tspan = (0.0, 1.0)
p = [1.0]
prob = SDEProblem(f, g, u₀, (0.0, 1.0), p; noise_rate_prototype = zeros(3, 2))
alg = RKMilGeneral()
StochasticDiffEq.get_WikJ(randn(2), prob, alg)

function loss(p, alg, sensealg)
  _prob = remake(prob, p = p)
  u = Array(solve(_prob, alg, dt = dt, saveat = 0.1, adaptive = false, sensealg = sensealg))
  sum(abs2, u)
end

function compute_sense(p, alg, sensealg)
  Random.seed!(1838456)
  Zygote.gradient(p -> loss(p, alg, sensealg), p)
end

# check compatibility of levy area computation algs with sensitivity algs
dp1 = compute_sense(p, RKMilGeneral(), ForwardDiffSensitivity()) # ([5.250521317359784],)
dp1 = compute_sense(p, RKMilGeneral(ii_approx = LevyArea.Fourier()), ForwardDiffSensitivity()) # MethodError

AbstractFloat in https://github.com/stochastics-uni-luebeck/LevyArea.jl/blob/68c5cb08ab103b4dcd3178651f7a5dd9ce8c666d/src/fourier.jl#L21 is probably a bit too constrained.

ERROR: LoadError: MethodError: no method matching levyarea(::Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, ::Int64, ::Fourier) Closest candidates are: levyarea(::AbstractVector{T}, ::Integer, ::Fourier) where T<:AbstractFloat at ~/.julia/packages/LevyArea/DJs6Y/src/fourier.jl:21 levyarea(::AbstractVector{T}, ::Integer, ::Milstein) where T<:AbstractFloat at ~/.julia/packages/LevyArea/DJs6Y/src/milstein.jl:22 levyarea(::AbstractVector{T}, ::Integer, ::Wiktorsson) where T<:AbstractFloat at ~/.julia/packages/LevyArea/DJs6Y/src/wiktorsson.jl:22 ... Stacktrace: [1] get_iterated_I(dt::Float64, dW::Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, dZ::Nothing, alg::Fourier, p::Nothing, c::Int64, γ::Rational{Int64}) @ StochasticDiffEq ~/.julia/dev/StochasticDiffEq/src/iterated_integrals.jl:478 [2] perform_step!(integrator::StochasticDiffEq.SDEIntegrator{RKMilGeneral{Fourier, Nothing}, false, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, Float64, Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Float64, Float64, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, NoiseProcess{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, 2, Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing, Nothing, typeof(DiffEqNoiseProcess.WHITE_NOISE_DIST), typeof(DiffEqNoiseProcess.WHITE_NOISE_BRIDGE), false, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, RSWM{Float64}, Nothing, RandomNumbers.Xorshifts.Xoroshiro128Plus}, Nothing, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, RODESolution{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, 2, Vector{Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}}, Nothing, Nothing, Vector{Float64}, NoiseProcess{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, 2, Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing, Nothing, typeof(DiffEqNoiseProcess.WHITE_NOISE_DIST), typeof(DiffEqNoiseProcess.WHITE_NOISE_BRIDGE), false, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, RSWM{Float64}, Nothing, RandomNumbers.Xorshifts.Xoroshiro128Plus}, SDEProblem{Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Tuple{Float64, Float64}, false, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing, SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(g), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}}, RKMilGeneral{Fourier, Nothing}, StochasticDiffEq.LinearInterpolationData{Vector{Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}}, Vector{Float64}}, DiffEqBase.DEStats}, StochasticDiffEq.RKMilGeneralConstantCache{Fourier}, SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(g), Nothing, StochasticDiffEq.SDEOptions{Float64, Float64, OrdinaryDiffEq.PIController{Float64}, typeof(DiffEqBase.ODE_DEFAULT_NORM), Nothing, CallbackSet{Tuple{}, Tuple{}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, Nothing, UnitRange{Int64}, Int64, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, Tuple{}, Vector{Float64}, Tuple{}}, Nothing, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, Nothing, Nothing}, cache::StochasticDiffEq.RKMilGeneralConstantCache{Fourier}) @ StochasticDiffEq ~/.julia/dev/StochasticDiffEq/src/perform_step/low_order.jl:385 [3] solve!(integrator::StochasticDiffEq.SDEIntegrator{RKMilGeneral{Fourier, Nothing}, false, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, Float64, Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Float64, Float64, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, NoiseProcess{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, 2, Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing, Nothing, typeof(DiffEqNoiseProcess.WHITE_NOISE_DIST), typeof(DiffEqNoiseProcess.WHITE_NOISE_BRIDGE), false, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, RSWM{Float64}, Nothing, RandomNumbers.Xorshifts.Xoroshiro128Plus}, Nothing, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, RODESolution{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, 2, Vector{Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}}, Nothing, Nothing, Vector{Float64}, NoiseProcess{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, 2, Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing, Nothing, typeof(DiffEqNoiseProcess.WHITE_NOISE_DIST), typeof(DiffEqNoiseProcess.WHITE_NOISE_BRIDGE), false, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, ResettableStacks.ResettableStack{Tuple{Float64, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing}, false}, RSWM{Float64}, Nothing, RandomNumbers.Xorshifts.Xoroshiro128Plus}, SDEProblem{Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Tuple{Float64, Float64}, false, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing, SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(g), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}}, RKMilGeneral{Fourier, Nothing}, StochasticDiffEq.LinearInterpolationData{Vector{Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}}, Vector{Float64}}, DiffEqBase.DEStats}, StochasticDiffEq.RKMilGeneralConstantCache{Fourier}, SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(g), Nothing, StochasticDiffEq.SDEOptions{Float64, Float64, OrdinaryDiffEq.PIController{Float64}, typeof(DiffEqBase.ODE_DEFAULT_NORM), Nothing, CallbackSet{Tuple{}, Tuple{}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, Nothing, UnitRange{Int64}, Int64, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, Tuple{}, Vector{Float64}, Tuple{}}, Nothing, ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}, Nothing, Nothing}) @ StochasticDiffEq ~/.julia/dev/StochasticDiffEq/src/solve.jl:611 [4] #__solve#110 @ ~/.julia/dev/StochasticDiffEq/src/solve.jl:7 [inlined] [5] #solve_call#37 @ ~/.julia/packages/DiffEqBase/1V2xg/src/solve.jl:61 [inlined] [6] solve_up(prob::SDEProblem{Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Tuple{Float64, Float64}, false, Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, Nothing, SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(g), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}}, sensealg::Nothing, u0::Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, p::Vector{ForwardDiff.Dual{ForwardDiff.Tag{SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Float64}, Float64, 3}}, args::RKMilGeneral{Fourier, Nothing}; kwargs::Base.Pairs{Symbol, Any, NTuple{4, Symbol}, NamedTuple{(:saveat, :save_idxs, :dt, :adaptive), Tuple{Vector{Float64}, UnitRange{Int64}, Rational{Int64}, Bool}}}) @ DiffEqBase ~/.julia/packages/DiffEqBase/1V2xg/src/solve.jl:87 [7] #solve#38 @ ~/.julia/packages/DiffEqBase/1V2xg/src/solve.jl:73 [inlined] [8] (::DiffEqSensitivity.var"#260#266"{0, Matrix{Float64}, Float64, Base.Pairs{Symbol, Real, Tuple{Symbol, Symbol}, NamedTuple{(:dt, :adaptive), Tuple{Rational{Int64}, Bool}}}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, false, Vector{Float64}, Nothing, SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(g), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Matrix{Float64}}, RKMilGeneral{Fourier, Nothing}, ForwardDiffSensitivity{0, nothing}, Vector{Float64}, Vector{Float64}, Tuple{}, UnitRange{Int64}})() @ DiffEqSensitivity ~/.julia/packages/DiffEqSensitivity/lGfyc/src/concrete_solve.jl:498 [9] unthunk @ ~/.julia/packages/ChainRulesCore/uxrij/src/tangent_types/thunks.jl:197 [inlined] [10] wrap_chainrules_output @ ~/.julia/packages/Zygote/FPUm3/src/compiler/chainrules.jl:104 [inlined] [11] map @ ./tuple.jl:223 [inlined] [12] map (repeats 3 times) @ ./tuple.jl:224 [inlined] [13] wrap_chainrules_output @ ~/.julia/packages/Zygote/FPUm3/src/compiler/chainrules.jl:105 [inlined] [14] ZBack @ ~/.julia/packages/Zygote/FPUm3/src/compiler/chainrules.jl:204 [inlined] [15] kw_zpullback @ ~/.julia/packages/Zygote/FPUm3/src/compiler/chainrules.jl:230 [inlined] [16] #213 @ ~/.julia/packages/Zygote/FPUm3/src/lib/lib.jl:203 [inlined] [17] (::Zygote.var"#1752#back#215"{Zygote.var"#213#214"{Tuple{NTuple{6, Nothing}, Tuple{Nothing}}, Zygote.var"#kw_zpullback#42"{DiffEqSensitivity.var"#forward_sensitivity_backpass#263"{0, Float64, Base.Pairs{Symbol, Real, Tuple{Symbol, Symbol}, NamedTuple{(:dt, :adaptive), Tuple{Rational{Int64}, Bool}}}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, false, Vector{Float64}, Nothing, SDEFunction{false, typeof(f), typeof(g), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(g), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Matrix{Float64}}, RKMilGeneral{Fourier, Nothing}, ForwardDiffSensitivity{0, nothing}, Vector{Float64}, Vector{Float64}, Tuple{}, UnitRange{Int64}}}}})(Δ::Matrix{Float64}) @ Zygote ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:67 [18] Pullback @ ~/.julia/packages/DiffEqBase/1V2xg/src/solve.jl:73 [inlined] [19] (::typeof(∂(#solve#38)))(Δ::Matrix{Float64}) @ Zygote ~/.julia/packages/Zygote/FPUm3/src/compiler/interface2.jl:0 [20] (::Zygote.var"#213#214"{Tuple{NTuple{6, Nothing}, Tuple{Nothing}}, typeof(∂(#solve#38))})(Δ::Matrix{Float64}) @ Zygote ~/.julia/packages/Zygote/FPUm3/src/lib/lib.jl:203 [21] (::Zygote.var"#1752#back#215"{Zygote.var"#213#214"{Tuple{NTuple{6, Nothing}, Tuple{Nothing}}, typeof(∂(#solve#38))}})(Δ::Matrix{Float64}) @ Zygote ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:67 [22] Pullback @ ~/.julia/packages/DiffEqBase/1V2xg/src/solve.jl:68 [inlined] [23] (::typeof(∂(solve##kw)))(Δ::Matrix{Float64}) @ Zygote ~/.julia/packages/Zygote/FPUm3/src/compiler/interface2.jl:0 [24] Pullback @ ~/LevyArea/Check_AD.jl:22 [inlined] [25] (::typeof(∂(loss)))(Δ::Float64) @ Zygote ~/.julia/packages/Zygote/FPUm3/src/compiler/interface2.jl:0 [26] Pullback @ ~/LevyArea/Check_AD.jl:28 [inlined] [27] (::typeof(∂(λ)))(Δ::Float64) @ Zygote ~/.julia/packages/Zygote/FPUm3/src/compiler/interface2.jl:0 [28] (::Zygote.var"#57#58"{typeof(∂(λ))})(Δ::Float64) @ Zygote ~/.julia/packages/Zygote/FPUm3/src/compiler/interface.jl:41 [29] gradient(f::Function, args::Vector{Float64}) @ Zygote ~/.julia/packages/Zygote/FPUm3/src/compiler/interface.jl:76 [30] compute_sense(p::Vector{Float64}, alg::RKMilGeneral{Fourier, Nothing}, sensealg::ForwardDiffSensitivity{0, nothing}) @ Main ~/LevyArea/Check_AD.jl:28 [31] top-level scope @ ~/LevyArea/Check_AD.jl:37 in expression starting at ~/LevyArea/Check_AD.jl:37

fkastner commented 2 years ago

I must admit, it's still not clear to me whether it makes sense to differentiate through an SDE solver but I relaxed the constraints in 30046f2930c740afc5d105bfabfed5d3ccd13e26 and it seems to work now.

ChrisRackauckas commented 2 years ago

It makes sense to differentiate through the SDE solver for parameter estimation purposes. Though I'm not sure the Levy area calculation needs to be differentiated for the same reason why dropping the RNG on the randn works (something @frankschae and I are making much more precise in an upcoming paper BTW), so the right answer may be to drop duals before hitting this code. But anyways, since it can be supported that's good.