Simulating a bond fund in python, part 1

Robert Shiller provides information about interest rates going back to 1871. However, knowing an interest rate isn’t that helpful for us when backtesting: we want to know the annual returns we might have earned if we had invested in a bond fund at that time.

We can make that conversion by simulating a bond fund.

The Simple Way

  1. Buy a 10-year Treasury.

In practice it would look like:

  1. In January 2002 buy a 10-year Treasury for $100

The only tricky part is figuring out how much you could have sold that 9-year Treasury for back in January 2003. You could try using actual historical data: maybe a list of bond sales in January and calculate things that way. But we can also take a short cut, thanks to “bond math”

Bond Math 101: Constant interest rates

To keep things simple, let’s assume there are no changes to interest rates.

Say you buy a $100 bond with a coupon rate of 3% and a term of 10-years. That means every year you will receive $3 in interest. And then at the end of the last year (when the bond matures) you will receive your $100 back.

Bonds, after all, are just a loan.

So it is easy to calculate that the bond starts out generating a total income of ($3 x 10years) + ($100 at the end) = $130.

A year passes. The bond has paid you $3 in interest. Now the bond will generate a total income of $127 over the rest of its life.

The math is straightforward and everyone can easily calculate how much your bond would pay them if they bought it from you. You can see that they are unlikely to pay you $128 when the bond won’t ever pay out more than $127.

Bond Math 201: Changing interest rates

In the real world, interest rates change.

You bought that 3% bond last year. Now interest rates have gone up to 4%.

Let’s pretend you buy a brand new 9-year bond that has a coupon rate of 4%. You spend $93.38 to buy the bond. That means every year you will get ($93.38 x 4%) = $3.73 in interest. After nine years you will have received $33.62 in interest. ($33.62 + $93.38) = $127.

We said before that our old bond also had a total lifetime value of $127 for the next 9 years. But we paid $100 for it. Now we can buy a bond for $93.38 that has the same total lifetime value over that same period of time.

Why would anyone pay more than $93.38 for your “$100” bond when they could just buy a new one? They won’t. That means your bond is only worth $93.38 now, since that’s the price you’d get if you tried to sell it.

If rates went down you’d see the opposite effect. Say that rates drop to 2%. Now someone has to buy $107.62 worth of 2% bonds to get $127 of money back. That bond that you bought for $100 can now be sold for $107.62.

Bond Math 301: Present Value calculations

I lied a bit above. If interest rates go up to 4%, your bond isn’t worth $93.38. It is actually only worth $92.56. I oversimplified to try to show why you bond would be worth less. The real calculation is more complex because you also need to factor in the interest on the interest.

Every year you get an interest payment. Above, I assumed that you receive that money and it sits around earning 0% for the next 9 years. In reality, you would take that money and reinvest it at the current rates.

Here’s a simplified 2-year example.

  1. You have a $100 bond with a $3 coupon rate.

Now compare that to buying a brand new bond with a $4 coupon rate.

  1. You have a $98.11 bond with a $4 coupon rate.

That’s just for 2 years. Now think about extending that out to 10 years. You’ll get interest on interest on interest. Luckily, spreadsheets and calculators have function that handles all the complexity for you: the present value calculation (PV)

=pv(4%, 2, $3, $100)
=$98.11
=pv(4%, 9, $3, $100)
=$92.56
=pv(2%, 9 $3, $100)
=$108.16

Using Present Value we can calculate the value of a bond at any given point in time.

Next we will put it all together and see it in action…

Written by

Learn how to enjoy early retirement in Vietnam. With charts and graphs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store