Now, assume that we have two bonds with two periods paying different cash flows. What's the term structure?
Assune that bond A has the following value
\begin{equation} p^A = DF_{1} \times C^A_{1} +DF_{2} \times C^A_{2} \end{equation}while B
\begin{equation} p^B = DF_{1} \times C^B_{1} +DF_{2} \times C^B_{2} \end{equation}Note that we assume that we know the prices.
We have two unknowns $DF_{1}$ and $DF_{2}$ and two equations (one for each bond). Generally, we can solve this problem using algebra. However, given that you must deal with a number of securities, it is better to work with matrices. Basically, we are going to put unknowns in one side, and constants (prices) in other.
The matrix representation of our problem is
$ \begin{bmatrix} C^A{1} & C^A{2} \ C^B{1} & C^B{2} \end{bmatrix} \begin{bmatrix} DF{1} \ DF{2}
\text{or} \quad A x =b $
where the matrix $A$ contains the coefficients associated to the unknowns $x$ and $b$ contains the constants.
We can do row (column) manipulation and find the solution for $x$. Here, we take a shortcut and pre-multiply for the inverse of matrix A.
$ A^{-1} A x = A^{-1} b \Rightarrow x = A^{-1}b $
We are going to ask R to solve this for us. We only need to enter the matrix A and B. In our simple case, A is $2\times2$, or 2 rows and 2 colums and $b$ is $2\times1$ or 2 rows and 1 column.
%pylab inline
%reload_ext rpy2.ipython
%%R
A <- matrix(NA,2,2) # Create a matrix A with missing values
# Assume CA1 = 8 CA2 = 108 and pA = 108.1 and CB1 = 0 and CB2 = 99 and pB = 92
A[1,1]<-8; A[1,2]<-108
A[2,1]<-0; A[2,2]<-99
b<-c(108.1,92)
#b
sol<- solve(A)%*%b # Note the % before and after * This means that we are doing matrix multiplication.
print(sol)
#t <- seq(2)
#t
#r <- sol^{-1/t} -1
#r
#plot(r,type="l",lty=2)
# We can recover the interest rate (1+r)^{-t}=DF or r = DF^{-1/t}-1
%%R
# Imagine that a bond for 4 years pays 2% as coupon and its price is 104
lis<- c(1,2,3,4)
roots<-polyroot(c(-104, 2, 2,2, 102))
dfsol<-round(Re(roots)[Re(roots)>0],6)
rsol<-1/dfsol-1
rsol<-round(rsol,5)*100
print(paste("The yield is ", rsol,"%", sep = ""))
from IPython.core.display import HTML
def css_styling():
import os
styles = open(os.path.expanduser("~/Finance-Bonds/custom.css"), "r").read()
return HTML(styles)
css_styling()