Skip to contents

SDEAdjointProblem(sol, sensealg, alg, ...) builds the adjoint of the solution of an SDEProblem() du = f dt + g dW, the backward SDE dlambda = -(f_u' lambda + g_u) dt - (g_u' lambda) o dW with the parameter gradient dgrad = -(f_p' lambda + g_p) dt - (g_p' lambda) o dW, driven by the noise path of the forward solution. RODEAdjointProblem() builds the adjoint of an RODEProblem() du = f(u, p, t, W) dt, the random ODE dlambda = -(f_u' lambda + g_u) dt with the forward noise values. InterpolatingAdjoint() reads the forward state from the saved solution; BacksolveAdjoint() solves it backward with the adjoint (with checkpointing, resetting it to the saved values at the saved times). With an Ito backward solve (the default after an Ito forward solve) the drift carries the Ito correction sum_jl q_jl D_{g_l} B_j of the adjoint diffusion B along the noise columns g_l, with q = dW dW' / dt of each forward step, so that both methods converge to the pathwise derivative; a Stratonovich backward solve uses the Stratonovich drift f_I - c / 2, c = sum_j D_{g_j} g_j (corfunc_analytical when given). Julia's adjoints leave out this correction.

Usage

SDEAdjointProblem(
  sol,
  sensealg = InterpolatingAdjoint(),
  alg = NULL,
  t = NULL,
  dgdu_discrete = NULL,
  dgdp_discrete = NULL,
  dgdu_continuous = NULL,
  dgdp_continuous = NULL,
  g = NULL,
  checkpoints = NULL,
  callback = NULL,
  corfunc_analytical = NULL,
  diffusion_jac = NULL,
  diffusion_paramjac = NULL,
  no_start = FALSE
)

RODEAdjointProblem(
  sol,
  sensealg = InterpolatingAdjoint(),
  alg = NULL,
  t = NULL,
  dgdu_discrete = NULL,
  dgdp_discrete = NULL,
  dgdu_continuous = NULL,
  dgdp_continuous = NULL,
  g = NULL,
  checkpoints = NULL,
  callback = NULL,
  no_start = FALSE
)

Arguments

sol

The solution of an SDEProblem or an RODEProblem.

sensealg

InterpolatingAdjoint() or BacksolveAdjoint(); noisemixing = TRUE uses the full Jacobian of diagonal noise.

alg

The algorithm of the adjoint solve; the forward algorithm when NULL.

t

Times of the discrete cost.

dgdu_discrete, dgdp_discrete

Functions (u, p, t, i) returning the gradient of the ith discrete cost term.

dgdu_continuous, dgdp_continuous

Functions (u, p, t) returning the gradients of the continuous cost.

g

The continuous cost g(u, p, t).

checkpoints

Unused (the saved times are the checkpoints).

callback

Must be NULL.

corfunc_analytical

The Ito correction sigma_u' sigma as a function (u, p, t).

diffusion_jac, diffusion_paramjac

Jacobians of vec(g(u, p, t)) by u and by p.

no_start

Leave out the discrete cost at the initial time.

Value

An SDEProblem or RODEProblem in the reversed time, with elements alg, dt, tstops and finish (a function of its solution returning list(du0, dp)).

Details

Julia integrates the adjoint backward in time with the reversed noise. R's SDE and RODE solvers integrate forward in time, so the returned problem is posed in the reversed time tau = t0 + T - t, which runs from t0 to T, with the negated drift and the forward noise read backward: the steps are those of the backward solve. Solve it with fixed steps on the forward grid (dt and tstops of the problem, which adjoint_sensitivities() passes). The derivatives are central differences (Julia: automatic differentiation); diffusion_jac(u, p, t) and diffusion_paramjac(u, p, t) give the Jacobians of vec(g). The continuous cost enters the drift only (Julia's diffusion also subtracts its gradient), and InterpolatingAdjoint() samples a discrete cost with the forward state at its time (Julia: the state of the last drift evaluation, one step later for EM() and RandomEM()). Events of the forward solve are supported for discrete callbacks; a nonsingular constant mass matrix is removed by M^-1 (singular ones are not supported).

Examples

f <- function(u, p, t) p[1] * u
g <- function(u, p, t) p[2] * u
W <- c(0, 0.3, 0.1, -0.2, 0.25)
prob <- SDEProblem(f, g, 1, c(0, 1), c(1, 0.5), noise = NoiseGrid(seq(0, 1, 0.25), W))
sol <- solve(prob, EulerHeun(), dt = 0.25)
# d u(1) / d(u0, p) along the path
adjoint_sensitivities(sol, EulerHeun(), t = 1, dgdu_discrete = function(u, p, t, i) 1,
                      sensealg = BacksolveAdjoint())
#> $du0
#> [1] 3.015428
#> 
#> $dp
#> [1] 2.8578246 0.5397145
#>