Thursday, May 01, 2014

Python and QuantLib - Bonds

[Previous post in this series]

A hundred years ago equities were the modern derivatives of mass destruction du jour, only traded by financial whizz kids.

Today the global debt market still dwarfs the equity market; but equity has a larger mind share amongst the public, perhaps because fixed income is harder to understand.

In finance, as with the wider world, simplicity and complexity are often two sides of the same coin.

On the face of it, bonds look more complicated than equities.  When you choose to buy a bond there are many terms and conditions which describe when you receive money and how much you'll be paid.  The specificity removes room for surprises.

QuantLib gives us the Schedule function which helps us set out the time table for coupon and principal payment.

bondSchedule = Schedule(issue_date, maturity_date, Period(payment_frequency), calendar, Unadjusted, Unadjusted, DateGeneration.Backward, False)

The FixedRateBond method creates our bond object and sets the rest of the details needed to value the bond.

bond = FixedRateBond(0, face, bondSchedule, [coupon], bondDiscountingTermStructure.dayCounter(), payment_convention, 100, issue_date)

Each payment can be costed out and we can put a price on each cash flow dependent on time.  In the previous option example we set the interest rate to a constant over time.  Now we will create a term structure which increases the cost over time.

We will create a dictionary for zero coupon bond yields by time.

zcQuotes = [ (0.0003, Period(1,Months)), (0.0004, Period(3,Months)) ... (0.0333, Period(25,Years)),(0.0348, Period(30,Years)) ]

and retrieve a term structure object from QuantLib

# Create deposit rate helpers
zcHelpers = [ DepositRateHelper(QuoteHandle(SimpleQuote(r)), tenor, fixing_days, calendar, payment_convention, True, day_counter) for (r,tenor) in zcQuotes ]

# Term structure to be used in discounting bond cash flows
ts = PiecewiseFlatForward(valuation_date, zcHelpers, day_counter)

This laborious setup code hints at the pedantic nature of bonds.

However, remember, stockholders aren't given any specific promises by the issuing company.  A stock holder is told he may or may not be given dividend payments at some time in future.  That's it.

How would you go about modelling that effectively? (Rhetorical question!)

A bond's fine grained detail gives you a much richer set of analytical tools than in the equity world and far better accuracy to boot.

The analytical depth available is result of the superficial complexity and underlying simplicity.

The upshot is precious nuggets of actionable insight; which you can derive handily enough from QuantLib.

Download a working example of the code here.

No comments: