关于数据变更余额的复利计算

Wol*_* Wu 6 r data.table

我有一个data.table,有一个平衡.余额基于每个期间的存款/取款.每个期间都有一个应该适用的利率.但是,我无法将利率与余额相结合,基本上将利率应用于余额,然后使用下一期间的更新余额来计算新值.

Balance_t1 = (0 + Deposit_t1)*(1+Interest_t1)

Balance_t2 = (Balance_t1 + Deposit_t2)*(1+Interest_t2)

Balance_t3 = (Balance_t2 + Deposit_t3)*(1+Interest_t3)
Run Code Online (Sandbox Code Playgroud)

我有以下内容 data.table

dtCash <- data.table(
  Deposit = c(100, 100, -300, 0), 
  Balance = c(100, 200, -100, -100),
  Interest=c(0.1, 0.01, 0.2, 0.1)
)
Run Code Online (Sandbox Code Playgroud)

结果将是:

dtCash <- data.table(
  Deposit = c(100, 100, -300, 0), 
  Balance = c(100, 200, -100, -100), 
  Interest=c(0.1, 0.01, 0.2, 0.1), 
  BalanceWithInterest = c(110, 212.1, -105.48, -116.028)
)
Run Code Online (Sandbox Code Playgroud)

如何在每个时段更新和引用更新的"余额"列?

jos*_*ber 3

看起来你正在寻找“累积和和乘积”,我不知道在 R 中可以实现这一点(除了,例如,使用 @dynamo 的 for 循环)。

话虽如此,这可以通过相对简单的 Rcpp 解决方案有效地完成:

library(Rcpp)
getBalance <- cppFunction(
  "NumericVector getBalance(NumericVector deposit,
                            NumericVector interest) {
    NumericVector result(deposit.size());
    double prevResult = 0.0;
    for (int i=0; i < deposit.size(); ++i) {
      result[i] = (prevResult + deposit[i]) * (1.0 + interest[i]);
      prevResult = result[i];
    }
    return result;
  }")
Deposit <- c(100, 100, -300, 0)  
Interest <- c(0.1, 0.01, 0.2, 0.1)
getBalance(Deposit, Interest)
# [1]  110.000  212.100 -105.480 -116.028
Run Code Online (Sandbox Code Playgroud)

为了了解 Rcpp 相对于 Base R 的效率改进:

# Base R solution
f2 = function(Deposit, Interest) {
  Balance <- c(0, rep(NA, length(Deposit)))
  for (i in 2:length(Balance)) {
    Balance[i] = (Balance[i-1] + Deposit[i-1]) * (1+Interest[i-1])
  }
  return(Balance[-1])
}

set.seed(144)
Deposit <- runif(1000000, -1, 2)
Interest = runif(1000000, 0, 0.05)
system.time(getBalance(Deposit, Interest))
#    user  system elapsed 
#   0.008   0.000   0.008 
system.time(f2(Deposit, Interest))
#    user  system elapsed 
#   4.701   0.008   4.730 
Run Code Online (Sandbox Code Playgroud)