[R-SIG-Finance] yield to maturity with optim?

Ajay Shah ajayshah at mayin.org
Fri Oct 24 20:02:41 CEST 2008


On Fri, Oct 24, 2008 at 02:01:18PM -0400, Whit Armstrong wrote:
> anyone using optim to solve for bond yields given price?
> 
> I've anyone has any quick and dirty code, I would be grateful.

You mean:

ytm <- function(C, T, p) {
  npv <- function(C, T, r) {
    sum(C * (1 + (0.01*r))^-T)
  }
  uniroot(function(r) npv(C, T, r) - p, interval=c(-20,100), tol=0.0001)$root
}

?

-- 
Ajay Shah                                      http://www.mayin.org/ajayshah  
ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.



More information about the R-SIG-Finance mailing list