CoCalc Public FilesFinance-Bonds / Yields.ipynb
Author: Jean Paul Rabanal Sobrino
Views : 84
Description: Jupyter notebook Finance-Bonds/Yields.ipynb
Compute Environment: Ubuntu 18.04 (Deprecated)

# Computing yields

• 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.

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

while B

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} \end{bmatrix} = \begin{bmatrix} p^A \\ p^B \end{bmatrix} \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 [11]:
%pylab inline

Populating the interactive namespace from numpy and matplotlib
In [12]:
%%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

[,1] [1,] 0.9670455 [2,] 0.9292929

## Yields

• 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 [15]:
%%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<-Re(roots)[Re(roots)>0]
rsol<-1/dfsol-1
print(paste("The yield is ", rsol,"%", sep = ""))

#dfsol<-round(Re(roots)[Re(roots)>0],6)
#
#rsol<-round(rsol,5)*100


[1] "The yield is 0.00975494274096445%"
In [14]:
from IPython.core.display import HTML
def css_styling():
import os