1. # \url{http://cvs.moodle.org/contrib/patches/question_calculated_extended/calculated/packages/financial/financial_class.php?view=co}
  2. # @author Enrique Garcia M. \email{egarcia@@egm.as}
  3. # @author Karsten W. \email{k.weinert@@gmx.net}
  4.  
  5. npv <- function(rate, values) sum(values / (1 + rate)^seq_along(values))
  6.  
  7. irr <- function(x, start=0.1) {
  8.   t <- seq_along(x)-1
  9.   f <- function(i) abs(sum(x/(1+i)^t))
  10.   return(nlm(f,start)$estimate)
  11. }
  12.  
  13. fv <- function(rate, nper, pmt, pv = 0.0, type = 0) {
  14.     pvif <- (1+rate)^nper # Present value interest factor
  15.     fvifa <- if(rate==0) nper else ((1+rate)^nper - 1) / rate
  16.     return(-((pv * pvif) + pmt * (1.0 + rate * type) * fvifa))
  17. }
  18.  
  19. pv <- function(rate, nper, pmt, fv = 0.0, type = 0) {
  20.     pvif <- (1+rate)^nper # Present value interest factor
  21.     fvifa <- if(rate==0) nper else ((1+rate)^nper - 1) / rate
  22.     return((-fv - pmt * (1.0 + rate * type) * fvifa) / pvif)
  23. }
  24.  
  25. pmt <- function(rate, nper, pv, fv=0, type=0) {
  26.   rr <- 1/(1+rate)^nper
  27.   res <- (-pv-fv*rr)*rate/(1-rr)
  28.   return(res/(1+rate*type))
  29. }
  30.  
  31. #