- Recall that $\frac{1}{(1+r)^t}\equiv DF_{t}$.
- If we want to find the yield, we can change our problem to solve for DFs.
- Our problem becomes a linear problem, which is much easier to work with. \begin{equation} p = DF_{t}\times C \end{equation}

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.

In [4]:

```
%pylab inline
%reload_ext rpy2.ipython
```

In [5]:

```
%%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
```

- Note that assuming constant interest rates (yields!) $DF_1$ is $x$ then, $DF_2$ is $x^2$ and so on...
- Finding the yield becomes to solve for a polynomial of degree $T$.
- Once again, we will ask R to do it for us.

In [6]:

```
%%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 = ""))
```

In [7]:

```
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()
```

Out[7]:

In [ ]:

```
```