Yield Table Creation
Yield Table Creation¶
What we want to do now is create a function which will take a given date and build a table which has the current date’s yield curve, the yield curve from 1 year ago from that date, and the yield curve from 5 years ago. We also want to grab the change in rates over the two periods.
from pandas import DataFrame
# Write and test the function
def create_yield_table(data: DataFrame, date: datetime) -> DataFrame:
"""
Function to build a yield table object
Parameters
----------
data : DataFrame
Yield curve data to be used
date : datetime
The date to use for the current date
Yields
------
DataFrame
A yield table
"""
# Find the other two dates
date2 = date.replace(year=date.year-1)
date3 = date.replace(year=date.year-5)
# Index into the three dates and copy
table = yield_curve.loc[[date, date2, date3]].copy()
# Transpose
table = table.T
# Rename the columns
table.columns = ["Current", "1 Year Ago Curve", "5 Years Ago Curve"]
# Get the changes in the yield curve
table["1 Year Change"] = table["Current"] - table["1 Year Ago Curve"]
table["5 Year Change"] = table["Current"] - table["5 Years Ago Curve"]
return table
yield_table = create_yield_table(yield_curve, datetime(2019,12,31))
print(yield_table)
Current 1 Year Ago Curve 5 Years Ago Curve 1 Year Change \
1 Mo 1.48 2.44 0.03 -0.96
3 Mo 1.55 2.45 0.04 -0.90
6 Mo 1.60 2.56 0.12 -0.96
1 Yr 1.59 2.63 0.25 -1.04
2 Yr 1.58 2.48 0.67 -0.90
3 Yr 1.62 2.46 1.10 -0.84
5 Yr 1.69 2.51 1.65 -0.82
7 Yr 1.83 2.59 1.97 -0.76
10 Yr 1.92 2.69 2.17 -0.77
20 Yr 2.25 2.87 2.47 -0.62
30 Yr 2.39 3.02 2.75 -0.63
5 Year Change
1 Mo 1.45
3 Mo 1.51
6 Mo 1.48
1 Yr 1.34
2 Yr 0.91
3 Yr 0.52
5 Yr 0.04
7 Yr -0.14
10 Yr -0.25
20 Yr -0.22
30 Yr -0.36
Re-indexing Columns¶
We used re-indexing before when we wanted to change the dates used in our data to include weekends. It is also possible to sue re-indexing on columns to change the order or add in columns not there. Below is an example of how we could re-organize the columns so that the change in the curves columns are next to their respective curve columns (i.e. 1 Year Change is next to 1 Year Ago Curve).
All that needs to be changed is to give an argument of columns followed by the re-index columns.
# Here you see how re-indexing can be done with columns
print(yield_table.reindex(columns=['Current', '1 Year Ago Curve','1 Year Change', '5 Years Ago Curve','5 Year Change']))
Current 1 Year Ago Curve 1 Year Change 5 Years Ago Curve \
1 Mo 1.48 2.44 -0.96 0.03
3 Mo 1.55 2.45 -0.90 0.04
6 Mo 1.60 2.56 -0.96 0.12
1 Yr 1.59 2.63 -1.04 0.25
2 Yr 1.58 2.48 -0.90 0.67
3 Yr 1.62 2.46 -0.84 1.10
5 Yr 1.69 2.51 -0.82 1.65
7 Yr 1.83 2.59 -0.76 1.97
10 Yr 1.92 2.69 -0.77 2.17
20 Yr 2.25 2.87 -0.62 2.47
30 Yr 2.39 3.02 -0.63 2.75
5 Year Change
1 Mo 1.45
3 Mo 1.51
6 Mo 1.48
1 Yr 1.34
2 Yr 0.91
3 Yr 0.52
5 Yr 0.04
7 Yr -0.14
10 Yr -0.25
20 Yr -0.22
30 Yr -0.36
Let's add this feature into our function from before!
# Revise the code
def create_yield_table(data: DataFrame, date: datetime) -> DataFrame:
"""
Function to build a yield table object
Parameters
----------
data : DataFrame
Yield curve data to be used
date : datetime
The date to use for the current date
Yields
------
DataFrame
A yield table
"""
# Find the other two dates
date2 = date.replace(year=date.year-1)
date3 = date.replace(year=date.year-5)
# Index into the three dates and copy
table = yield_curve.loc[[date, date2, date3]].copy()
# Transpose
table = table.T
# Rename the columns
table.columns = ["Current", "1 Year Ago Curve", "5 Years Ago Curve"]
# Get the changes in the yield curve
table["1 Year Change"] = table["Current"] - table["1 Year Ago Curve"]
table["5 Year Change"] = table["Current"] - table["5 Years Ago Curve"]
# Re-index the columns
table = table.reindex(columns=['Current', '1 Year Ago Curve','1 Year Change',
'5 Years Ago Curve','5 Year Change'])
return table
yield_table = create_yield_table(yield_curve, datetime(2019,12,31))
print(yield_table)
Current 1 Year Ago Curve 1 Year Change 5 Years Ago Curve \
1 Mo 1.48 2.44 -0.96 0.03
3 Mo 1.55 2.45 -0.90 0.04
6 Mo 1.60 2.56 -0.96 0.12
1 Yr 1.59 2.63 -1.04 0.25
2 Yr 1.58 2.48 -0.90 0.67
3 Yr 1.62 2.46 -0.84 1.10
5 Yr 1.69 2.51 -0.82 1.65
7 Yr 1.83 2.59 -0.76 1.97
10 Yr 1.92 2.69 -0.77 2.17
20 Yr 2.25 2.87 -0.62 2.47
30 Yr 2.39 3.02 -0.63 2.75
5 Year Change
1 Mo 1.45
3 Mo 1.51
6 Mo 1.48
1 Yr 1.34
2 Yr 0.91
3 Yr 0.52
5 Yr 0.04
7 Yr -0.14
10 Yr -0.25
20 Yr -0.22
30 Yr -0.36