Archive for August, 2010


List Comprehension in F#

List comprehension collectively refers to language features that make it easy to define lists. Often the creation of lists is based on modifications to an existing list (such as filtering it, or transforming it).

Prior to the introduction of Linq in the .NET 3.0 Framework there wasn't any real support for list comprehensions. However, since Linq came along you have all the standard query operators to query existing lists assigning the result to a new list. That all fine and dandy but the creation/initialization of lists (or arrays) was and still is rather limited in C#. There is the Enumerable class that offers the Range() and Repeat() methods but that's not at all overwhelming. Just to summarize, these Enumerable static methods can be used to perform array initialisation using fairly terse syntax as follows:

// array initialisation (for small arrays)
int[] x = Enumerable.Repeat(-1, 10).ToArray();
int[] y = Enumerable.Range(0, 10).ToArray();
int[] perfectSquares = Enumerable.Range(1, 10).Select(f => f*f).ToArray();

However, as many people found out this doesn't run nearly as fast as a simple for-loop to do the same thing. Why? Probably because Linq is iterating over the collection element by element and passing them through a lambda expression, dynamically allocation items to an array as it goes.

In F#, there is an elegant syntax called sequence expressions for generating lists, arrays and sequences (an IEnumerable is called "Seq" in F#). To create a sequence/list/array of consecutive integers you can do the following:

let seqOfLetters = seq {'A' .. 'Z'} // a sequence
let seqOfNumbers = seq {1 .. 10}    // a sequence
let listOfNumbers = [1 .. 10]       // a list
let arrayOfNumbers = [|1 .. 10|]    // an array

Note that the type of sequence generated is dependent on the bracket syntax you use.

To create non-consecutive numeric sequences (it won't work for character sequences) you can introduce a skip parameter as follows:

let seqOfEvenNumbers = seq {2 .. 2 .. 100}    // 2,4,6,8,...

Much like Linq in C#, you can also apply a filter to an existing sequence by using syntax of the following form:

seq { for pattern in container -> expr }

For example:

let listOfSquare = [ for i in 1..10 -> i*i ]

This is equivalent to the following:

let listOfSquare = [ for i in 1..10 do yield i*i ]

Don't get me wrong - I'm a big fan of C#, but there are some things that F# is better at, and list comprehension is one of them.


Interest Rate Swaps 101

What is an IR Swap?
A plain-vanilla interest rate (IR) swap is a bilateral OTC contractual arrangement where the counterparties agree to swap streams of future interest payments based on a specified "notional" principal for a pre-defined period of time. In the "plain vanilla" case this is done using a common currency on a fixed-for-floating basis whereby one party exchanges a stream of fixed interest payments for a stream of floating interest payments that are based on the future movement of a specified floating reference rate like BBSW or LIBOR. Of course, the other party does the reciprocal operation contemporaneously. The interest payments of the swap are based on a notional principal meaning there is no upfront exchange of principal - it wouldn't make any sense since the net exchange would be zero. The start and end dates, the coupon frequency, the notional principal, the day count convention to be used, along with a number of other input parameters are all negotiated up-front.

What Exactly is a Reference Rate?
The reference rate is the published rate that determines the future floating interest payments. Obviously, it is not published until it is known so the floating interest amount is not known until the time it is to be paid. It's the same situation if you have a variable rate home loan - you don't know what that rate will be in 9 months or 2 years time.

One very popular reference rate base is LIBOR, the London Interbank Offered Rate. LIBOR is the interest rate that banks with high credit ratings from ratings agencies (rating of AA- or above) charge each other for short-term financing. LIBOR is set daily by the British Banking Association (BBA) and is considered the benchmark for floating short-term interest rates. Here's an extract of published LIBOR rates taken from the Financial Times on Aug 4.

Notice that LIBOR is quoted in numerous currencies and numerous "tenors" (maturities). In general, the tenor of the reference rate used in an IR swap will be the same as the coupon payment frequency of the fixed leg of the swap.

In Australia, the 90-day Bank Bill Swap rate (BBSW) and the Australian Bank Bill Swap Bid Rate (BBSY) are heavily used reference rates. BBSW is compiled by the Australian Financial Markets Association (AFMA) at 10am every business day based on the average MID-price of market rates supplied by a select number of Australian bank bill market makers. BBSY is calculated similarly except that the average BID-price is used instead of the average MID-price. You can read more about their methodology here. Note that BBSW and BBSY are NOT to be confused with the RBA "cash rate".

The key takeaway is this: no single bank sets these reference rates, they are compiled by independent authorities using a transparent algorithm based on information from numerous financial institutions.

Also note that often the floating rate used by IR swaps is the reference rate plus (or minus) a margin. e.g. LIBOR + 0.5% (50 basis points).

Why Use IR Swaps?
There are many reasons, but put simply... swaps allow an entity to rapidly modify the nature of it's IR exposure from fixed to floating or vice versa. In the context of risk management an IR swap can be used to get greater certainty about future interest payments and lower your effective cost of funding. Having greater certainty about cash flows allows companies to ensure capital adequacy for future obligations.

Note that counterparty risk is still present because the other party may default, and interest rate risk still exists because you could have locked in a higher rate and miss out of favourable floating rate moves.

Working Through An Example
Assume two entities, AAA Corp and BBB Corp, enter into a 5 year fixed-for-floating USD vanilla swap with AAA Corp paying fixed and receiving floating, and BBB Corp doing the opposite.

Typically we say the entity that is paying fixed is "long" the swap, whilst the other entity is short the swap. So here AAA Corp is long and BBB Corp is short the swap.

Furthermore, assume the contract specifies that the fixed rate is 6% semi-annual, the floating interest payments will be pegged to 3-month LIBOR plus 50 basis points, the notional principal is 100 million USD and the floating payment frequency is quarterly.

This implies that AAA Corp will pay 6.0% of $100 million USD on a semi-annual basis (so 300k USD twice a year) to BBB Corp for the next 5 years, whilst BBB Corp will pay, approximately, 1/4 * (3-month LIBOR + 0.5% of $100 million) four times per year. So if the 3-month LIBOR rate is 2.3% on the day the payment amount is calculated (the "reset" date), then BBB Corp is obligated to pay 1/4 * (2.3% + 0.5%) = 0.7% * $100 million = 700k USD to AAA Corp for that period.

In practice, the above fractions are likely to differ depending on the day count convention specified in the contract.

In the next post we'll talk about how to value an IR Swap.


Censored Exploration of Dark Pools

In an ACM journal a little while back was a reprint of a very interesting academic paper addressing the problem of allocating a large order efficiently across a number of "dark pools" (alternate trading exchanges).

The approach the authors use is an exploration-exploitation loop whereby they treat the "demand" at each dark pool as a (censored) random drawing from an unknown distribution in the exploration phase, using modified Kaplan-Meier maximum likelihood estimators in the process, and then employ a greedy algorithm to exploit the current information.

It's a very clever approach and worth a read. You can find the paper here. I'm sure it hasn't gone unnoticed by Hedge funds, investment banks and prop-trading outfits!