You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言中不使用for循环实现分期本息敞口计算?

Solution: Vectorized/Recursive Calculation Without For Loops in R

First, let's formalize the recursive relationships to simplify the calculation (this avoids having to compute interest and exposure separately in a loop):

Given:

  • Let ( E_i ) = exposure at period ( i ), ( I_i ) = interest at period ( i ), ( M_i ) = installment at period ( i )
  • Initial values: ( M_1 = 0 ), ( I_1 = 0 ), ( E_1 = 1000 )
  • For ( i \geq 2 ): ( M_i = 100 ), ( I_i = 0.005 \times E_{i-1} )
  • Substitute ( I_i ) into the exposure formula:
    [
    E_i = E_{i-1} - (M_i - I_i) = E_{i-1} \times 1.005 - M_i
    ]

This linear recursive formula lets us compute the entire exposure sequence in a vectorized way using base R's Reduce() or tidyverse's purrr::accumulate(), no for loops needed.

Method 1: Base R (No External Libraries)

# Define parameters
initial_exposure <- 1000
interest_rate <- 0.005
installment_fixed <- 100
n_periods <- 12  # Adjust this to your desired number of periods

# Create installment vector (first period = 0, rest = 100)
installment <- c(0, rep(installment_fixed, n_periods - 1))

# Compute exposure sequence using Reduce with accumulate=TRUE
exposure <- Reduce(
  function(prev_exposure, current_installment) {
    prev_exposure * (1 + interest_rate) - current_installment
  },
  x = installment[-1],  # Skip first installment since we start with initial exposure
  init = initial_exposure,
  accumulate = TRUE
)

# Compute interest sequence (first value = 0, rest = prior exposure * rate)
interest <- c(0, interest_rate * head(exposure, -1))

# Combine into a data frame
result_df <- data.frame(
  installment = installment,
  interest = interest,
  exposure = exposure
)

# View the result
print(result_df)

Method 2: Tidyverse (Using purrr::accumulate())

If you prefer the tidyverse ecosystem, accumulate() works similarly and is more readable for some:

library(purrr)

# Same parameter setup as above
initial_exposure <- 1000
interest_rate <- 0.005
installment_fixed <- 100
n_periods <- 12
installment <- c(0, rep(installment_fixed, n_periods - 1))

# Compute exposure sequence
exposure <- accumulate(
  installment[-1],
  .init = initial_exposure,
  ~ .x * (1 + interest_rate) - .y
)

# Compute interest and combine into data frame
interest <- c(0, interest_rate * head(exposure, -1))
result_df <- data.frame(installment, interest, exposure)

print(result_df)

Verification

Let's check the first few rows to confirm correctness:

  • Period 1: installment=0, interest=0, exposure=1000 (matches your initial data)
  • Period 2: interest=0.005*1000=5, exposure=1000*1.005 -100=905 (matches your example calculation)
  • Period 3: interest=0.005*905=4.525, exposure=905*1.005 -100=809.525 (correct per the formula)

Both methods are efficient even for large numbers of periods, as they avoid the overhead of explicit for loops in R.

内容的提问来源于stack exchange,提问作者Dom Jo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:51:20