我正在尝试计算不同数量的项目的净现值(NPV),内部收益率(IRR)和回报(PB)时间,以便评估哪个投资项目提供最佳回报。< / p>
到目前为止,我能够分别为每个项目的几行代码计算它。但我要做的是:编写一个函数,接受包含许多不同项目及其现金流量的矩阵,并自动返回NPV,IRR和PB。该函数应接受将现金流量贴现作为附加输入参数的无风险利率(利率)。
有人知道如何解决这个问题吗?关于如何编写一个函数,给出一个带结果的矩阵?
计算每个度量本身的代码如下所示:
library(FinCal)
#NPV Project 1 & Project 2
NPV.Project1 <- NPV(-1000, c(1250, 10, 10, 20, 20), c(1:5), 0.06 )
NPV.Project2 <- NPV(-1000, c(-10, 0, 10, 20, 2000), c(1:5), 0.06 )
#Solution: NPV2 with 509 > NPV1 with 227 -> Pick Project 2
#IRR Project 1 & Project 2
IRR.Project1 <- IRR(-1000, c(1250, 10, 10, 20, 20), c(1:5))
IRR.Project2 <- IRR(-1000, c(-10, 0, 10, 20, 2000), c(1:5))
#Solution: IRR.Project1 with 28% > IRR.Project2 with 15% -> Pick Project 1
#PB Project 1 & Project 2
#PB Project 1
cf0 <- -1000
cf <- c(1250, 10, 10, 20, 20)
t <- 5
temp <- 0
#Calculating the Payback period
for (i in 1:5){
temp[i]=sum(cf[1:i])
}
for (i in 1:5){
if ((temp[i]+cf0) > 0){
payback.Project1 <- i
break
}
print(payback.Project1)
}
#PB Project 2
cf0 <- -1000
cf <- c(-10, 0, 10, 20, 2000)
t <- 5
temp <- 0
#Calculating the Payback period
for (i in 1:5){
temp[i]=sum(cf[1:i])
}
for (i in 1:5){
if ((temp[i]+cf0) > 0){
payback.Project2 <- i
break
}
print(payback.Project2)
}
#Solution: Period for PB with Project 1 is way smaller (1 year) than with Project 2 (5 years)
答案 0 :(得分:0)
W=function(data,rate){
NPV=apply(data,1,npv,r=rate)
IRR=apply(data,1,irr)
PB=apply(data,1,function(x)min(which(cumsum(x)[-1]>0)))
data.frame(NPV=NPV,IRR=IRR,PB=PB)
}
funfun=function(data,r){
mapply(apply,list(data),1,c(function(x)npv(r,x),irr,function(x)min(which(cumsum(x)[-1]>0))))
}
W(dat,0.06)
NPV IRR PB
A 227.3285 0.2808516 1
B 509.3204 0.1508564 5
funfun(dat,0.06)
[,1] [,2] [,3]
A 227.3285 0.2808516 1
B 509.3204 0.1508564 5
当然,您可以给出最终的矩阵列名称。希望这有帮助
以下是使用的矩阵数据:
dat= structure(c(-1000, -1000, 1250, -10, 10, 0, 10, 10, 20, 20, 20,
2000), .Dim = c(2L, 6L), .Dimnames = list(c("A", "B"), NULL))
答案 1 :(得分:0)
您的数据:
cf_df <- data.frame(
Project1 = c(-1000, 1250, 10, 10, 20, 20),
Project2 = c(-1000, -10, 0, 10, 20, 2000)
)
以下是一些有用的功能,您可以考虑保留以处理资本预算问题:
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))
}
pbp <- function(x, ...){
# calculates payback period (PBP)
#
# x - cash flows vector
# ... - ignored
i <- match(1, sign(cumsum(x)))
i-2+(-cumsum(x)[i-1]/x[i])
}
dpbp <- function(x, r, t0=FALSE){
# calculates discounted payback period (DPBP) 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
pbp(dcf(x, r, t0))
}
irr <- function(x, t0=FALSE, ...){
# calculates internal rate of return (IRR) given cash flow
#
# x - cash flows vector
# t0 - cash flow starts in year 0, default is FALSE
tryCatch(uniroot(f=function(i){sum(dcf(x, i, t0))},
interval=c(0,1))$root,
error=function(e) return(NA)
)
}
将这些功能与dplyr
的强大功能相结合,您将拥有一个能够与CFO最喜爱的电子表格竞争的工具箱:
library(dplyr)
library(tidyr)
library(forcats)
cf_df %>%
summarise_all(funs(NPV=npv,PBP=pbp, DPBP=dpbp, IRR=irr), r=0.06, t0=TRUE) %>%
gather(key=key, value = value) %>%
separate(key, into = c("Project", "Metric")) %>%
spread(key=Project, value=value) %>%
mutate(Metric=fct_relevel(Metric, "NPV", "IRR", "PBP", "DPBP"),
Metric=fct_recode(Metric,
`Net Present Value (NPV), USD mln`="NPV",
`Internal Rate of Return (IRR), %`="IRR",
`Payback Period, years` = "PBP",
`Discounted Payback Period, years`="DPBP")) %>%
arrange(as.numeric(Metric))
# Metric Project1 Project2
#1 Net Present Value (NPV), USD mln 227.3284770 509.3204496
#2 Internal Rate of Return (IRR), % 0.2808485 0.1508404
#3 Payback Period, years 0.8000000 4.4900000
#4 Discounted Payback Period, years 0.8480000 4.6592072