# [R] Yield to Maturity using R

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Tue Feb 2 13:18:12 CET 2010

```Hi Madhavi,
the error message means, that your function returns NA evaluated at the
lower limit of the search interval.
try f.ytm(0) to check that.

I think,

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
}

will not return what you expected, since the code evaluates solely E
=NULL (tenure * no_comp - 1) times. (the opening curly bracket is misplaced)

hth

> 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))
>
>
> 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
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

```