Building a Zero Curve with Forward Rate Agreements Using Pandas | by Sam Fung | Towards Data Science

In finance world, if you wanted to price an instrument and figure out the future value at t(n) from t0 (now), you would need to use the spot yield curve. Among the professional traders, the spot yield curve is called zero curve.
If you have a $1000 now to invest, you could easily get the spot rate by going to a bank and ask to deposit you money in a 1 year CD. The CD rate is your benchmark. If you invested into something (assuming the risk is relatively the same) with lesser return than the CD, you know you would be better off putting the money in the CD. This is no brainer.
But what if you know you would have $1000 a year from now and want to invest it for one year? You can’t walk into a bank and try to lock in an interest rate that is a year from now. The bank won’t and probably can’t tell you what the future interest rates are. Maybe different department of the bank can but just not the one you walk in because the target customer are different.
In fact banks do know what the future interest rates are. That is what FRA is.
FRA, or Future Rate Agreement, is an agreement between two parties such that if you lend your money, you would get the specified interest plus principal back at the end of the term.
In this article, we will build a zero curve based on FRAs (Forward Rate Agreement) using Pandas. With this zero curve, you can easily price something anywhere from one day to any number of days up to next ten years.
For simplicity, the FRA we use is a one year term. In reality, the Eurodollar future, which is a FRA, can either be one month or three months long. Please note that all interest rate regardless of its term is ALWAYS quoted as annual rate.
import pandas as pdfra = pd.DataFrame({    'Description': ['2019', '2020', '2021', '2022', '2023'],    'start_date': pd.date_range('2019-01-01', periods=5, freq='YS'),    'end_date': pd.date_range('2019-01-01', periods=5, freq='Y'),    'rate': [2.2375, 2.4594, 2.6818, 2.7422, 2.6625]})
Here we have 5 years of FRA as of 2019–01–01. If you deposit the money on 2019–01–01, you would get 2.2375% interest at the end of the first year, 2.4594% the second year, so on and so forth.
We know the interest rate of each year, but if we wanted to find out the compounded interest rate at the end, I can assure you that the answer is not merely adding the interest rates together. What we do below is that we compound the growth year after year. Let’s break down the steps.
Step 1: Calculate the growth for the period from M to N, hence, mxn_growth.
fra['mxn_growth'] = 1 + fra['rate'] / 100
Step 2: The compounded growth is previous compounded growth multiplied by the growth of this period. Because the growth is compounded since time 0, we call it 0xn_growth.
fra['0xn_growth'] = fra['mxn_growth'].cumprod()
Step 3: Each FRA is a one year term but the growth is a compound of previous years.
fra['years'] = 1fra['cummulative_years'] = fra['years'].cumsum()
Step 4: Finally we know the total growth 0xn_growth for the total number of years cummulative_years. We just need to normalize it to annual rate. As we pointed out earlier, interest rate is ALWAYS quoted as annual rate.
fra['zero_rate'] = fra['0xn_growth'] ** (1 / fra['cummulative_years']) - 1
There you have it. You want to know what your return is at the end of 2020, 2.3484% is your interest rate.
Wait! You might say, this is too easy.
Yes, indeed, remember we intentionally made each FRA a one year term. What if they are one month or three months like the Eurodollar futures? How would you do it differently? I will leave that challenge to you. If you want to jump right into the answer, read The Math of Compounding Interest is not as Simple (Difficult) as You Think.
This article is also available in Juypter Notebook format. Feel free to play with it.