/
cf_functions.R
1215 lines (1085 loc) · 37.7 KB
/
cf_functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
parse_for_currency_value <-
function(x) {
value <-
x %>%
readr::parse_number() %>%
formattable::currency()
return(value)
}
parse_for_percentage <-
function(x) {
value <-
x %>%
parse_number()
if (value >= 1) {
value <-
value / 100
}
value <-
value %>%
percent()
return(value)
}
parse_multiple <-
function(x) {
value <-
x %>%
parse_number
return(value)
}
cap_rate_valuation <-
function(cap_rate = .0615,
net_operating_income = "$27,500,000",
cost_of_sale = "5%",
debt_balance = "$350,000,000",
return_wide = T) {
noi <-
net_operating_income %>%
parse_for_currency_value
debt <-
debt_balance %>%
parse_for_currency_value()
pct_cap_rate <-
cap_rate %>%
parse_for_percentage()
pct_sale_cost <-
cost_of_sale %>%
parse_for_percentage()
amountValuationGross <-
noi / cap_rate
amountCostSale <-
-((pct_sale_cost %>% as.numeric) * amountValuationGross)
amountValuationNet <-
amountValuationGross + amountCostSale
amountDebtRepayment <-
-min(amountValuationNet, debt)
amountEquityDistribution <-
-max(0, amountValuationNet + amountDebtRepayment) %>% currency
cash_check <-
!amountValuationGross + amountCostSale + amountDebtRepayment + amountEquityDistribution == 0
if (cash_check) {
stop("Cash waterfall does not tie")
}
value_df <-
tibble(
pctCapRate = pct_cap_rate,
pctCostSale = pct_sale_cost,
amountNetOperatingIncome = noi,
amountDebtBalance = debt,
amountValuationGross,
amountCostSale,
amountValuationNet,
amountDebtRepayment,
amountEquityDistribution
)
if (!return_wide) {
value_df <-
value_df %>%
dplyr::select(-amountValuationNet) %>%
gather(
item,
value,
-c(
pctCapRate,
amountNetOperatingIncome,
amountDebtBalance,
pctCostSale
)
) %>%
mutate(value = value %>% currency(digits = 2)) %>%
suppressWarnings()
}
return(value_df)
}
ebtida_multiple_value <-
function(ebitda_multiple = 10,
ebitda = "$27,500,000",
cost_of_sale = "5%",
debt_balance = "$350,000,000",
return_wide = T) {
ebitda <-
ebitda %>%
parse_for_currency_value
debt <-
debt_balance %>%
parse_for_currency_value()
multipleEBITDA <-
ebitda_multiple %>%
parse_multiple()
pct_sale_cost <-
cost_of_sale %>%
parse_for_percentage()
amountValuationGross <-
ebitda * multipleEBITDA
amountCostSale <-
-((pct_sale_cost %>% as.numeric) * amountValuationGross)
amountValuationNet <-
amountValuationGross + amountCostSale
amountDebtRepayment <-
-min(amountValuationNet, debt)
amountEquityDistribution <-
-max(0, amountValuationNet + amountDebtRepayment) %>% currency
cash_check <-
!amountValuationGross + amountCostSale + amountDebtRepayment + amountEquityDistribution == 0
if (cash_check) {
stop("Cash waterfall does not tie")
}
value_df <-
tibble(
multipleEBITDA,
pctCostSale = pct_sale_cost,
amountEBITDA = ebitda,
amountDebtBalance = debt,
amountValuationGross,
amountCostSale,
amountValuationNet,
amountDebtRepayment,
amountEquityDistribution
)
if (!return_wide) {
value_df <-
value_df %>%
dplyr::select(-amountValuationNet) %>%
gather(item,
value,
-c(
multipleEBITDA,
amountEBITDA,
amountDebtBalance,
pctCostSale
)) %>%
mutate(value = value %>% currency(digits = 2)) %>%
suppressWarnings()
}
return(value_df)
}
#' Residual value, capitalization rate method
#'
#' This function calculates \href{https://en.wikipedia.org/wiki/Residual_value}{residual values}
#' based upon the specified inputs. See \code{\link{calculate_residual_valuation_ebitda_multiples}} for
#' EBITDA multiple method.
#'
#' @param cap_rates vector of capitalization rates in percent or character form
#' @param net_operating_income vector of net operating income in numeric or character numeric/currency form
#' @param cost_of_sale vector of cost of sale in percent or character percent form
#' @param debt_balance vector of anticipated debt balance at sale in numeric or character numeric/currency form
#' @param return_wide \code{TRUE} return wide or \code{FALSE}
#' @import readr dplyr purrr formattable
#' @return a \code{tibble}
#' @family calculation
#' @family residual value calculation
#' @export
#'
#' @examples
#' calculate_residual_valuation_cap_rates(cap_rates = c(.05, .0525, .06, .2),
#' net_operating_income = "$27,500,000", cost_of_sale = "5%",debt_balance = "$350,000,000", return_wide = T)
calculate_residual_valuation_cap_rates <-
function(cap_rates = c(.05, .0525, .06, .2),
net_operating_income = "$27,500,000",
cost_of_sale = "5%",
debt_balance = "$350,000,000",
return_wide = T) {
scenario_matrix <-
expand.grid(
cap_rate = cap_rates,
noi = net_operating_income,
cost_sale = cost_of_sale,
debt = debt_balance,
stringsAsFactors = F
) %>%
as_tibble
scenario_df <-
1:nrow(scenario_matrix) %>%
future_map_dfr(function(x) {
cap_rate_valuation(
cap_rate = scenario_matrix$cap_rate[[x]],
net_operating_income = scenario_matrix$noi[[x]],
cost_of_sale = scenario_matrix$cost_sale[[x]],
debt_balance = scenario_matrix$debt[[x]],
return_wide = T
)
}) %>%
mutate(idScenario = 1:n()) %>%
dplyr::select(idScenario, everything())
scenario_df <-
scenario_df %>%
mutate_at(.vars =
scenario_df %>% dplyr::select(dplyr::matches("^pct")) %>% names,
funs(. %>% percent(digits = 2))) %>%
mutate_at(.vars =
scenario_df %>% dplyr::select(dplyr::matches("^amount")) %>% names,
funs(. %>% currency(digits = 2)))
if (!return_wide) {
scenario_df <-
scenario_df %>%
dplyr::select(-amountValuationNet) %>%
gather(
item,
value,
-c(
idScenario,
pctCapRate,
amountNetOperatingIncome,
amountDebtBalance,
pctCostSale
)
) %>%
mutate(value = value %>% currency(digits = 2)) %>%
suppressWarnings()
}
return(scenario_df)
}
#' Residual value, EBITDA multiple method
#'
#' This function calculates \href{https://en.wikipedia.org/wiki/Residual_value}{residual values}
#' based upon the specified inputs. See \code{\link{calculate_residual_valuation_cap_rates}} for
#' capitalization rate method.
#'
#' @param ebitda_multiples vector of EBITDA multiples in numeric or character
#' @param ebitda vector of EBITDA in numeric or character numeric/currency form
#' @param cost_of_sale vector of cost of sale in percent or character percent form
#' @param debt_balance vector of anticipated debt balance at sale in numeric or character numeric/currency form
#' @param return_wide return data in wide or long form
#' @import readr dplyr purrr formattable
#' @return a \code{tibble}
#' @export
#' @family calculation
#' @family residual value calculation
#' @examples
#' calculate_residual_valuation_ebitda_multiples(ebitda_multiples = c(5, 10, 15, 20), ebitda = "$27,500,000", cost_of_sale = "5%", debt_balance = "$350,000,000", return_wide = T)
calculate_residual_valuation_ebitda_multiples <-
function(ebitda_multiples = c(5, 10, 15, 20),
ebitda = "$27,500,000",
cost_of_sale = "5%",
debt_balance = "$350,000,000",
return_wide = T) {
scenario_matrix <-
expand.grid(
ebitda_multiple = ebitda_multiples,
ebitda = ebitda,
cost_sale = cost_of_sale,
debt = debt_balance,
stringsAsFactors = F
) %>%
as_tibble
scenario_df <-
1:nrow(scenario_matrix) %>%
future_map_dfr(function(x) {
ebtida_multiple_value(
ebitda_multiple = scenario_matrix$ebitda_multiple[[x]],
ebitda = scenario_matrix$ebitda[[x]],
cost_of_sale = scenario_matrix$cost_sale[[x]],
debt_balance = scenario_matrix$debt[[x]],
return_wide = T
)
}) %>%
mutate(idScenario = 1:n()) %>%
dplyr::select(idScenario, everything())
scenario_df <-
scenario_df %>%
mutate_at(.vars =
scenario_df %>% dplyr::select(dplyr::matches("^pct")) %>% names,
funs(. %>% percent(digits = 2))) %>%
mutate_at(.vars =
scenario_df %>% dplyr::select(dplyr::matches("^amount")) %>% names,
funs(. %>% currency(digits = 2)))
if (!return_wide) {
scenario_df <-
scenario_df %>%
dplyr::select(-amountValuationNet) %>%
gather(item,
value,
-c(
multipleEBITDA,
amountEBITDA,
amountDebtBalance,
pctCostSale
)) %>%
mutate(value = value %>% currency(digits = 2)) %>%
suppressWarnings()
}
return(scenario_df)
}
post_money_valuation <-
function(pre_money_valuation = "$45,000,000",
percent_sold = "10%") {
options(scipen = 999999)
pre_money <-
pre_money_valuation %>%
parse_for_currency_value()
pct_sold <-
percent_sold %>%
parse_for_percentage() %>%
as.numeric
new_capital <-
(pct_sold * pre_money) %>% currency
total_val <-
pre_money + new_capital
valuation_data <-
tibble(valuationPreMoney = pre_money,
amountCapitalInvestment = new_capital) %>%
mutate(
pctOwnershipExistingShareholders = (pre_money / total_val) %>% percent,
pctOwnershipNewInvestment = (new_capital / total_val) %>% percent
)
return(valuation_data)
}
#' Post money valuation splits
#'
#' This function calculates the the post-money
#' ownership splits of an entity for specified inputs.
#'
#' @param pre_money_valuation vector of of valuations in numeric or character
#' @param percent_sold vector of of amount of business sold in percent or character percent form
#' @param return_wide Return data in wide or long form
#' @import readr dplyr purrr formattable tidyr
#' @return a \code{tibble()}
#' @family calculation
#' @family venture capital
#' @export
#'
#' @examples
#' calculate_valuation_post_money(pre_money_valuation = "$45,000,000", percent_sold = "10%", return_wide = T)
calculate_valuation_post_money <-
function(pre_money_valuation = "$45,000,000",
percent_sold = "10%",
return_wide = T) {
scenario_matrix <-
expand.grid(
pre_money_valuation = pre_money_valuation,
percent_sold = percent_sold,
stringsAsFactors = F
) %>%
as_tibble
scenario_df <-
1:nrow(scenario_matrix) %>%
future_map_dfr(function(x) {
post_money_valuation(
pre_money_valuation = scenario_matrix$pre_money_valuation[[x]],
percent_sold = scenario_matrix$percent_sold[[x]]
)
}) %>%
mutate(idScenario = 1:n()) %>%
dplyr::select(idScenario, everything())
scenario_df <-
scenario_df %>%
mutate_at(.vars =
scenario_df %>% dplyr::select(dplyr::matches("^pct")) %>% names,
funs(. %>% percent(digits = 2))) %>%
mutate_at(.vars =
scenario_df %>% dplyr::select(dplyr::matches("^amount|valuation")) %>% names,
funs(. %>% currency(digits = 2)))
if (!return_wide) {
scenario_df <-
scenario_df %>%
dplyr::select(-dplyr::matches("pct")) %>%
gather(item, value, -c(idScenario)) %>%
mutate(value = value %>% currency(digits = 2)) %>%
suppressWarnings()
}
return(scenario_df)
}
#' Share proceeds
#'
#' This function calculates
#' distributable proceeds from
#' a share sale based upon specified inputs.
#'
#' @param price numeric price
#' @param shares share count
#'
#' @return numeric \code{vector}
#' @export
#' @family calculation
#' @importFrom formattable currency
#' @examples
#' calculate_share_proceeds(price = 9, shares = 150000)
calculate_share_proceeds <-
function(price = 10, shares = 1000000) {
proceeds <-
(price * shares) %>% currency()
return(proceeds)
}
calculate_basis <-
function(purchase_price = "$10,000,000",
capitalized_acquisition_costs = "$300,0000",
capital_investment = "$1,200,000") {
options(scipen = 999)
if (purchase_price %>% is_null) {
stop("Please enter a purchase price")
}
amountPurchasePrice <-
purchase_price %>%
parse_for_currency_value()
if (!capitalized_acquisition_costs %>% is_null) {
amountCapitalizedCosts <-
capitalized_acquisition_costs %>%
parse_for_currency_value()
} else {
amountCapitalizedCosts <-
0
}
if (!capitalized_acquisition_costs %>% is_null) {
amountCapitalInvestment <-
capital_investment %>%
parse_for_currency_value()
} else {
capital_investment <-
0
}
basis_df <-
tibble(amountPurchasePrice,
amountCapitalInvestment,
amountCapitalizedCosts) %>%
mutate(amountBasis = amountPurchasePrice + amountCapitalInvestment + amountCapitalizedCosts)
return(basis_df)
}
calculate_capitalization <-
function(purchase_price = "$9,700,000",
capitalized_acquisition_costs = "$300,000",
capital_investment = "$0",
loan_to_cost = .7,
borrow_capital_investment = T,
borrow_capitalized_costs = F,
leverage_threshold = .95) {
if (loan_to_cost %>% is_null()) {
stop("Please enter a loan to cost even if it is zero")
}
if (leverage_threshold %>% is_null()) {
leverage_threshold <-
1
}
pct_ltc <-
loan_to_cost %>% parse_for_percentage()
if (pct_ltc > leverage_threshold) {
leverage_message <-
"\nDon't be a reckless idiot, remember what happend to Lehman Brothers???\nDon't know Lehman Brothers, Google it\n" %>%
paste0(
pct_ltc,
' is unprudent leverage\nA more reasonable amount of leverage is: ',
leverage_threshold %>% formattable::percent(),
'\nChange your leverage assumptions and try again'
)
stop(leverage_message)
}
basis_df <-
calculate_basis(
purchase_price = purchase_price,
capitalized_acquisition_costs = capitalized_acquisition_costs,
capital_investment = capital_investment
)
if (borrow_capitalized_costs) {
debt_basis <-
basis_df %>% mutate(amountDebtBasis = amountPurchasePrice + amountCapitalizedCosts) %>%
.$amountDebtBasis
} else {
debt_basis <-
basis_df$amountPurchasePrice
}
loan_proceeds <-
debt_basis * pct_ltc
if (borrow_capital_investment) {
loan_proceeds <-
loan_proceeds + basis_df$amountCapitalInvestment
}
capital_stack_df <-
basis_df %>%
mutate(
amountLoanProceeds = -loan_proceeds,
amountEquity = -(amountBasis + amountLoanProceeds)
)
return(capital_stack_df)
}
get_data_monthly_periods <-
function(start_date = "2016-06-01",
term_years = 25,
term_months = 0) {
periods <-
term_years * 12 + term_months
periods <-
0:periods
start_date <-
start_date %>% lubridate::ymd %>% as.Date()
get_end_of_period <-
function(period = 0) {
if (period == 0) {
period_date <-
start_date %m+% months(period) %>%
as.character() %>%
as.Date()
}
if (period == 1) {
period_date <-
start_date %m+% months(0) %>%
timeDate::timeLastDayInMonth %>%
as.character() %>%
as.Date()
}
if (period > 1) {
period_date <-
start_date %m+% months(period - 1) %>%
timeDate::timeLastDayInMonth %>%
as.character() %>%
as.Date()
}
period_df <-
tibble(idPeriod = period, datePeriod = period_date)
return(period_df)
}
all_periods <-
periods %>%
future_map(function(x) {
get_end_of_period(period = x)
}) %>%
compact %>%
bind_rows()
all_periods <-
all_periods %>%
mutate(yearPeriod = ifelse(idPeriod == 0, 0, (idPeriod %/% 12) + 1)) %>%
dplyr::select(idPeriod, yearPeriod, everything())
return(all_periods)
}
pmt <-
function (r, n, pv, fv, type = 0) {
if (type != 0 && type != 1) {
print("Error: type should be 0 or 1!")
}
else {
pmt <- (pv + fv / (1 + r) ^ n) * r / (1 - 1 / (1 + r) ^ n) * (-1) *
(1 + r) ^ (-1 * type)
return(pmt)
}
}
#' Loan payment calculation
#'
#' Calculate loan payment information
#' based upon specified parameters.
#'
#' @param loan_start_date date loan starts in year, month, date form
#' @param amount_initial_draw amount of initial draw
#' @param is_interest_only \code{TRUE} has interest only periods
#' @param interest_only_periods count of interest only periods
#' @param interest_rate interest rate in character or numeric form
#' @param is_actual_360 \code{TRUE} interest calculated on actual/360 basis
#' @param amortization_years amortization in years
#' @param amortization_months amortization additional months
#' @param term_years term of the loan in years
#' @param term_months term of the loan additional months
#' @param pct_loan_fee loan fee in percent
#' @param balloon_year year loan baloons
#' @param override_monthly_interest \code{TRUE} override
#' @param interest_reserve_period periods of interest reserve
#' @param balloon_month month loan baloons
#' @param return_annual_summary \code{TRUE} returns annual summary
#'
#' @return a \code{tibble}
#' @export
#' @family leveraged finance calculation
#' @family calculation
#' @examples
#' calculate_loan_payment(loan_start_date = "2016-06-01", amount_initial_draw = 3000, is_interest_only = F, interest_only_periods = 24,
#' interest_rate = "10%", is_actual_360 = TRUE, amortization_years = 10, amortization_months = 0,
#' term_years = 10, term_months = 0, pct_loan_fee = 0, balloon_year = 10,
#' override_monthly_interest = FALSE, interest_reserve_period = 0, balloon_month = 0,
#' return_annual_summary = F)
calculate_loan_payment <-
function(loan_start_date = "2016-06-01",
amount_initial_draw = 3000,
is_interest_only = F,
interest_only_periods = 24,
interest_rate = "10%",
is_actual_360 = TRUE,
amortization_years = 10,
amortization_months = 0,
term_years = 10,
term_months = 0,
pct_loan_fee = 0,
balloon_year = 10,
override_monthly_interest = FALSE,
interest_reserve_period = 0,
balloon_month = 0,
return_annual_summary = F) {
options(scipen = 99999)
options(digits = 10)
interest_rate <-
interest_rate %>%
parse_for_percentage()
if (is_actual_360 == T) {
daily_interest <-
interest_rate / 360
net_rate <-
interest_rate / 360 * 365
} else {
daily_interest <-
interest_rate / 365
net_rate <-
interest_rate
}
amortization_periods <-
(amortization_years * 12) + amortization_months
loan_periods <-
(balloon_year * 12) + balloon_month
loan_period_df <-
get_data_monthly_periods(start_date = loan_start_date,
term_years = term_years,
term_months = term_months)
loan_period_df <-
loan_period_df %>%
mutate(
isIO = ifelse(is_interest_only == T &
idPeriod <= interest_only_periods, T, F),
isActiveLoan = ifelse(idPeriod <= loan_periods, T, F),
amountInitialDraw = ifelse(idPeriod == 0, amount_initial_draw, 0),
amountLoanFee = amountInitialDraw * pct_loan_fee
)
loan_period_df <-
loan_period_df %>%
dplyr::filter(isActiveLoan == T)
periods <-
loan_period_df$idPeriod
all_payment_data <-
tibble()
for (period in periods) {
period_index <-
period + 1
datePeriod <-
loan_period_df$datePeriod[period_index]
drawInitial <-
loan_period_df$amountInitialDraw[period_index]
drawAdditional <-
0 # loan_period_df$amountAdditionalDraw[period_index] -- layer in eventully
is_interest_only <-
loan_period_df$isIO[period_index]
periodFee <-
loan_period_df$amountLoanFee[period_index]
if (period == 0) {
month_df <-
tibble(
idPeriod = period,
dateStartPeriod = datePeriod,
dateEndPeriod = datePeriod,
isIO = is_interest_only,
balanceInitial = 0 %>% currency(digits = 2),
amountInitialDraw = drawInitial %>% currency(digits = 2),
amountAdditionalDraw = drawAdditional %>% currency(digits = 2),
paymentInterest = 0 %>% currency(digits = 2),
paymentPrincipal = 0 %>% currency(digits = 2),
amountRepayment = 0 %>% currency(digits = 2),
amountLoanFee = periodFee %>% currency(digits = 2)
) %>%
mutate(balanceEnd = amountInitialDraw + amountAdditionalDraw)
}
if (period > 0) {
initial_balance <-
all_payment_data$balanceEnd[period_index - 1]
total_balance <-
initial_balance + drawInitial + drawAdditional
balance_basis <-
(all_payment_data$amountInitialDraw %>% sum()) +
all_payment_data$amountAdditionalDraw %>% sum()
start_month <-
timeDate::timeFirstDayInMonth(datePeriod) %>% as.Date()
days <-
(datePeriod - start_month) + 1
month_days <-
as.numeric(days, units = 'days')
if (override_monthly_interest == T) {
monthly_interest <-
net_rate / 12
} else {
monthly_interest <-
daily_interest * month_days
}
paymentInterest <-
monthly_interest * (total_balance)
if (interest_reserve_period > 0 &
period <= interest_reserve_period) {
paymentInterest <-
0
}
if (is_interest_only == T) {
paymentTotal <-
paymentInterest
} else {
paymentTotal <-
pmt(
r = monthly_interest,
pv = balance_basis,
fv = 0,
n = amortization_periods
)
}
paymentPrincipal <-
abs(paymentTotal) - paymentInterest
if (period == loan_periods) {
amountRepayment <-
initial_balance + drawInitial + drawAdditional - paymentPrincipal
} else {
amountRepayment <-
0
}
month_df <-
tibble(
idPeriod = period,
dateStartPeriod = start_month,
dateEndPeriod = datePeriod,
isIO = is_interest_only,
balanceInitial = initial_balance %>% currency(digits = 2),
amountInitialDraw = drawInitial %>% currency(digits = 2),
amountAdditionalDraw = drawAdditional %>% currency(digits = 2),
paymentInterest = -paymentInterest %>% currency(digits = 2),
paymentPrincipal = -paymentPrincipal %>% currency(digits = 2),
amountRepayment = -amountRepayment %>% currency(digits = 2),
amountLoanFee = periodFee %>% currency(digits = 2)
) %>%
mutate(
balanceEnd =
balanceInitial + amountInitialDraw + amountAdditionalDraw +
paymentPrincipal + amountRepayment
)
}
all_payment_data <-
month_df %>%
bind_rows(all_payment_data) %>%
arrange((idPeriod))
}
all_payment_data <-
all_payment_data %>%
mutate(
balanceInitial = balanceInitial %>% currency(digits = 2),
amountInitialDraw = amountInitialDraw %>% currency(digits = 2),
amountAdditionalDraw = amountAdditionalDraw %>% currency(digits = 2),
paymentInterest = paymentInterest %>% currency(digits = 2),
paymentPrincipal = paymentPrincipal %>% currency(digits = 2),
amountRepayment = amountRepayment %>% currency(digits = 2),
amountLoanFee = amountLoanFee %>% currency(digits = 2),
balanceEnd = balanceEnd %>% currency(digits = 2)
) %>%
mutate(yearPeriod = ifelse(idPeriod > 0,
((idPeriod - 1) %/% 12) + 1,
0)) %>%
dplyr::select(yearPeriod, everything())
if (return_annual_summary == T) {
all_payment_data <-
all_payment_data %>%
group_by(yearPeriod) %>%
summarise(
dateStartPeriod = min(dateStartPeriod),
dateEndPeriod = max(dateEndPeriod),
amountInitialDraw = sum(amountInitialDraw),
amountAdditionalDraw = sum(amountAdditionalDraw),
paymentInterest = sum(paymentInterest),
paymentPrincipal = sum(paymentPrincipal),
amountRepayment = sum(amountRepayment),
amountLoanFee = sum(amountLoanFee),
cfLoan = (
amountInitialDraw + amountAdditionalDraw + paymentInterest + paymentPrincipal + amountRepayment + amountLoanFee
) %>% currency(digits = 2)
) %>%
mutate(
balanceEnd = cumsum(amountInitialDraw) + cumsum(paymentPrincipal) + cumsum(amountRepayment)
) %>%
ungroup
}
return(all_payment_data)
}
calculate_average_payment <-
function(amount_initial_draw = 3000,
is_interest_only = F,
interest_only_periods = 24,
interest_rate = "10%",
is_actual_360 = T,
amortization_years = 10,
amortization_months = 0,
term_years = 10,
term_months = 0,
pct_loan_fee = 0,
balloon_year = 10,
override_monthly_interest = F,
interest_reserve_period = 0,
balloon_month = 0) {
library(lubridate)
first_of_the_month <-
(ceiling_date((Sys.Date() %m+% months(0)), "month") - days(1)) + 1
pmt_df <-
calculate_loan_payment(
loan_start_date = first_of_the_month,
amount_initial_draw = amount_initial_draw,
is_interest_only = is_interest_only,
interest_only_periods = interest_only_periods,
interest_rate = interest_rate,
is_actual_360 = is_actual_360,
amortization_years = amortization_years,
amortization_months = amortization_months,
term_years = term_years,
term_months = term_months,
pct_loan_fee = pct_loan_fee,
balloon_year = balloon_year,
balloon_month = balloon_month,
return_annual_summary = F
)
pmt_df <-
pmt_df %>%
dplyr::filter(!idPeriod == 0) %>%
group_by(yearPeriod) %>%
summarise(
paymentPrincipal = sum(paymentPrincipal, na.rm = T),
paymentInterest = sum(paymentInterest, na.rm = T)
) %>%
ungroup %>%
summarise(
meanPrincipal = mean(paymentPrincipal, na.rm = T) %>% formattable::currency(),
meanInterest = mean(paymentInterest, na.rm = T) %>% formattable::currency()
) %>%
mutate(meanPayment = meanPrincipal + meanInterest)
return(pmt_df)
}
calculate_leverage_metric <-
function(purchase_price = "$9,700,000",
capitalized_acquisition_costs = "$300,000",
capital_investment = "0",
revenue = "$1,500,000",
expenses = "$115,000",
loan_to_cost = .7,
borrow_capital_investment = F,
borrow_capitalized_costs = T,
leverage_threshold = .95,
is_interest_only = TRUE,
interest_only_periods = 12,
interest_rate = "5%",
is_actual_360 = TRUE,
amortization_years = 30,
amortization_months = 0,
term_years = 10,
term_months = 0,
pct_loan_fee = 0,
balloon_year = 10,
balloon_month = 0,
return_message = F) {
basis_df <-
calculate_capitalization(
purchase_price = purchase_price,
capitalized_acquisition_costs = capitalized_acquisition_costs,
capital_investment = capital_investment,
loan_to_cost = loan_to_cost,
borrow_capital_investment = borrow_capital_investment,
borrow_capitalized_costs = borrow_capitalized_costs
)
revenue_amount <-
revenue %>%
parse_for_currency_value()
expense_amount <-
expenses %>%
parse_for_currency_value()
if (expense_amount > 0) {
expense_amount <-
-expense_amount
}
operating_income <-
revenue_amount + expense_amount
interest_rate <-
interest_rate %>%