How to price a Bond

Before I get into finishing writing the second part of my simple Mortgage calculator which calculates how much you have to pay per month if you want your mortgage paid off in a certain time frame, I want to go over how to mathematically value a bond. After this, I will have a bond calculator at a later date programmed in Javascript.

What is a bond?

A bond is a debt instrument used by a company or a sovereign state in order to raise capital. A bond is a contract where, in return for purchasing the bond, the company promises to periodically pay interest for a period of time and at the end of the period pay the principal of the bond.

Of course, there are many types of bonds and many interesting variants, for this one I will only be discussing the straight bond which is a bond that pays a coupon (interest) periodically and then pays back its face-value (principle) when the bond matures.

Market Value vs Face-Value

So – typically at the beginning a company will sell a bond at par, this is when face-value is equal to the market value (typically $100 or $1000) however there are many factors that can change the market value of a bond.

For example, if a company improves its credit rating, the bond will typically become worth more than the face-value. In this situation, the bond is said to be sold at a premium. On the converse, if its credit rating slides, then the bond will sell below par or be sold at a discount. There are other factors such as the interest rate set by the federal reserve bank.

Pricing a Straight Bond

So again from a previous post, we price an asset by discounting its future payouts. The bond however periodically pays a constant rate of payments and then finally a large payment at the end – from here we see that the straight bond is just an annuity, plus an additional lump sum payment.

 \frac{c}{r/p} (1-\frac{1}{(1+r/p)^pt})

Here c is the coupon or the interest rate of the bond times the face value of the bond divided by the number of payments per year. r, in this case, is not the interest rate of the bond but the interest rate associated with a company with a certain credit rating. For example, an AAA credit rating would have a lower r, then a BB credit rating. Finally, p is the number of payments per year and t is the number of years.

Now we have the final part of the bond which is the principle that will be paid at maturity. Let’s call this value M. Since M is paid out after t years, we discount it t years with discount rate r, or:

\frac{M}{(1+r)^t}

Now we add it together to get the value of our bond:

 \frac{c}{r/p} (1-\frac{1}{(1+r/p)^pt}) + \frac{M}{(1+r)^t}

Let’s take an example. Banana Corporation issues a bond with a $1000 face value and a 5% coupon rate at par and coupons are paid semiannually for 10 years,. This means at the discount rate at this time is 5%.

 \frac{25}{.05/2} (1-\frac{1}{(1+0.05/2)^{2*10}}) + \frac{1000}{(1+.05)^{10}} = 1000

However all of a sudden we find out that Banana Corporation has a cash flow issues and thus its rating is reduced. This means that the discount rate for Banana Corporation is 7.5% instead of 5%:

 \frac{25}{.075/2} (1-\frac{1}{(1+0.075/2)^{2*10}}) + \frac{1000}{(1+.075)^{10}} = 832.598

Note, even though the discount rate has changed, the coupon rate doesn’t. Thus the bond is now less valuable and is said to be sold at a discount.

Thank you for reading my article, if you found it useful please like and share and if you have any questions please feel free to leave them in the comments – I may use your question for a further post.

Advertisements

How long until I pay off my Mortgage? Mortgage calculator using Javascript

Hi! In this post, I will be doing an application of an annuity. Remember to form the previous posts, here and here, that an annuity is an investment that pays a series of future payments and from those payments, I want to know how much is a reasonable price to pay for it. A mortgage, if you the mortgage borrower pay a constant stream of cash, is an annuity for the mortgage lender, except here the value of the annuity is the difference between the cost of the home and the down payment and what we need to figure out now is how many payments we’d need to make.

1

For the Calculator use the Link bellow:

https://jsfiddle.net/Zerkon/su01bz26/1/show/

Why is a Mortgage an annuity?

If you think about it an annuity is an investment that pays a series of future payments. If you were to take out a mortgage to buy a home and then you’d pay a constant amount every month, this is an annuity, not for you but the lender.

This case the price of the annuity is the difference between your house and the down payment so there are two things you can calculate for. First, you can find how long it would take to pay off the mortgage if you have a constant payment each month. Second, you can find how much you have to pay each month if you want it paid off in a specific time limit.

In this post I will be calculating the time, in a future post, I will be calculating the size of the payments.

remember that the formula for pricing an annuity is:

p = \frac{c}{r} (1- \frac{1}{(1+r)^t})

Here p will be the difference between the cost of your house and the down payment, r is the monthly interest rate of the mortgage loan and c is the monthly payments. We now solve for t which gives us:

t = \frac{ln(c)-ln(c-pr)}{ln(1+r)}

I’ll quickly show you the HTML section of the code and then go over to the heavy lifting using Javascript.

HTML

Here is the layout for the calculator

<h2>Simple Mortgage Calculator (time)</h2>
<table style="width:100%">
<tr>
<td width="150px"> Cost of property</td>
<td>
<input id="price"></td>
</tr>
<tr>
<td> Down Payment</td>
<td>
<input id="down"></td>
</tr>
<tr>
<td> Interest Rate (%)</td>
<td>
<input id="interest"></td>
</tr>
<tr>
<td> Monthly Payments</td>
<td>
<input id="payments"></td>
</tr>
<tr>
<td></td>
<td>
<button type="button" onclick="myFunction()">Submit</button></td>
</tr>
</table>
<p id="answer"></p>
<p id="answer2"></p>


Javascript

With Javascript log1P(x) = ln(1+x) so thus I had to subtract 1 from every value that was placed in log1P(x) to make sure that I got an accurate value.


var temp2 = Math.log1p(payments - 1) - Math.log1p(payments - temp1 - 1);

Another consideration I made was the fact that if you were to make monthly payments that were smaller than the interest, the calculator would warn you that it would be impossible to pay off the mortgage and would give you a minimum value for payments.

Also, note that although the user is entering in the stated rate of interest, it is the effective rate of interest that is used. This is calculated by dividing the rate by 365 adding 1 and raising it to the power of 365. I then take the 12th root to find the rate for each month.

Here’s the full code:


function myFunction() {
var text, text2;
var time;

var property = document.getElementById("price").value;
var down_payment = document.getElementById("down").value;
var interest = document.getElementById("interest").value;
var payments = document.getElementById("payments").value;

if (isNaN(property) || isNaN(down_payment) || isNaN(interest) || isNaN(payments)) {
//checks to see if the input is valid
text = "Input not valid";
} else {
if (property < 0 || down_payment < 0 || interest < 0 || payments < 0) {
//checks to see if the values are positive
text = "Input not valid";
} else {
//do computations here

//compute the actual monthly rate
var rate = Math.pow(1 + interest / 36500, 365);
rate = Math.pow(rate, .0833) - 1;

//calculate intermittent values
var temp1 = (property - down_payment) * rate;

//if the interest from the mortgage is larger then the payments then the debt will never be repaied
if (temp1 < payments) {
//calculates the time it takes to pay off the mortgage
var temp2 = Math.log1p(payments - 1) - Math.log1p(payments - temp1 - 1);
temp2 = temp2 / Math.log1p(rate);
temp2 = Math.ceil(temp2);

temp3 = temp2 / 12;
temp3 = Math.floor(temp3);

temp4 = temp2 - temp3 * 12;

text = "you will be able to pay off your mortgage in ";

if (temp3 == 1) {
text = text + "one year ";
} else if (temp3 > 1) {
text = text + temp2 + " years ";
}

if (temp4 == 1) {
if (temp3 > 0) text + "and ";
text = text + "one month";
} else if (temp4 > 1) {
if (temp3 > 0) text + "and ";
text = text + temp4 + " months";
}
} else {
text = "Payments are too small, mortgate will not be paid off. Payments must be more then $" + temp1.toFixed(3) + " per month";
}
}
}
document.getElementById("answer").innerHTML = text;
// document.getElementById("answer2").innerHTML = text2;
}

Thank you for reading my article, if you found it useful please like and share and if you have any questions please feel free to leave them in the comments – I may use your question for a further post.

How to price an investment

This is a response to a comment I had on a previous post about valuing a simple annuity due using javascript. There is a concept in finance called the time value of money, that is money today is not worth the same as money tomorrow and we price our investment instruments accordingly.

The price of a security is generalized as follows

p = mx

Where P is the price, x is the future payout and m is the stochastic discount factor. What this means that the price of the security is dependent on what I will receive in the future as a payout. For the annuity, the payout (payouts) are the series of payments that I will receive in the future, but remember money in the future isn’t worth the same as money today, that’s where m comes in.

m in the case of the simple annuity represents the ratio of the value of money today over the value of money tomorrow. Basically, what is the least amount of money I want to receive sometime in the future for me to part with 1 dollar?

Let’s say the prevailing interest rate is 10% annually and I will receive a payment of $100 in one year if I bought that single payment ordinary annuity. This means that I will only part with $1 if I know it’s worth at least $1.10 in a year. Thus:

p = 1.00/1.10 * 100 = 90.91

Which means for that $100 payment I will only buy it for at most $90.91. At the same time, if I sell this security, the buyer will also only buy for at most $90.91.

Of course, an annuity with more than 1 payments has more steps in derivation but I’ll get to it in a later post. The idea, however, will be used to price bonds, stocks, options and many different types of securities.

Thank you for reading my article, if you found it useful please like and share and if you have any questions please feel free to leave them in the comments – I may use your question for a further post.

Valuing a simple Annuity Due using Javascript

This post is about valuing a simple annuity due using Javascript programming. This post will be building off of a previous post Valuing a Simple Ordinary Annuity using Javascript since Annuity Due and Ordinary Annuities are very similar. To try out this calculator click the following link:

https://jsfiddle.net/Zerkon/fygkL78s/2/show/

Simple Annuity Calculator
Simple Annuity calculator modified to handle Annuity Due as well as Ordinary Annuity

 

What is an Annuity Due

Just like an Ordinary Annuity, an Annuity Due is a series of payments. The difference is that an ordinary annuity pays at the end of a payment period whereas annuity due is paid at the beginning. This means that the present value of each payment of an annuity due is different than an ordinary annuity. For example, we have an annual payment of $100 and the prevailing interest rate is 5%.

For an annuity, due it’s 100/1.05^0 = 100 since we are paid at the beginning of the year and for ordinary annuity it’s 100/1.05^1= 95.24 since we are paid at the end of the year.

With that difference, let’s get coding.

HTML

For the layout for the controls, it will be almost exactly the same as in my previous post, however, I am adding an additional feature which will allow us to choose whether we want to evaluate the security as an annuity due or ordinary annuity.

<tr>
<td>Annuity Type</td>
<td>
<select id="type">
<option value="ord"> Ordinary Annuity</option>
<option value="due">Annuity Due</option>
</select></td>
</tr>

This snippet of code allows the user to choose which type of annuity they want.

Here’s the full HTML, scroll down to get the Javascript part.

</pre>
<h2>Simple Annuity Calculator</h2>
<table style="width:100%">
<tr>
<td>Annuity Type</td>
<td>
<select id="type">
<option value="ord"> Ordinary Annuity</option>
<option value="due">Annuity Due</option>
</select></td>
</tr>
<tr>
<td width="150px"> Payment Amount
<td>
<input id="payment"></tr>
<tr>
<td> Interest Rate (%)</td>
<td>
<input id="interest"></td>
</tr>
<tr>
<td> Frequency of Payments
<td>
<select id="period">
<option value="annually"> Annually</option>
<option value="semi annually"> Semi Annually</option>
<option value="quarterly"> Quarterly</option>
<option value="monthly"> Monthly</option>
<option value="bimonthly"> Bi Monthly </option>
<option value="weekly"> Weekly </option>
</select></tr>
<tr>
<td> Number of Years
<td>
<input id="years"></tr>
<tr>
<td></td>
<td>
<button type="button" onclick="myFunction()">Submit</button></td>
</tr>
</table>
<p id="demo"></p>

Javascript

The idea here is the same as with Ordinary annuities. Again I am making the assumption that the payments will be constant and unchanging. Thus I get the same mathematical equation as Ordinary Annuity,

Except I multiply in 1+r since we are discounting on period less.

As a result, I also add it to the code as follows.


//if the user choose and annuity due

if(annuitytype == "due") value = value * (1 + temp);

Remember the user gets to choose which type of annuity they want so the discount factor will only be multiplied in if the user explicitly chooses Annuity Due.

Here is the full Javascript Code:


function myFunction() {
var text;
var value = 0.0;

// gets the values from the inputs
var Payment = document.getElementById("payment").value;
var Interest = document.getElementById("interest").value;
var Years = document.getElementById("years").value;
var period = document.getElementById("period").value;
var lifespan = 1;

//lifespan variable is the frequency of payments per year
if (period == "annually") lifespan = 1;
if (period == "semi annually") lifespan = 2;
if (period == "quarterly") lifespan = 4;
if (period == "monthly") lifespan = 12;
if (period == "bimonthly") lifespan = 24;
if (period == "weekly") lifespan = 52;

// checks to see if these values are numbers
if (isNaN(Payment) || isNaN(Interest) || isNaN(Years)) {
text = "Input not valid";
}
else {
//calculates the present value of the annuity
var temp = Interest / (100*lifespan);
var temp2 = Math.pow((1 + temp), (lifespan * Years));
value = Payment * (1 - 1 / temp2) / (temp);

//if user chooses annuity due
if(annuitytype == "due") value = value * (1 + temp);

//grammatical cases for the output of the function
if(lifespan == 1 && Years == 1) text = "The Annuity pays $" + Payment + ", once per year for one year, is worth $" + value.toFixed(3);
else if(lifespan == 1) text = "The Annuity pays $" + Payment + ", once per year for " + Years + " years, is worth $" + value.toFixed(3);
else if(Years == 1) text = "The Annuity pays $" + Payment + ", " + lifespan + " times per year for one year, is worth $" + value.toFixed(3);
else text = "The Annuity pays $" + Payment + ", " + lifespan + " times per year for " + Years + " years, is worth $" + value.toFixed(3);
}

//outputs either an error message or the value of the annuity
document.getElementById("demo").innerHTML = text;
}

Thank you for reading my article, if you found it useful please like and share and if you have any questions please feel free to leave them in the comments – I may use your question for a further post.

Wait! $20 today isn’t worth as much tomorrow????

For anyone who read my post on “Valuing an ordinary Annuity using Javascript” and wondered why everything was discounted by the interest rate, there’s a concept in finance called time value of money. Of course, there’s also inflation but we’ll get to it sometime later.

Basically, the interest is the amount of money you’d want in the future plus the principle if you were to loan the principle it out today. I ran into a funny example when I was still an undergrad and was sneaking into the graduate finance class.

Imagine two college students, one name Peter and the other one named Dave. Peter is very thrifty and always has the cash to spare. Dave isn’t. So one day Dave does up to Peter, “Pete! I need $100 to pay for my hot date tonight! I know you have the money.”

Peter, has $100 but he wanted to put his money into a bond fund that pays 5% annually. So in order for Peter to give up the $100, wants Dave to pay at least 5% interest if not more. How much more depends on how reliable Dave is to repay the loan. (in this case – it’s probably a very high interest)

So if they agree on a deal that one year in the future Dave will pay Peter back $105, that $105 to Peter would be worth to him then just as much as $100 dollars does now. At the same time if Dave overpays, let’s say $110, then that is worth $104.76 dollars to Peter today. If Dave underpays, let’s say $100, then it will only be worth $95.24 to Peter today.

Hence the idea behind present value and discount rates.

If you like my post feel free to like and share if you have any questions feel free to leave them in the comments – as I may use them for a future post.

Valuing a simple ordinary annuity using Javascript

In this post, I will be showing how to write a Javascript software for a valuing a simple ordinary annuity where I assume the payments are constant. Click the following link for the final result:

https://jsfiddle.net/Zerkon/r76y8a1f/6/show

Simple Annuity Calculator
Screen shot of the Simple Ordinary Annuity Calculator

What is an annuity

An Annuity is simple a series of payments over a period of time. The value of an annuity is basically the sum of the present values of each payment. Money has a different value in the future so hence we need to calculate the present value.

I am doing an ordinary annuity which means that the payout is received at the end of the period, this is VERY IMPORTANT as the time the payout is received greatly changes the price. Second I will assume that each payment is constant and the interest rate will be the same throughout the lifespan of the entire annuity.

\frac{c}{(1+r/p)^1} + \frac{c}{(1+r/p)^2} + \frac{c}{(1+r/p)^3} + ... + \frac{c}{(1+r/p)^pt} = \frac{c}{r/p} (1-\frac{1}{(1+r/p)^pt})

Where c is the payment, r is the interest rate, p is the number of periods per year and t is the number of years. From here I will use Javascript for the heavy lifting and html5 for the layout.

Html5

This calculator was coded using Javascript, first things first I created the layout for the calculator using HTML. The code for it is as follows:

</pre>
<h2>Simple Annuity Calculator</h2>
<table style="width:100%">
<tr>
<td width="150px"> Payment Amount
<td>
<input id="payment"></tr>
<tr>
<td> Interest Rate (%)</td>
<td>
<input id="interest"></td>
</tr>
<tr>
<td> Frequency of Payments
<td>
<select id="period">
<option value="annually"> Annually</option>
<option value="semi annually"> Semi Annually</option>
<option value="quarterly"> Quarterly</option>
<option value="monthly"> Monthly</option>
<option value="bimonthly"> Bi Monthly </option>
<option value="weekly"> Weekly </option>
</select></tr>
<tr>
<td> Number of Years
<td>
<input id="years"></tr>
<tr>
<td></td>
<td>
<button type="button" onclick="myFunction()">Submit</button></td>
</tr>
</table>
<p id="demo"></p>

<pre>

Javascript

Now that we have this code finished, I’m getting towards the heavy lifting of the program. This part is written in Javascript.

I will again be using the function from before:

\frac{c}{r/p} (1-\frac{1}{(1+r/p)^pt})

The program will take the Interest, Payment and Years as input from the user and then allows the user to choose how often the annuity will pay.


function myFunction() {
var text;
    var value = 0.0;
    // gets the values from the inputs
    var Payment = document.getElementById("payment").value;
    var Interest = document.getElementById("interest").value;
    var Years = document.getElementById("years").value;
    var period = document.getElementById("period").value;
    var lifespan = 1;

//lifespan variable is the frequency of payments per year
    if (period == "annually") lifespan = 1;
    if (period == "semi annually") lifespan = 2;
    if (period == "quarterly") lifespan = 4;
    if (period == "monthly") lifespan = 12;
    if (period == "bimonthly") lifespan = 24;
    if (period == "weekly") lifespan = 52;
// checks to see if these values are numbers
    if (isNaN(Payment) || isNaN(Interest) || isNaN(Years)) {
        text = "Input not valid";
    }
    else {
        //calculates the present value of the annuity
        var temp = Interest / (100*lifespan);
        var temp2 = Math.pow((1 + temp), (lifespan * Years));
        value = Payment * (1 - 1 / temp2) / (temp);
//grammatical cases for the output of the function
        if(lifespan == 1 && Years == 1) text = "The Annuity pays $" + Payment + ", once per year for one year, is worth $" + value.toFixed(3);
        else if(lifespan == 1) text = "The Annuity pays $" + Payment + ", once per year for " + Years + " years, is worth $" + value.toFixed(3);
        else if(Years == 1) text = "The Annuity pays $" + Payment + ", " + lifespan + " times per year for one year, is worth $" + value.toFixed(3);
        else text = "The Annuity pays $" + Payment + ", " + lifespan + " times per year for " + Years + " years, is worth $" + value.toFixed(3);
    }
    //outputs either an error message or the value of the annuity
    document.getElementById("demo").innerHTML = text;
}

Thank you for reading my article, if you found it useful please like and share and if you have any questions please feel free to leave them in the comments – I may use your question for a further post.

Hello World!

Hi, I’m Leo Phong Vu, I’m currently an MBA student at University of California Riverside’s AGSM program. I have a BS in pure mathematics and several years experience writing software, so I’ve decided to write a blog about finance, financial history and of course financial software.

Most of my software projects will be written in javascript, and I will share the source code in my posts as well. Feel free to use them.