财务盈亏平衡点,考虑一系列现金流量在R中。

huangapple go评论61阅读模式
英文:

financial break even point given a stream of cashflows in R

问题

假设我每个月有一个收入为1200

利率为1% - 因此1年后价格将增加1%。

我想找出每项投资要多少年才能收回成本。

假设一个投资成本为200,000,每月收入为1200。

第一年和随后几年的年收入将是:

第一年 = 1200 * 12
额外年份 = (1200 * 12) * (1 + 0.01)^c(1:5)
c(第一年, 额外年份)

14400.00 14544.00 14689.44 14836.33 14984.70 15134.54

我想要使上面示例中的“5”成为“动态”,直到找到收支平衡点。

在此示例中,我有:

sum(c(第一年, 额外年份))
198854.3

所以投资尚未收回成本。将其调整为“12”将给我收支平衡点:

第一年 = 1200 * 12
额外年份 = (1200 * 12) * (1 + 0.01)^c(1:12)
sum(c(第一年, 额外年份))

如果可能的话,我想确定那一年的月份它将实现收支平衡(因此在这个示例中,在第12年的第12个月实现收支平衡,但其他投资可能在第6年的第8个月实现收支平衡等)。

英文:

Suppose I have every month an income of 1200
The interest rate is 1% - so after 1 year the price will increase 1%

I would like to find out how many years it will take until each investment will break even

Suppose an investment costs 200,000 with a momthly income of 1200

My first year and subsequent years annual income will be:

firstYear = 1200 * 12
additionalYears = (1200*12)*(1+0.01)^c(1:5)
c(firstYear, additionalYears)

14400.00 14544.00 14689.44 14836.33 14984.70 15134.54

I would like to make the "5" in the above example dynamic until it find the breakeven point.

In this example I have:

sum(c(firstYear, additionalYears))
198854.3

So the investment did not breakeven yet. Adjusting it to "12" gives me the breakeven point:

firstYear = 1200 * 12
additionalYears = (1200*12)*(1+0.01)^c(1:12)
sum(c(firstYear, additionalYears))

If possible I would like to determine the month of that year it will break even (so given this example it breakseven in month 12 of year 12, but others might break even in month 8 of year 6 etc.

答案1

得分: 1

以下是代码部分的翻译:

代替使用循环,您可以使用向量。将最大年限设置为100,创建一个包含100个收入增长的向量和一个包含100个增长因子的向量。将两者相乘,然后得到成本(负数)和收入的累积和。计算总和为负数的次数,这就是您的盈亏平衡点。

cost = -200000 # 负成本
income = 1200*12 # 年收入
i = 0.01 # 年度0后的收入增长利率

# 重复14400 101次,将其乘以(1+r)^n - R是向量化的
income100 = rep(income, 101) * ((1+i) ^ seq(0,100)) 

# 从收入的累积和中减去成本
cumincome = cost + cumsum(income100) 

# 有多少次是负数?
yrs = sum(cumincome < 0)

# 下一年还剩多少需要收回
yrs + (-cumincome[yrs] / income100[yrs+1])
[1] 13.06991

将此放入一个函数中:

break_even_years <- function(cost, income, interest=0, period = "monthly"){
  
  if(cost >= 0) cost = -cost
  
  if(period == "monthly") income = income * 12
  
  income100 = rep(income, 101) * ((1+interest) ^ seq(0,100))
  cumincome = cost + cumsum(income100)
  # 有多少次是负数?
  yrs = sum(cumincome < 0)
  
  # 下一年还剩多少需要收回
  yrs + (-cumincome[yrs] / income100[yrs+1])
  
}

使用该函数:

purrr::map2(
  .x = cost,
  .y = investment,
  ~ break_even_years(.x, .y, interest = 0.03, "annual"))
[[1]]
[1] 28.90435

[[2]]
[1] 23.75858

[[3]]
[1] 6.391264

[[4]]
[1] 3.505453

性能基准测试:

library(microbenchmark)

microbenchmark(break_even_years(200000,1000,0.01), 
               find_break_even_year(1000, 200000, 0.01, 100), times = 1000)
Unit: microseconds
                                          expr   min      lq      mean median      uq
          break_even_years(200000, 1000, 0.01)  50.9   87.10  257.4185  119.0  159.05
 find_break_even_year(1000, 200000, 0.01, 100) 853.5 1247.05 3432.5157 1556.2 2391.35
      max neval
  36938.0  1000
 145980.6  1000

希望这些翻译对您有帮助。

英文:

Instead of using a loop, you can use vectors. Set the max years to 100, create a vector of 100 incomes to grow and a vector of 100 growth factors. Multiply the two and get a cumulative sum of the cost (negative) and the incomes. Count the number of times the sum is negative, that is your break even.

cost = -200000 # negative cost
income = 1200*12 # annual income
i = 0.01 # interest rate to grow income after year 0.

# repeat 14400 101 times, multiply it by (1+r)^n - R is vectorised
income100 = rep(income, 101) * ((1+i) ^ seq(0,100)) 

# subtract the cost from the cumulative sum of income 
cumincome = cost + cumsum(income100) 

# how many are negative?
yrs = sum(cumincome &lt; 0)

# how much left to recover in next year
yrs + (-cumincome[yrs] / income100[yrs+1])
[1] 13.06991

Putting this into a function

break_even_years &lt;- function(cost, income, interest=0, period = &quot;monthly&quot;){
  
  if(cost &gt;= 0) cost = -cost
  
  if(period == &quot;monthly&quot;) income = income * 12
  
  income100 = rep(income, 101) * ((1+interest) ^ seq(0,100))
  cumincome = cost + cumsum(income100)
  # how many are negative?
  yrs = sum(cumincome &lt; 0)
  
  # how much left to recover in next year
  yrs + (-cumincome[yrs] / income100[yrs+1])
  
}

Using the function

purrr::map2(
  .x = cost,
  .y = investment,
  ~ break_even_years(.x, .y, interest = 0.03, &quot;annual&quot;))
[[1]]
[1] 28.90435

[[2]]
[1] 23.75858

[[3]]
[1] 6.391264

[[4]]
[1] 3.505453

Benchmarking

library(microbenchmark)

microbenchmark(break_even_years(200000,1000,0.01), 
               find_break_even_year(1000, 200000, 0.01, 100), times = 1000)

Unit: microseconds
                                          expr   min      lq      mean median      uq
          break_even_years(200000, 1000, 0.01)  50.9   87.10  257.4185  119.0  159.05
 find_break_even_year(1000, 200000, 0.01, 100) 853.5 1247.05 3432.5157 1556.2 2391.35
      max neval
  36938.0  1000
 145980.6  1000

答案2

得分: 0

我认为这回答了我的问题。如果有人可以帮助不使用for循环函数,那将非常有帮助。

library(tidyverse)
investment = c(1000, 2000, 5000, 27000)
interest_rate = 0.03
cost = c(45000, 67900, 34678, 98367)
max_years = 100
future_value = list()

find_break_even_year <- function(CF, investment, interest_rate, max_years){
  for (year in 1:max_years){
    #print(year)
    future_value[[year]] <- CF * (1 + interest_rate)^year
    future_value_sums = sum(unlist(future_value))
    if(future_value_sums >= investment)
      return(year)
  }
}

purrr::map2(
  .x = investment,
  .y = cost,
  ~ find_break_even_year(.x, .y, interest_rate = 0.03, max_years = 100)
)
英文:

I think this answers my question. If anybody can help with not using the forloop function that would be very helpful.

library(tidyverse)
investment = c(1000, 2000, 5000, 27000)
interest_rate = 0.03
cost = c(45000, 67900, 34678, 98367)
max_years = 100
future_value = list()

find_break_even_year &lt;- function(CF, investment, interest_rate, max_years){
  for (year in 1:max_years){
    #print(year)
    future_value[[year]] &lt;- CF * (1 + interest_rate)^year
    future_value_sums = sum(unlist(future_value))
    if(future_value_sums &gt;= investment)
      return(year)
  }
}

purrr::map2(
  .x = investment,
  .y = cost,
  ~ find_break_even_year(.x, .y, interest_rate = 0.03, max_years = 100)
)

huangapple
  • 本文由 发表于 2023年2月10日 06:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定