

Dear R helpers,
 
 
Yesterday I had raised following query which was addressed by Mr Ellison. The query and the wonderful solution as provided by Mr. Ellison are as given below.
  
## PROBLEM
 
I am calculating the 'Yield to Maturity' for the Bond with following characteristics.
  
Its a $1000 face value, 3 year bond with 10% annual coupon and is priced at 101. The yield to maturity can be calculated after solving the equation - 
  
1010 = [100 / (1+ytm)]  + [100 / (1+ytm)^2] + [ 1100 / (1 + ytm)^3]
  
This can be solved by trial and error method s.t. ytm = 9.601%. I wanted to find out how to solve this equation in R.
   
## SOLUTION 
 
Mr. Elisson had given me following wonderful solution 
 
f.ytm<-function(ytm) 100 / (1+ytm)  +100 / ((1+ytm)^2) + 1100 / ((1 +
ytm)^3) -1010

uniroot(f.ytm, interval=c(0,25))  

#$root has the answer
 
And I got the answer as 9.601.
 
## _____________________________________________________________
 
I was just trying to generalize this solution to any equation and accordingly written a code as given below. 
 
The following input I will be reading using csv file and thus my equation will change if tenure or no_comp etc. changes. So taking into account the variable nature of the input, I am trying to write a generalized code.
 
## Input
 
price = 101       # Price of bond
tenure = 3          
no_comp = 1      # no of times coupon paid in a year.
coupon_rate = 0.10  # i.e. 10%
face_value  = 100
 
# Computations
 
coupon_payment = face_value * coupon_rate
cash_flow = c(rep(c(coupon_payemnt), (no_comp * tenure - 1)), face_value + coupon_payment)
cash_flow
 
## I am trying to customize the code as given by Mr Ellison.
 
f.ytm = function(ytm)
 
{
 
 for (i in 1 : (tenure * no_comp - 1))
 E = NULL
 F = NULL
 {
 E[i] = cash_flow[i]/(1+ytm)^i
 F = (sum(E) + (face_value + coupon_payment)/((1+ytm)^(tenure * no_comp))) - price
    }
}
 
solution = uniroot(f.ytm, interval=c(0,25))  
 
ytm = solution$root
 
However, when I execute this code I get following error.
 
> solution = uniroot(f.ytm, interval=c(0,25))  
Error in uniroot(f.ytm, interval = c(0, 25)) : f.lower = f(lower) is NA

Please guide. ytm should be 0.09601 (i.e. 9.601%)
 
 
with regards
 
Madhavi Bhave
 
 


      Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! http://downloads.yahoo.com/in/internetexplorer/
	[[alternative HTML version deleted]]

