diff --git a/R/HelperFunctions.R b/R/HelperFunctions.R index b33d073d907940f3fe0f6696108926883bd5d7ee..e8e945c39f28898c6ac7adf02a404fe57afc314f 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 19b5d86c5d5fa9d7d2719e72805d5159eb069d8e..f45fe1f33e1fc62d0104fde394160b1fd3a4e65d 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 0baa3360bc8d36f416bfce78f3f6b0d49824c239..3a525c2bcba37656e4bd06a3570a8010d78bf212 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 154467720493728fcf439cfbe38851249f6c56a3..7818e70f583c0d143f115a4dace79e3a89eed4a1 100644 --- a/R/InsuranceTarif.R +++ b/R/InsuranceTarif.R @@ -818,7 +818,7 @@ InsuranceTarif = R6Class( } }, - reservesWithProfit = function(params, values, ...) { + reservesAfterProfit = function(params, values, ...) { # TODO },