diff --git a/PresentValueSeries/PresentValue - Episode 2.ipynb b/PresentValueSeries/PresentValue - Episode 2.ipynb index 37e9ce42..804597e7 100644 --- a/PresentValueSeries/PresentValue - Episode 2.ipynb +++ b/PresentValueSeries/PresentValue - Episode 2.ipynb @@ -168,10 +168,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"YieldCurve.xlsx\")", - "\n .WithType()", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"YieldCurve.xlsx\").WithType().WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -180,10 +177,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"Cashflows.xlsx\")", - "\n .WithFormat(\"Cashflow\")", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"Cashflows.xlsx\").WithFormat(\"Cashflow\").WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -507,4 +501,4 @@ "outputs": [] } ] -} +} \ No newline at end of file diff --git a/PresentValueSeries/PresentValue - Episode 3.ipynb b/PresentValueSeries/PresentValue - Episode 3.ipynb index 16605c69..3463cc41 100644 --- a/PresentValueSeries/PresentValue - Episode 3.ipynb +++ b/PresentValueSeries/PresentValue - Episode 3.ipynb @@ -168,10 +168,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"DataNodes_CH.xlsx\")", - "\n .WithFormat(\"DataNode\")", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"DataNodes_CH.xlsx\").WithFormat(\"DataNode\").WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -180,10 +177,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"DataNodes_DE.xlsx\")", - "\n .WithFormat(\"DataNode\")", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"DataNodes_DE.xlsx\").WithFormat(\"DataNode\").WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -192,10 +186,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"YieldCurve.xlsx\")", - "\n .WithType()", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"YieldCurve.xlsx\").WithType().WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -204,10 +195,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"Cashflows.xlsx\")", - "\n .WithFormat(\"Cashflow\")", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"Cashflows.xlsx\").WithFormat(\"Cashflow\").WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -216,10 +204,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"CF_CH_2021_12.xlsx\")", - "\n .WithFormat(\"Cashflow\")", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"CF_CH_2021_12.xlsx\").WithFormat(\"Cashflow\").WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -228,10 +213,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"CF_DE_2021_12.xlsx\")", - "\n .WithFormat(\"Cashflow\")", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"CF_DE_2021_12.xlsx\").WithFormat(\"Cashflow\").WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -240,10 +222,7 @@ { "cell_type": "code", "source": [ - "await Import.FromFile(\"CF_DE_2022_12.xlsx\")", - "\n .WithFormat(\"Cashflow\")", - "\n .WithTarget(DataSource)", - "\n .ExecuteAsync()" + "await Import.FromFile(\"CF_DE_2022_12.xlsx\").WithFormat(\"Cashflow\").WithTarget(DataSource).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, diff --git a/ifrs17-template/Files/Parameters/YieldCurve_2020_1.csv b/ifrs17-template/Files/Parameters/YieldCurve_2020_1.csv index c94e347e..c97a6549 100644 --- a/ifrs17-template/Files/Parameters/YieldCurve_2020_1.csv +++ b/ifrs17-template/Files/Parameters/YieldCurve_2020_1.csv @@ -1,6 +1,6 @@ @@Main Year,Month -2020,12 +2020,1 @@YieldCurve Currency,Values0,Values1,Values2,Values3 USD,0.002,0.002,0.002,0.002 diff --git a/ifrs17-template/Files/Parameters/YieldCurve_2020_12.csv b/ifrs17-template/Files/Parameters/YieldCurve_2020_12.csv index 48e9669d..b6fcb54c 100644 --- a/ifrs17-template/Files/Parameters/YieldCurve_2020_12.csv +++ b/ifrs17-template/Files/Parameters/YieldCurve_2020_12.csv @@ -1,6 +1,6 @@ @@Main Year,Month -2020,1 +2020,12 @@YieldCurve Currency,Values0,Values1,Values2,Values3 USD,0.002,0.002,0.002,0.002 diff --git a/ifrs17-template/Test/ScenarioDataImportTest.ipynb b/ifrs17-template/Test/ScenarioDataImportTest.ipynb index c372ff33..e6603fef 100644 --- a/ifrs17-template/Test/ScenarioDataImportTest.ipynb +++ b/ifrs17-template/Test/ScenarioDataImportTest.ipynb @@ -42,16 +42,49 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "# Setting Constants" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ "var ws = Workspace.CreateNew();", - "\nws.InitializeFrom(DataSource);", - "\nvar argsBestEstimate = new ImportArgs (\"CH\", 2020, 12, Periodicity.Quarterly, null, ImportFormats.Cashflow);", - "\nvar argsScenario = new ImportArgs (\"CH\", 2020, 12, Periodicity.Quarterly, \"Test\", ImportFormats.Cashflow);", - "\nvar initStorage = new ImportStorage(argsBestEstimate, DataSource, ws);", - "\nawait initStorage.InitializeAsync();", - "\nvar actualETs = initStorage.EstimateTypesByImportFormat[ImportFormats.Actual];", + "\nws.InitializeFrom(DataSource);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var argsBestEstimate = new ImportArgs (\"CH\", 2020, 12, Periodicity.Quarterly, null, ImportFormats.Cashflow);", + "\nvar argsScenario = new ImportArgs (\"CH\", 2020, 12, Periodicity.Quarterly, \"Test\", ImportFormats.Cashflow);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var initStorage = new ImportStorage(argsBestEstimate, DataSource, ws);", + "\nawait initStorage.InitializeAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var actualETs = initStorage.EstimateTypesByImportFormat[ImportFormats.Actual];", "\nvar cashflowETs = initStorage.EstimateTypesByImportFormat[ImportFormats.Cashflow];", "\nvar actualOrCashflowETs = actualETs.Intersect(cashflowETs);", "\nvar onlyActualETs = actualETs.Except(cashflowETs);", @@ -192,9 +225,11 @@ { "cell_type": "code", "source": [ - "actualVars.Length.Should().Be(0);", - "\ncashflowVars.Length.Should().NotBe(0);", - "\ndiffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);" + "if(EnableScenario) {", + "\n actualVars.Length.Should().Be(0);", + "\n cashflowVars.Length.Should().NotBe(0);", + "\n diffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -250,9 +285,11 @@ { "cell_type": "code", "source": [ - "actualVars.Length.Should().NotBe(0);", - "\ncashflowVars.Length.Should().Be(0);", - "\ndiffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);" + "if(EnableScenario) {", + "\n actualVars.Length.Should().NotBe(0);", + "\n cashflowVars.Length.Should().Be(0);", + "\n diffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -317,9 +354,11 @@ { "cell_type": "code", "source": [ - "actualVars.Length.Should().Be(0);", - "\ncashflowVars.Length.Should().NotBe(0);", - "\ndiffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);" + "if(EnableScenario) {", + "\n actualVars.Length.Should().Be(0);", + "\n cashflowVars.Length.Should().NotBe(0);", + "\n diffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -376,9 +415,11 @@ { "cell_type": "code", "source": [ - "diffs.Where(x => x.EstimateType == EstimateTypes.DA).ToArray().Length.Should().NotBe(0);", - "\ndiffs.Where(x => x.EstimateType == EstimateTypes.F).ToArray().Length.Should().Be(0);", - "\ndiffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);" + "if(EnableScenario) {", + "\n diffs.Where(x => x.EstimateType == EstimateTypes.DA).ToArray().Length.Should().NotBe(0);", + "\n diffs.Where(x => x.EstimateType == EstimateTypes.F).ToArray().Length.Should().Be(0);", + "\n diffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -441,9 +482,11 @@ { "cell_type": "code", "source": [ - "diffs.Where(x => x.EstimateType == EstimateTypes.DA).ToArray().Length.Should().NotBe(0);", - "\nivsScenarioActualNoACAAEA.Where(x => x.EstimateType == EstimateTypes.F).Count().Should().NotBe(0);", - "\nivsScenarioActualNoACAAEA.Where(x => x.EstimateType == EstimateTypes.DA && Math.Abs(x.Value) > Precision).Count().Should().Be(0);" + "if(EnableScenario) {", + "\n diffs.Where(x => x.EstimateType == EstimateTypes.DA).ToArray().Length.Should().NotBe(0);", + "\n ivsScenarioActualNoACAAEA.Where(x => x.EstimateType == EstimateTypes.F).Count().Should().NotBe(0);", + "\n ivsScenarioActualNoACAAEA.Where(x => x.EstimateType == EstimateTypes.DA && Math.Abs(x.Value) > Precision).Count().Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -525,7 +568,7 @@ { "cell_type": "code", "source": [ - "diffs.Count().Should().Be(0);" + "if(EnableScenario) diffs.Count().Should().Be(0);" ], "metadata": {}, "execution_count": 0, @@ -619,8 +662,10 @@ { "cell_type": "code", "source": [ - "diffs.Where(x => onlyCashflowETs.Contains(x.EstimateType) && x.EstimateType != EstimateTypes.F && Math.Abs(x.Value) > Precision).Count().Should().Be(0);", - "\ndiffs.Where(x => onlyActualETs.Contains(x.EstimateType) && Math.Abs(x.Value) > Precision).Count().Should().Be(0);" + "if(EnableScenario) {", + "\n diffs.Where(x => onlyCashflowETs.Contains(x.EstimateType) && x.EstimateType != EstimateTypes.F && Math.Abs(x.Value) > Precision).Count().Should().Be(0);", + "\n diffs.Where(x => onlyActualETs.Contains(x.EstimateType) && Math.Abs(x.Value) > Precision).Count().Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -680,9 +725,11 @@ { "cell_type": "code", "source": [ - "cashflowVars.Length.Should().NotBe(0);", - "\nactualVars.Length.Should().Be(0);", - "\ndiffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);" + "if(EnableScenario) {", + "\n cashflowVars.Length.Should().NotBe(0);", + "\n actualVars.Length.Should().Be(0);", + "\n diffs.Except(actualVars.Union(cashflowVars).Union(actualOrCashflowVars)).ToArray().Length.Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -762,9 +809,11 @@ { "cell_type": "code", "source": [ - "actualVars.Length.Should().Be(0);", - "\ncashflowVars.Length.Should().NotBe(0);", - "\ncashflowVars.Where(x => x.EstimateType == EstimateTypes.DA).Count().Should().Be(0);" + "if(EnableScenario) {", + "\n actualVars.Length.Should().Be(0);", + "\n cashflowVars.Length.Should().NotBe(0);", + "\n cashflowVars.Where(x => x.EstimateType == EstimateTypes.DA).Count().Should().Be(0);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -877,8 +926,28 @@ { "cell_type": "code", "source": [ - "actualVars.Length.Should().Be(0);", - "\ncashflowVars.Length.Should().NotBe(0);" + "if(EnableScenario) {", + "\n actualVars.Length.Should().Be(0);", + "\n cashflowVars.Length.Should().NotBe(0);", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" ], "metadata": {}, "execution_count": 0, diff --git a/ifrs17-template/Test/ScenarioYieldCurveImportTest.ipynb b/ifrs17-template/Test/ScenarioYieldCurveImportTest.ipynb new file mode 100644 index 00000000..002c811c --- /dev/null +++ b/ifrs17-template/Test/ScenarioYieldCurveImportTest.ipynb @@ -0,0 +1,513 @@ +{ + "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": "markdown", + "source": [ + "

Scenario Yield Curve Import Test

" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#!eval-notebook \"../Initialization/InitSystemorphBaseToMemory\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset(x => x.ResetType().ResetType().ResetType());" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Setting Constants" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var ws = Workspace.CreateNew();", + "\nws.InitializeFrom(DataSource);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var argsBestEstimate = new ImportArgs (\"CH\", 2020, 12, Periodicity.Quarterly, null, ImportFormats.Cashflow);", + "\nvar argsScenarioYieldCurve = new ImportArgs (\"CH\", 2020, 12, Periodicity.Quarterly, \"YCUP1.0pct\", ImportFormats.Cashflow);", + "\nvar argsScenarioTransactionalData = new ImportArgs (\"CH\", 2020, 12, Periodicity.Quarterly, \"SRUP1.0pct\", ImportFormats.Cashflow);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var initStorage = new ImportStorage(argsBestEstimate, DataSource, ws);", + "\nawait initStorage.InitializeAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var actualEstimateTypes = initStorage.EstimateTypesByImportFormat[ImportFormats.Actual];", + "\nvar cashflowEstimateTypes = initStorage.EstimateTypesByImportFormat[ImportFormats.Cashflow];", + "\nvar actualAndCashflowEstimateTypes = actualEstimateTypes.Intersect(cashflowEstimateTypes);", + "\nvar onlyActualEstimateTypes = actualEstimateTypes.Except(cashflowEstimateTypes);", + "\nvar onlyCashflowEstimateTypes = cashflowEstimateTypes.Except(actualEstimateTypes);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Best Estimate Initialization" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var basicYieldCurve = @\"", + "\n@@Main", + "\nYear,Month,Scenario", + "\n2020,1,", + "\n@@YieldCurve", + "\nCurrency,Values0,Values1,Values2,Values3", + "\nUSD,0.002,0.002,0.002,0.002\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(basicYieldCurve).WithFormat(ImportFormats.YieldCurve).WithTarget(DataSource).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var cashflowBestEstimateInit = @\"", + "\n@@Main", + "\nReportingNode,Year,Month,Scenario", + "\nCH,2020,12,", + "\n@@Cashflow", + "\nDataNode,AmountType,EstimateType,AocType,Novelty,AccidentYear,Values0,Values1,Values2,Values3,Values4,Values5,Values6,Values7,Values8,Values9,Values10,Values11,Values12,Values13,Values14,Values15,Values16,Values17,Values18,Values19,Values20,Values21,Values22,Values23", + "\nDT1.1,PR,BE,CL,C,,100,0,0,100,0,0,100,0,0,100,0,0,0,100,0,0,100,0,0,100,0,0,100,0", + "\nDT1.1,NIC,BE,CL,C,,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25", + "\nDT1.1,,CU,CL,C,,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-3", + "\nDT1.1,,RA,CL,C,,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5,-2.5", + "\nDTR1.1,PR,BE,CL,C,,50,0,0,50,0,0,50,0,0,50,0,0,50,0,0,50,0,0,50,0,0,50,0,0", + "\nDTR1.1,NIC,BE,CL,C,,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5", + "\nDTR1.1,,CU,CL,C,,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5", + "\nDTR1.1,,RA,CL,C,,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25,1.25\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var actualsBestEstimateInit = @\"", + "\n@@Main", + "\nReportingNode,Year,Month,Scenario", + "\nCH,2020,12,", + "\n@@Actual", + "\nDataNode,AocType,AmountType,EstimateType,AccidentYear,Value", + "\nDT1.1,CF,ACA,A,,-10", + "\nDT1.1,CF,AEA,A,,-5", + "\nDT1.1,CF,NIC,A,,-280", + "\nDT1.1,CF,PR,A,,400", + "\nDTR1.1,CF,PR,A,,-200", + "\nDTR1.1,CF,NIC,A,,140\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(cashflowBestEstimateInit).WithFormat(ImportFormats.Cashflow).WithTarget(DataSource).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(actualsBestEstimateInit).WithFormat(ImportFormats.Actual).WithTarget(DataSource).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await DataSource.Partition.SetAsync(argsBestEstimate);", + "\nvar ivsBestEstimateInit = await DataSource.Query().ToArrayAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Scenario Yield Curve Import" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var yieldCurveScenario = @\"", + "\n@@Main", + "\nYear,Month,Scenario", + "\n2020,12,YCUP1.0pct", + "\n@@YieldCurve", + "\nCurrency,Values0,Values1,Values2,Values3", + "\nEUR,0.108,0.108,0.118,0.119", + "\nCHF,0.102,0.102,0.102,0.102", + "\nUSD,0.102,0.102,0.102,0.102\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(yieldCurveScenario).WithFormat(ImportFormats.YieldCurve).WithTarget(DataSource).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await DataSource.Partition.SetAsync(argsScenarioYieldCurve);", + "\nvar ivsScenarioYieldCurve = await DataSource.Query().ToArrayAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var diffs = ivsScenarioYieldCurve.Where(x => Math.Abs(x.Value) > Precision).Except(ivsBestEstimateInit, IfrsVariableComparer.Instance());", + "\nvar actualVars = diffs.Where(x => onlyActualEstimateTypes.Contains(x.EstimateType)).ToArray();", + "\nvar cashflowVars = diffs.Where(x => onlyCashflowEstimateTypes.Contains(x.EstimateType)).ToArray();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "if(EnableScenario) {", + "\n actualVars.Length.Should().Be(0);", + "\n cashflowVars.Length.Should().NotBe(0);", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Re-Import of the same Yield Curve does not trigger IFRS17 Calculations" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(yieldCurveScenario).WithFormat(ImportFormats.YieldCurve).WithTarget(DataSource).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await DataSource.Partition.SetAsync(argsScenarioYieldCurve);", + "\nvar ivsScenarioYieldCurveReimport = await DataSource.Query().ToArrayAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var diffs = ivsScenarioYieldCurve.Except(ivsScenarioYieldCurve, IfrsVariableComparer.Instance());" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "if(EnableScenario) diffs.Count().Should().Be(0);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Multiple Scenarios ", + "\n", + "\nIn this example, a new Cashflow for a different Scenario - SRUP1.0pct - is imported. ", + "\nThe aim is to check how the Yield Curve importer behaves when a new Best Estimate Yield Curve is imported. ", + "\nWe can identify the following two cases: ", + "\n - a Scenario including a dedicated Yield Curve: only the Best Estimate variables are re-calculated;", + "\n - a Scenario including only transactional data: re-calculation is needed to account for the updated Best Estimate Yield Curve. " + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var cashflowScenario = @\"", + "\n@@Main", + "\nReportingNode,Year,Month,Scenario", + "\nCH,2020,12,SRUP1.0pct", + "\n@@Cashflow", + "\nDataNode,AmountType,EstimateType,AocType,Novelty,AccidentYear,Values0,Values1,Values2,Values3,Values4,Values5,Values6,Values7,Values8,Values9,Values10,Values11,Values12,Values13,Values14,Values15,Values16,Values17,Values18,Values19,Values20,Values21,Values22,Values23", + "\nDT1.1,PR,BE,CL,C,,110,0,0,110,0,0,110,0,0,110,0,0,0,110,0,0,110,0,0,110,0,0,110,0", + "\nDT1.1,NIC,BE,CL,C,,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5,-27.5", + "\nDT1.1,,RA,CL,C,,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75,-2.75\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(cashflowScenario).WithFormat(ImportFormats.Cashflow).WithTarget(DataSource).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await DataSource.Partition.SetAsync(argsScenarioTransactionalData);", + "\nvar ivsScenarioTransactionalData = await DataSource.Query().ToArrayAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var bestEstimateYieldCurve = @\"", + "\n@@Main", + "\nYear,Month", + "\n2020,12", + "\n@@YieldCurve", + "\nCurrency,Values0,Values1,Values2,Values3", + "\nEUR,0.108,0.108,0.118,0.119", + "\nCHF,0.103,0.103,0.103,0.103", + "\nUSD,0.903,0.103,0.103,0.103\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(bestEstimateYieldCurve).WithFormat(ImportFormats.YieldCurve).WithTarget(DataSource).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Best Estimate variables are re-calculated" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await DataSource.Partition.SetAsync(argsBestEstimate);", + "\nvar ivsBestEstimateUpdated = await DataSource.Query().ToArrayAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var diffs = ivsBestEstimateUpdated.Except(ivsBestEstimateInit, IfrsVariableComparer.Instance());" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "if(EnableScenario) if(EnableScenario) diffs.Count().Should().NotBe(0);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Scenario with dedicated Yield Curve does not change" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await DataSource.Partition.SetAsync(argsScenarioYieldCurve);", + "\nvar ivsScenarioYieldCurveUpdated = await DataSource.Query().ToArrayAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var diffs = ivsScenarioYieldCurveUpdated.Where(x => Math.Abs(x.Value) > Precision).Except(ivsScenarioYieldCurve, IfrsVariableComparer.Instance());" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "if(EnableScenario) diffs.Count().Should().Be(0);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Scenario including Transactional Data solely is re-calculated" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await DataSource.Partition.SetAsync(argsScenarioTransactionalData);", + "\nvar ivsScenarioTransactionalDataUpdated = await DataSource.Query().ToArrayAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var diffs = ivsScenarioTransactionalDataUpdated.Where(x => Math.Abs(x.Value) > Precision).Except(ivsScenarioTransactionalData, IfrsVariableComparer.Instance());" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "if(EnableScenario) diffs.Count().Should().NotBe(0);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/ifrs17-template/Test/Tests.ipynb b/ifrs17-template/Test/Tests.ipynb index c493097e..3a29c378 100644 --- a/ifrs17-template/Test/Tests.ipynb +++ b/ifrs17-template/Test/Tests.ipynb @@ -30,7 +30,7 @@ "cell_type": "markdown", "source": [ "Comprehensive collection of tests executed on top of the Systemorph use cases (initialization).", - "\n
Execute this Notebook using at least 16Gb RAM." + "\n
Execute this Notebook using at least 20Gb RAM." ], "metadata": {}, "execution_count": 0, @@ -99,6 +99,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "#!eval-notebook \"ScenarioYieldCurveImportTest\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -154,4 +163,4 @@ "outputs": [] } ] -} \ No newline at end of file +} diff --git a/ifrs17/Constants/Consts.ipynb b/ifrs17/Constants/Consts.ipynb index 08842d5a..23d90c66 100644 --- a/ifrs17/Constants/Consts.ipynb +++ b/ifrs17/Constants/Consts.ipynb @@ -27,6 +27,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "public const bool EnableScenario = false;" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ diff --git a/ifrs17/Constants/Validations.ipynb b/ifrs17/Constants/Validations.ipynb index 5f22aca2..b68ea84e 100644 --- a/ifrs17/Constants/Validations.ipynb +++ b/ifrs17/Constants/Validations.ipynb @@ -158,7 +158,7 @@ "\n (Error.CreditDefaultRateNotFound , 1) => $\"Credit Default Rate not found for rating {s[0]}.\",", "\n (Error.MissingPremiumAllocation , 1) => $\"Premium Allocation Rate not found for Group of Contract {s[0]}.\", // TODO: this is now a warning to be produced by a validation in the importers (default is 1)", "\n (Error.ReinsuranceCoverage , 1) => $\"Reinsurance Allocation Rate not found for Group of Insurance Contract {s[0]}.\",", - "\n (Error.YieldCurveNotFound , 3) => $\"Yield Curve not found for currency {s[0]}, year {s[1]}, and month {s[2]}.\",", + "\n (Error.YieldCurveNotFound , 5) => $\"Yield Curve not found for Currency {s[0]}, Year {s[1]}, Month {s[2]}, Scenario {(s[3] == null ? \"Best Estimate\" : s[3])} and Name {s[4]}.\",", "\n (Error.YieldCurvePeriodNotApplicable , 2) => $\"YieldCurve period NotApplicable not valid for AoC Step with AoC Type {s[0]} and Novelty {s[1]}.\",", "\n (Error.EconomicBasisNotFound , 1) => $\"EconomicBasis not valid for DataNode {s[0]}.\",", "\n (Error.AccountingVariableTypeNotFound , 1) => $\"AccountingVariableType {s[0]} not found.\",", @@ -184,7 +184,8 @@ "public static string Get (Warning w, params string[] s) => (w, s.Length) switch {", "\n // Import", "\n (Warning.ActiveDataNodeWithCashflowBOPI , 1) => $\"Cash flow with AoC Type: {AocTypes.BOP} and Novelty: {Novelties.I} for Group of Contract {s[0]} is not allowed because previous period data are available.\",", - "\n (Warning.ScenarioReCalculations , 1) => $\"The import of the current file for the Best Estimate scenario makes the result of dependent Scenarios out of date. Hence, the following Scenarios are recalculated: {s[0]}.\", ", + "\n (Warning.VariablesAlreadyImported , 0) => $\"The import of the current file does not contain any new data. Hence, no data will be saved or calculations will be performed.\",", + "\n (Warning.ScenarioReCalculations , 1) => $\"The present Best Estimate import makes the result of dependent Scenarios out of date. Hence, the following Scenarios are re-calculated: {s[0]}.\", ", "\n // Default", "\n (Warning.Generic , _) => $\"{s[0]}\",", "\n (_ , _) => $\"Warning not found.\"", diff --git a/ifrs17/Import/ImportStorage.ipynb b/ifrs17/Import/ImportStorage.ipynb index f6fca951..e5136298 100644 --- a/ifrs17/Import/ImportStorage.ipynb +++ b/ifrs17/Import/ImportStorage.ipynb @@ -145,7 +145,7 @@ "\n await hierarchyCache.InitializeAsync();", "\n ", "\n //EstimateType to load and to update", - "\n EstimateTypesByImportFormat = new InputSource[] { InputSource.Opening, InputSource.Actual, InputSource.Cashflow,}", + "\n EstimateTypesByImportFormat = new InputSource[] { InputSource.Opening, InputSource.Actual, InputSource.Cashflow }", "\n .ToDictionary(x => x.ToString(), ", "\n x => estimateTypes", "\n .Where(et => et.InputSource.Contains(x))", diff --git a/ifrs17/Import/Importers.ipynb b/ifrs17/Import/Importers.ipynb index 52c5a764..e4f2bcda 100644 --- a/ifrs17/Import/Importers.ipynb +++ b/ifrs17/Import/Importers.ipynb @@ -100,7 +100,8 @@ "\n await workspace.Partition.SetAsync(TargetPartitionByReportingNode.Id);", "\n await dataSource.Partition.SetAsync(TargetPartitionByReportingNode.Id);", "\n ", - "\n if(args.Year != default(int) && args.Month != default(int))", + "\n if(args.ImportFormat == ImportFormats.Cashflow || args.ImportFormat == ImportFormats.Actual || ", + "\n args.ImportFormat == ImportFormats.SimpleValue || args.ImportFormat == ImportFormats.Opening)", "\n {", "\n TargetPartitionByReportingNodeAndPeriod = (await workspace.Query()", "\n .Where(p => p.ReportingNode == args.ReportingNode &&", @@ -135,10 +136,8 @@ "\n ImportFormats.Opening => aocConfigurationByAocStep.Where(x => x.InputSource.Contains(InputSource.Opening) && x.DataType == DataType.Optional).GroupBy(x => new AocStep(x.AocType, x.Novelty), (k,v) => k).ToHashSet(),", "\n ImportFormats.SimpleValue => aocConfigurationByAocStep.GroupBy(x => new AocStep(x.AocType, x.Novelty), (k,v) => k).Concat((await dataSource.Query().ToArrayAsync())", "\n .Select(vt => new AocStep(vt.SystemName,null))).ToHashSet(),", - "\n _ => Enumerable.Empty().ToHashSet(),", + "\n _ => Enumerable.Empty().ToHashSet(),", "\n };", - "\n ", - "\n //DataNodes", "\n DataNodeDataBySystemName = args.ImportFormat == ImportFormats.Opening ", "\n ? (await LoadDataNodesAsync(dataSource, args)).Where(kvp => kvp.Value.Year == args.Year).ToDictionary(kvp => kvp.Key, kvp => kvp.Value)", "\n : await LoadDataNodesAsync(dataSource, args);", @@ -326,10 +325,10 @@ "\n if(ApplicationMessage.HasErrors()) return null;", "\n", "\n var main = mainTab.Rows.First();", - "\n var reportingNode = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.ReportingNode)) ? (string)main[nameof(ReportingNode)] : default(string);", - "\n var scenario = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.Scenario)) ? (string)main[nameof(Scenario)] : default(string);", - "\n var year = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.Year)) ? (int)Convert.ChangeType(main[nameof(Args.Year)], typeof(int)) : default(int);", - "\n var month = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.Month)) ? (int)Convert.ChangeType(main[nameof(Args.Month)], typeof(int)) : default(int);", + "\n var reportingNode = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.ReportingNode)) && main[nameof(Args.ReportingNode)] != null ? (string)main[nameof(ReportingNode)] : default(string);", + "\n var scenario = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.Scenario)) && main[nameof(Args.Scenario)] != null ? (string)main[nameof(Scenario)] : default(string);", + "\n var year = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.Year)) && main[nameof(Args.Year)] != null ? (int)Convert.ChangeType(main[nameof(Args.Year)], typeof(int)) : default(int);", + "\n var month = mainTab.Columns.Any(x => x.ColumnName == nameof(Args.Month)) && main[nameof(Args.Month)] != null ? (int)Convert.ChangeType(main[nameof(Args.Month)], typeof(int)) : default(int);", "\n", "\n return new ImportArgs(reportingNode, year, month, default(Periodicity), scenario, default(string));", "\n}" @@ -362,9 +361,57 @@ { "cell_type": "markdown", "source": [ - "## Get Args and commit Partition", + "## Create Partition", + "\n", + "\nThese are the methods used to create the partition if not already existing in the DataSource. " + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public async Task CommitPartitionAsync(ImportArgs args, params IDataSource[] dataSources)", + "\n{", + "\n foreach (var dataSource in dataSources) {", + "\n switch(typeof(IPartition).Name) {", + "\n case nameof(PartitionByReportingNode) : {", + "\n await dataSource.UpdateAsync( new[] { new PartitionByReportingNode { ", + "\n Id = (Guid)(await DataSource.Partition.GetKeyForInstanceAsync(args)),", + "\n ReportingNode = args.ReportingNode } } );", + "\n break;", + "\n }", + "\n case nameof(PartitionByReportingNodeAndPeriod) : {", + "\n args.ValidateArgsForPeriod();", + "\n if(ApplicationMessage.HasErrors()) return;", + "\n await dataSource.UpdateAsync( new[]{ new PartitionByReportingNodeAndPeriod { ", + "\n Id = (Guid)(await DataSource.Partition.GetKeyForInstanceAsync(args)),", + "\n Year = args.Year,", + "\n Month = args.Month,", + "\n ReportingNode = args.ReportingNode, ", + "\n Scenario = args.Scenario } } );", + "\n break;", + "\n }", + "\n default : {", + "\n ApplicationMessage.Log(Error.PartitionTypeNotFound, typeof(IPartition).Name); ", + "\n return;", + "\n }", + "\n }", + "\n await dataSource.CommitAsync();", + "\n }", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Get Args and create Partition", "\n", - "\nThis is the main method to get Args which reference a specific data partition. All validations are triggered after parsing and if the partition is not already existing in the DataSource it is created and committed. " + "\nThis is the main method to get Args which reference a specific data partition. All validations are triggered after parsing and if the partition is not already existing in the DataSource it is created. " ], "metadata": {}, "execution_count": 0, @@ -378,32 +425,7 @@ "\n var args = GetArgsFromMain(dataSet);", "\n if(ApplicationMessage.HasErrors()) return null;", "\n if(args.ReportingNode == default(string)) { ApplicationMessage.Log(Error.ReportingNodeInMainNotFound); return null; }", - "\n", - "\n switch(typeof(IPartition).Name) {", - "\n case nameof(PartitionByReportingNode) : {", - "\n await DataSource.UpdateAsync( new[] { new PartitionByReportingNode { ", - "\n Id = (Guid)(await DataSource.Partition.GetKeyForInstanceAsync(args)),", - "\n ReportingNode = args.ReportingNode } } );", - "\n break;", - "\n }", - "\n case nameof(PartitionByReportingNodeAndPeriod) : {", - "\n args.ValidateArgsForPeriod();", - "\n if(ApplicationMessage.HasErrors()) return null;", - "\n await DataSource.UpdateAsync( new[]{ new PartitionByReportingNodeAndPeriod { ", - "\n Id = (Guid)(await DataSource.Partition.GetKeyForInstanceAsync(args)),", - "\n Year = args.Year,", - "\n Month = args.Month,", - "\n ReportingNode = args.ReportingNode, ", - "\n Scenario = args.Scenario } } );", - "\n break;", - "\n }", - "\n default : {", - "\n ApplicationMessage.Log(Error.PartitionTypeNotFound, typeof(IPartition).Name); ", - "\n return null;", - "\n }", - "\n }", - "\n ", - "\n await DataSource.CommitAsync();", + "\n await CommitPartitionAsync(args, DataSource);", "\n return args;", "\n}" ], @@ -481,6 +503,7 @@ "\nThe following methods are used in the different importers to compute the [IfrsVariables](../DataModel/DataStructure#ifrs-variable).", "\n", "\nGetAllArgsAsync retrieves the partitions or Args that require computation. They are the union of the primary args (the one read from the main tab of the imported file) with the secondary args (senarios which depends on the best estimate data).", + "\n", "\nComputeAsync computes the IfrsVariables for a given partition (identified by its ImportArgs) and stores the results in a disposable workspace. This then serves as DataSource in the calculation of the secondary partitions (identified by the secondary args)." ], "metadata": {}, @@ -490,19 +513,42 @@ { "cell_type": "code", "source": [ - "public async Task GetAllArgsAsync(ImportArgs primaryArgs)", + "public async Task GetAllArgsAsync(ImportArgs args, IDataSource dataSource, string format)", "\n{", - "\n if (primaryArgs.Scenario != null)", - "\n return primaryArgs.RepeatOnce().ToArray();", - "\n", - "\n var secondaryArgs = await DataSource.Query() ", - "\n .Where(x => x.ReportingNode == primaryArgs.ReportingNode && x.Year == primaryArgs.Year && x.Month == primaryArgs.Month && x.Scenario != null)", - "\n .Select(x => new ImportArgs(x.ReportingNode, x.Year, x.Month, default(Periodicity), x.Scenario, ImportFormats.Cashflow)).ToArrayAsync();", + "\n ImportArgs[] allArgs;", + "\n switch(format)", + "\n {", + "\n case ImportFormats.YieldCurve : ", + "\n {", + "\n if(args.Scenario == null) {", + "\n var scenariosWithYieldCurves = await dataSource.Query().Where(x => x.Year == args.Year && x.Month == args.Month && x.Scenario != null)", + "\n .Select(x => x.Scenario).Distinct().ToArrayAsync();", + "\n var targetPartitions = await dataSource.Query()", + "\n .Where(x => x.Year == args.Year && x.Month == args.Month && !scenariosWithYieldCurves.Contains(x.Scenario)).OrderBy(x => x.Scenario).ToArrayAsync();", + "\n var targetScenarios = targetPartitions.Where(x => x.Scenario != null).Select(x => x.Scenario);", + "\n if(targetScenarios.Any()) ApplicationMessage.Log(Warning.ScenarioReCalculations, String.Join(\", \", targetScenarios));", + "\n allArgs = targetPartitions.Select(x => new ImportArgs(x.ReportingNode, x.Year, x.Month, default(Periodicity), x.Scenario, ImportFormats.Cashflow)).ToArray();", + "\n }", + "\n else {", + "\n allArgs = (await dataSource.Query()", + "\n .Where(x => x.Year == args.Year && x.Month == args.Month && x.Scenario == null).ToArrayAsync())", + "\n .Select(x => new ImportArgs(x.ReportingNode, x.Year, x.Month, default(Periodicity), args.Scenario, ImportFormats.Cashflow)).ToArray();", + "\n }", + "\n break;", + "\n }", + "\n default : ", + "\n {", + "\n if(args.Scenario != null) return args.RepeatOnce().ToArray();", + "\n var secondaryArgs = await DataSource.Query() ", + "\n .Where(x => x.ReportingNode == args.ReportingNode && x.Year == args.Year && x.Month == args.Month && x.Scenario != null)", + "\n .Select(x => new ImportArgs(x.ReportingNode, x.Year, x.Month, default(Periodicity), x.Scenario, ImportFormats.Cashflow)).ToArrayAsync();", "\n ", - "\n if (secondaryArgs.Any())", - "\n ApplicationMessage.Log(Warning.ScenarioReCalculations, String.Join(\", \", secondaryArgs.Select(x => x.Scenario)));", - "\n ", - "\n return primaryArgs.RepeatOnce().Concat(secondaryArgs).ToArray();", + "\n if(secondaryArgs.Any()) ApplicationMessage.Log(Warning.ScenarioReCalculations, String.Join(\", \", secondaryArgs.Select(x => x.Scenario)));", + "\n allArgs = args.RepeatOnce().Concat(secondaryArgs).ToArray();", + "\n break;", + "\n }", + "\n }", + "\n return allArgs.Where(x => (!EnableScenario && x.Scenario == null) || EnableScenario).ToArray();", "\n}" ], "metadata": {}, @@ -525,7 +571,7 @@ "\n if(Activity.HasErrors()) return Activity.Finish();", "\n", "\n if(storage.DefaultPartition != storage.TargetPartition){", - "\n var bestEstimateIvs = await DataSource.LoadPartitionedDataAsync(storage.DefaultPartition, storage.TargetPartition);", + "\n var bestEstimateIvs = await DataSource.LoadPartitionedDataAsync(storage.DefaultPartition);", "\n ivs = ivs.Where(iv => Math.Abs(iv.Value) >= Precision).ToArray().Concat(", "\n ivs.Where(iv => Math.Abs(iv.Value) < Precision).Intersect(bestEstimateIvs, EqualityComparer.Instance).Select(iv => iv with {Value = 0.0}).ToArray());", "\n }", @@ -678,44 +724,64 @@ "source": [ "Import.DefineFormat(ImportFormats.YieldCurve, async (options, dataSet) => {", "\n Activity.Start();", - "\n var args = GetArgsFromMain(dataSet) with {ImportFormat = ImportFormats.YieldCurve};", - "\n args.ValidateArgsForPeriod();", - "\n if(ApplicationMessage.HasErrors()) return Activity.Finish();", - "\n", + "\n var primaryArgs = GetArgsFromMain(dataSet) with {ImportFormat = ImportFormats.YieldCurve};", + "\n primaryArgs.ValidateArgsForPeriod();", + "\n if(ApplicationMessage.HasErrors()) return Activity.Finish();", "\n var workspace = Workspace.CreateNew();", - "\n workspace.Initialize(x => x.FromSource(DataSource).DisableInitialization().DisableInitialization());", + "\n workspace.Initialize(x => x.FromSource(options.TargetDataSource).DisableInitialization()", + "\n .DisableInitialization().DisableInitialization());", "\n", - "\n var committedYieldCurves = await DataSource.Query().ToArrayAsync();", + "\n var committedYieldCurves = await options.TargetDataSource.Query().ToArrayAsync();", "\n var hasNameColumn = dataSet.Tables[ImportFormats.YieldCurve].Columns.Any(x => x.ColumnName == nameof(YieldCurve.Name));", - "\n var importLog = await Import.FromDataSet(dataSet).WithType((dataset, datarow) => { ", - "\n var yieldCurve = new YieldCurve {", - "\n Currency = datarow.Field(nameof(YieldCurve.Currency)),", - "\n Year = args.Year,", - "\n Month = args.Month, ", - "\n Scenario = args.Scenario,", - "\n Values = datarow.Table.Columns.Where(c => c.ColumnName.StartsWith(nameof(YieldCurve.Values))).OrderBy(c => c.ColumnName.Length).ThenBy(c => c.ColumnName)", - "\n .Select(x => datarow.Field(x.ColumnName).CheckStringForExponentialAndConvertToDouble()).ToArray(),", - "\n Name = hasNameColumn", - "\n ? datarow.Field(nameof(YieldCurve.Name))", - "\n : default(string)", - "\n };", - "\n return committedYieldCurves.Contains(yieldCurve, YieldCurveComparer.Instance())", - "\n ? null", - "\n : yieldCurve;", - "\n }", - "\n ).WithTarget(workspace).ExecuteAsync(); ", - "\n ", + "\n var importLog = await Import.FromDataSet(dataSet).WithType((dataset, datarow) => { ", + "\n var yieldCurve = new YieldCurve { ", + "\n Currency = datarow.Field(nameof(YieldCurve.Currency)),", + "\n Year = primaryArgs.Year,", + "\n Month = primaryArgs.Month, ", + "\n Scenario = primaryArgs.Scenario,", + "\n Values = datarow.Table.Columns.Where(c => c.ColumnName.StartsWith(nameof(YieldCurve.Values))).OrderBy(c => c.ColumnName.Length).ThenBy(c => c.ColumnName)", + "\n .Select(x => datarow.Field(x.ColumnName).CheckStringForExponentialAndConvertToDouble()).ToArray(),", + "\n Name = hasNameColumn ? datarow.Field(nameof(YieldCurve.Name)) : default(string)", + "\n };", + "\n return committedYieldCurves.Contains(yieldCurve, YieldCurveComparer.Instance()) ? null : yieldCurve;", + "\n }).WithTarget(workspace).ExecuteAsync(); ", + "\n", "\n if(importLog.Errors.Any()) return Activity.Finish().Merge(importLog); ", - "\n ", - "\n var toCommitYieldCurves = await workspace.Query().ToArrayAsync();", - "\n if (!toCommitYieldCurves.Any()){", + "\n var toCommitYieldCurves = await workspace.Query().ToArrayAsync();", + "\n if (!toCommitYieldCurves.Any()) {", "\n ApplicationMessage.Log(Warning.VariablesAlreadyImported); ", "\n return Activity.Finish().Merge(importLog);", "\n }", - "\n ", - "\n await DataSource.UpdateAsync(toCommitYieldCurves);", - "\n await DataSource.CommitAsync();", + "\n", + "\n var allArgs = await GetAllArgsAsync(primaryArgs, options.TargetDataSource, ImportFormats.YieldCurve);", + "\n var updatedCurrencies = toCommitYieldCurves.Select(x => x.Currency).Distinct();", + "\n var dataNodesToUpdate = await workspace.Query().Where(x => updatedCurrencies.Contains(x.ContractualCurrency)).Select(x => x.SystemName).ToArrayAsync();", + "\n var workspaceToCompute = Workspace.CreateNew();", + "\n workspaceToCompute.Initialize(x => x.FromSource(options.TargetDataSource));", "\n ", + "\n foreach (var args in allArgs) {", + "\n await CommitPartitionAsync(args, workspace, workspaceToCompute);", + "\n var targetPartition = (Guid)(await options.TargetDataSource.Partition.GetKeyForInstanceAsync(args));", + "\n var defaultPartition = (Guid)(await options.TargetDataSource.Partition.GetKeyForInstanceAsync(", + "\n new ImportArgs(args.ReportingNode, args.Year, args.Month, default(Periodicity), null, ImportFormats.Cashflow) ));", + "\n if(ApplicationMessage.HasErrors()) return Activity.Finish().Merge(importLog);", + "\n ", + "\n // Avoid starting the computation if no best estimate cash flow has ever been imported ", + "\n if(args.Scenario == null) {", + "\n await options.TargetDataSource.Partition.SetAsync(null);", + "\n if(!(await options.TargetDataSource.Query().Where(x => x.Partition == targetPartition).Take(1).ToArrayAsync()).Any()) continue;", + "\n }", + "\n", + "\n // Remove data nodes which are unaffected by the updated yield curves", + "\n await workspaceToCompute.DeleteAsync( await workspaceToCompute.Query()", + "\n .Where(x => !(dataNodesToUpdate.Contains(x.DataNode) && (x.Partition == targetPartition || x.Partition == defaultPartition))).ToArrayAsync() );", + "\n", + "\n importLog = importLog.Merge(await ComputeAsync(args, workspace, workspaceToCompute, false));", + "\n if(importLog.Errors.Any()) return Activity.Finish().Merge(importLog);", + "\n }", + "\n", + "\n await workspaceToCompute.UpdateAsync(toCommitYieldCurves);", + "\n await workspaceToCompute.CommitToTargetAsync(options.TargetDataSource);", "\n return Activity.Finish().Merge(importLog);", "\n});" ], @@ -1160,7 +1226,7 @@ "\n var primaryArgs = await GetArgsAndCommitPartitionAsync(dataSet) with {ImportFormat = ImportFormats.Cashflow};", "\n if(Activity.HasErrors()) return Activity.Finish();", "\n", - "\n var allArgs = await GetAllArgsAsync(primaryArgs);", + "\n var allArgs = await GetAllArgsAsync(primaryArgs, options.TargetDataSource, ImportFormats.Cashflow);", "\n await DataNodeFactoryAsync(dataSet, ImportFormats.Cashflow, primaryArgs);", "\n if(Activity.HasErrors()) return Activity.Finish();", "\n ", @@ -1261,7 +1327,7 @@ "\n var primaryArgs = await GetArgsAndCommitPartitionAsync(dataSet) with {ImportFormat = ImportFormats.Actual};", "\n if(Activity.HasErrors()) return Activity.Finish();", "\n", - "\n var allArgs = await GetAllArgsAsync(primaryArgs);", + "\n var allArgs = await GetAllArgsAsync(primaryArgs, options.TargetDataSource, ImportFormats.Actual);", "\n await DataNodeFactoryAsync(dataSet, ImportFormats.Actual, primaryArgs);", "\n if(Activity.HasErrors()) return Activity.Finish();", "\n", diff --git a/ifrs17/Utils/Queries.ipynb b/ifrs17/Utils/Queries.ipynb index 2a102c0f..6fa652ed 100644 --- a/ifrs17/Utils/Queries.ipynb +++ b/ifrs17/Utils/Queries.ipynb @@ -193,7 +193,7 @@ "\n var loadedYc = (await querySource.LoadCurrentParameterAsync(argsNew, x => x.Currency, x => x.Currency == dn.ContractualCurrency && x.Name == dn.YieldCurveName));", "\n ", "\n if (!loadedYc.TryGetValue(dn.ContractualCurrency, out var lockedYc))", - "\n ApplicationMessage.Log(Error.YieldCurveNotFound, dn.ContractualCurrency, argsNew.Year.ToString(), argsNew.Month.ToString());", + "\n ApplicationMessage.Log(Error.YieldCurveNotFound, dn.ContractualCurrency, argsNew.Year.ToString(), argsNew.Month.ToString(), argsNew.Scenario, dn.YieldCurveName);", "\n ", "\n lockedInYieldCurveByGoc[dn.DataNode] = lockedYc;", "\n }", @@ -235,7 +235,7 @@ "\n : true));", "\n ", "\n if (!loadedYc.TryGetValue(key.ContractualCurrency, out var currentYcDict))", - "\n ApplicationMessage.Log(Error.YieldCurveNotFound, key.ContractualCurrency, args.Year.ToString(), args.Month.ToString());", + "\n ApplicationMessage.Log(Error.YieldCurveNotFound, key.ContractualCurrency, args.Year.ToString(), args.Month.ToString(), args.Scenario, key.YieldCurveName);", "\n", "\n foreach(var dn in dnByValAppContrCurrYcName[key])", "\n {", @@ -432,15 +432,15 @@ { "cell_type": "code", "source": [ - "public static async Task LoadPartitionedDataAsync(this IDataSource querySource, Guid partition, Guid? partitionBackup = null)", + "public static async Task LoadPartitionedDataAsync(this IDataSource querySource, Guid partition)", "\n where T : IPartitioned", "\n where P : IPartition", - "\n", "\n{", + "\n var partitionBackup = (Guid)querySource.Partition.GetCurrent(typeof(P).Name);", "\n await querySource.Partition.SetAsync

(partition);", "\n // Temporary workaround for physical database: where clause is necessary", "\n var data = await querySource.Query().Where(x => x.Partition == partition).ToArrayAsync();", - "\n if(partitionBackup != null) await querySource.Partition.SetAsync

(partitionBackup);", + "\n await querySource.Partition.SetAsync

(partitionBackup);", "\n return data;", "\n}" ], @@ -455,19 +455,19 @@ "\n where T : IPartitioned", "\n where P : IPartition", "\n{", - "\n var isRelaxed = targetPartition != defaultPartition &&", - "\n ((format != ImportFormats.Cashflow && typeof(T).Name == nameof(IfrsVariable)) ||", + "\n var isRelaxed = ((format != ImportFormats.Cashflow && typeof(T).Name == nameof(IfrsVariable)) ||", "\n (format == ImportFormats.Cashflow && typeof(T).Name == nameof(RawVariable)));", "\n", "\n var variablesFromWorkspace = await workspace.LoadPartitionedDataAsync(targetPartition);", "\n if(!isRelaxed || variablesFromWorkspace.Any()) return variablesFromWorkspace;", "\n", - "\n // For scenario re-calculation", + "\n // This is for scenario re-calculation", "\n var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(targetPartition);", "\n if(variablesFromDataSource.Any()) return variablesFromDataSource;", "\n", - "\n // For scenarios related to parameters solely", - "\n return await dataSource.LoadPartitionedDataAsync(defaultPartition, targetPartition);", + "\n // This is for scenarios affecting parameters solely", + "\n // And for the best estimate when parameters are updated", + "\n return await dataSource.LoadPartitionedDataAsync(defaultPartition);", "\n}" ], "metadata": {},