From 7dc4bc8b2ed04449d7dc7f2e4ab8639a61e8b4dc Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Mon, 9 Nov 2020 09:30:43 +0100
Subject: [PATCH] ProfitParticipation: Fix helper functions, add
 PP.benefit.none;

---
 NAMESPACE                           |  1 +
 R/ProfitParticipation_Functions.R   | 18 +++++++++++++-----
 man/ProfitParticipationFunctions.Rd |  9 +++++++--
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/NAMESPACE b/NAMESPACE
index 91ad374..953feff 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -22,6 +22,7 @@ export(PP.base.meanContractualReserve)
 export(PP.base.previousContractualReserve)
 export(PP.base.sumInsured)
 export(PP.base.totalProfitAssignment)
+export(PP.benefit.None)
 export(PP.benefit.Profit)
 export(PP.benefit.ProfitGuaranteeSupporting)
 export(PP.benefit.ProfitPlusGuaranteedInterest)
diff --git a/R/ProfitParticipation_Functions.R b/R/ProfitParticipation_Functions.R
index 492514e..942b3b5 100644
--- a/R/ProfitParticipation_Functions.R
+++ b/R/ProfitParticipation_Functions.R
@@ -258,7 +258,7 @@ PP.calculate.RateOnBaseSGFFactor = function(base, rate, waiting, rates, params,
 sumProfits = function(profits, cols) {
   # extract the columns -- if they exist -- and sum them up:
   rowSums(
-    profits[, intersect(c(), colnames(profits))]
+    profits[, intersect(cols, colnames(profits)), drop = FALSE]
   )
 }
 
@@ -305,17 +305,17 @@ PP.benefit.ProfitPlusHalfGuaranteedInterest = function(profits, rates, ...) {
 };
 
 #' @describeIn ProfitParticipationFunctions
-#' Calculate accrued benefit as total profit with interest for one year (min of guarantee and total interest)
+#' Calculate accrued benefit as total profit with interest for one year (max of guarantee and total interest)
 #' @export
 PP.benefit.ProfitPlusInterestMinGuaranteeTotal = function(profits, rates, ...) {
-  profits[,"regularBonus"] * (1 + pmin(rates$guaranteedInterest, rates$totalInterest))
+  profits[,"regularBonus"] * (1 + pmax(rates$guaranteedInterest, rates$totalInterest))
 };
 
 #' @describeIn ProfitParticipationFunctions
-#' Calculate accrued benefit as total profit with interest for half a year (min of guarantee and total interest)
+#' Calculate accrued benefit as total profit with interest for half a year (max of guarantee and total interest)
 #' @export
 PP.benefit.ProfitPlusHalfInterestMinGuaranteeTotal = function(profits, rates, ...) {
-  profits[,"regularBonus"] * (1 + pmin(rates$guaranteedInterest, rates$totalInterest)/2)
+  profits[,"regularBonus"] * (1 + pmax(rates$guaranteedInterest, rates$totalInterest)/2)
 };
 
 #' @describeIn ProfitParticipationFunctions
@@ -348,4 +348,12 @@ PP.benefit.TerminalBonus = function(profits, params, ...) {
   sumProfits(profits, c("TBF", "terminalBonusReserve"))
 };
 
+#' @describeIn ProfitParticipationFunctions
+#' No benefit paid out
+#' @export
+PP.benefit.None = function(profits, ...) {
+  0
+};
+
+
 "dummy"
diff --git a/man/ProfitParticipationFunctions.Rd b/man/ProfitParticipationFunctions.Rd
index 463246d..810b4bd 100644
--- a/man/ProfitParticipationFunctions.Rd
+++ b/man/ProfitParticipationFunctions.Rd
@@ -40,6 +40,7 @@
 \alias{PP.benefit.TerminalBonus5YearsProRata}
 \alias{PP.benefit.TerminalBonus5Years}
 \alias{PP.benefit.TerminalBonus}
+\alias{PP.benefit.None}
 \title{Helper functions for profit participation}
 \usage{
 PP.base.NULL(rates, params, values, ...)
@@ -141,6 +142,8 @@ PP.benefit.TerminalBonus5YearsProRata(profits, params, ...)
 PP.benefit.TerminalBonus5Years(profits, params, ...)
 
 PP.benefit.TerminalBonus(profits, params, ...)
+
+PP.benefit.None(profits, ...)
 }
 \arguments{
 \item{rates}{data.frame of profit rates}
@@ -250,9 +253,9 @@ calculation function.
 
 \item \code{PP.benefit.ProfitPlusHalfGuaranteedInterest}: Calculate death benefit as total profit with (guaranteed) interest for one year
 
-\item \code{PP.benefit.ProfitPlusInterestMinGuaranteeTotal}: Calculate accrued benefit as total profit with interest for one year (min of guarantee and total interest)
+\item \code{PP.benefit.ProfitPlusInterestMinGuaranteeTotal}: Calculate accrued benefit as total profit with interest for one year (max of guarantee and total interest)
 
-\item \code{PP.benefit.ProfitPlusHalfInterestMinGuaranteeTotal}: Calculate accrued benefit as total profit with interest for half a year (min of guarantee and total interest)
+\item \code{PP.benefit.ProfitPlusHalfInterestMinGuaranteeTotal}: Calculate accrued benefit as total profit with interest for half a year (max of guarantee and total interest)
 
 \item \code{PP.benefit.ProfitGuaranteeSupporting}: Calculate accrued benefit as regular profit, but used to cover initial Zillmerization
 
@@ -261,5 +264,7 @@ calculation function.
 \item \code{PP.benefit.TerminalBonus5Years}: Terminal bonus is only paid out during the last 5 years of the contract (but never during the first 10 years)
 
 \item \code{PP.benefit.TerminalBonus}: Calculate benefit from terminal bonus (full bonus), either old-style terminal bonus reserve or Terminal Bonus Fund (TBF)
+
+\item \code{PP.benefit.None}: No benefit paid out
 }}
 
-- 
GitLab