From bce684665342e04b6451c4f7ce987524519fc574 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Tue, 12 Apr 2016 23:27:19 +0200
Subject: [PATCH] Fix benefits paid in arrears, fix multiple payments per year

---
 R/HelperFunctions.R |  2 +-
 R/InsuranceTarif.R  | 19 ++++++++++++++-----
 R/TODO.txt          |  2 ++
 3 files changed, 17 insertions(+), 6 deletions(-)
 create mode 100644 R/TODO.txt

diff --git a/R/HelperFunctions.R b/R/HelperFunctions.R
index be97ab1..680fa0d 100644
--- a/R/HelperFunctions.R
+++ b/R/HelperFunctions.R
@@ -64,7 +64,7 @@ correctionPaymentFrequency = function(m = 1, i = self$i, order = 0) {
   # correction are used like an explicit premium frequency loading on the premium.
   if (order >=0 ) beta = beta + (m-1)/(2*m);
   # For higher orders, simply add one term after the other!
-  if (order >= 1)     beta = beta + (m^2-1)/(6*m^2)*i;
+  if (order >= 1)     beta = beta + (m^2-1)/(6*m^2)*i; # S-Versicherung: *(1-i/2)
   # order 1.5 has a special term that should NOT be used for higher-order approximations!
   if (order == 1.5)   beta = beta + (1-m^2)/(12*m^2)*i^2;
 
diff --git a/R/InsuranceTarif.R b/R/InsuranceTarif.R
index 1ff3bfc..32e537f 100644
--- a/R/InsuranceTarif.R
+++ b/R/InsuranceTarif.R
@@ -53,8 +53,8 @@ InsuranceTarif = R6Class(
     sumRebate = 0,
 
     costs = list(),
-    benefitFrequencyLoading = list("1" = 0.0, "2" = 0.01, "4" = 0.015, "12" = 0.02), # TODO: Properly implement this
-    premiumFrequencyLoading = list("1" = 0.0, "2" = 0.01, "4" = 0.015, "12" = 0.02), # TODO: Implement this
+    benefitFrequencyLoading = list("1" = 0.0, "2" = 0.0, "4" = 0.0, "12" = 0.0), # TODO: Properly implement this
+    premiumFrequencyLoading = list("1" = 0.0, "2" = 0.0, "4" = 0.0, "12" = 0.0), # TODO: Implement this
     loadings = list(    # Loadings can also be function(sumInsured, premiums)    # TODO: Add other possible arguments
         "tax" = 0.04,                      # insurance tax, factor on each premium paid
         "unitcosts" = 0,                   # annual unit cost for each policy (Stückkosten), absolute value
@@ -423,7 +423,12 @@ InsuranceContract = R6Class(
 
     #### The code:
 
-    initialize = function(tarif, age, policyPeriod, premiumPeriod = policyPeriod, sumInsured = 1,  ..., deferral = 0, YOB = 1975) {
+    initialize = function(tarif, age, policyPeriod,
+                          premiumPeriod = policyPeriod, sumInsured = 1,
+                          ...,
+                          premiumPayments = "in advance", benefitPayments = "in advance",
+                          premiumFrequency = 1, benefitFrequency = 1,
+                          deferral = 0, YOB = 1975) {
       self$tarif = tarif;
       self$age = age;
       self$policyPeriod = policyPeriod;
@@ -431,6 +436,10 @@ InsuranceContract = R6Class(
       self$sumInsured = sumInsured;
       if (!missing(deferral))     self$deferral = deferral;
       if (!missing(YOB))          self$YOB = YOB;
+      if (!missing(premiumPayments)) self$premiumPayments = premiumPayments;
+      if (!missing(benefitPayments)) self$benefitPayments = benefitPayments;
+      if (!missing(premiumFrequency)) self$premiumFrequency = premiumFrequency;
+      if (!missing(benefitFrequency)) self$benefitFrequency = benefitFrequency;
 
       self$determineTransitionProbabilities();
       self$determineCashFlows();
@@ -445,14 +454,14 @@ InsuranceContract = R6Class(
 
     determineCashFlows = function() {
       self$cashFlowsBasic = self$tarif$getBasicCashFlows(YOB = self$YOB, age = self$age, policyPeriod = self$policyPeriod, premiumPeriod = self$premiumPeriod);
-      self$cashFlows = self$tarif$getCashFlows(age = self$age, premiumPayments = self$premiumPayments, policyPeriod = self$policyPeriod, premiumPaymentPeriod = self$premiumPeriod, basicCashFlows = self$cashFlowsBasic);
+      self$cashFlows = self$tarif$getCashFlows(age = self$age, premiumPayments = self$premiumPayments, benefitPayments = self$benefitPayments, policyPeriod = self$policyPeriod, premiumPaymentPeriod = self$premiumPeriod, basicCashFlows = self$cashFlowsBasic);
       self$premiumSum = sum(self$cashFlows$premiums_advance + self$cashFlows$premiums_arrears);
       self$cashFlowsCosts = self$tarif$getCashFlowsCosts(YOB = self$YOB, age = self$age, premiumPaymentPeriod = self$premiumPeriod, policyPeriod = self$policyPeriod);
       list("benefits"= self$cashFlows, "costs"=self$cashFlowCosts, "premiumSum" = self$premiumSum)
     },
 
     calculatePresentValues = function() {
-      self$presentValues = self$tarif$presentValueCashFlows(self$cashFlows, age = self$age, YOB = self$YOB);
+      self$presentValues = self$tarif$presentValueCashFlows(self$cashFlows, age = self$age, YOB = self$YOB, premiumFrequency = self$premiumFrequency, benefitFrequency = self$benefitFrequency);
       self$presentValuesCosts = self$tarif$presentValueCashFlowsCosts(self$cashFlowsCosts, age = self$age, YOB = self$YOB);
       list("benefits" = self$presentValues, "costs" = self$presentValuesCosts)
     },
diff --git a/R/TODO.txt b/R/TODO.txt
new file mode 100644
index 0000000..75eb583
--- /dev/null
+++ b/R/TODO.txt
@@ -0,0 +1,2 @@
+TODO:
+  - Alpha-Kosten an laufender Rentenzahlung bei Unterjährigkeit (Kosten werden nicht unterjährig berechnet!)
-- 
GitLab