计算所有时间点的现金流的净现值

时间:2017-11-21 00:06:01

标签: r finance

我有以下数据框包含几个项目的现金流量。例如:

test <- data.frame(ID = c(rep("A",3), rep("B",4)), 
        time = c("y3","y2","y1","y4","y3","y2","y1"),
        Cfs= c(rep(1,3),rep(2,4)),
        interest = c(rep(0.1,3),rep(0.05,4)))

ID  time    CFs interest
A   y3      1   0.1
A   y2      1   0.1
A   y1      1   0.1
B   y4      2   0.05
B   y3      2   0.05
B   y2      2   0.05
B   y1      2   0.05

我想在每个项目的每个时间点产生净现值,因此最终输出应该如下所示:

ID  time   CFs  interest    NPV
A   y3     1    0.1         2.487
A   y2     1    0.1         1.736
A   y1     1    0.1         0.909
B   y4     2    0.05        7.092
B   y3     2    0.05        5.446
B   y2     2    0.05        3.719
B   y1     2    0.05        1.905

我能够通过阅读一些旧帖来计算每个项目的总现金流的净现值,但我不知道在每个时间段如何做到这一点。此外,由于实际数据集非常大(300k +),我也试图避免循环。

由于

1 个答案:

答案 0 :(得分:1)

您可能会发现其中一些辅助函数很有用

dcf <- function(x, r, t0=FALSE){
  # calculates discounted cash flows (DCF) given cash flow and discount rate
  #
  # x - cash flows vector
  # r - vector or discount rates, in decimals. Single values will be recycled
  # t0 - cash flow starts in year 0, default is FALSE, i.e. discount rate in first period is zero.
  if(length(r)==1){
    r <- rep(r, length(x))
    if(t0==TRUE){r[1]<-0}
  }
  x/cumprod(1+r)
}

npv <- function(x, r, t0=FALSE){
  # calculates net present value (NPV) given cash flow and discount rate
  #
  # x - cash flows vector
  # r - discount rate, in decimals
  # t0 - cash flow starts in year 0, default is FALSE
  sum(dcf(x, r, t0))
}

现在,我们可以应用dplyr

的力量
library(dplyr)

test %>% mutate_if(is.factor, as.character) %>% 
  arrange(ID, time) %>% 
  group_by(ID) %>% 
  mutate(DCF=cumsum(dcf(x=Cfs, r=interest)))

#> # A tibble: 7 x 5
#> # Groups:   ID [2]
#>      ID  time   Cfs interest       DCF
#>   <chr> <chr> <dbl>    <dbl>     <dbl>
#> 1     A    y1     1     0.10 0.9090909
#> 2     A    y2     1     0.10 1.7355372
#> 3     A    y3     1     0.10 2.4868520
#> 4     B    y1     2     0.05 1.9047619
#> 5     B    y2     2     0.05 3.7188209
#> 6     B    y3     2     0.05 5.4464961
#> 7     B    y4     2     0.05 7.0919010