From 05710ff4510e2363bec5956e7d7192eac581aa10 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Sun, 29 Jan 2023 21:30:07 +0100 Subject: [PATCH] Fix typos, whitespace, docs --- R/InsuranceContract.R | 8 ++-- R/InsuranceTarif.R | 78 ++++++++++++++++---------------- man/InsuranceContract.Rd | 7 ++- man/InsuranceTarif.Rd | 1 + tests/testthat/test-CF-Annuity.R | 34 -------------- 5 files changed, 46 insertions(+), 82 deletions(-) diff --git a/R/InsuranceContract.R b/R/InsuranceContract.R index 492353a..1579491 100644 --- a/R/InsuranceContract.R +++ b/R/InsuranceContract.R @@ -527,8 +527,7 @@ InsuranceContract = R6Class( #' initialCapital and the parent's parameters as fall-back values. #' @param comment The comment to use in the history snapshot. #' @param ... Additional parameters to be passed to - #' \ifelse{html}{\href{#method-new}{\code{InsuranceContract$new()}}} - #' {\code{InsuranceContract$new()()}} to create the contract + #' \ifelse{html}{\href{#method-new}{\code{InsuranceContract$new}}}{\code{InsuranceContract$new}} to create the contract #' extension object. #' #' @examples @@ -595,7 +594,7 @@ InsuranceContract = R6Class( #' directly, but internally, whenever a contract is created or #' modified. #' - #' There is, hoever, a legitimate case to call this function + #' There is, however, a legitimate case to call this function #' when a contract was initially created with a value of #' \code{calculate} other than "all", so not all values of the #' contract were calculated. When one later needs more values @@ -604,7 +603,6 @@ InsuranceContract = R6Class( #' and reapplied again afterwards. So even in this case it is #' probably easier to create the contract object from scratch #' again. - #' #' @param calculate Which values to calculate. See [CalculationEnum] #' @param valuesFrom Calculate only values starting from this time step #' on (all values before that time will be preserved). This is @@ -629,7 +627,7 @@ InsuranceContract = R6Class( #' at time \code{premiumCalculationTime} at all. #' @param recalculatePremiumSum Whether to recalculate the overall premium #' sum when the premium is recalculated. - #' @param history_comment The comment for the history snapshot entyr + #' @param history_comment The comment for the history snapshot entry #' @param history_type The type (free-form string) to record in the history snapshot #' calculateContract = function(calculate = "all", valuesFrom = 0, premiumCalculationTime = 0, preservePastPV = TRUE, additionalCapital = 0, recalculatePremiums = TRUE, recalculatePremiumSum = TRUE, history_comment = NULL, history_type = "Contract") { diff --git a/R/InsuranceTarif.R b/R/InsuranceTarif.R index 8021a9c..37077eb 100644 --- a/R/InsuranceTarif.R +++ b/R/InsuranceTarif.R @@ -809,20 +809,20 @@ InsuranceTarif = R6Class( browser(); } - # TODO: Set up a nice list with coefficients for each type of cashflow, - # rather than multiplying each item manually (this also mitigates the risk - # of forgetting a dimension, because then the dimensions would not match, - # while here it's easy to overlook a multiplication) - # Multiply each CF column by the corresponding basis - # - # All propSI cash flows are already set up with the correct multiple - # of the sumInsured (in cashFlowsBasic) for non-constant sums insured. - # So here, we don't need to multiply with values$cashFlowsBasic$sumInsured! - propGP = c("premiums_advance", "premiums_arrears"); - propSI = c("guaranteed_advance", "guaranteed_arrears", - "survival_advance", "survival_arrears", "death_SumInsured", - "death_PremiumFree", "disease_SumInsured"); - propPS = c("death_GrossPremium", "death_Refund_past"); + # TODO: Set up a nice list with coefficients for each type of cashflow, + # rather than multiplying each item manually (this also mitigates the risk + # of forgetting a dimension, because then the dimensions would not match, + # while here it's easy to overlook a multiplication) + # Multiply each CF column by the corresponding basis + # + # All propSI cash flows are already set up with the correct multiple + # of the sumInsured (in cashFlowsBasic) for non-constant sums insured. + # So here, we don't need to multiply with values$cashFlowsBasic$sumInsured! + propGP = c("premiums_advance", "premiums_arrears"); + propSI = c("guaranteed_advance", "guaranteed_arrears", + "survival_advance", "survival_arrears", "death_SumInsured", + "death_PremiumFree", "disease_SumInsured"); + propPS = c("death_GrossPremium", "death_Refund_past"); values$cashFlows[,propGP] = values$cashFlows[,propGP] * values$premiums[["gross"]]; values$cashFlows[,propSI] = values$cashFlows[,propSI] * params$ContractData$sumInsured; values$cashFlows[,propPS] = values$cashFlows[,propPS] * values$premiums[["gross"]] * params$ContractData$premiumRefund; @@ -837,8 +837,8 @@ InsuranceTarif = R6Class( values$cashFlowsCosts = values$cashFlowsCosts[,,"SumInsured",] * params$ContractData$sumInsured + values$cashFlowsCosts[,,"SumPremiums",] * values$unitPremiumSum * values$premiums[["gross"]] + values$cashFlowsCosts[,,"GrossPremium",] * values$premiums[["gross"]] + - values$cashFlowsCosts[,,"NetPremium",] * values$premiums[["net"]] + - values$cashFlowsCosts[,,"Constant",]; + values$cashFlowsCosts[,,"NetPremium",] * values$premiums[["net"]] + + values$cashFlowsCosts[,,"Constant",]; # Handle survival CF differently, because we don't want ".survival" in the column names! cbind(values$cashFlows, values$cashFlowsCosts[,,"survival"], values$cashFlowsCosts[,,-1]) @@ -1703,29 +1703,29 @@ InsuranceTarif = R6Class( pvf$survival(advance = cf) }, - #' @description Calculate the premium frequency loading, i.e. the surcharge - #' on the premium for those cases where the premium is not paid yearly. - #' Return values can be either a numeric value or a named list with all - #' possible premium frequencies as keys. - #' @param loading The premiumFrequencyLoading parameter of the Contract or Tariff to be evaluated - #' @param frequency The premiumFrequency parameter of the contract - evaluateFrequencyLoading = function(loading, frequency, params, values) { - frequencyLoading = valueOrFunction(loading, frequency = frequency, params = params, values = values); - if (is.null(frequencyLoading)) { - 0 - } else if (is.list(frequencyLoading)) { - if (as.character(frequency) %in% names(frequencyLoading)) { - frequencyLoading[[as.character(frequency)]] - } else { - warning("Unable to handle premium frequency ", frequency, " with the given loading ", frequencyLoading); - } - } else if (is.numeric(frequencyLoading)) { - frequencyLoading - } else { - warning("premiumFrequencyLoading must be a number or a named list, given: ", frequencyLoading); - 0 - } - }, + #' @description Calculate the premium frequency loading, i.e. the surcharge + #' on the premium for those cases where the premium is not paid yearly. + #' Return values can be either a numeric value or a named list with all + #' possible premium frequencies as keys. + #' @param loading The premiumFrequencyLoading parameter of the Contract or Tariff to be evaluated + #' @param frequency The premiumFrequency parameter of the contract + evaluateFrequencyLoading = function(loading, frequency, params, values) { + frequencyLoading = valueOrFunction(loading, frequency = frequency, params = params, values = values); + if (is.null(frequencyLoading)) { + 0 + } else if (is.list(frequencyLoading)) { + if (as.character(frequency) %in% names(frequencyLoading)) { + frequencyLoading[[as.character(frequency)]] + } else { + warning("Unable to handle premium frequency ", frequency, " with the given loading ", frequencyLoading); + } + } else if (is.numeric(frequencyLoading)) { + frequencyLoading + } else { + warning("premiumFrequencyLoading must be a number or a named list, given: ", frequencyLoading); + 0 + } + }, diff --git a/man/InsuranceContract.Rd b/man/InsuranceContract.Rd index e31e169..6c44951 100644 --- a/man/InsuranceContract.Rd +++ b/man/InsuranceContract.Rd @@ -228,7 +228,7 @@ starting from and preserving all values before a later time directly, but internally, whenever a contract is created or modified. -\if{html}{\out{<div class="sourceCode">}}\preformatted{ There is, hoever, a legitimate case to call this function +\if{html}{\out{<div class="sourceCode">}}\preformatted{ There is, however, a legitimate case to call this function when a contract was initially created with a value of \code{calculate} other than "all", so not all values of the contract were calculated. When one later needs more values @@ -606,8 +606,7 @@ initialCapital and the parent's parameters as fall-back values.} \item{\code{comment}}{The comment to use in the history snapshot.} \item{\code{...}}{Additional parameters to be passed to -\ifelse{html}{\href{#method-new}{\code{InsuranceContract$new()}}} -{\code{InsuranceContract$new()()}} to create the contract +\ifelse{html}{\href{#method-new}{\code{InsuranceContract$new}}}{\code{InsuranceContract$new}} to create the contract extension object.} } \if{html}{\out{</div>}} @@ -674,7 +673,7 @@ at time \code{premiumCalculationTime} at all.} \item{\code{recalculatePremiumSum}}{Whether to recalculate the overall premium sum when the premium is recalculated.} -\item{\code{history_comment}}{The comment for the history snapshot entyr} +\item{\code{history_comment}}{The comment for the history snapshot entry} \item{\code{history_type}}{The type (free-form string) to record in the history snapshot} } diff --git a/man/InsuranceTarif.Rd b/man/InsuranceTarif.Rd index 6a5d167..e6dd304 100644 --- a/man/InsuranceTarif.Rd +++ b/man/InsuranceTarif.Rd @@ -155,6 +155,7 @@ all fields.} \item \href{#method-InsuranceTarif-getAbsPresentValues}{\code{InsuranceTarif$getAbsPresentValues()}} \item \href{#method-InsuranceTarif-presentValueBenefits}{\code{InsuranceTarif$presentValueBenefits()}} \item \href{#method-InsuranceTarif-getPremiumCoefficients}{\code{InsuranceTarif$getPremiumCoefficients()}} +\item \href{#method-InsuranceTarif-sumInsuredCalculation}{\code{InsuranceTarif$sumInsuredCalculation()}} \item \href{#method-InsuranceTarif-premiumCalculation}{\code{InsuranceTarif$premiumCalculation()}} \item \href{#method-InsuranceTarif-reserveCalculation}{\code{InsuranceTarif$reserveCalculation()}} \item \href{#method-InsuranceTarif-getBalanceSheetReserveFactor}{\code{InsuranceTarif$getBalanceSheetReserveFactor()}} diff --git a/tests/testthat/test-CF-Annuity.R b/tests/testthat/test-CF-Annuity.R index 90ab579..512d364 100644 --- a/tests/testthat/test-CF-Annuity.R +++ b/tests/testthat/test-CF-Annuity.R @@ -32,37 +32,3 @@ test_that("Annuity Cash Flows", { # premium payment start immediately for the whole contract period expect_equal(Contract.Annuity$Values$cashFlows$survival_advance, c(rep(1, 55), 0)) }) - -Tarif.Annuity = InsuranceTarif$new( - name = "Example Tariff - Immediate Annuity", - type = "annuity", - tarif = "Ann1", - desc = "An annuity with single-premium", - premiumPeriod = 1, - - mortalityTable = AVOe2005R.unisex, - i = 0.005 -) -Contract.Annuity = InsuranceContract$new( - tarif = Tarif.Annuity, - age = 65, YOB = 1955, - sumInsured = 1200, - policyPeriod = 55, - contractClosing = as.Date("2020-09-01") -) - -Contract.Annuity$Parameters$ContractData$sumInsured -Contract.Annuity$Values$premiums - - -Contract.Annuity1 = InsuranceContract$new( - tarif = Tarif.Annuity, - age = 65, YOB = 1955, - premium = 32004.67817, - # sumInsured = NULL, - policyPeriod = 55, - contractClosing = as.Date("2020-09-01") -) -Contract.Annuity1$Parameters$ContractData$sumInsured - -Contract.Annuity1$Values$premiums -- GitLab