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 per period), on a coupon date
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 in periods, which includes the accrued interest the seller has earned since the last coupon, . The quoted clean price is the dirty price less the accrued interest. Accrued interest is exact; the dirty price has the factor 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,
and modified duration is the percentage fall in price per unit rise in yield: . Convexity is the second-order term, , 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, cashflows | the 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 } |
@ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.