Commit f088d51b authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Fix dynamics that start at t=0

Problem was that padding convert the date to a numeric, so summing the date from the original main contract part with any other dynamic block worked. But when the dynamic block was not padded, because it started at t=0, the date was not (wrongly) converted to numeric, so R failed adding two dates...

For the balance sheet reserve, I now simply match corresponding rows by date (available as a column in the data.frame) rather than relying on information how many rows to pad.

closes #65
parent 33b4adad
......@@ -688,13 +688,30 @@ InsuranceContract = R6Class(
arr1 + arr2
}
}
consolidateField = function(field) {
sumKeyedArrays = function(arr1 = NULL, arr2 = NULL) {
if (is.null(arr2)) {
arr1
} else if (is.null(arr1)) {
arr2
} else {
bind_rows(arr1, arr2) %>%
group_by(date) %>%
summarise_all(list(sum))
}
}
consolidateField = function(field, keyed = FALSE) {
vals = NULL
if (length(self$blocks) == 0) {
vals = self$Values[[field]]
}
for (b in self$blocks) {
vals = sumPaddedArrays(arr1 = vals, arr2 = b$Values[[field]], pad2 = b$Parameters$ContractData$blockStart)
if (keyed) {
# The rows of the two data.frames can be associated by the values of a certain column
vals = sumKeyedArrays(arr1 = vals, arr2 = b$Values[[field]])
} else {
# Simply pad the arrays and sum them up:
vals = sumPaddedArrays(arr1 = vals, arr2 = b$Values[[field]], pad2 = b$Parameters$ContractData$blockStart)
}
}
mergeValues(starting = self$Values[[field]], ending = vals, t = valuesFrom);
}
......@@ -721,7 +738,7 @@ InsuranceContract = R6Class(
self$Values$premiumCompositionSums = consolidateField("premiumCompositionSums")
self$Values$premiumCompositionPV = consolidateField("premiumCompositionPV")
self$Values$reserves = consolidateField("reserves")
self$Values$reservesBalanceSheet = consolidateField("reservesBalanceSheet")
self$Values$reservesBalanceSheet = consolidateField("reservesBalanceSheet", keyed = TRUE)
# TODO: Basic Data cannot simply be summed, e.g. the interest rate!
self$Values$basicData = consolidateField("basicData")
# self$Values$basicData[,c("InterestRate", "PolicyDuration", "PremiumPeriod")] = NULL
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment