在不同实例中在FinCal中应用NPV功能

问题描述 投票:0回答:2

我试图在FinCal中使用npv函数来确定物理与云服务器上5年期间的回报。

library(FinCal)

前180行的输入:

structure(list(Instance = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Month = c(0L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 
55L, 56L, 57L, 58L, 59L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 
28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 
54L, 55L, 56L, 57L, 58L, 59L), `Stack Cost` = c(40576, 20375.94, 
26934.1295, 24173.6488988, 23374.5681589, 22156.796303, 20514.1159058, 
22893.0239893, 22381.2331456, 21340.8013577, 24599.0848532, 31664.1175566, 
29980.2514319, 28954.2421695, 27917.2288726, 27946.8099023, 27217.8445978, 
25174.7127677, 26939.2260817, 27401.4824941, 28199.1623113, 26672.2574364, 
27584.5229519, 29851.1124262, 29096.3524914, 28106.7452835, 31204.9747189, 
38572.2106284, 33162.4396314, 33672.235937, 34767.0079422, 34938.1269681, 
36283.1379178, 35636.5881624, 38300.1207079, 38727.0234135, 37122.8876092, 
37426.0114304, 38939.9225573, 46841.4926219, 41925.4461027, 44217.6177912, 
44188.9088891, 44406.6550061, 43629.1667764, 45095.7189915, 45192.1872183, 
53610.8519983, 52153.534643, 61020.8631852, 52960.5118554, 53866.0566886, 
55945.8242923, 57524.0249012, 58592.9857353, 55592.7588386, 55073.2684838, 
58666.5396997, 58020.8982841, 59648.9812413, 40576, 24540.3, 
35310.9304, 26754.911901, 28416.7359709, 27223.8113406, 26739.5167563, 
25524.8335209, 24713.6232643, 24625.8056849, 23695.5854924, 22649.8133923, 
21805.3322805, 35219.7904663, 26308.7471747, 24498.225142, 23535.4026835, 
24086.2262606, 28329.2605811, 23773.467166, 21459.2992905, 20120.7791115, 
21335.078882, 21775.9379101, 19853.6423265, 21423.2513905, 19592.6872787, 
19917.1762087, 25129.4387121, 18378.7743335, 19049.0460402, 19375.4046752, 
16537.3400699, 16735.5779372, 17290.8314616, 15826.989282, 15305.4267081, 
14778.2587269, 14245.4423381, 17229.6240882, 19754.9797626, 19360.6085071, 
19948.2317906, 19054.2158392, 23381.421196, 28001.752427, 29145.6474723, 
30047.4538025, 28924.7729543, 29066.9617905, 31391.3393688, 34512.0253179, 
34552.5230922, 34722.8262374, 35941.2415157, 36776.2903777, 37089.914569, 
34349.4802651, 31313.6281517, 36333.4857533, 40576, 23233.8, 
19106.0595, 19402.7958113, 25416.1317071, 23489.6229321, 24017.3994377, 
24342.765145, 24254.5058247, 26926.808959, 28951.2861526, 29622.0258881, 
30511.0903378, 32624.0474801, 32141.1718985, 37730.3412054, 32349.841164, 
34577.0920209, 34419.1695301, 36498.6633727, 46359.5822264, 43561.7303308, 
46606.5891413, 52284.4396083, 50560.3804087, 58001.6672674, 61489.9597625, 
55509.2327725, 55322.2086149, 55478.62463, 54009.2732161, 57659.5641001, 
66814.2365486, 60939.7200641, 60528.8459075, 62136.5869576, 62442.5553766, 
64313.5049571, 64388.5989147, 60697.2138562, 61606.3352057, 61909.5931391, 
60364.8063798, 57935.5974737, 61209.4110695, 63763.5539428, 66090.1442789, 
65025.164051, 66997.0678684, 67837.9197935, 68043.9045788, 75477.2483755, 
68620.9657927, 67135.1429231, 71810.9347346, 67402.7596534, 66279.9768119, 
68997.8668527, 70598.1663854, 67531.3152774), `Cloud Cost` = c(26225, 
24985.355, 26081.01605, 28044.7975572, 26645.8159446, 24722.6435231, 
21906.8934815, 23919.050745, 24156.6529652, 23173.4137642, 27956.8544206, 
29069.0868171, 31577.2821607, 28509.272162, 28327.6284311, 29750.3304929, 
29587.9883653, 27765.564108, 31309.6380778, 32446.7443926, 34855.1748832, 
34266.5859603, 35344.1320296, 39400.3950817, 39436.3576471, 38149.7723922, 
41882.5150594, 44697.4587654, 44038.1927483, 45540.3690779, 46314.381563, 
45138.806592, 48024.3467632, 48103.5682303, 50361.2837922, 50856.7150388, 
50374.6135355, 51264.72246, 54232.9872328, 57692.5115965, 57863.5442616, 
59983.2092944, 58337.2640039, 58236.0381845, 58635.5307914, 61787.4233665, 
62225.985044, 66673.5870572, 70417.1118141, 76383.6846516, 72537.6398212, 
72453.1394013, 76627.5749616, 77443.586842, 79601.1582784, 76039.6461876, 
75452.5665367, 78513.3435654, 77453.6111671, 78723.4019607, 26225, 
29854.58, 29604.993275, 30584.51552, 31986.3038154, 29153.7794391, 
29392.5860872, 26697.376117, 26456.6155322, 24738.0959906, 24804.4851646, 
22556.4065085, 19811.7686497, 22650.4509327, 24073.7305007, 21276.5111272, 
19938.9074786, 20985.3816557, 19925.1196038, 19603.5701313, 17099.5029578, 
16727.5011904, 16772.1234203, 18051.6524548, 14025.6676523, 15107.5775993, 
13012.3192558, 12593.7019859, 13824.1400484, 10414.0835856, 13078.5953907, 
14178.6387073, 11472.3231837, 11604.5661997, 13342.1179319, 11474.6138667, 
11089.9852666, 12010.9745321, 9849.15004257, 14461.8189772, 17331.954816, 
16265.1454606, 18258.7691652, 18488.7667544, 23258.8730453, 29405.4894916, 
32468.3433183, 32471.5131732, 32482.8464422, 32401.5986603, 37118.0408773, 
40235.304356, 40149.1175849, 41454.8636103, 41000.2143665, 43359.4249374, 
43377.1464583, 40572.9304796, 36892.9549128, 37462.0086872, 26225, 
28782.245, 24343.77305, 25468.53689, 26904.9341718, 30079.5813207, 
31173.0784235, 31743.5108683, 31020.1159199, 32529.0838947, 34057.671747, 
35396.897855, 34723.0335173, 36379.36238, 35953.9763214, 37574.3071678, 
38126.921322, 42358.9027077, 43222.5242297, 44842.4634302, 50191.1254946, 
54944.8282259, 57904.6251222, 58110.1258147, 62478.8849521, 64704.7662633, 
68199.1460856, 64945.5140677, 63175.5225962, 63876.6229239, 59951.5413363, 
63676.3894892, 67228.2356599, 63981.1056769, 62443.9035216, 64727.4925848, 
65865.6429293, 68280.0117056, 69833.3844609, 66419.0646262, 68129.2029963, 
67263.6232588, 64708.3439582, 61405.9063161, 65807.7539822, 67238.232041, 
69561.9349339, 69060.3368444, 71396.6589574, 72892.7996983, 74429.1146724, 
74854.452701, 71349.8958556, 70338.0641944, 67505.2271309, 69961.440786, 
67332.8932319, 71086.57946, 66843.3644286, 67175.6340875), Discount_Rate = c(0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07), difference = c(-14351, 4609.415, -853.113450000001, 
3871.1486584, 3271.2477857, 2565.8472201, 1392.7775757, 1026.0267557, 
1775.4198196, 1832.6124065, 3357.7695674, -2595.0307395, 1597.0307288, 
-444.9700075, 410.399558500001, 1803.5205906, 2370.1437675, 2590.8513403, 
4370.4119961, 5045.2618985, 6656.0125719, 7594.3285239, 7759.6090777, 
9549.2826555, 10340.0051557, 10043.0271087, 10677.5403405, 6125.248137, 
10875.7531169, 11868.1331409, 11547.3736208, 10200.6796239, 11741.2088454, 
12466.9800679, 12061.1630843, 12129.6916253, 13251.7259263, 13838.7110296, 
15293.0646755, 10851.0189746, 15938.0981589, 15765.5915032, 14148.3551148, 
13829.3831784, 15006.364015, 16691.704375, 17033.7978257, 13062.7350589, 
18263.5771711, 15362.8214664, 19577.1279658, 18587.0827127, 20681.7506693, 
19919.5619408, 21008.1725431, 20446.887349, 20379.2980529, 19846.8038657, 
19432.712883, 19074.4207194, -14351, 5314.28, -5705.937125, 3829.603619, 
3569.5678445, 1929.9680985, 2653.0693309, 1172.5425961, 1742.9922679, 
112.2903057, 1108.8996722, -93.4068837999985, -1993.5636308, 
-12569.3395336, -2235.016674, -3221.7140148, -3596.4952049, -3100.8446049, 
-8404.1409773, -4169.8970347, -4359.7963327, -3393.2779211, -4562.9554617, 
-3724.2854553, -5827.9746742, -6315.6737912, -6580.3680229, -7323.4742228, 
-11305.2986637, -7964.6907479, -5970.4506495, -5196.7659679, 
-5065.0168862, -5131.0117375, -3948.7135297, -4352.3754153, -4215.4414415, 
-2767.2841948, -4396.29229553, -2767.805111, -2423.0249466, -3095.4630465, 
-1689.4626254, -565.449084799999, -122.5481507, 1403.7370646, 
3322.695846, 2424.0593707, 3558.0734879, 3334.6368698, 5726.7015085, 
5723.2790381, 5596.5944927, 6732.03737290001, 5058.9728508, 6583.1345597, 
6287.2318893, 6223.4502145, 5579.3267611, 1128.5229339, -14351, 
5548.445, 5237.71355, 6065.7410787, 1488.8024647, 6589.9583886, 
7155.6789858, 7400.7457233, 6765.6100952, 5602.2749357, 5106.3855944, 
5774.8719669, 4211.9431795, 3755.3148999, 3812.8044229, -156.034037600002, 
5777.080158, 7781.8106868, 8803.3546996, 8343.8000575, 3831.5432682, 
11383.0978951, 11298.0359809, 5825.68620639999, 11918.5045434, 
6703.0989959, 6709.1863231, 9436.2812952, 7853.31398129999, 8397.9982939, 
5942.26812019999, 6016.82538910001, 413.9991113, 3041.3856128, 
1915.0576141, 2590.9056272, 3423.0875527, 3966.5067485, 5444.78554619999, 
5721.85077, 6522.86779059999, 5354.0301197, 4343.5375784, 3470.3088424, 
4598.3429127, 3474.6780982, 3471.790655, 4035.17279339999, 4399.59108899999, 
5054.87990480001, 6385.21009360001, -622.795674499997, 2728.93006290001, 
3202.9212713, -4305.70760369999, 2558.6811326, 1052.91642000001, 
2088.7126073, -3754.8019568, -355.681189900002)), row.names = c(NA, 
-180L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Instance", 
"Month", "Stack Cost", "Cloud Cost", "Discount_Rate", "difference"
))

数据框的负责人:

head(npvproblem)


 # A tibble: 6 x 6
  Instance Month `Stack Cost` `Cloud Cost` Discount_Rate  difference
     <int> <int>        <dbl>        <dbl>         <dbl>       <dbl>
1        0     0     40576.00     26225.00          0.07 -14351.0000
2        0     1     20375.94     24985.35          0.07   4609.4150
3        0     2     26934.13     26081.02          0.07   -853.1135
4        0     3     24173.65     28044.80          0.07   3871.1487
5        0     4     23374.57     26645.82          0.07   3271.2478
6        0     5     22156.80     24722.64          0.07   2565.8472

FinCal的npv函数遵循以下语法:

npv(r, cf)

其中r =贴现率,cf =现金流量,其中初始现金流量是支出,后续现金流量是回报。

我想要做的是:我想通过Instance应用此功能,其中差异是我们初始模拟产生的现金流量。因此,在Instance 0的情况下,我们基本上会:

npv(r=.07, cf=c(-14351.00000, 4609.41500,... down to the final month of instance zero)

我想在我们模拟的所有六千个实例中应用它。这感觉就像一个lapply类型的问题,因为我想返回一个列表,但我承认我对如何构建这样的东西感到茫然。

r apply lapply
2个回答
0
投票

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

dcf <- function(x, r, t0=FALSE){
  # calculates discounted cash flows (DCF) given cash flow and discount rate
  #
  # x - cash flows vector
  # r - vector or numeric value of discount rate(s), 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.
  #      Will be ignored if r is a vector
  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)

glimpse(df)
#> Observations: 180
#> Variables: 6
#> $ Instance      <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
#> $ Month         <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...
#> $ `Stack Cost`  <dbl> 40576.00, 20375.94, 26934.13, ...
#> $ `Cloud Cost`  <dbl> 26225.00, 24985.35, 26081.02, ...
#> $ Discount_Rate <dbl> 0.07, 0.07, 0.07, 0.07, 0.07, ...
#> $ difference    <dbl> -14351.0000, 4609.4150, -853.1...


df %>% group_by(Instance) %>% 
  arrange(Month) %>%  
  summarise(NPV=npv(x=difference, r=Discount_Rate))

#> # A tibble: 3 x 2
#>   Instance       NPV
#>      <int>     <dbl>
#> 1        0  45439.03
#> 2        1 -29167.37
#> 3        2  60362.36

您建议的库FinCaldoes不允许可变折扣率。因此,您必须使用单个值(比如每组中的第一个)。 FinCal也使用从零开始的折扣(在我的函数中用t0=TRUE模拟)因此结果会略有不同

df %>% group_by(Instance) %>% 
  arrange(Month) %>%  
  summarise(NPV=FinCal::npv(cf=difference, r=first(Discount_Rate)))

#> # A tibble: 3 x 2
#>   Instance       NPV
#>      <int>     <dbl>
#> 1        0  48619.76
#> 2        1 -31209.09
#> 3        2  64587.73

0
投票

可能有更优雅的方式,我也假设每个实例id的速率是常量,但这看起来很有效。

> df
  ind instance month    stack    cloud rate        diff
1   1        0     0 40576.00 26225.00 0.07 -14351.0000
2   2        0     1 20375.94 24985.35 0.07   4609.4150
3   3        0     2 26934.13 26081.02 0.07   -853.1135
4   4        0     3 24173.65 28044.80 0.07   3871.1487
5   5        0     4 23374.57 26645.82 0.07   3271.2478
6   6        0     5 22156.80 24722.64 0.07   2565.8472
> instances <- unique(df$instance)
> sapply(instances, function(x) npv(unique(df[df$instance==x,]$rate), df[df$instance==x,]$diff))
[1] -3303.235
>
© www.soinside.com 2019 - 2024. All rights reserved.