Topic

Bonds

A bond is a schedule of coupons and a repayment. Its price at a yield is a sum, its yield at a price is the root of an equation with exactly one solution, and its sensitivity to yield is its duration.

const b = bond({ face: 100, coupon: "5%", frequency: 2, periods: 20 });
b.price("6%");             // { dirty, clean, accrued }: 92.561263…, exact
b.yield(100);              // 1/20: at par the yield is the coupon, exactly
b.yield("95.25");          // an interval that contains the yield
b.duration("6%");          // { macaulay, modified, convexity }, exact

const d = bond({ coupon: "4.5%", settlement: "2027-03-15", maturity: "2032-11-15" });
d.accrued;                 // 3/2: 120 days of a 180-day period, on 30/360

Price

With coupon C per period, redemption R, n periods left and yield y a year compounded f times (so i=y/fi = y/f per period), on a coupon date

P=∑k=1nC(1+i)k+R(1+i)n. P = \sum_{k=1}^{n} \frac{C}{(1+i)^k} + \frac{R}{(1+i)^n}.

This is a fraction when y is, and price returns it exactly. When the yield equals the coupon rate the price is the face value; above it the bond sells at a discount, below at a premium.

Between coupon dates

Give settlement and maturity instead of periods. The coupon dates are counted back from maturity, and the fraction w of the current period still to run comes from the day-count convention. The buyer pays the dirty price, every cash flow discounted for its real distance w,w+1,…w, w+1, \dots in periods, which includes the accrued interest the seller has earned since the last coupon, C(1−w)C(1-w). The quoted clean price is the dirty price less the accrued interest. Accrued interest is exact; the dirty price has the factor (1+i)−w(1+i)^{-w} with a fractional exponent, so it is an interval.

Yield to maturity

The yield at which the price formula gives the market price. Every cash flow is positive, so the price falls steadily as the yield rises and there is exactly one solution. On a coupon date it is a root of a polynomial and is returned exactly when rational; otherwise it is bracketed by bisection on exact values and returned as an interval of the width you ask for.

Duration and convexity

Macaulay duration is the average time of the cash flows weighted by their present values,

D=1P∑ktk ck(1+i)ftk, D = \frac{1}{P}\sum_k t_k\,\frac{c_k}{(1+i)^{f t_k}},

and modified duration D/(1+i)D/(1+i) is the percentage fall in price per unit rise in yield: dP/P≈−Dmod dy\mathrm dP/P \approx -D_{\text{mod}}\,\mathrm dy. Convexity is the second-order term, 12C (dy)2\tfrac12 C\,(\mathrm dy)^2, always in the holder's favour. A zero-coupon bond's duration is its life; coupons shorten it. All three are ratios in which the fractional-period factor cancels, so they are exact even between coupon dates.

member of bond({ face, coupon, frequency, redemption, periods | settlement, maturity, dayCount })value
couponAmount, periods, accrued, accruedDays, first, cashflowsthe schedule; first is the fraction of a period to the next coupon
price(yield, { digits }){ dirty, clean, accrued }
yield(cleanPrice, { digits })Rational, or an interval
duration(yield){ macaulay, modified, convexity, percentPerPoint }
Try itEdit the code and run it with @ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs