XML Parsing: Stripping comments from an XML file

Saturday, December 18, 2010


In this post, we will see how to strip comments from an XML file from an UNIX shell through the use of sed command, I am writing this because recently I was developing an XML parser to parse XML content and the problem is that the contents gets parsed irrespective of whether it's commented out or not, therefore the best way to avoid that is to strip comments before parsing the XML, after some research, I found the following sed command which will do the trick.

cat <XML_file> | sed 's/<!--.*-->//' | sed '/<!--/,/-->/d'

The code has two piped sed commands, the first sed command

sed 's/<!--.*-->//' - Matches all single line comments and substitutes them with a null character, I can't use sed delete option here because in a single line when you have uncommented XML content, it will also get deleted along with the comment when you use delete option, to prevent that, use sed subsitutes, the only disadvantage with this approach is that a commented line won't be deleted, but the comments will be.

The second sed command sed '/<!--/,/-->/d' follows the multi-line sed syntax where we match a start pattern and an end pattern and delete all lines from the line in which the start pattern is matched till the line in which the end pattern is matched.

So simple isn't it!

Example: 


<sample>
<!-- Sample XML to show XML comment stripping -->

<tag1>tag1name</tag1> <!-- I am tag 1 -->

<!-- Commenting out tag 2 to tag 3
<tag2>tag2name</tag2>
<tag3>tag3name</tag3>
-->

<tag4>tag4name</tag4>
<!-- This is tag 4 -->
</sample>

$ cat sample.xml | sed 's/<!--.*-->//' | sed '/<!--/,/-->/d'
<sample>


<tag1>tag1name</tag1>


<tag4>tag4name</tag4>

</sample>
$

References:


Investment Science - Bond Portfolio Immunization Example

Tuesday, September 21, 2010

Based on the knowledge we gained from the previous posts on Bond portfolio immunization, it's time to look into a comprehensive example on Bond Portfolio immunization which directly brings me to an interesting problem from Investment Science by Luenberger on bond portfolio immunization - Problem 3.12

Problem 3.12

(Bond Selection) Consider the four bonds having annual payments as shown in the below table, they are traded to produce a 15% yield.
 
 
(a) Determine the price of each bond

We know that for all the bonds, Yield λ = 15%, m = 1


For Bond A, F = 1000, C = 100, n = 3
For Bond B, F = 1000, C = 50, n = 3
For Bond C, F = 1000, C = 0, n = 3 (Zero coupon bond of 3 year duration)
For Bond D, F = 1000, C = 0, n = 1 (Zero coupon bond of 1 year duration)

Based on the Bond price formula discussed in the previous article, we find that,

Price of bond A, PA = $885.832, PB = $771.671, PC = $657.516 and PD = $869.565

(b) Determine the duration of each bond (not the Modified Duration)

From on the Bond duration formula discussed in the previous article, we find that

Duration of bond A, DA = 2.716, DB = 2.836, DC = 3 and DD = 1

(c) Which bond is most sensitive to a change in yield?

From the Bond Priced Sensitivity formula discussed in the previous article, we find that bond C is more sensitive to a change in yield (of course, it's a zero coupon bond with 3 year duration)

(d) Suppose you owe $2000 at the end of 2 years. Concern about interest rate risk suggests that a porfolio consisting of the bonds and the obligation should be immunized, If VA, VB, VC and VD are the total values of bonds purchased of types A, B, C and D, respectively, what are the necessary constraints to implement the immunization? [Hint: There are two equations. (Do not Solve.)]

From the bond portfolio immunization constraints discussed in the previous article, we find that

Present Value of the obligation P = 2000 / (1.15 ^ 2) = $1512.287

If VA , VB , VC and VD represents the value of each bond (which is equal to the price of the bond multiplied by the number of quantities of the bond purchased, see this article)

VA + VB + VC + VD = P = $1512.287
DA * VA + DB * VB + DC * VC + DD * VD = 2 * 1512.287 or

2.716 * VA + 2.836 * VB + 3 * VC + VD = 3024.574

(e) In order to immunize the portfolio, you decide to use bond C and another bond, Which other bond should you choose? Find the amounts (in total value) of each of these to purchase

We know that one of the immunization constraints is that the average duration of the bond portfolio should be greater than or equal to the duration of the obligation, in this case if we select bond D with C, then the average duration (3 + 1) / 2 = 2 matches the duration of the obligation which is exactly 2 years, therefore we select bond C and bond D, then from the bond portfolio immunization constraint in section (d), we have

VC + VD = 1512.287
3 * VC + VD = 3024.574

Solving, we get, VC = VD = $756.14

Therefore, number of shares of bond C = VC / PC = 756.14 / 657.516 = 1.15
Number of shares of bond D = VD / PD = 756.14 / 869.565 = 0.869

Therefore if one buys 1.15 shares of bond C and 0.869 shares of bond D, the bond portfolio will be immunized.

(f) You decided in (e) to use bond C in the immunization. Would other choices, including perhaps a combination of bonds, lead to lower cost?

Obviously not, based on the bond portfolio immunization constraints since the sum of the total value of the bonds should be equal to the present value of the obligation (see section (d)), alternate choice of bonds will not lead to lower cost, but one should make sure that the average duration of the bonds in the portfolio should be greater than or equal to the duration of the obligation.

A big post indeed, if we understand this, we can immunize any bond portfolio and ensure that we will never lose money.

Acknowledgements: Thanks to Prof. Luenberger for posting this problem in his valuable text book on Investment Science and Prof. Charles Feinstein for his lucid explanation of the bond portfolio immunization concept, this is one of the valuable lessons we learned in bond investments.

Investment Science: The Price of a bond and it's duration using excel/spreadsheet

Saturday, September 18, 2010

In this post, we will compute the price of a bond and it's duration using a an excel/spreadsheet, as always I use openoffice for my spreadsheet computations and you can always import the openoffice spreadsheet as a Windows compatible excel file.

For calculating the price of the bond and it's duration, we will take a sample problem from Investment Science by Luenberger (Problem 3.10)

Problem 3.10 Find the price and duration of a 10-year, 8% bond that is trading at a yield of 10%.

Based on the formula for the price of a bond and it's duration as discussed in the previous article, we know that for the above case

The price of the bond P = $83.537
Duration of the bond D = 6.84

The same can be calculated quite easily using the below excel/spreadsheet which can be re-used to calculate the price/duration of any bond.



Investment Science: Bond Price Sensitivity

Tuesday, September 14, 2010

Before looking into a practical example on immunizing a bond portfolio, let's look at an important concept related to bonds, the Bond - price sensitivity, as we know that the price of a bond is affected by changes in yield and in this post we will look into the mathematical formula for computing the change in price of a bond with respect to a given change in yield.

The bond price sensitivity formula is given below


Where

P = Price of the bond
D = Macaulay Duration of the bond
λ = Yield to Maturity (YTM)
m = Number of coupon periods
dP = Change in price
dλ = Change in yield

The above formula can also be written as


Where DM is the modified duration of the bond.


As we said before, zero coupon bonds are more sensitive to changes in yield than the coupon bonds, let's see how the price of the bond gets affected for a small change in yield.

For this example, first we will consider a 30-year, 8% bond that is trading at a yield of 10% with yearly coupon payments.

For this bond, based on the bond price formula discussed in the previous article, we get,

P = $81.146 and D = 10.646 (refer to the duraton formula)

Assume that the yield increases by 1% (dλ = 0.01) in the above scenario in which case based on the above formula for Bond - sensitivity, we have

dP = -1/1.1 * 10.646 * 81.146 * 0.01 = -7.853 or the price reduces by 9.68% (-7.853/81.146) if the yield increases by 1%.

Consider a 10-year, zero coupon bond that is trading at a yield of 10%, for this bond,

P = $5.7308 and D = 30

For a 1% increase in yield (dλ = 0.01), we have

dP = -1/1.1 * 30 * 5.7308 * 0.01 = -1.562 or the price reduces by 27.25% (compared to 9.68% for the coupon bond)

Hence zero coupon bonds of longer duration are more sensitive to changes in yield, with this knowledge, we are all set to explore a real world example on bond portfolio immunization in the next article.

MS&E 242S in Stanford - A Quality course in Investment Science

Saturday, September 11, 2010

If you are following my recent posts, they are related to Investment Science - the recent course I completed in Stanford, just want to write a little about that and also on the importance of understanding about investments.

The MS&E 242S is offered through SCPD (a program which accepts working professionals to study part time there) in Stanford, though initially I was uncomfortable in grasping some of the investment  terminologies, but now I am very keen to write about those because I want to keep a reference of the important concepts of investment science which can be put into practical use.

Let me give you a short summary of what this course is about.

1. What will you learn?
2. Which part of it I liked the most?
3. References to this course
4. Where to enroll to this course

1. What will you learn? Well it's mainly about the scientific principles and practical applications of investment science which will help you to make good investment decisions like deciding among several investment options, understanding different kinds of investments, investment decisions under uncertainty and so on, everything clearly elucidated with classic real world examples

2. Which part of it I liked the most? Well it's everything because I wasn't aware of those concepts before I took this course but what fascinated me the most is the techniques to immunize an investment portfolio (specific to bond investments) against factors that affect the value of the investments over time (like interest rates, yields), ideally that should inspire anyone to research more on the techniques to immunize different kind of investments, because at the end of that day no one wants to lose money....

3. References to this course: One of the best books on this subject is Investment Science by Luenberger, ideally it should be on your bookshelf if you are keen on investment related studies, but it's also a very good read to understand different concepts in investments which you may hear in day to day life like Mortgages, auto loans, long term growth of an investment , modern investment portfolio and so on, you will learn more on solving some problems at the end of each chapter.

4. How to enroll in this course: It's offered in different quarters, but check this page for more details.

Investment Science: Bond Portfolio Immunization

Thursday, September 9, 2010

In this article, we will look into an important concept in bond investments, which is immunizing a bond portfolio, assume that you have a future obligation (or a financial need) to meet with your current bond investments and ensure that the future obligation is met irrespective of market conditions (changes in yield/interest rates), or in other words you want to immunize your bond portfolio against changes in yield.

Immunizing a bond portfolio is something which has practical applications in investment science in that it can be mathematically proven that when you immunize your bond portfolio, you will be protected against changes in yield (whether they go up or down), though we don't need to dig deep into the mathematics of it, in this article we will see the necessary conditions to be met for immunizing your bond portfolio.

It can be easily said that zero coupon bonds are the first choice solution for immunizing without having a portfolio, if you have a financial obligation of F after a duration of D years, you can buy a zero coupon bond with it's present value P which will pay it's face value F after D years, but assume that zero coupon bonds are not available, in which case you need to invest in a bond portfolio to meet your needs.

For the sake of simplicity, assume that there are two bonds available with

Bond 1: Price = P1 , Duration = D1
Bond 2: Price = P2 , Duration = D2

Consider that you have an obligation F (with a present value of P) after D years which you wish to meet by investing in the above two bonds, then the required conditions for bond portfolio immunization are

Step 1: The duration of the obligation D should be such that


(The weighted duration of the bonds should be greater than or equal to the duration of the obligation)

Step 2: Compute the present value of the obligation


Where 'm' is the number of coupon periods in an year, λ is the market yield.

Step 3: Buy 'x' shares of Bond 1 and 'y' shares of bond 2 such that


Therefore based on the above equations, if you have a future obligation F with a present value of P and duration D, you can meet your obligation by investing in 'x' shares of Bond 1 and 'y' shares of Bond 2 subject to the above constraints to protect against changes in yield, the above criteria for bond portfolio immunization can easily be extended to 'n' bonds.

I know it's may not be easy to understand this important concept without an example, we will look into a more practical example on how to immunize a bond portfolio in the next article.

Techtalks: What's this blog about?

Tuesday, September 7, 2010

Yes, you got the question right, a question I wanted to answer for a long time, though in my introduction, I gave a prelude of what this blog is all about, let me give a more in-depth overview in the form of a series of Question/Answers.

1. What's this blog about?
2. What's in it for me?
3. Why should I subscribe to this blog and what's the value?
4. What's your commitment to Techtalks what about the future of this blog?

1. What's this blog about?

As the name say's its all - Techtalks, the articles of this blog will be related to several subjects varying from technology, management, troubleshooting, FAQ, programming, useful code, technical tips, best practices, definitions, problem solving and so on, also I write about some technical stuff which I use frequently in my day-to-day work which serves as a personal reference to me and may be useful for others as well.

2. What's in it for me?

Good question, the chances are that if you landed on to this blog, that means you may have found something which you've been looking for here, be it some useful code or a troubleshooting tip or a reference to an article or some information you've been looking for, and that answers the question, What's in it for me, you've found some value here and that's what we strive to achieve.

3. Why should I subscribe to this blog and what's the value?

First of all thanks for subscribing to this blog, this means you are interested in following the articles (may be in your area of interest) in this blog, in general I write about several stuff which may be useful at some point to someone, in fact I myself refer this blog for at times when I want to refer some solutions, that being said I understand that it's hard for someone to follow all articles in a diverse blog like this focussing on different areas, but the main point is

"You will learn something from your subscription some day"

Take my words, I subscribe to multiple blogs focussing on different areas (the most prominent of them is about.com), but some days I get articles which are of interest to me, when I read those as a subscriber, I feel glad that I subscribed to that particular blog, same thing happens with every other blog, though it's hard to subscribe to many blogs and follow all articles, but sooner than later you may find something which will be of interest to you.

4. What's your commitment to Techtalks what about the future of this blog?

My commitment is "I will post an interesting article to this blog everyday", take this word from me, there are lot's of interesting stuff to write about in different domains and share those with readers and we have plans to expand this blog in future to make it more professional, productive and user friendly for the readers, so there are lots of fun ahead, keep watching and posting your comments.

Last but not the least, I get mails/blog comments from several readers who have their own questions/doubts on some articles, I will to respond to those in my free time, but chances are that the answer you 've been looking for may already be there (typical example is my post related to Socket programming which attracted several readers), also feel free to send constructive feedbacks/corrections to me as and when appropriate.

Setting up an NFS share on Linux

In this post, we will see how to setup an NFS share in linux and access remote directories shared from the NFS server from an NFS client machine.

Assuming that you have an NFS server with an IP 10.5.150.1 and NFS client 10.5.150.2, the steps to share and access the NFS shared directory is as follows.

1. Lets assume that the NFS directory you want to share is /export/NFS-Server/share in the NFS server box.
[root@10.5.150.1]# pwd
/export/NFS-Server/share
[root@10.5.150.1]# ls -l
total 0
-rw-r--r-- 1 root root 0 Sep  6 14:08 nfs_server_file.txt
[root@10.5.150.1]#
2. If you want to grant read/write permissions for this directory, then edit /etc/exports and add the directory entry you want to share with the read/write permissions as shown below.
[root@10.5.150.1]# chmod 777 /export/NFS-Server/share/
[root@10.5.150.1]# cat /etc/exports
# Generated by Allstart on Wed Sep  1 04:53:40 2010
/export/NFS-Server/share   *(rw)
# Put custom additions below (Do not change/remove this line)
[root@10.5.150.1]#
Note: You can additionally configure the /etc/hosts.allow and /etc/hosts.deny for increased security for the NFS share, but those details are outside the scope of this article.

3. Once the directory is shared in the NFS server, restart NFS service for the share to be active.
[root@10.5.150.1]# /etc/init.d/nfs stop
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS quotas:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
[root@10.5.150.1]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
[root@10.5.150.1]#
4. Now the NFS share can be accessed from the client system which can be mounted to the shared directory on the NFS server.

(Assuming that the directory is mounted on /export/NFS-Client/share on the NFS client 10.5.150.2)
[root@10.5.150.2]# pwd
/
[root@10.5.150.2]# mkdir -p /export/NFS-Client/share
[root@10.5.150.2]# ls -l /export/NFS-Client/share/
total 0
[root@10.5.150.2]# mount 10.5.150.1:/export/NFS-Server/share/ /export/NFS-Client/share/
[root@10.5.150.2]# cd /export/NFS-Client/share/
[root@10.5.150.2]# ls -l
total 0
-rw-r--r-- 1 root root 0 Sep  6 17:38 nfs_server_file.txt
[root@10.5.150.2]# touch new-file.txt
[root@10.5.150.2]# ls -l
total 0
-rw-r--r-- 1 65534 65534 0 Sep  6 17:40 new-file.txt
-rw-r--r-- 1 root  root  0 Sep  6 17:38 nfs_server_file.txt
[root@10.5.150.2]# umount -f /export/NFS-Client/share/
[root@10.5.150.2]# ls -l /export/NFS-Client/share/
total 0
[root@10.5.150.2]# 

Investment Science: The price of a bond and it's relationship to the Present Value formula

Monday, September 6, 2010

In this article, we will see the relationship between the price of a bond and the present value (of an investment/annuity) formula, we will take both types of bonds into account, namely the zero-coupon bond and coupon bond.

We know that the price of a bond is given by


where

P = Price of the bond
F = Face value of the bond
C = Coupon payment per year
m = Number of periods in an year the coupon is paid (Typically m = 2 or coupons are paid every 6 months)
n = Number of coupon periods left (with m coupon periods per year)
λ = Yield to maturity

For a zero coupon bond (C = 0), therefore the above formula becomes


We know the relationship between the Present value and the Future value of an investment (compounded at 'm' periods per year at an interest rate 'r' for 'n' years) is given by



Note the similarities, the price of a zero coupon bond is the face value discounted by it's yield to maturity (YTM),  something similar to the above present value/future value relationship.

The zero coupon bonds typically have longer durations and they are more sensitive to changes in yield than the coupon bonds.

For a coupon bond, the price is given by



We know that the Present value (P) of an annuity which pays an amount A every individual period (an year) for 'n' years at an yearly interest rate 'r' is given by


The first term in the price of the coupon bond can be considered as a part which discounts the face value of the bond (similar to the present value/future value relationship as shown above) and the second term can be considered as the part which discounts the coupon payments which occurs every period (till the bond maturity date), similar to the Present value of an annuity formula.

The above similarities will help one easily understand how the price of a bond is derived.

Investment Science: Duration of a bond

Sunday, September 5, 2010


In this post, we will look into the formula for the duration of a bond, in simple terms, the duration of a bond reflects the sensitiveness of the price of the bond with respect to changes in interest rates or duration measures (dP/dλ) (Percentage change in price over changes in yield) of the bond, duration is mentioned in years and in general bonds with longer duration are more sensitive to changes in yield than bonds with shorter durations.

In this article, we will look into the formula for the Macaulay duration of a bond with a simple example on computing the same to get a quick understanding of the duration of a bond, in further posts, we will explore more on the duration of a bond and how it plays an important role when considering a bond portfolio to meet a given obligation.

The Macaulay duration of a bond is given by

Where

m = Number of coupon periods per year
c = Coupon rate per period
y = Yield per period
n = Number of coupon periods left (with m coupon periods per year)

To compute the Macaulay duration of a bond, we will solve a problem from
Investment Science by Luenberger (Problem 3.10)

Problem 3.10 Find the price and duration of a 10-year, 8% bond that is trading at a yield of 10%.

Here

m = 2 (By default, assume that coupon payments happen every 6 months or 2 periods per year)
C = Coupon payment = 8% of 100 = $8 (By default, assume the face value of the bond F = 100)
Coupon rate per period c = 8%/2 = 4% = 0.04
Yield per period y = 10%/2 = 5% = 0.05
Number of coupon periods remaining, n = 10, n * m = 20

The price of the bond is given by (Refer the article on the price and yield of a bond)

P = $87.537

Based on the above formula, the duration of the bond is given by



D = 6.84


Duration of a zero coupon bond:
What will be the duration of a 10-year, zero coupon bond that is trading at a yield of 10%? It is 10 years (Check the formula for additional proof).

How to keep your system working in Windows 7 when your close your laptop lid

Saturday, September 4, 2010

The reason I am writing this post is because of the frustration I had at times when I started some tasks in my laptop (especially the number of ssh sessions I open through Putty in my VPN connection) expecting it to keep running after I go sleep, but to my surprise often when I resume working, I notice the following.

1. The system goes to sleep state.
2. My VPN connection through Internet goes down when I resume working and so on.

Though we can control the system behavior when closing the laptop lid, but I realized that that alone may not suffice if one wants to keep the system working without any slight change in behavior due to system power settings, therefore select the safest option in Power settings when you are away and you need to keep your computer working on for hours and days without any interruptions.

1. Click Start (The bottom leftmost button with Windows symbol in your laptop) -> Control Panel -> Hardware and Sound -> Change what the power buttons do

2. Select the following settings for different options as shown below (see the figure below as well):

2.1 When I press the power button: Select Shut down for both On Battery and Plugged in options
2.2 When I press the sleep button: Select Do nothing for both On Battery and Plugged in options
2.3 When I close the lid: Select Do nothing for both On Battery and Plugged in options


Note: I am not arguing that it's the best option, but it works for me.

Investment Science: Sum of a Geometric series and applications in computing Net Present Value

Thursday, September 2, 2010

In this post, we will see two very simple, yet powerful shortcuts for calculating the Net Present Value (NPV) of a cash flow, though you may have learnt these formulas already, but they are very useful when finding the Net Present Value of a cash flow stream, especially for long cash flow streams spanning multiple periods.

1. Sum of a geometric series from 0 to n (n +1 terms):

Given a constant 'a' and a constant ratio 'r'


Example:

Consider a cash flow stream (40, 40, 40, 40) at an yearly interest rate 10%, the present value of this cash flow stream is given by


Through the above geometric series formula (with a = 40, r = 1/1.1 and n = 4), it's given by


2. Sum of a geometric series from 1 to n: 

Given a constant 'a' and a constant ratio 'r'


Example:

Consider a cash flow stream (-10, 40, 40, 40) at an yearly interest rate 10%, the present value of this cash flow stream is given by


Through the above geometric series formula, we get (add -10 to the sum of geometric series from 1 to n with a = 40, r = 1/1.1 and n = 3)


Thus, the sum of a geometric progression has a practical application when computing the Net Present Value of a cash flow stream.

Investment Science: The price of a bond and it's yield

In the previous post, we saw an introduction about bonds, in this post we will see the formula for the price of a bond, before that we will introduce what is called the yield of a bond.

The Yield to Maturity (YTM) of a bond is the internal rate of return of a bond at it's market price, the price of a bond and it's yield are inversely proportional, if the price goes down, the yield goes up and vice-versa. A par-bond is one where the yield is equal to the interest rate (coupon rate).

Given the above definition, the price of a bond (P) is given by,


where

P = Price of the bond
F = Face value of the bond
C = Coupon payment per year
m = Number of periods in an year the coupon is paid (Typically m = 2 or coupons are paid every 6 months)
n = Number of coupon periods left (with m coupon periods per year)
λ = Yield to maturity

Lets look at an example from
Investment Science by Luenberger to calculate the price of a bond (Problem 3.9).

Problem 3.9

An 8% bond with 18 years to maturity has a yield of 9%. What is the price of this bond.

In this problem, it is implied that the face value of the bond F = $100 (unless specified otherwise).

m = 2 (Unless explicitly specified, coupons are paid every 6 months in an year)
 
λ = 9%, λ/2 = 4.5%
n = 18, n * m = 36
C = 8% or the coupon payment paid per year = 8% * 100 = $8

Substituting the above values in the Bond price formula, we get


P = $91.164


Investment Science: The Basics of investing in Bonds

Tuesday, August 31, 2010

If there is one major thing I learnt from the Investment Science course, it's the investments in bonds and how to immunize your portfolio against interest rates, in this article, we will see the definition of a bond and  the types of bonds with a simple example.

A bond is an investment which pays you a fixed amount of money at it's maturity date and a coupon bond will pay you periodic payments (coupon payments) in addition to the fixed amount of money you will receive at the end of it's maturity.

The price of a bond (P) is the money you pay to buy the bond.
The face value of a bond (F) is the money you will receive at it's maturity date.
The coupon payment (C) is the money you will receive periodically till the maturity date of the bond (based on the coupon rate), this means in a coupon bond, you will receive (C + F) at it's maturity time.

Typically coupons are paid two periods in an year or every 6 months.

As obvious from the above discussion, bonds are classified into two types

1. Coupon bond - Which pay periodic coupons till maturity.

2. Zero coupon bond - Here there are no coupon payments involved, you buy a bond at a price P and get money equal to the face value of the bond (F) at maturity, therefore the Price (P) of a bond is the present value of the bond which pays F in future.

Let me answer some FAQ about bonds (More questions and answers will be added when I get additional information on bonds).

1. Who issues bonds and why? Bonds are issued by Governments, corporations, banks and other institutions whereby they can raise quick capital which can be used for several projects.

2. What if the bond issuer defaults? Typically Government bonds (like the one issued by US treasury) are essentially considered risk-free investments (they never default), however you may need to learn about the Bond credit ratings before buying a bond.

3. What factors influence bond market? The simple answer is interest rate, when you say the bond market is down, it means the interest rates are high and vice-versa, we will learn more about this in a future article.

Let's conclude this section with a small example.

Find the periodic coupon payment (occurs every 6 months or two periods in an year) of a $1000 bond at 5% coupon rate.

Here Coupon payments per year C = 1000 * 5% = $50

Number of periods in an year m = 2

Therefore periodic coupon payment (which occurs every 6 months) = C/m = 50/2 = $25.

Investment Science: Variable rate Mortgage

When you pay a mortgage/loan, the interest rates may not always be same, interest rates may vary in the due course of the loan depending on the market rates and a given interest rate may be applicable for only some period of time as per the contract, in this case what's the best thing to do when the interest rate changes (assume rates increase)? For answering this question we will look into a classic example from  Investment Science by Luenberger (Problem 3.8).

Problem 3.8

Variable rate mortgage: The smith family just took out a variable-rate mortgage on their new home. The mortgage value is $100,000, the term is 30 years and initially the interest rate is 8%. The interest rate is guaranteed for 5 years, after which time the rate will be adjusted according to prevailing rates. The new rate can be applied to their loan by either changing the payment amount or by changing the length of the mortgage.

(a) What is the original yearly mortgage payment? (Assume payments are yearly)
(b) What will be the mortgage balance after 5 years?
(c) If the interest rate rate on the mortgage changes to 9% after 5 years, what will be the new yearly payment that will keep the termination time the same?
(d) Under the interest change in (c), what will be the new term if payments remain the same?

We know that the present value (P) of an annuity that pays an amount A every individual period for 'm' periods for 'n' years at an yearly interest rate 'r' is given by


Given

(a) P= $100,000 n = 30, m = 1, n * m = 30, r = 8%, r/m = 8% = 0.08

Therefore


A = $8882.74 (The original yearly mortgage payment)

(b) The mortgage balance after 5 years is given by (refer this article)


Solving, we get, Mortgage balance after 5 years = $94,821.12

Now we have a new set of values after 5 years, which is

P = $94,821.12 (This becomes the new outstanding principal), m = 1, r = 9%, r/m = 9% = 0.09

(c) Here we need to keep the termination time the same (which is 30 - 5 = 25), therefore we need to find the new payment which will complete the loan at this termination time, or in other words,


A = $9653.4
The new yearly payment that will keep the termination time the same is $9653.4.

(d) Here we need to keep the same old monthly payments as we got in (a), but need to find the new term
(termination time) at which the loan will complete after the interest rate change (which increased to 9% from 8%, therefore if your payments are same at 9% interest rate, the new term is going to increase).


n = 37.56 years

In other words, it will take (37.56 + 5 = 42.56 years) to pay out the loan when you keep the payments same after 5 years at 9% interest rate.

The learning exercise: Which is beneficial, option (c) or (d)?

Investment Science: Amortization schedule of a Mortgage/loan payment

Monday, August 30, 2010



In this post, we will look into the amortization schedule of a mortgage/loan payment, which will breakdown the loan into equal periodic payments (typically monthly payments for a car of home loan), also this schedule will contain the interest paid on a particular period or month, the principal paid, balance principal remaining after the current payment, the schedule repeats itself till the outstanding payment reaches zero.

We will illustrate the amortization schedule with a simple example.

Consider a loan of $1000 for 5 years at an yearly interest rate of 10% payable every year.

We know that the present value (P) of an annuity that pays an amount A every individual period for 'm' periods for 'n' years at an yearly interest rate 'r' is given by
For the above case,

P = $1000, n = 5, m = 1, r = 10%, r/m = 10%, therefore

A = $263.80

This is the yearly amount paid for the loan, the total interest paid = A * (n * m) - P = (263.80 * 5) - 1000 = $319

We will break this down into yearly schedule based on the below reusable spreadsheet for computing the amortization schedule for a given mortgage/loan payment.


The above spreadsheet can be used for any mortgage/loan payment for a principal 'P' for 'm' periods per year for 'n' years at an yearly interest rate r%.

From the above figure, we can infer the following properties.

1. The balance in principle at any period i is given by

For example, for the above loan schedule, the balance principal after two years is given by


Using the spreadsheet:

The above excel can be reused for any mortgage/loan payment for a principal 'P' for 'm' periods per year for 'n' years at an yearly interest rate r%, for using this, all you need to do is to copy the contents of row 15 (corresponding to period 1) of the spreadsheet to the next n * m - 1 rows (till the balance principal becomes zero).

Investment Science - Understanding the mathematics of Mortgage payments - 1

Sunday, August 29, 2010

In the last post, we have seen the annuity formula and an example on how you pay a car loan for a given interest rate for a fixed period of time, in this post we will look into an example to illustrate how the mortgage period affects your total interest payment, for this we will look into an example problem from Investment Science by Luenberger (Problem 3.6).

Problem 3.6

The biweekly mortgage: Here is a proposal that has been advanced as a way for homeowners to save thousands of dollars on mortgage payments: pay biweekly instead of monthly. Specifically if monthly payments are x, it is suggested that one instead pay x/2 every two weeks (for a total of 26 payments per year). This will pay down the mortgage faster, saving interest. The savings are surprisingly dramatic for this seemingly minor modification - often cutting the total interest payment by over one-third. Assume a loan amount of $100,000 for 30 years at 10% interest, compounded monthly.

(a) Under a monthly payment program, what are the monthly payments and the total interest paid over the course of the 30 years?
(b) Under the biweekly program, when will the loan be completely repaid and what are the savings in total interest paid over the monthly program (You may assume biweekly compounding for this part.)

We know that the present value (P) of an annuity that pays an amount A every individual period for 'm' periods for 'n' years at an yearly interest rate 'r' is given by


(a) In monthly payment program

P = 100000, r = 10%, n = 30, m = 12, r/m = 10%/12 = 0.1 / 12 = 0.0083333, therefore


Solving, we get A = $877.568

Total interest paid over the course of 30 years = A * (n * m) - P = 877.568 * 30 * 12 - 100000  =  $215924.48

(b) In biweekly payment program

A = $877.586 / 2 = $438.784
m = 26, r/m = 10% / 26 = 0.1 / 26 = 0.0038461, therefore


Solving, we get n = 20.95

In the biweekly program, the loan will be repaid in 20.95 years (compared to 30 years in the monthly payment program)

Total interest paid in biweekly payment program = A * (n * m) - P = 438.784 * 20.95 * 26 - 100000 = $139005.645

Therefore total interest savings in biweekly payment program compared to monthly payment program = 215924.48 - 139005.645 = $76,918.835 or 76918.835 / 215924.48 = 0.3562 = 35.62% (Wow! when we dig deep into financial mathematics, we can always find a treasure!)

Investment Science: Perpetual Annuity - How to compute your monthly loan payment

Friday, August 27, 2010

In this post on Investment Science, we will look into the perpetual annuity formula with a simple example.

What is an annuity? In simple words it's a stream of fixed payments over a fixed period of time, the best example is mortgage, consider a car loan or a home loan you may have taken, how to derive the monthly payments for your loan, how much interest you pay for over the period of your loan, how much interest your pay every month, how much balance principal you pay in your monthly loan  payment? Which is beneficial, paying additional mortgage for short term or paying less mortgage for long term? Questions galore. In the next few posts we will be exactly looking into these.

Keep in mind that learning the mathematics of your mortgage payment will help you make informed decisions and to start with that, just remember this simple, yet powerful one, the annuity formula.

The present value (P) of an annuity which pays an amount A every individual period (an year) for 'n' years at an yearly interest rate 'r' is given by


Note: If the number of periods per year is > 1, say 'm' periods per year (m = 12 for mortgages since mortgages are paid monthly), the above formula for a total of n years becomes


This simple, yet powerful formula forms the basis for annuity payments, we will look a practical example.

Example:

Suppose you have taken a car loan for $15000 which is to be paid every month over 5 years at an interest rate of 5%, then how much you need to pay every month.

Here

P = $15000
m = 12 (Monthly payment)
Monthly interest rate = r/m = 5%/12 = 0.05/12 = 0.004166
Number of years the loan is to be paid, n = 5
Number of periods the loan is to be paid = n * m = 5 * 12 = 60

Therefore


A = $283.063

Therefore over the period of 5 years, the interest you would have paid = A * (n * m) - P = 283.063 * 60 - 15000 = $1983.78042


Troubleshooting: Method "/lib/svc/method/fs-local" failed with exit status 95 in Solaris - Possible Solution

Yesterday, when I was working with my Solaris system, after reboot, I couldn't ssh into the system, the ssh command failed with the "Connection refused" error, after some research (by logging into the serial console of the server and rebooting the system again through the serial console), I found the below system error message.

svc:/system/filesystem/local:default: Method "/lib/svc/method/fs-local" failed with exit status 95

Then everything came to standstill till I figured out the solution.

Solution:

1. The possible reason for ssh "Connection Refused" may be due to the fact that some filesystem, whose entry is in /etc/vfstab would have failed to mount properly.

A typical vfstab entry looks like the below one.

$ cat /etc/vfstab
#device         device          mount           FS      fsck    mount   mount
#to mount       to fsck         point           type    pass    at boot options
#
fd      -       /dev/fd fd      -       no      -
/proc   -       /proc   proc    -       no      -
/dev/dsk/c0t2d0s1       -       -       swap    -       no      -
/dev/dsk/c0t2d0s0       /dev/rdsk/c0t2d0s0      /       ufs     1       no      logging
...
...

2. To find which entry was causing the problem, I checked the system log, the entries were as follows.

$ cat /var/svc/log/system-filesystem-local:default.log
"/var/svc/log/system-filesystem-local:default.log" 149 lines, 9256 characters
[ Dec  3 01:25:35 Rereading configuration. ]
[ Dec  3 01:25:52 Executing start method ("/lib/svc/method/fs-local") ]
[ Dec  3 01:25:52 Method "start" exited with status 0 ]
[ Dec  3 01:31:54 Executing start method ("/lib/svc/method/fs-local") ]
....
....
WARNING: /sbin/mountall -l failed: exit status 33
[ Aug 27 07:33:14 Method "start" exited with status 95 ]
[ Aug 27 07:35:37 Leaving maintenance because clear requested. ]
[ Aug 27 07:35:37 Enabled. ]
[ Aug 27 07:35:37 Executing start method ("/lib/svc/method/fs-local") ]
mount: /var/js/1/boot: No such file or directory
WARNING: /sbin/mountall -l failed: exit status 1
[ Aug 27 07:35:37 Method "start" exited with status 95 ]
[ Aug 27 07:37:19 Leaving maintenance because clear requested. ]
[ Aug 27 07:37:19 Enabled. ]
[ Aug 27 07:37:19 Executing start method ("/lib/svc/method/fs-local") ]
mount: /var/js/1/boot: No such file or directory
WARNING: /sbin/mountall -l failed: exit status 33
[ Aug 27 07:37:19 Method "start" exited with status 95 ]
svc:/system/filesystem/local:default: Method "/lib/svc/method/fs-local" failed with exit status 95

Looks like there was an entry /var/js/1/boot in /etc/vfstab which didn't mount because the filesystem was not present, hence the error.

3. After finding the root cause for the problem, I removed the line which mounted the /var/js/1/boot filesystem in /etc/vfstab and rebooted the system, now I could login through ssh.

Therefore when you get ssh "Connection refused" error, chances are that some filesystem whose entry is in /etc/vfstab would have failed to mount, in which case check the system log, remove the line causing the problem from /etc/vfstab and ssh should work fine.


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.