diff --git a/ifrs17/Constants/Validations.ipynb b/ifrs17/Constants/Validations.ipynb index 3fdcf95e..2072f37f 100644 --- a/ifrs17/Constants/Validations.ipynb +++ b/ifrs17/Constants/Validations.ipynb @@ -86,6 +86,8 @@ "\n NotSupportedAocStepReference, MultipleEoP,", "\n // Data completeness", "\n MissingDataAtPosting, MissingCombinedLiability, MissingCoverageUnit, ", + "\n // Index", + "\n NegativeIndex,", "\n // Default", "\n Generic", "\n};" @@ -169,6 +171,8 @@ "\n (Error.MissingDataAtPosting , 1) => $\"Missing imported data for {s[0]} DataNode.\",", "\n (Error.MissingCombinedLiability , 2) => $\"Missing Combined Liability AoC Type for DataNode {s[0]} and AmountType {s[1]}.\",", "\n (Error.MissingCoverageUnit , 1) => $\"Missing Coverage Unit cash flow for {s[0]} DataNode.\",", + "\n // Index", + "\n (Error.NegativeIndex , 0) => $\"Index was out of range. Must be non-negative.\",", "\n // Default", "\n (Error.Generic , _) => $\"{s[0]}\",", "\n (_ , _) => $\"Error not found.\"", diff --git a/ifrs17/Import/ImportScopeCalculation.ipynb b/ifrs17/Import/ImportScopeCalculation.ipynb index 5ea716a5..47c21764 100644 --- a/ifrs17/Import/ImportScopeCalculation.ipynb +++ b/ifrs17/Import/ImportScopeCalculation.ipynb @@ -805,11 +805,11 @@ "\n switch (periodType) {", "\n case PeriodType.BeginningOfPeriod :", "\n for (var i = 0; i < parentDiscountedValues.Length; i++)", - "\n ret[i] = -1d * (parentDiscountedValues[i] - parentNominalValues[i]) * (GetElementOrDefault(monthlyInterestFactor, i/12) - 1d );", + "\n ret[i] = -1d * (parentDiscountedValues[i] - parentNominalValues[i]) * (monthlyInterestFactor.GetValidElement(i/12) - 1d );", "\n break;", "\n default :", "\n for (var i = 0; i < parentDiscountedValues.Length; i++)", - "\n ret[i] = -1d * parentDiscountedValues[i] * (GetElementOrDefault(monthlyInterestFactor, i/12) - 1d );", + "\n ret[i] = -1d * parentDiscountedValues[i] * (monthlyInterestFactor.GetValidElement(i/12) - 1d );", "\n break;", "\n }", "\n ", @@ -830,8 +830,8 @@ "\n var interestOnClaimsCashflowCreditRisk = new double[nominalClaimsCashflow.Length];", "\n var effectCreditRisk = new double[nominalClaimsCashflow.Length];", "\n for (var i = nominalClaimsCashflow.Length - 1; i >= 0; i--) {", - "\n interestOnClaimsCashflow[i] = 1 / GetElementOrDefault(monthlyInterestFactor, i/12) * (interestOnClaimsCashflow.ElementAtOrDefault(i + 1) + nominalClaimsCashflow[i] - nominalClaimsCashflow.ElementAtOrDefault(i + 1));", - "\n interestOnClaimsCashflowCreditRisk[i] = 1 / GetElementOrDefault(monthlyInterestFactor, i/12) * (Math.Exp(-nonPerformanceRiskRate) * interestOnClaimsCashflowCreditRisk.ElementAtOrDefault(i + 1) + nominalClaimsCashflow[i] - nominalClaimsCashflow.ElementAtOrDefault(i + 1));", + "\n interestOnClaimsCashflow[i] = 1 / monthlyInterestFactor.GetValidElement(i/12) * (interestOnClaimsCashflow.ElementAtOrDefault(i + 1) + nominalClaimsCashflow[i] - nominalClaimsCashflow.ElementAtOrDefault(i + 1));", + "\n interestOnClaimsCashflowCreditRisk[i] = 1 / monthlyInterestFactor.GetValidElement(i/12) * (Math.Exp(-nonPerformanceRiskRate) * interestOnClaimsCashflowCreditRisk.ElementAtOrDefault(i + 1) + nominalClaimsCashflow[i] - nominalClaimsCashflow.ElementAtOrDefault(i + 1));", "\n effectCreditRisk[i] = interestOnClaimsCashflow[i] - interestOnClaimsCashflowCreditRisk[i];", "\n }", "\n ", @@ -1698,7 +1698,7 @@ "\n private double[] monthlyInterestFactor => GetScope(Identity, o => o.WithContext(EconomicBasis)).Interest;", "\n ", "\n private double interestAccretionFactor => Enumerable.Range(shift,timeStep)", - "\n .Select(i => GetElementOrDefault(monthlyInterestFactor, i/12))", + "\n .Select(i => monthlyInterestFactor.GetValidElement(i/12))", "\n .Aggregate(1d, (x, y) => x * y ) - 1d;", "\n ", "\n double TechnicalMargin.Value => AggregatedValue * interestAccretionFactor;", diff --git a/ifrs17/Utils/ApplicationMessage.ipynb b/ifrs17/Utils/ApplicationMessage.ipynb index 990dd62b..2d9e3d3e 100644 --- a/ifrs17/Utils/ApplicationMessage.ipynb +++ b/ifrs17/Utils/ApplicationMessage.ipynb @@ -110,9 +110,9 @@ "\n Status = a.Status == ActivityLogStatus.Failed || b.Status == ActivityLogStatus.Failed ? ActivityLogStatus.Failed : ActivityLogStatus.Succeeded,", "\n StartDateTime = a.StartDateTime < b.StartDateTime ? a.StartDateTime : b.StartDateTime,", "\n FinishDateTime = a.FinishDateTime > b.FinishDateTime ? a.FinishDateTime : b.FinishDateTime,", - "\n Errors = a.Errors.Concat(b.Errors).ToList(),", - "\n Warnings = a.Warnings.Concat(b.Warnings).ToList(),", - "\n Infos = a.Infos.Concat(b.Infos).ToList(),", + "\n Errors = a.Errors.Concat(b.Errors).ToHashSet().ToList(),", + "\n Warnings = a.Warnings.Concat(b.Warnings).ToHashSet().ToList(),", + "\n Infos = a.Infos.Concat(b.Infos).ToHashSet().ToList(),", "\n };", "\n}", "\n", diff --git a/ifrs17/Utils/Extensions.ipynb b/ifrs17/Utils/Extensions.ipynb index d23cd61e..beea37c7 100644 --- a/ifrs17/Utils/Extensions.ipynb +++ b/ifrs17/Utils/Extensions.ipynb @@ -75,18 +75,21 @@ { "cell_type": "code", "source": [ - "// because the default(T) is something else than the first/last element. What about \"static T GetValidElement(this IList array, int index)\"? ", - "\nstatic T GetElementOrDefault(this ICollection array, int index)", + "static T GetValidElement(this ICollection collection, int index)", "\n{ ", - "\n var count = array.Count;", - "\n if (array == null || count == 0)", + "\n var count = collection.Count;", + "\n if (collection == null || count == 0)", "\n return default(T);", "\n", - "\n return index < 0", - "\n ? array.ElementAt(0) // should this case be removed?", - "\n : index < count", - "\n ? array.ElementAt(index)", - "\n : array.ElementAt(count -1);", + "\n if (index < 0)", + "\n {", + "\n ApplicationMessage.Log(Error.NegativeIndex);", + "\n return default;", + "\n }", + "\n", + "\n return index < count", + "\n ? collection.ElementAt(index)", + "\n : collection.ElementAt(count -1);", "\n}" ], "metadata": {}, diff --git a/ifrs17/Utils/ImportCalculationMethods.ipynb b/ifrs17/Utils/ImportCalculationMethods.ipynb index 4e8270a2..02c03ab8 100644 --- a/ifrs17/Utils/ImportCalculationMethods.ipynb +++ b/ifrs17/Utils/ImportCalculationMethods.ipynb @@ -109,12 +109,12 @@ "\n if(periodType == PeriodType.BeginningOfPeriod)", "\n {", "\n for (var i = nominalValues.Length - 1; i >= 0; i--)", - "\n ret[i] = nominalValues[i] + GetElementOrDefault(ret, i + 1) * GetElementOrDefault(monthlyDiscounting, i/12);", + "\n ret[i] = nominalValues[i] + ret.GetValidElement(i + 1) * monthlyDiscounting.GetValidElement(i/12);", "\n return ret;", "\n }", "\n ", "\n for (var i = nominalValues.Length - 1; i >= 0; i--)", - "\n ret[i] = ( nominalValues[i] + GetElementOrDefault(ret, i + 1) ) * GetElementOrDefault(monthlyDiscounting, i/12);", + "\n ret[i] = ( nominalValues[i] + ret.GetValidElement(i + 1) ) * monthlyDiscounting.GetValidElement(i/12);", "\n ", "\n return ret;", "\n}" @@ -130,7 +130,7 @@ "\n{ ", "\n return Enumerable.Range(0, nominalValues.Length)", "\n .Select( t => Enumerable.Range(t, nominalValues.Length-t)", - "\n .Select( tau => nominalValues[tau] * Math.Pow(GetElementOrDefault(monthlyDiscounting, t/12), tau-t+1) * (Math.Exp(-nonPerformanceRiskRate*(tau-t)) - 1) )", + "\n .Select( tau => nominalValues[tau] * Math.Pow(monthlyDiscounting.GetValidElement(t/12), tau-t+1) * (Math.Exp(-nonPerformanceRiskRate*(tau-t)) - 1) )", "\n .Sum() )", "\n .ToArray();", "\n}" @@ -155,12 +155,12 @@ "\n if(period == PeriodType.BeginningOfPeriod)", "\n {", "\n for (var i = cdcf.Length - 1; i >= 0; i--)", - "\n cdcf[i] = values[i] + GetElementOrDefault(cdcf, i + 1) * GetElementOrDefault(yearlyDiscountRates, i/12);", + "\n cdcf[i] = values[i] + cdcf.GetValidElement(i + 1) * yearlyDiscountRates.GetValidElement(i/12);", "\n }", "\n else", "\n { ", "\n for (var i = cdcf.Length - 1; i >= 0; i--)", - "\n cdcf[i] = ( values[i] + GetElementOrDefault(cdcf, i + 1) ) * GetElementOrDefault(yearlyDiscountRates, i/12);", + "\n cdcf[i] = ( values[i] + cdcf.GetValidElement(i + 1) ) * yearlyDiscountRates.GetValidElement(i/12);", "\n }", "\n return rv with { Values = cdcf };", "\n })",