Friday, May 02, 2014

Learning QuantLib with Python - Implied Volatility

[Previous post]

However much finance would like to disown its father discipline; it very much belongs to the economics world which in turn boils down to sociology for nerds.

Economics is about understanding people and how they make decisions and financial models are fairytales about how people operate in the land of markets.  They are not very accurate.

What happens when people do something unexpected or 'irrational'?  A financial engineer will calibrate her financial model to reality.

There are two financial 'laws' which hold pretty consistently (apart from death and taxes) but a apart from those everything else is a metaphor and a search for intuition rather than steadfast knowledge about how the financial world operates.

Implied volatility is a fudge factor which we add to option pricing models as the rubber hits the road (volatility is a useful fudge because it's not directly measurable; no one can readily refute it!).

Incidentally there is another unknown.  Future dividend yield which you can find estimated at

QuantLib makes finding the implied volatility of an option very easy.

Continuing from where we left off with our previous option example.  Add a line to solve for implied volatility using its market value like so

implied_volatility_rate = option.impliedVolatility(market_value, process)

and simply set our volatility to the rate we just found (OO for the win!)

Runnable code can be downloaded here.

Assuming the model is correct (big assumption) we now have a nicely capsulated way to understand our American option and understand how we will be impacted by all manner of possible bumps in the road ahead.

Financial models are inaccurate.  In this case it is obvious by how they inconsistently report implied volatilities on the same stock for the same expiry (i.e. the implied volatility smile).

In contrast to the constant nature of laws, financial models are capricious friends.

Nevertheless approximate intuition is far better than nothing.

[Next post in series]

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.