SigmaField

Practice Fixed income

Fit a mean-reverting rate path with an ODE

Medium+25 pointsAbout 30 minutesFixed income

The brief

A central bank has published its projected path for the policy rate, quarter by quarter for three years. It starts at today's 5.25% and falls towards 3%, the bank's estimate of the neutral rate. A rates desk models the policy rate with the mean-reversion equation

drdt=k(θ−r)\frac{dr}{dt} = k(\theta - r)

where rr is the policy rate, θ\theta the neutral rate and kk the speed, per year. The desk wants the speed that fits the projection, how fast the gap closes, and the yield curve the path implies.

Your task

  1. Write rate_path(t, r0, theta, k): the solution with r(0)=r0r(0) = r_0, at a time tt in years or an array of times. Raise ValueError for a negative kk.
  2. Write fit_speed(times, rates, theta): the k≥0k \ge 0 whose path from rates[0] makes the sum of squared differences from rates smallest. Raise ValueError if the lists differ in length or hold fewer than two points. Raise it too if the times do not start at 0 and increase, or if the path starts at theta.
  3. Write half_life(k), the time for the gap r−θr - \theta to halve, and time_to_within(gap, r0, theta, k). The second is the first time at which ∣r−θ∣≤|r - \theta| \le gap, and 0 if that already holds. Both raise ValueError if kk is not positive, and time_to_within if gap is not positive.
  4. Write zero_yield(T, r0, theta, k): the continuous zero-coupon yield to maturity TT when the short rate follows the path. At T=0T = 0 it is r0r_0. Raise ValueError for a negative TT or kk.

The data

TIMES holds the 13 quarterly dates in years from today, and PROJECTION the bank's projected rates as decimals, published to 0.05 percentage points. NEUTRAL is the bank's neutral rate. The projection is synthetic.

Hint: Where to start

Lesson 6.1.1 solves y′=k(M−y)y' = k(M - y) by separating the variables: y=M+(y0−M)e−kty = M + (y_0 - M)e^{-kt}. Lesson 6.1.2 reaches the same answer with an integrating factor.

Hint: Fitting the speed

With θ\theta and r0r_0 fixed, the path depends on kk alone, so the fit is a search in one variable. Case study 4.4.5 fits a curve by least squares, and warns that minimize stops early when rates are typed as decimals.

Hint: Yields from the path

Case study 6.1.4 prices a zero-coupon bond under a known short rate, and finds that the yield is the average rate, 1T∫0Tr(s) ds\frac{1}{T}\int_0^T r(s)\,ds. The integral of e−kse^{-ks} has a closed form.