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

Computing yields

  • Recall that 1(1+r)tDFt\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 DF1DF_{1} and DF2DF_{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

[C1AC2AC1BC2B][DF1DF2]=[pApB]orAx=b \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 AA contains the coefficients associated to the unknowns xx and bb contains the constants.

We can do row (column) manipulation and find the solution for xx. Here, we take a shortcut and pre-multiply for the inverse of matrix A.

A1Ax=A1bx=A1b 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×22\times2, or 2 rows and 2 colums and bb is 2×12\times1 or 2 rows and 1 column.

In [11]:
%pylab inline %reload_ext rpy2.ipython
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


  • Note that assuming constant interest rates (yields!) DF1DF_1 is xx then, DF2DF_2 is x2x^2 and so on...
  • Finding the yield becomes to solve for a polynomial of degree TT.
  • 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 styles = open(os.path.expanduser("~/Finance-Bonds/custom.css"), "r").read() return HTML(styles) css_styling()
In [ ]: