Skip to contents

adjoint_sensitivities(sol, alg, ...) returns the gradient of a cost functional of the solution with respect to the initial values and the parameters, by solving the adjoint equation backward in time: G = int g(u, p, t) dt (continuous cost g, or its gradients dgdu_continuous and dgdp_continuous) plus sum_i g_i(u(t_i)) at the times t (discrete cost, given by its gradients dgdu_discrete(u, p, t, i) and optionally dgdp_discrete(u, p, t, i)). Gradients that are not given are taken by finite differences of g.

Usage

adjoint_sensitivities(
  sol,
  alg = NULL,
  t = NULL,
  dgdu_discrete = NULL,
  dgdp_discrete = NULL,
  dgdu_continuous = NULL,
  dgdp_continuous = NULL,
  g = NULL,
  abstol = 1e-06,
  reltol = 0.001,
  checkpoints = NULL,
  callback = NULL,
  no_start = FALSE,
  sensealg = NULL,
  dgdu = NULL,
  dgdp = NULL,
  dgdt_discrete = NULL,
  ...
)

ODEAdjointProblem(
  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,
  abstol = 1e-06,
  reltol = 0.001,
  dgdt_discrete = NULL
)

Arguments

sol

A solution of an ODEProblem (or a nonlinear or steady state solution, see SteadyStateAdjoint()).

alg

ODE algorithm of the backward 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).

abstol, reltol

Tolerances of the backward solve.

checkpoints

Saved times used by checkpointing methods (default sol$t).

callback

Further callbacks for the backward solve.

no_start

Leave out the discrete cost at the initial time.

sensealg

An adjoint method, InterpolatingAdjoint() by default (or the sensealg the solution was computed with).

dgdu, dgdp

For steady state and nonlinear solutions: functions (u, p) returning the gradients of the cost g(u, p) (or give g).

dgdt_discrete

For ODEs with continuous events: a function (t) giving the derivative of the cost with respect to the time of the samples saved at the event time t, which move with the event (the cotangent of sol$t), or NULL.

...

Options for the backward solve().

Value

A list with du0 and dp.

Details

The forward solution must be dense (solve without saveat, with dense = TRUE, or with sensealg = ...) unless the method uses checkpointing. Problems with events must be solved with solve(prob, alg, sensealg = ...), which records the events so they are differentiated: the adjoint jumps at each event by the transposed linearization of the event, including the dependence of the event time on the state and parameters, and changes of parameters by the event. Mass matrix (semi-explicit index

  1. DAEs are supported by the interpolating, quadrature and Gauss methods. For nonlinear and steady state solutions, see SteadyStateAdjoint(); for optimization solutions, OptimizationAdjoint().

ODEAdjointProblem() returns the backward problem without solving it; its element finish turns the backward solution into the gradients.

The result follows Julia: dp is the gradient with respect to the problem parameters with the initial values held fixed. When the initial values depend on the parameters through initialization, solve_gradient() adds that dependence.

Examples

prob <- ODEProblem(function(u, p, t) -p[1] * u, 1, c(0, 1), 2)
sol <- solve(prob, Tsit5(), reltol = 1e-10, abstol = 1e-12)
# G = u(1): dG/du0 = exp(-2), dG/dp = -exp(-2)
res <- adjoint_sensitivities(sol, Tsit5(), t = 1, dgdu_discrete = function(u, p, t, i) 1,
                             abstol = 1e-10, reltol = 1e-10)
c(res$du0, res$dp) / exp(-2)
#> [1]  1 -1