Lab 2 Details for MPCS 51050

Each lab will consist of a small problem and details of  how to proceed. You need to submit labs to the TAs for grading--see submission instructions below.  Generally, unless otherwise specified, you will have one week to complete each assigned lab.

See the syllabus for information on grading.  Turning in lab assignments is required.  Submit your assignments as a tarball to the subversion repository according to the directions on the syllabus page.

You may write these solutions in either Java or C++...your choice.

Lab 2   Due: 5:00 pm, Tuesday, April 22, 2014

Problem 1 (Composite & Strategy Patterns): 

Consider a rudimentary fixed income pricing system.  The system is made up of Accounts.  Each account may contain both individual bonds and portfolios for clients.  The individual bonds in the account are traded as well as the bonds in the portfolios.

As part of the fixed-income analytics, traders will often investigate present value, price, yield and related measures.  One of the related measures is known as the Macaulay Duration.

The derivation of the formula for calculating Macaulay Duration is given as follows: 

The slope of the price/yield curve for a coupon-bearing bond is:


where:
            P is the principal paid at expiration T
           V is the present value of all cash payments until maturity
           N the number of coupons
            y is the constant interest rate (or yield, assuming continuous compounding)
           Ci the coupon paid on date ti (less or equal to T)

The price/yield curve, when divided by the negative inverse of the Present Value at time t gives us:


which gives us the Macaulay Duration (MD).  The concept of duration has its origin back in 1938 with Frederick Macaulay.  We are often interested in the sensitivity of bonds to the movement of underlying factors.  When we are dealing with smaller modulations in yield, duration gives a good first-order approximation of the change in value relative to a change in yield.  Duration is expressed in years and takes into consideration the timing of the cash flows and the size of all the cash flows.  Duration doesn't measure return, but rather, the sensitivity of a bond to a change in underlying interest rates.  We use duration to gauge how much specific bonds will increase or decrease in price if interest rates change...that is to say, it allows us to evaluate how much interest rate risk you are taking on when a new bond is bought and added to a portfolio. 

For our purposes for this lab, we can use the discrete version of the formula, yielding a slightly simplified computation:


where:
            m is the number of coupons
            k the coupon frequency (no fractional payment periods)
            y is the yield per year, in decimal form, compounded at every coupon period
            c the coupon paid per year as a percentage of the principal

For example, let us assume bond μ to be a $100 2-year bond compounded semi-annually with a 5% yield with a coupon frequency of 2 (payments/year), and a coupon of 18% per year.  In this scenario, y = .05, k = 2, c = 18, and m would equal 4 (k * years).  Using the simplified formula above, the Macaulay Duration for bond μ would be 1.7917624. 

As another example, let us assume bond φ is a $100 5-year bond compounded semi-annually with a 4% yield with a coupon frequency of 2 (payments/year), and a coupon of 20% per year.  In this scenario, y = .04, k = 2, c = 20, and m would equal 10 (k * years).  Using the simplified formula above, the Macaulay Duration for this bond φ would be 3.7813337.

In short, Macaulay Duration is the weighted average time until all cash flows are received, measured in years.  Another measure of duration that is often used is known as Modified Duration.  Modified Duration is the measure of the percentage change in price for a given change in yield.  For smaller movements in yield, the numeric results of Macualay and Modified Duration are similar; however, Modified Duration, instead of being a measure of time, is a measure of value, specifically, the rate of change of price sensitivity with respect to the percentage rate of change of price with respect to yield.

The formula for Modified Duration is:


where, in the case of continuously compounded yields:



In the continuous setting, the formula for modified duration is the same as the formula for Macaulay duration.  However, for our purposes for this lab, we will use the discrete formula (again yielding a slightly simplified computation):



where:
            MD is the Macaulay Duration calculated for the same bond
            y is the Yield (per year, in decimal form)
            k is the integer compounding frequency

What you need to implement:

You are to design the bond class and you should incorporate for each bond the number of coupons, the coupon frequency, the yield (per year in decimal form) and the coupon paid per year in percent form (i.e., m, k, y, and c above), along with the face value of the bond (a "$100,000 bond").  Your portfolio is a composite and should be instantiated with a minimum of five individual bonds with different yields and number of years, assuming continuous compounding.  You should have two strategies for the lab, one that implements a calculation of Macaulay Duration across all the bonds in the portoflio, and another strategy that implements a calculation of Modified Duration across all the bonds in the portfolio.  Your program should instantiate the portofolio of bonds, iterate through the portfolio and print out the details of all the bonds in the portfolio, and finally print out both the Macaulay and Modified durations of the portfolio using the two strategies.  In order to print out the portfolio durations, you will need to perform a weighted sum of the durations of each individual bond in the portfolio.

In order to do this, you may assume a simple weighted average of the individual bond duration calculations and call it the Macaulay Duration for the portfolio and Modified Duration for the portfolio, respectively.  For the weighting, assume a portfolio's duration is equal to the weighted average of the durations of the bonds in the portfolio:

Portfolio Duration = w1D1 + w2D2 ...+ wnDn

This standard definition defines the weight as proportional to how much of the portfolio consists of a certain bond, or the percentage market value of an individual bond as a percentage of the overall market value of the portfolio.  In order to calculate the Present Value (PV) of a bond:


where:

C is the coupon rate of the bond
F is the face value of the bond
r is the current prevailing interest rate (you will assume r = 2.5% for all bonds in all portfolios, and may hardcode it as such)
t is the time periods occuring over the term of the bond (e.g., a two-year semi-annual bond will have four time periods, etc.)

In short, the bond's current market value is equal to the face value plus the present value of interest payments.  For example, let us assume bond κ to be a $100,000 10 year bond compounded annually (a coupon frequency of 1 payment/year), and a coupon of 8% per year.  The prevailing market interest rate we will assume is 2.5%.   In this scenario, F = 100,000, C = .08, r is .025, and t is 10 (years).  Using the formula above, the present value of bond κ would be $148,136.  As another example, assume bond γ to be a $10,000 5 year bond compounded semi-annually (a coupon frequency of 2 payment/year), and a coupon of 8% per year.  The prevailing market interest rate we will assume is 2.5%.   In this scenario, F = 10,000, C = .04 (=8% / 2 periods/year), r is .025, and t is 10 (=5 years * 2 periods/year).  The present value of bond γ would be $11,313.

You are to deliver in code a single account that contains five portfolios each of which contains five bond instruments.  The account is also to contain five individual bonds that are not attached to a given portfolio.  You are to implement a Composite pattern for the account with strategies for duration calculations on the individual bonds and portoflios.  You should write an Iterator for your traversal of the Composite.  Please note that you may not use language-specific classes for your composite, strategy or iterator.  That is to say, you may not use structures such as STL lists, vectors, deques, maps, etc., nor can you use STL iterators.  You may not use Java Collections or Iterators.  You are expected to "roll your own" composite, strategy and iterator.  Having said that, you certainly do not need to create expansive implementations of collections and iterators beyond what is the bare minimum to support the above requirements.

Your program should print out something like the following:

$ ./mycomposite
I am a composite and I have 5 portfolios and 5 individual bonds. 
The durations of the five individual bonds are:
bondname1 MD is X and ModD is Y
bondname2 MD is A and ModD is B
etc.
I also have five portfolios and the durations of the portfolios are as follows:
portfolioname1 MD is X and MODD is Y
portfolioname2 MD is A and MODD is B
etc.

Your exact output does not need to match the above but the above will give you a gist of what we're wanting it to do.

Submitting:

Submit your assignments in a folder called lab2 to the subversion repository.  Place all of your  Lab 2 work into one folder. You will only be graded on your material within a reasonably labeled folder.
Also, please include a README text file that contains any instructions for the TAs to assist with grading, such as how to compile, run, or even understand your code.