title: Stochastic differential equation sensitivity analysis with specified noise
body: <p>I am trying to calculate the gradient of a functional of a stochastic differential equation (SDE) solution given a specific realization of the noise. I can successfully calculate these gradients if I leave the noise unspecified, as shown in <a href="https://github.com/JuliaDiffEq/DiffEqFlux.jl#using-other-differential-equations" rel="nofollow noreferrer">DiffEqFlux.jl: Using Other Differential Equations</a>. I can also successfully obtain the solution to my SDE for a specific noise realization, like shown in <a href="https://diffeq.sciml.ai/dev/features/noise_process/#NoiseWrapper-Example-1" rel="nofollow noreferrer">DifferentialEquations.jl: NoiseWrapper Example</a>. When I try and put the two together, though, the code returns an error.</p>
  <p>Here is a minimal working example adapted from the two separate examples referenced above:</p>
  <pre><code>using StochasticDiffEq, DiffEqBase, DiffEqNoiseProcess, DiffEqSensitivity, Zygote
  
  function lotka_volterra(du,u,p,t)
    x, y = u
    α, β, δ, γ = p
    du[1] = dx = α*x - β*x*y
    du[2] = dy = -δ*y + γ*x*y
  end
  function lotka_volterra_noise(du,u,p,t)
    du[1] = 0.1u[1]
    du[2] = 0.1u[2]
  end
  dt = 1//2^(4)
  u0 = [1.0,1.0]
  p = [2.2, 1.0, 2.0, 0.4]
  prob1 = SDEProblem(lotka_volterra,lotka_volterra_noise,u0,(0.0,10.0),p)
  sol1 = solve(prob1,EM(),dt=dt,save_noise=true)
  
  W2 = NoiseWrapper(sol1.W)
  prob2 = SDEProblem(lotka_volterra,lotka_volterra_noise,u0,(0.0,10.0),p,noise=W2)
  sol2 = solve(prob2,EM(),dt=dt)
  
  function predict_sde1(p)
    Array(concrete_solve(remake(prob1,p=p),EM(),dt=dt,sensealg=ForwardDiffSensitivity(),saveat=0.1))
  end
  loss_sde1(p) = sum(abs2,x-1 for x in predict_sde1(p))
  
  loss_sde1(p)
  
  # This gradient is successfully calculated
  Zygote.gradient(loss_sde1,p)
  
  function predict_sde2(p)
    W2 = NoiseWrapper(sol1.W)
    Array(concrete_solve(remake(prob2,p=p,noise=W2),EM(),dt=dt,sensealg=ForwardDiffSensitivity(),saveat=0.1))
  end
  loss_sde2(p) = sum(abs2,x-1 for x in predict_sde2(p))
  
  # This loss is successfully calculated
  loss_sde2(p)
  
  # This gradient calculation raises and error
  Zygote.gradient(loss_sde2,p)
  </code></pre>
  <p>The error I get at the end of running this code is</p>
  <pre><code>TypeError: in setfield!, expected Float64, got ForwardDiff.Dual{Nothing,Float64,4}
  
  Stacktrace:
   [1] setproperty! at ./Base.jl:21 [inlined]
  ...
  </code></pre>
  <p>followed by an interminable conclusion to the stacktrace (I can post it if you think it would be helpful, but since it's longer than the rest of this question I'd rather not clutter things up off the bat).</p>
  <p>Is calculating gradients for SDE problems with specified noise realizations not currently supported, or am I just not making the appropriate function calls? I could easily believe the latter, since it was a bit of a struggle just to get to the point where the working parts of the above code worked, but I couldn't find any clue as to what I had incorrectly supplied after stepping through this code with the Juno debugger.</p>