From 38006e8780b7e2af685c5b14bbb6ecb0952dd218 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Sun, 15 Mar 2020 22:45:33 +0100
Subject: [PATCH] Multiple profit participation scenarios (WIP)

---
 R/HelperFunctions.R     | 31 +++++++++++++++++++++++++++++++
 R/InsuranceContract.R   | 17 ++++++++++++++---
 R/InsuranceParameters.R |  4 +++-
 R/InsuranceTarif.R      |  2 +-
 4 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/R/HelperFunctions.R b/R/HelperFunctions.R
index b33d073..e8e945c 100644
--- a/R/HelperFunctions.R
+++ b/R/HelperFunctions.R
@@ -336,3 +336,34 @@ fallbackFields = function(fields, valuelist) {
 # extractProfitRates = function(rates, )
 #' @export
 rollingmean = function(x) (tail(x, -1) + head(x, -1))/2
+
+
+######################################################################=#
+# Functions for handling sub-contract blocks                        ####
+
+# Helper functions to prepend/append rows to the arrays and sum them up
+padArray = function(arr = NULL, pad = 0, len = 0) {
+  padEnd = max(0, len - pad - NROW(arr)) # if len is too short, return an array containing at least the arr
+  nrcols = ifelse(is.null(arr), 0, NCOL(arr))
+  rbind(
+    array(0, dim = c(pad, nrcols)) %>% `colnames<-`(colnames(arr)),
+    arr,
+    array(0, dim = c(padEnd, nrcols)) %>% `colnames<-`(colnames(arr))
+  ) %>% `colnames<-`(colnames(arr))
+}
+
+sumPaddedArrays = function(arr1 = NULL, arr2 = NULL, pad1 = 0, pad2 = 0) {
+  newlen = max(pad1 + NROW(arr1), pad2 + NROW(arr2))
+  if (is.null(arr2)) {
+    padArray(arr1, pad = pad1, len = newlen)
+  } else if (is.null(arr1)) {
+    padArray(arr2, pad = pad2, len = newlen)
+  } else {
+    # First prepend trailing zero rows according to pad1/pad2:
+    arr1 = padArray(arr1, pad = pad1, len = newlen)
+    arr2 = padArray(arr2, pad = pad2, len = newlen)
+
+    # arr1 and arr2 now should have the same dimensions => sum them up
+    arr1 + arr2
+  }
+}
diff --git a/R/InsuranceContract.R b/R/InsuranceContract.R
index 19b5d86..f45fe1f 100644
--- a/R/InsuranceContract.R
+++ b/R/InsuranceContract.R
@@ -407,6 +407,17 @@ InsuranceContract = R6Class(
             invisible(self)
         },
 
+        # Calculate one profit scenario and return all values
+        profitScenario = function(...) {
+            private$calculateProfitParticipation(...)
+        },
+
+        # Calculate one profit scenario and store it in the contract (e.g. to be exported to Excel), this function can be chained!
+        addProfitScenario = function(id, ...) {
+            self$Values$profitScenarios[id] = self$profitScenario(...)
+            invisible(self)
+        },
+
         dummy.public = NULL
     ),
 
@@ -570,7 +581,7 @@ InsuranceContract = R6Class(
 
         profitParticipation = function(...) {
             self$Values$profitParticipation = private$calculateProfitParticipation(...);
-            self$Values$reservesInclProfit = private$calculateReservesWithProfit(...);
+            self$Values$reservesAfterProfit = private$calculateReservesAfterProfit(...);
 
             # For convenience, return the profit participation table:
             self$Values$profitParticipation
@@ -579,8 +590,8 @@ InsuranceContract = R6Class(
         calculateProfitParticipation = function(...) {
             self$tarif$calculateProfitParticipation(params = self$Parameters, values = self$Values, ...);
         },
-        calculateReservesWithProfit = function(...) {
-            self$tarif$reservesWithProfit(params = self$Parameters, values = self$Values, ...);
+        calculateReservesAfterProfit = function(...) {
+            self$tarif$reservesAfterProfit(params = self$Parameters, values = self$Values, ...);
         },
 
 
diff --git a/R/InsuranceParameters.R b/R/InsuranceParameters.R
index 0baa336..3a525c2 100644
--- a/R/InsuranceParameters.R
+++ b/R/InsuranceParameters.R
@@ -74,7 +74,9 @@ InsuranceContract.Values = list(
     reserves = NULL,
     reservesBalanceSheet = NULL,
 
-    premiumComposition = NULL
+    premiumComposition = NULL,
+
+    profitParticipation = list()
 );
 
 # InsuranceContract.ParameterDefault #######################################
diff --git a/R/InsuranceTarif.R b/R/InsuranceTarif.R
index 1544677..7818e70 100644
--- a/R/InsuranceTarif.R
+++ b/R/InsuranceTarif.R
@@ -818,7 +818,7 @@ InsuranceTarif = R6Class(
         }
     },
 
-    reservesWithProfit = function(params, values, ...) {
+    reservesAfterProfit = function(params, values, ...) {
         # TODO
     },
 
-- 
GitLab