How it works


  • Let's say our model has NN parameters.

  • The set of allowable parameter values is denoted ΘRN\Theta \subseteq \mathbb{R}^N. For instance, maybe parameter values must be positive, in which case Θ\Theta is the positive orthant.

  • Any particular model configuration can be defined by a vector of NN parameters: θΘ\theta \in \Theta.

  • We denote the cost(/loss) function as

C:ΘR. C: \Theta \to \mathbb{R}.

C(θ)C(\theta) tells us 'how badly' the model behaves when using the parameter vector θ\theta. The smaller C(θ)C(\theta) is, the better the model behaves.

Background: model-fitting

Let's quickly recap the problem of model-fitting (optimising the model), and define some notation for subsequent use.


(bracketed blocks are optional. Ideal outputs could be experimental data)

But we can abstract away fixed blocks to get our cost function:


  • When we make a model of a system, we want it recreate some desired behaviour(s).

  • To do so, we make a cost (also called loss) function, that takes in model behaviour, and spits out how bad it is. Larger loss <=> less desirable model behaviour.

  • Of course, the modeller has to define the measure of bad behaviour. For instance, squared deviation from data the model is supposed to fit (is used too much).

By the way

  • Most systems one would model in Biology and Engineering (my interests) are input-output systems. You turn the steering wheel, the car veers. You inject current, the neuron fires. You add a cytokine, the cell's metabolic system responds.

  • Therefore reasonable cost functions should penalise bad model behaviour for multiple inputs. I want my car to turn left when I steer left. I also want my car to turn right when I steer right.

  • Don't conveniently forget this to make the modelling/analysis easier. Too many biological models do. I'm also guilty. MinimallyDisruptiveCurves.jl allows you easily sum multiple cost functions (one for each input). It evaluates them in a multithreaded way, so that performance doesn't take such a hit.


Model fitting (also known as training, optimisation, calibration, ...) is running:

θ=argminθC(θ) \theta^* = \arg\min_\theta C(\theta)

subject to θΘ\theta \in \Theta.

  • Since C(θ)C(\theta) is a scalar function (returns a scalar), we can think of it as a landscape, where height is analogous to C(θ)C(\theta), and lateral co-ordinates are the parameters θ\theta of the problem. Then model fitting is just finding a path to the deepest point of the landscape! (see below)

This gif was taken from an Intro to Optimization blog post, website here

What are we trying to do

Recall the goal. Given a fitted model, we want to:

Find functional relationships between model parameters that best preserve model behaviour.

To do this, our curve generator wants to

get as far away as possible from the initial parameters, while keeping the cost function as low as possible.

By the way

  • Optimisation consists of descending to the lowest point of the cost landscape (without worrying about your particular trajectory).

  • We, on the other hand, are trying to flow along the valleys of the landscape. Like a river. But one that has enough energy to go uphill to a specified elevation (the momentum parameter) if it can't find a flat/downhill direction.

Let's express this mathematically. We can define a curve on parameter space as a function:

γ:[0,F]Θ. \gamma: [0, F] \to \Theta.
  • The input s[0,F]s \in [0, F] to the curve is a scalar number, that specifies 'how far along' the curve we are. So γ(0)\gamma(0) is the initial point, and γ(F)\gamma(F) is the final point.

Any point γ(s)\gamma(s) on the curve has a derivative, γ(s)\gamma'(s). This is the direction in which it is pointing (i.e. the direction of the tangent). Let's fix

  • γ(0)=θ\gamma(0) = \theta^*, the locally optimal parameter vector gained from model fitting.

  • γ(0)\gamma'(0), the initial starting direction of the curve, as a user-defined parameter.

We will denote the set of allowable curves γ\gamma (i.e. those satisfying the above constraints), as Γ\Gamma.

By the way

We are going to iteratively add constraints to the set of allowable curves, and thus shrink Γ\Gamma.

Now we want all points on our curve to define low-cost parameter configurations. In other words, we want C(γ(s))C(\gamma(s)) to be small for any s[0,F]s \in [0, F]. How can we distinguish between 'good' and 'bad' curves? How about this:

J[γ]=γC[γ(s)] dsJ[\gamma] = \int_{\gamma} C[\gamma(s)] \ ds

JJ is the line integral of the cost CC along the curve γ\gamma. If J[γ]J[\gamma] is small, then CC is necessarily small along the entire curve.

By the way

Vector calculus 101: we can expand out the line integral as

γC[γ(s)] ds=0FC[γ(s)]γ(s)2 ds \int_{\gamma} C[\gamma(s)] \ ds = \int_0^F C[\gamma(s)] \| \gamma(s) \|_2 \ ds

So now the best curve would be the solution of

minγΓJ[γ]. \min_{\gamma \in \Gamma} J[\gamma].

But wait!

What's preventing the curve from just buzzing around γ(0)=θ\gamma(0) = \theta^*. Since we know that C(θ)C(\theta^*) is as low as can be?

Absolutely nothing!

So let's add a constraint that the direction of the curve must always be pointing away from θ\theta^*. Mathematically:

ddsγ(s)θ2>0,s[0,F]. \frac{d}{ds} \| \gamma(s) - \theta^* \|_2 > 0, \qquad \forall s \in [0, F].

But wait again!

What if the curve 'doesn't want to evolve' because every direction increases the cost. So it just...stays short.

Absolutely nothing again!

We want a curve that actually changes the parameters appreciably. So let's constrain the length of the curve, by setting how fast it moves as a function of ss:

γ(s)2=1s[0,F]. \| \gamma'(s) \|_2 = 1 \qquad \forall s \in [0, F].

Now the curve has to have length FF.

So to wrap up, our allowable curves are:

Γ={γ:[0,F]Θ:ddsγ(s)θ2>0;γ(s)2=1;γ(0)=θ;γ(0)=δθ}, \Gamma = \Big\{\gamma:[0,F] \to \Theta: \\ \frac{d}{ds} \| \gamma(s) - \theta^* \|_2 > 0; \\ \| \gamma'(s) \|_2 = 1; \\ \gamma(0) = \theta^*; \\ \gamma'(0) = \delta \theta^* \Big\},

for some fixed initial direction δθRN\delta \theta^* \in \mathbb{R}^N. Our mathematical problem is

minγΓJ[γ],whereJ[γ]=γC[γ(s)] ds. \min_{\gamma \in \Gamma} J[\gamma], \text{where} \\ J[\gamma] = \int_{\gamma} C[\gamma(s)] \ ds.

How we do it

If equation (10) reminds you of a variational calculus problem, you would be right! Now we just have to solve the Euler-Lagrange equations, right?

Wrong. It's too hard! Try it yourself if you want. I'm not going to explain why ... lots of potential curves, not so many constraints.

So instead of searching directly over curves, we are going to turn this into a dynamic problem. Stop thinking about curves as static shapes. Start thinking about curves as things that are drawn over time.

This turns it into an optimal control problem:

  1. Imagine a rocket sitting at θ\theta^*

  2. It needs to traverse a trajectory in parameter space

  3. At each point in time, we send it a control input, that determines its current direction (i.e. we steer the rocket).

  4. Problem: Design a steering law so that the trajectory traced by the rocket is a (locally) optimal solution of equation (10).

By the way

  • The rocket analogy is there for a reason. Optimal control theory got going in the 1950s, when the USSR and the USA suddenly got interested in designing aircraft/rocket trajectories that were optimal for doing various military things. No prizes for guessing why.

  • Here's a history

  • Two solutions emerged, separated by the iron curtain. Pontryagin's maximum principle, and Bellman's principle of optimality.

  • Using one of these methods, we shall seize the means of curve generation, Comrade!

Let's mathematically express equation (10) as an optimal control problem:

  • At each time tt, we choose an input u(t)u(t). The entire trajectory, from t=0t = 0 to t=Ft=F, we denote uu. The space of possible trajectories is U\mathcal{U}. So uUu \in \mathcal{U}.

  • The dynamics of the 'rocket' are θ(t)=u(t)\theta'(t) = u(t) (i.e. we directly control rocket velocity).

  • By transcribing the constraints on γ(s)\gamma'(s) in the previous section to constraints on u(s)u(s), we define the space of possible input trajectories as

U={u:u(t)2=1;u(t),γ(t)θ0;u(0)=δθ;t[0,F]} \mathcal{U} = \Bigg\{u: \|u(t)\|_2 = 1; \langle u(t), \gamma(t) - \theta^* \rangle \geq 0; \\ u(0) = \delta \theta^*; \quad \forall t \in [0, F] \Bigg\}
  • Our minimisation problem (10) is now

minuUJ(u),whereJ(u)=γC dγ, and γ(t)=u(t), s[0,F]. \min_{u \in \mathcal{U}} J(u), \text{where} \\ J(u) = \int_\gamma C \ d \gamma, \text{ and } \gamma'(t) = u(t), \ \forall s \in [0,F].
  • You can then apply Pontryagin's maximum principle to get some complicated constraints on u(s)u(s).

  • You can work through these constraints (see P135 of my PhD thesis) and translate them into a differential equation.

By the way

  • Usually, solving the optimal control problem using Pontryagin's maximum principle requires solution of a boundary value problem. These are annoying to solve.

  • Luckily, by tinkering with constraints (see PhD thesis reference), we factor out the boundary constraints, and end up with an explicit ordinary differential equation that defines optimal curve evolution.

Now, we are going to write down the differential equations for curve evolution

  • They are complicated and you should feel free to skip them. Intuition will be provided subsequently.

... no one more thing.

Pontryagin's maximum principle requires dynamics for not only the state of the system (i.e. the position γ(s)\gamma(s) of the rocket) but also the costate of the system. What is this? Something roughly analogous to the momentum of the rocket, but also affected by all the input constraints we have set.

So we get two curves. One for the state γ(s)\gamma(s), and the other for the costate, which we shall denote λ(s)\lambda(s).

So now, the coupled dynamics for state and costate. Angle brackets denote inner (dot) products.

  1. Let H:=C[γ(F)]H := C[\gamma(F)] be the final cost. We set this a priori as a hyperparameter. Curve evolution stops prematurely if C[γ(s)]HC[\gamma(s)] \geq H.

By the way

  • Pre-setting HH is the trick that turns this from a boundary value problem into a (much easier) ODE problem.

  1. Let μ2(s)=C[γ(s)]H2\mu_2(s) = \frac{C[\gamma(s)] - H}{2}.

  2. Define μ1(s)0\mu_1(s) \geq 0, and impose the constraint μ1(s)u(s),γ(s)θ=0\mu_1(s)\langle u(s), \gamma(s) - \theta^* \rangle = 0.

  3. Let λ(0)=(HC[γ(0)])u(0)\lambda(0) = \big(H - C[\gamma(0)]\big)u(0)


λ(s)=μ1(s)u(s)θC[γ(s)] \lambda'(s) = \mu_1(s) u(s) - \nabla_{\theta} C[\gamma(s)] γ(s)=u(s) \gamma'(s) = u(s) u(s)=λ(s)μ1(s)[γ(s)θ]2μ2(s). u(s) = \frac{\lambda(s) - \mu_1(s)[\gamma(s) - \theta^*]}{2 \mu_2(s)}.

Given C[γ(s)]C[\gamma(s)] (the current cost), θC[γ(s)]\nabla_{\theta} C[\gamma(s)] (and it's gradient), we can solve this as a differential equation in the variables γ(s)\gamma(s) and λ(s)\lambda(s).

By the way

  • Get some intuition by setting μ1(s)=0\mu_1(s) = 0 in your head, then reviewing the equations. This occurs when the curve is currently pointing away from θ\theta^*.

  • Then you see that u(s)u(s), the curve direction, is proportional to λ(s)\lambda(s), the costate (\approx momentum).

  • Meanwhile, the costate is integrating the negative cost gradient, θC[γ(s)]-\nabla_{\theta} C[\gamma(s)]

  • Notice the analogy with optimisation algorithms based on gradient descent with momentum, if you're into that kind of thing.


What we are trying to do

We wanted a method to construct curves in parameter space, where

  • Every point on the curve represents a set of model parameters that is as low cost as possible.

  • The curves don't 'double back' on themselves (monotonically increasing distance from origin).

  • The curves have a fixed length FF.

  • The user sets the initial direction of the curve.

This resulted in the optimisation problem of equation (10).

How we do it

To make solution of this variational problem tractable, we turned it into an optimal control problem.

  • We made the analogy of a rocket, with an automatic steering law, traversing parameter space

  • We asked what steering law would result in a rocket trajectory corresponding to a curve satisfying the above requirements.

This resulted in a set of differential equations, that evolve paired 'position' and 'momentum' variables for the rocket. Position represents the point on parameter space (i.e. the curve).

  • Solution of this differential equation requires evaluation of the cost function and it's gradient at every step.