%pylab inline
import pandas as pd
import inst, fmt
Populating the interactive namespace from numpy and matplotlib
Implement the Thomas algorithm for solving tridiagonal linear systems:
Do your own reading and research on Thomas algorithm and implement and test it in Python.
Given the following benchmark swap quotes for USD, suppose all swap coupons are paid semi-annually:
mats = np.array([1, 2, 3, 5, 7, 10, 12, 15, 20, 25]) * 1.
par = np.array([.042, .043, .047, .054, .057, .06, .061, .059, .056, .0555])
df_swap = pd.DataFrame(np.array([par]).T*100, columns=["Par Rate (%)"],
index=map(lambda m: '%dY' % m, mats))
fmt.displayDF(df_swap.T, 2, 2)
1Y | 2Y | 3Y | 5Y | 7Y | 10Y | 12Y | 15Y | 20Y | 25Y | |
---|---|---|---|---|---|---|---|---|---|---|
Par Rate (%) | 4.2 | 4.3 | 4.7 | 5.4 | 5.7 | 6.0 | 6.1 | 5.9 | 5.6 | 5.5 |
The swap pricers are provided by the python class Swap
and the pricing function priceSwap
, both are defined in swap.py under the folder nyumath2048/lib of the github repository.
Note the following assumptions of the swap pricer provided:
The following are some sample codes of how to use the swap pricers:
from swap import Swap, priceSwap
import lin
# the pricing function if curve(t) is the cumulative yield, ie, -log(b(t))
def y2pv(swap, curve) :
discf = lambda ts: np.exp(-curve(ts))
return priceSwap(swap, discf)
# create the benchmark instruments
bm_swaps = {Swap(m, c, 2) : 0 for m, c in zip (mats, par)}
# price a single swap with a straight curve in y(t)
flat = lin.RationalTension(lbd = 5.)
flat.build(mats, mats*.04) # create a dummy curve of flat 0.04 rate
pvs = {swap.maturity : y2pv(swap, flat) for swap in bm_swaps.keys()}
print("PV by maturity")
print("\n".join(["%.2g Y : %.4g" % (m, v) for m, v in pvs.items()]))
PV by maturity 1 Y : 0.00155 2 Y : 0.004943 3 Y : 0.01846 5 Y : 0.06101 7 Y : 0.1003 10 Y : 0.1599 12 Y : 0.1943 15 Y : 0.2077 20 Y : 0.2126 25 Y : 0.2362
priceSwap
functionderiv()
method in the Tension spline class.Hint:
help()
, please use the python codes in the lecture slides as examples.mixf=0.5
in the inst.iterboot
function, or your own)