From 812b293d975bc6cebb1b8471f241f5e1f8f421fd Mon Sep 17 00:00:00 2001 From: Sara Busato Date: Mon, 12 Dec 2022 13:30:14 +0100 Subject: [PATCH 1/4] fixing double[] aggregation --- ifrs17/Import/ImportScopeCalculation.ipynb | 6 ++-- ifrs17/Utils/Extensions.ipynb | 38 +++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/ifrs17/Import/ImportScopeCalculation.ipynb b/ifrs17/Import/ImportScopeCalculation.ipynb index 59184a84..097661f1 100644 --- a/ifrs17/Import/ImportScopeCalculation.ipynb +++ b/ifrs17/Import/ImportScopeCalculation.ipynb @@ -549,7 +549,7 @@ "\n{", "\n private double[] NominalClaimsCashflow => GetStorage().GetClaims()", "\n .Select(claim => GetStorage().GetValues(Identity.Id with {AocType = referenceAocStep.AocType, Novelty = referenceAocStep.Novelty}, claim, Identity.EstimateType, Identity.AccidentYear))", - "\n .Aggregate();", + "\n .AggregateDoubleArray();", "\n ", "\n private double nonPerformanceRiskRate => GetStorage().GetNonPerformanceRiskRate(Identity.Id);", "\n ", @@ -566,7 +566,7 @@ "\n{", "\n double[] NominalCashflow.Values => GetStorage().GetClaims()", "\n .Select(claim => GetStorage().GetValues(Identity.Id with {AocType = referenceAocStep.AocType, Novelty = referenceAocStep.Novelty}, claim, Identity.EstimateType, Identity.AccidentYear))", - "\n .Aggregate();", + "\n .AggregateDoubleArray();", "\n}" ], "metadata": {}, @@ -691,7 +691,7 @@ "\n .Values", "\n .Select(aoc => GetScope((Identity.Id with {AocType = aoc.AocType, Novelty = aoc.Novelty}, Identity.AmountType, Identity.EstimateType, Identity.Accidentyear)).Values)", "\n .Where(cf => cf.Count() > 0)", - "\n .Aggregate();", + "\n .AggregateDoubleArray();", "\n ", "\n double[] Values => Subtract(CurrentValues, PreviousValues);", "\n}" diff --git a/ifrs17/Utils/Extensions.ipynb b/ifrs17/Utils/Extensions.ipynb index a403aeff..60ec432f 100644 --- a/ifrs17/Utils/Extensions.ipynb +++ b/ifrs17/Utils/Extensions.ipynb @@ -36,6 +36,33 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "# Loading external dependencies" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#r \"nuget:morelinq\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "using static MoreLinq.Extensions.ZipLongestExtension;" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -96,7 +123,16 @@ { "cell_type": "code", "source": [ - "public static double[] Prune (this IEnumerable source, double precision = Precision) => source.Reverse().SkipWhile(x => Math.Abs(x) < precision).Reverse().ToArray();" + "public static double[] Prune(this IEnumerable source, double precision = Precision) => source.Reverse().SkipWhile(x => Math.Abs(x) < precision).Reverse().ToArray();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public static double[] AggregateDoubleArray(this IEnumerable> source) => source.Aggregate((x, y) => x.ZipLongest(y, (a, b) => a + b)).ToArray();" ], "metadata": {}, "execution_count": 0, From 623922693df89e70796386d790d753bee4b352c9 Mon Sep 17 00:00:00 2001 From: Sara Busato Date: Mon, 12 Dec 2022 16:25:17 +0100 Subject: [PATCH 2/4] Adding test for AggregateDoubleArray --- ifrs17/Test/AggregateDoubleArrayTest.ipynb | 73 ++++++++++++++++++++++ ifrs17/Test/Tests.ipynb | 9 +++ 2 files changed, 82 insertions(+) create mode 100644 ifrs17/Test/AggregateDoubleArrayTest.ipynb diff --git a/ifrs17/Test/AggregateDoubleArrayTest.ipynb b/ifrs17/Test/AggregateDoubleArrayTest.ipynb new file mode 100644 index 00000000..4958ce9c --- /dev/null +++ b/ifrs17/Test/AggregateDoubleArrayTest.ipynb @@ -0,0 +1,73 @@ +{ + "metadata": { + "authors": [], + "kernelspec": { + "display_name": "Formula Framework", + "language": "C#", + "name": "C#" + }, + "language_info": { + "file_extension": ".cs", + "mimetype": "text/plain", + "name": "C#" + } + }, + "nbformat": 4, + "nbformat_minor": 5, + "cells": [ + { + "cell_type": "code", + "source": [ + "#!import \"../Utils/Extensions\"", + "\n#!import \"../Utils/TestHelper\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public void CheckAggregateDoubleArray(double[][] arrayOfDoubleArrays)", + "\n{", + "\n var maxLength = arrayOfDoubleArrays.Select(x => x.Length).Max();", + "\n", + "\n var arraysWithMaxLength = arrayOfDoubleArrays.Where(x => x.Length == maxLength);", + "\n var remainingArrays = arrayOfDoubleArrays.Except(arraysWithMaxLength);", + "\n ", + "\n //Include arrays with max length as first", + "\n var aggregatedArrayMaxLengthFirst = arraysWithMaxLength.Concat(remainingArrays).AggregateDoubleArray();", + "\n aggregatedArrayMaxLengthFirst.Length.Should().Be(maxLength);", + "\n", + "\n //Include arrays with max length as last", + "\n var aggregatedArrayMaxLengthLast = remainingArrays.Concat(arraysWithMaxLength).AggregateDoubleArray();", + "\n aggregatedArrayMaxLengthLast.Length.Should().Be(maxLength);", + "\n", + "\n CheckEquality(aggregatedArrayMaxLengthFirst, aggregatedArrayMaxLengthLast).Should().Be(true);", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var array = new double [] [] {new[] {-100d,-50d }, new[] {-100d,-50d,-50d,-50d,0d,0d,0d,0d, 0d,0d,}, new[] {-100d,-50d,-50d,-50d,0d,0d,0d,0d, 0d,0d,}, new[] {-100d,-50d,-50d,-50d,0d,0d,0d,0d, 0d,0d,} };", + "\nCheckAggregateDoubleArray(array)" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/ifrs17/Test/Tests.ipynb b/ifrs17/Test/Tests.ipynb index e2f0e955..ef37f404 100644 --- a/ifrs17/Test/Tests.ipynb +++ b/ifrs17/Test/Tests.ipynb @@ -41,6 +41,15 @@ "metadata": {}, "execution_count": 0, "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#!eval-notebook \"AggregateDoubleArrayTest\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file From 1b14d73d21b70fab6dcffefe6eace11f908b46a1 Mon Sep 17 00:00:00 2001 From: Sara Busato Date: Mon, 12 Dec 2022 17:30:18 +0100 Subject: [PATCH 3/4] fixed AggregateDoubleArray --- ifrs17/Utils/Extensions.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ifrs17/Utils/Extensions.ipynb b/ifrs17/Utils/Extensions.ipynb index 60ec432f..9e335a22 100644 --- a/ifrs17/Utils/Extensions.ipynb +++ b/ifrs17/Utils/Extensions.ipynb @@ -132,7 +132,7 @@ { "cell_type": "code", "source": [ - "public static double[] AggregateDoubleArray(this IEnumerable> source) => source.Aggregate((x, y) => x.ZipLongest(y, (a, b) => a + b)).ToArray();" + "public static double[] AggregateDoubleArray(this IEnumerable> source) => source.Where(x => x is not null).DefaultIfEmpty(Enumerable.Empty()).Aggregate((x, y) => x.ZipLongest(y, (a, b) => a + b)).ToArray();" ], "metadata": {}, "execution_count": 0, From 919eb9a8519b3693aa226fc55f8647c4881cbfa5 Mon Sep 17 00:00:00 2001 From: Sara Busato Date: Mon, 12 Dec 2022 17:38:18 +0100 Subject: [PATCH 4/4] AggregateDoubleArrayTest enhancement --- ifrs17/Test/AggregateDoubleArrayTest.ipynb | 90 ++++++++++++++++++---- 1 file changed, 74 insertions(+), 16 deletions(-) diff --git a/ifrs17/Test/AggregateDoubleArrayTest.ipynb b/ifrs17/Test/AggregateDoubleArrayTest.ipynb index 4958ce9c..6849995e 100644 --- a/ifrs17/Test/AggregateDoubleArrayTest.ipynb +++ b/ifrs17/Test/AggregateDoubleArrayTest.ipynb @@ -28,22 +28,10 @@ { "cell_type": "code", "source": [ - "public void CheckAggregateDoubleArray(double[][] arrayOfDoubleArrays)", + "public void CheckAggregateDoubleArray(double[][] arrayOfDoubleArrays, double[] bmAggregatedArray)", "\n{", - "\n var maxLength = arrayOfDoubleArrays.Select(x => x.Length).Max();", - "\n", - "\n var arraysWithMaxLength = arrayOfDoubleArrays.Where(x => x.Length == maxLength);", - "\n var remainingArrays = arrayOfDoubleArrays.Except(arraysWithMaxLength);", - "\n ", - "\n //Include arrays with max length as first", - "\n var aggregatedArrayMaxLengthFirst = arraysWithMaxLength.Concat(remainingArrays).AggregateDoubleArray();", - "\n aggregatedArrayMaxLengthFirst.Length.Should().Be(maxLength);", - "\n", - "\n //Include arrays with max length as last", - "\n var aggregatedArrayMaxLengthLast = remainingArrays.Concat(arraysWithMaxLength).AggregateDoubleArray();", - "\n aggregatedArrayMaxLengthLast.Length.Should().Be(maxLength);", - "\n", - "\n CheckEquality(aggregatedArrayMaxLengthFirst, aggregatedArrayMaxLengthLast).Should().Be(true);", + "\n var aggregatedArray = arrayOfDoubleArrays.AggregateDoubleArray();", + "\n aggregatedArray.Should().BeEquivalentTo(bmAggregatedArray);", "\n}" ], "metadata": {}, @@ -54,7 +42,77 @@ "cell_type": "code", "source": [ "var array = new double [] [] {new[] {-100d,-50d }, new[] {-100d,-50d,-50d,-50d,0d,0d,0d,0d, 0d,0d,}, new[] {-100d,-50d,-50d,-50d,0d,0d,0d,0d, 0d,0d,}, new[] {-100d,-50d,-50d,-50d,0d,0d,0d,0d, 0d,0d,} };", - "\nCheckAggregateDoubleArray(array)" + "\nCheckAggregateDoubleArray(array, new[] {-400d,-200d,-150d,-150d,0d,0d,0d,0d, 0d,0d,})" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = new double [] [] {null, new[] {-100d,-50d }};", + "\nCheckAggregateDoubleArray(array, new[] {-100d,-50d })" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = new double [] [] {new[] {-100d,-50d }, null};", + "\nCheckAggregateDoubleArray(array, new[] {-100d,-50d })" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = new double [] [] {Enumerable.Empty().ToArray(), new[] {-100d,-50d }};", + "\nCheckAggregateDoubleArray(array, new[] {-100d,-50d })" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = new double [] [] {Enumerable.Empty().ToArray(), Enumerable.Empty().ToArray()};", + "\nCheckAggregateDoubleArray(array, Enumerable.Empty().ToArray())" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = new double [] [] {new[] {-100d,-50d }};", + "\nCheckAggregateDoubleArray(array, new[] {-100d,-50d })" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = new double [] [] {Enumerable.Empty().ToArray()};", + "\nCheckAggregateDoubleArray(array, Enumerable.Empty().ToArray())" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array = new double [] [] {null};", + "\nCheckAggregateDoubleArray(array, Enumerable.Empty().ToArray())" ], "metadata": {}, "execution_count": 0,