diff --git a/fpna-template/Files/AmountType.ipynb b/fpna-template/Files/AmountType.ipynb index 888b28a2..8e4e45ad 100644 --- a/fpna-template/Files/AmountType.ipynb +++ b/fpna-template/Files/AmountType.ipynb @@ -55,42 +55,41 @@ "\nXC,Claims Incurred Pattern,", "\nXN,Claims Paid Pattern,\"", "\n;", - "\nvar assetAmountTypes = @\"@@AssetAmountType", - "\nSystemName,DisplayName,Parent", - "\nSH,Shares,", - "\nRE,Real Estate,", - "\nCH,Cash,", - "\nBO,Bonds,", - "\nCB,CouponBond,BO", - "\nCBC,Coupon,CB", - "\nCBF,FaceValue,CB\"", - "\n;", + "\n", "\nvar solvencyBsAmountTypes = @\"@@SolvencyBsAmountType", "\nSystemName,DisplayName,Parent", "\nS2A,Solvency II Assets,", - "\nS2IA,Invested Assets,S2A", + "\nS2ASH,Shares,S2A", + "\nS2ARE,Real Estate,S2A", + "\nS2ACH,Cash,S2A", + "\nS2ABO,Bonds,S2A", + "\nS2AOT,Other Assets,S2A", "\nS2L,Solvency II Liabilities,", - "\nS2TP,Technical Provisions,S2L", - "\nS2RM,Risk Margin,S2TP", - "\nS2BEL,Best Estimate Liability,S2TP", - "\nS2OF,Own Funds,S2L", - "\nS2SCR,SCR,S2OF", - "\nS2FC,Free Capital,S2OF\"", + "\nS2LTP,Technical Provisions,S2L", + "\nS2LRM,Risk Margin,S2LTP", + "\nS2LBEL,Best Estimate Liability,S2LTP", + "\nS2LOF,Own Funds,S2L", + "\nS2LSCR,SCR,S2LOF", + "\nS2LFC,Free Capital,S2LOF\"", "\n;", "\nvar ifrsBsAmountTypes = @\"@@IfrsBsAmountType", "\nSystemName,DisplayName,Parent", - "\nIFRSA,IFRS17 Assets,", - "\nIFRSIA,Invested Assets,IFRSA", - "\nIFRSOCI,Asset OCI,IFRSA", - "\nIFRSL,IFRS17 Liabilites,", - "\nIFRSBE,Best Estimate Liabilities,IFRSL", - "\nIFRSBEL,BEL Book Value,IFRSBE", - "\nIFRSBELOCI,Best Estimate Liabilities OCI,IFRSBE", - "\nIFRSRA,Risk Adjustment,IFRSL", - "\nIFRSCSM,CSM,IFRSL", - "\nIFRSEQ,Shareholders Equity,", - "\nIFRSEQOCI,Net OCI Reserve,IFRSEQ", - "\nIFRSEQRE,Retained Earnings,IFRSEQ\"", + "\nIFA,IFRS Assets,", + "\nIFASH,Shares,IFA", + "\nIFARE,Real Estate,IFA", + "\nIFACH,Cash,IFA", + "\nIFABO,Bonds,IFA", + "\nIFAOT,Other Assets,IFA", + "\nIFAOCI,Asset OCI,IFA", + "\nIFL,IFRS Liabilites,", + "\nIFLBE,Best Estimate Liabilities,IFL", + "\nIFLBEV,BEL Book Value,IFLBE", + "\nIFLBEOCI,Best Estimate Liabilities OCI,IFLBE", + "\nIFLRA,Risk Adjustment,IFL", + "\nIFLCSM,CSM,IFL", + "\nIFLEQ,Shareholders Equity,", + "\nIFLEQOCI,Net OCI Reserve,IFLEQ", + "\nIFLEQRE,Retained Earnings,IFLEQ\"", "\n;" ], "metadata": {}, diff --git a/fpna-template/Files/Cashflows.ipynb b/fpna-template/Files/Cashflows.ipynb index 6cce9eb5..312fdffa 100644 --- a/fpna-template/Files/Cashflows.ipynb +++ b/fpna-template/Files/Cashflows.ipynb @@ -16,13 +16,46 @@ "nbformat": 4, "nbformat_minor": 5, "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Cashflow Definition Input" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Generic" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ - "var basicCashflows = @\"@@BasicCashflow", + "var CashflowsDefInput = @\"", + "\n@@Main", + "\nReportingNode,Year,Month,Scenario,", + "\nCH,2020,12,,", + "\n@@BasicCashflow", "\nDataNode,AmountType,Shape,InitialValue,Length,Shift,Values0,Values1,Values2,Values3,Values4,", "\nG.BBA.LRC.2020.P,PR,Constant,10000,5,0,,,,,", "\nG.BBA.LRC.2020.P,PR,Linear,1000,4,1,0.5,0.5,0.5,0.5,", + "\n@@BoundedCashflow ", + "\nDataNode,AmountType,Shape,InitialValue,Length,Shift,Minimum,Maximum", + "\nG.BBA.LRC.2020.P,PR,Random,6000,4,1,100,400", + "\n@@ReferencedCashflow", + "\nDataNode,AmountType,AccidentYear,Shape,InitialValue,Length,Shift,ReferenceAmountType,Values0,Values1,Values2,Values3,Values4", + "\nG.BBA.LRC.2020.P,PR,,Reference,,,,PR,0.5,0.5,0.5,0.5,0.5", + "\nG.BBA.LRC.2020.P,AEM,,Reference,,,,PR,-0.5,-0.5,-0.5,-0.5,-0.5", + "\nG.BBA.LRC.2020.P,CL,,Reference,,,,PR,-0.25,-0.25,-0.25,-0.25,-0.25", + "\nG.BBA.LRC.2020.P,CL,,Reference,,,,AEM,0.5,0.5,0.5,0.5,0.5", + "\nG.BBA.LRC.2020.P,S2ASH,,Reference,,,,PR,1,1,1,1,1", + "\nG.BBA.LRC.2020.P,S2LTP,,Reference,,,,PR,3,3,3,4,5", "\n\";" ], "metadata": {}, @@ -30,12 +63,9 @@ "outputs": [] }, { - "cell_type": "code", + "cell_type": "markdown", "source": [ - "var boundedCashflows = @\"@@BoundedCashflow ", - "\nDataNode,AmountType,Shape,InitialValue,Length,Shift,Minimum,Maximum", - "\nG.BBA.LRC.2020.P,PR,Random,6000,4,1,100,400", - "\n\";" + "## Solvency" ], "metadata": {}, "execution_count": 0, @@ -44,15 +74,21 @@ { "cell_type": "code", "source": [ - "var referencedCashflows = @\"@@ReferencedCashflow", + "var SolvencyCashflowsDefInput = @\"", + "\n@@Main", + "\nReportingNode,Year,Month,Scenario,", + "\nCH,2020,12,,", + "\n@@BasicCashflow", + "\nDataNode,AmountType,Shape,InitialValue,Length,Shift,Values0,Values1,Values2,Values3,Values4,", + "\nG.BBA.LRC.2020.P,PR,Constant,10000,5,0,,,,,", + "\nG.BBA.LRC.2020.P,PR,Linear,1000,4,1,0.5,0.5,0.5,0.5,", + "\n@@ReferencedCashflow", "\nDataNode,AmountType,AccidentYear,Shape,InitialValue,Length,Shift,ReferenceAmountType,Values0,Values1,Values2,Values3,Values4", "\nG.BBA.LRC.2020.P,PR,,Reference,,,,PR,0.5,0.5,0.5,0.5,0.5", "\nG.BBA.LRC.2020.P,AEM,,Reference,,,,PR,-0.5,-0.5,-0.5,-0.5,-0.5", "\nG.BBA.LRC.2020.P,CL,,Reference,,,,PR,-0.25,-0.25,-0.25,-0.25,-0.25", "\nG.BBA.LRC.2020.P,CL,,Reference,,,,AEM,0.5,0.5,0.5,0.5,0.5", - "\nG.BBA.LRC.2020.P,S2TP,,Reference,,,,PR,3,3,3,4,5", - "\n\";", - "\n" + "\n\";" ], "metadata": {}, "execution_count": 0, diff --git a/fpna-template/Files/RiskDriver.ipynb b/fpna-template/Files/RiskDriver.ipynb new file mode 100644 index 00000000..0bb13862 --- /dev/null +++ b/fpna-template/Files/RiskDriver.ipynb @@ -0,0 +1,103 @@ +{ + "metadata": { + "authors": [], + "id": "QeoE_xy_RkWkEesq4zH7Yw", + "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": [ + "# Risk Driver" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var riskDrivers = @\"@@RiskDriver", + "\nSystemName,DisplayName,Parent", + "\nT,All Risks,", + "\nADJ,Adjustments to Risk,T", + "\nOPR,Operational Risk,T", + "\nBR,Basic Risk,T", + "\nMR,Market Risk,BR", + "\nEQR,Equity,MR", + "\nPPR,Property,MR", + "\nIRR,Interest Rate,MR", + "\nSPR,Spread,MR", + "\nCUR,Currency,MR", + "\nCOR,Concentration,MR", + "\nHR,Health Insurance Risk,BR", + "\nHSR,Health Similar to Life Techniques Risk,HR", + "\nHDIS,Disability Morbidity Heath SLT Risk,HSR", + "\nHMOR,Mortality Heath SLT Risk,HSR", + "\nHLON,Longevity Heath SLT Risk,HSR", + "\nHLAP,Lapse Heath SLT Risk,HSR", + "\nHEXP,Expenses Heath SLT Risk,HSR", + "\nHREV,Revision Heath SLT Risk,HSR", + "\nHCAT,Catastrophe Heath SLT Risk,HR", + "\nHNR,Health Not Similar to Life Techniques Risk,HR", + "\nHPRR,Premium Reserve Heath NSLT Risk,HNR", + "\nHLAP,Lapse Heath NSLT Risk,HNR", + "\nLIR,Life Insurance Risk,BR", + "\nLMOR,Mortality Life Risk,LIR", + "\nLDIS,Disability Morbidity Life Risk,LIR", + "\nLLON,Longevity Life Risk,LIR", + "\nLLAP,Lapse Life Risk,LIR", + "\nLEXP,Expenses Life Risk,LIR", + "\nLREV,Revision Life Risk,LIR", + "\nLCAT,Catastrophe Life Risk,LIR", + "\nNR,Non-Life Insurance Risk,BR", + "\nNPRR,Premium Reserve Non-Life Risk,NR", + "\nNLAP,Lapse Non-Life Risk,NR", + "\nNCAT,Catastrophe Non-Life Risk,NR", + "\nDR,Counterparty Default Risk,BR", + "\nIR,Intangible Risk,BR\"", + "\n;" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Risk Driver Correlation Matrix" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var riskDriverCorrelationMatrix = @\"@@BoundedCashflow ", + "\nDataNode,RiskDriver,AmountType,Shape,InitialValue,Length,Shift,Minimum,Maximum", + "\nDT1,LIR,PPR,Costant,0.75,5,0,-1,1", + "\nDT1,LIR,LDIS,Costant,0.75,5,0,-1,1", + "\nDT1,MR,LIR,Costant,0.75,5,0,-1,1", + "\nDT1,MR,HSR,Costant,0.75,5,0,-1,1", + "\nDT1,LIR,HSR,Costant,0.75,5,0,-1,1", + "\n\";", + "\n" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/fpna-template/Import/CloseImportTemplate.ipynb b/fpna-template/Import/FpnaCloseImportTemplate.ipynb similarity index 76% rename from fpna-template/Import/CloseImportTemplate.ipynb rename to fpna-template/Import/FpnaCloseImportTemplate.ipynb index f007d5e7..78339755 100644 --- a/fpna-template/Import/CloseImportTemplate.ipynb +++ b/fpna-template/Import/FpnaCloseImportTemplate.ipynb @@ -55,8 +55,8 @@ { "cell_type": "code", "source": [ - "var reportingNode = \"DE\";", - "\nvar year = 2022;", + "var reportingNode = \"CH\";", + "\nvar year = 2020;", "\nvar month = 12;", "\nvar importFormat = ImportFormats.Cashflow;", "\nvar periodicity = Periodicity.Monthly;", @@ -124,6 +124,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "storage.CashflowDefinitions" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -203,7 +212,16 @@ "cell_type": "code", "source": [ "var referenced = cashflowVariables.Where(x => x.Layer.Contains(\"Reference\"));", - "\nreferenced.AggregateOver()" + "\nreferenced" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# debug Solvency" ], "metadata": {}, "execution_count": 0, @@ -212,7 +230,38 @@ { "cell_type": "code", "source": [ - "" + "var solvencystorage = new SolvencyStorage(args, DataSource, Workspace);", + "\nawait solvencystorage.InitializeAsync();", + "\nsolvencystorage.CashflowDefinitions" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface IModel : IMutableScopeWithStorage{}", + "\nvar solvencyUniverse = Scopes.ForStorage(solvencystorage).ToScope();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var solvencyIdentities = solvencystorage.Identities;" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var cashflowVariables = Scopes.ForIdentities(solvencyIdentities, solvencystorage).ToScopes().SelectMany(x => x.FpaVariables).ToArray();", + "\ncashflowVariables" ], "metadata": {}, "execution_count": 0, diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index 1d2026e2..72f581b4 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -29,7 +29,16 @@ { "cell_type": "markdown", "source": [ - "# Set default DataSource" + "# Initialize DataSource" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## New DataSource" ], "metadata": {}, "execution_count": 0, @@ -45,6 +54,36 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Initialize from IFRS 17 calculation engine template" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "// var ifrs17templateInit2 = $\"#!import \\\"//{projectTemplateName}/{environmentTemplateName}/Initialization/InitSystemorphUtils\\\"\";", + "\n// ifrs17templateInit2 = $\"#!import \\\"../../{projectTemplateName}/Initialization/InitSystemorphUtils\\\"\";", + "\n// #!eval ifrs17templateInit2" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "// //TODO this works only when everything is loaded in one project", + "\n// await Import.InitializeDataNodesAsync(DataSource, $\"../../{projectTemplateName}/Files/\", \"CH\")" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -83,6 +122,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Amount Types" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -95,10 +143,10 @@ { "cell_type": "code", "source": [ - "await Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", + "// EarningsAmountType overlaps with the IFRS17 => clean up the data", + "\nawait Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync(); //uncomment this when not initiliazing form ifrs17Template", "\nawait Import.FromString(ratioAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(patternAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", - "\nawait Import.FromString(assetAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(solvencyBsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(ifrsBsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();" ], @@ -109,7 +157,7 @@ { "cell_type": "markdown", "source": [ - "# Import Cashflows" + "## Risk Driver" ], "metadata": {}, "execution_count": 0, @@ -118,7 +166,25 @@ { "cell_type": "code", "source": [ - "#!import \"../Files/Cashflows\"" + "#!import \"../Files/RiskDriver\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(riskDrivers).WithType().WithTarget(DataSource).ExecuteAsync();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Import Cashflows" ], "metadata": {}, "execution_count": 0, @@ -127,7 +193,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(basicCashflows).WithType().WithTarget(Workspace).ExecuteAsync()" + "#!import \"../Files/Cashflows\"" ], "metadata": {}, "execution_count": 0, @@ -136,7 +202,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(boundedCashflows).WithType().WithTarget(Workspace).ExecuteAsync()" + "Debug.Enable = true;" ], "metadata": {}, "execution_count": 0, @@ -145,7 +211,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(referencedCashflows).WithType().WithTarget(Workspace).ExecuteAsync()" + "await Import.FromString(CashflowsDefInput).WithFormat(\"CashflowDefinition\").WithTarget(Workspace).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -154,7 +220,7 @@ { "cell_type": "code", "source": [ - "" + "await Import.FromString(SolvencyCashflowsDefInput).WithFormat(SolvencyImportFormats.SolvencyCashflowDefinition).WithTarget(Workspace).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, diff --git a/fpna-template/Report/Earnings.ipynb b/fpna-template/Report/Earnings.ipynb index 9842ce09..02bd06da 100644 --- a/fpna-template/Report/Earnings.ipynb +++ b/fpna-template/Report/Earnings.ipynb @@ -78,7 +78,8 @@ "source": [ "await Report.ForDataCube(", "\n cashflowVariables.ToDataCube().ToReportType()", - "\n .Filter(x => storage.EarningsAmountTypes.Contains(x.AmountType)))", + "\n //.Filter(x => storage.EarningsAmountTypes.Contains(x.AmountType))", + "\n )", "\n.SliceRowsBy(\"AmountType\", \"Layer\")", "\n.SliceColumnsBy(\"Projection\")", "\n.WithQuerySource(DataSource)", @@ -108,14 +109,15 @@ "cell_type": "code", "source": [ "await Report.ForDataCube(", - "\n cashflowVariables.ToDataCube().ToReportType()", - "\n .Filter(x => storage.EarningsAmountTypes.Contains(x.AmountType)))", + "\n cashflowVariables.ToDataCube().ToReportType().AggregateBy(\"AmountType\")", + "\n //.Filter(x => storage.EarningsAmountTypes.Contains(x.AmountType))", + "\n )", "\n.SliceRowsBy(\"AmountType\")", "\n.SliceColumnsBy(\"Projection\") ", - "\n .WithQuerySource(DataSource)", - "\n .ToBarChart()", - "\n .WithColorScheme(Palettes.Brewer.PuBu3)", - "\n .ExecuteAsync()" + "\n.WithQuerySource(DataSource)", + "\n.ToBarChart()", + "\n.WithColorScheme(Palettes.Brewer.PuBu3)", + "\n.ExecuteAsync()" ], "metadata": {}, "execution_count": 0, diff --git a/fpna-template/Report/Solvency.ipynb b/fpna-template/Report/Solvency.ipynb index 37ea49d1..cf7e7bc4 100644 --- a/fpna-template/Report/Solvency.ipynb +++ b/fpna-template/Report/Solvency.ipynb @@ -78,7 +78,8 @@ "source": [ "await Report.ForDataCube(", "\n cashflowVariables.ToDataCube().ToReportType()", - "\n .Filter(x => storage.SolvencyBsAmountTypes.Contains(x.AmountType)))", + "\n //.Filter(x => storage.SolvencyBsAmountTypes.Contains(x.AmountType))", + "\n )", "\n.SliceRowsBy(\"AmountType\", \"Layer\")", "\n.SliceColumnsBy(\"Projection\")", "\n.WithQuerySource(DataSource)", @@ -109,7 +110,8 @@ "source": [ "await Report.ForDataCube(", "\n cashflowVariables.ToDataCube().ToReportType()", - "\n .Filter(x => storage.SolvencyBsAmountTypes.Contains(x.AmountType)))", + "\n //.Filter(x => storage.SolvencyBsAmountTypes.Contains(x.AmountType))", + "\n )", "\n.SliceRowsBy(\"AmountType\")", "\n.SliceColumnsBy(\"Projection\")", "\n .WithQuerySource(DataSource)", diff --git a/fpna-template/Test/CHashFlowerTest.ipynb b/fpna-template/Test/CHashFlowerTest.ipynb new file mode 100644 index 00000000..a91bd3a8 --- /dev/null +++ b/fpna-template/Test/CHashFlowerTest.ipynb @@ -0,0 +1,306 @@ +{ + "metadata": { + "authors": [], + "id": "0Sv-iuexO02G_9VRlhVTLw", + "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": [ + "

CHashFlower calculation Test

" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#!import \"../Initialization/InitSystemorphToMemory\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Args" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var reportingNode = \"CH\";", + "\nvar year = 2020;", + "\nvar month = 12;", + "\nvar importFormat = ImportFormats.Cashflow;", + "\nvar periodicity = Periodicity.Monthly;", + "\nvar scenario = (string)null;", + "\n", + "\nImportArgs args = new ImportArgs(reportingNode, year, month, periodicity, scenario, importFormat);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Basic" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var workspace = Workspace.CreateNew();", + "\nworkspace.Initialize(x => x.FromSource(DataSource));" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var basicCashflows = @\"", + "\n@@Main", + "\nReportingNode,Year,Month,Scenario,", + "\nCH,2020,12,,", + "\n@@BasicCashflow", + "\nDataNode,AmountType,Shape,InitialValue,Length,Shift,Values0,Values1,Values2,Values3,Values4,", + "\nG.BBA.LRC.2020.P,PR,Constant,10,5,0,,,,,", + "\nG.BBA.LRC.2020.P,PR,Linear,10,5,1,2,-1,0.5,0.2", + "\n\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(basicCashflows).WithFormat(\"CashflowDefinition\").WithTarget(workspace).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var storage = new CashflowStorage(args, DataSource, workspace);", + "\nawait storage.InitializeAsync();", + "\n", + "\nvar identities = storage.Identities;", + "\n", + "\npublic interface IModel : IMutableScopeWithStorage{}", + "\nvar universe = Scopes.ForStorage(storage).ToScope();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var cashflowVariables = Scopes.ForIdentities(identities, storage).ToScopes().SelectMany(x => x.FpaVariables).ToArray();", + "\ncashflowVariables" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "double[] array1 = new double[] { 2.0, 3.0, 6.7 };", + "\narray1.SequenceEqual(new double[] { 2.0, 3.0, 6.7 }, Precision);" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "cashflowVariables.Single(x => x.Layer.Contains(\"Constant\")).Values.SequenceEqual(new double[] { 10, 10, 10, 10, 10 }, Precision).Should().BeTrue();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "cashflowVariables.Single(x => x.Layer.Contains(\"Linear\")).Values.SequenceEqual(new double[] { 0, 20, -20, -10, -2}, Precision).Should().BeTrue();", + "\n", + "\n// Check computed : new double[] { 0, 10, -10, -5, -1} ; ", + "\n// The first weight is used for the skipped value => useless Values input up to Shift", + "\n// The first value after the skip is the InitialValue making the Value1 column useless" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "workspace.Dispose()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Bounded" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var workspace = Workspace.CreateNew();", + "\nworkspace.Initialize(x => x.FromSource(DataSource));" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var boundedCashflows = @\"", + "\n@@Main", + "\nReportingNode,Year,Month,Scenario,", + "\nCH,2020,12,,", + "\n@@BoundedCashflow ", + "\nDataNode,AmountType,Shape,InitialValue,Length,Shift,Minimum,Maximum", + "\nG.BBA.LRC.2020.P,PR,Random,6000,4,1,100,101", + "\n\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "await Import.FromString(boundedCashflows).WithFormat(\"CashflowDefinition\").WithTarget(workspace).ExecuteAsync()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var storage = new CashflowStorage(args, DataSource, workspace);", + "\nawait storage.InitializeAsync();", + "\nvar identities = storage.Identities;", + "\n", + "\npublic interface IModel : IMutableScopeWithStorage{}", + "\nvar universe = Scopes.ForStorage(storage).ToScope();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "((ICashflowStorage)storage).GetCashflow(identities.First())" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var cashflowVariables = Scopes.ForIdentities(identities, storage).ToScopes().SelectMany(x => x.FpaVariables).ToArray();", + "\ncashflowVariables" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "cashflowVariables.Single().Values.First().Should().BeApproximately(0d, Precision);", + "\ncashflowVariables.Single().Values.Skip(1).All(x => x < 101d && x > 100d).Should().BeTrue();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "workspace.Dispose()" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Reference" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/fpna-template/Test/Tests.ipynb b/fpna-template/Test/Tests.ipynb new file mode 100644 index 00000000..ee26f27f --- /dev/null +++ b/fpna-template/Test/Tests.ipynb @@ -0,0 +1,38 @@ +{ + "metadata": { + "authors": [], + "id": "SetsIWimYkGD7OexDiPkSw", + "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": [ + "

Tests

" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#!import \"CHashFlowerTest\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/fpna/Constants/Consts.ipynb b/fpna/Constants/Consts.ipynb new file mode 100644 index 00000000..c44dea86 --- /dev/null +++ b/fpna/Constants/Consts.ipynb @@ -0,0 +1,31 @@ +{ + "metadata": { + "authors": [], + "id": "bJt8xTHAUESnpCCmxGokOg", + "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": [ + "public static class SolvencyImportFormats{", + "\n public const string SolvencyCashflowDefinition = nameof(SolvencyCashflowDefinition); // Importer for Cash flows Defition ", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/fpna/Constants/Ifrs17CalculationEngine.ipynb b/fpna/Constants/Ifrs17CalculationEngine.ipynb index cfa2868b..b23fe482 100644 --- a/fpna/Constants/Ifrs17CalculationEngine.ipynb +++ b/fpna/Constants/Ifrs17CalculationEngine.ipynb @@ -31,7 +31,8 @@ "var projectName = \"ifrs17\";", "\nvar environmentName = \"dev\";", "\nvar notebookName = \"CalculationEngine\";", - "\nvar calculationEngine = $\"#!import \\\"//{projectName}/{environmentName}/{notebookName}\\\"\";" + "\nvar calculationEngine = $\"#!import \\\"//{projectName}/{environmentName}/{notebookName}\\\"\";", + "\n//var calculationEngine = $\"#!import \\\"../../{projectName}/{notebookName}\\\"\";" ], "metadata": {}, "execution_count": 0, @@ -45,6 +46,25 @@ "metadata": {}, "execution_count": 0, "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# IFRS 17 Template" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var projectTemplateName = \"ifrs17-template\";", + "\nvar environmentTemplateName = \"dev\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file diff --git a/fpna/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index 2ff9a4b5..d570da03 100644 --- a/fpna/DataModel/DataStructure.ipynb +++ b/fpna/DataModel/DataStructure.ipynb @@ -35,6 +35,16 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "#!import \"../Constants/Enums\"", + "\n#!import \"../Constants/Consts\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -46,6 +56,74 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "# Interfaces" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface IBased", + "\n{", + "\n [IdentityProperty][Dimension(typeof(Shape), nameof(Shape))] // TODO find a better name", + "\n public Shape Shape { get; init; }", + "\n public double InitialValue { get; init; }", + "\n public int Length { get; init; }", + "\n public int Shift { get; init; }", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface IBounded", + "\n{", + "\n public double Minimum { get; init; }", + "\n public double Maximum { get; init; }", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface IReferenced", + "\n{", + "\n [IdentityProperty]", + "\n [Dimension(typeof(AmountType))] ", + "\n public string ReferenceAmountType { get; init; }", + "\n", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface IWithPillar", + "\n{", + "\n [IdentityProperty]", + "\n //[Dimension(typeof(Pillar))] ", + "\n public string Pillar { get; init; }", + "\n", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -70,7 +148,6 @@ "public record EarningsAmountType : AmountType {} // TODO resolve the potential conflict in SystemName", "\npublic record RatioAmountType : AmountType {}", "\npublic record PatternAmountType : AmountType {}", - "\npublic record AssetAmountType : AmountType {}", "\npublic record SolvencyBsAmountType : AmountType {}", "\npublic record IfrsBsAmountType : AmountType {}" ], @@ -90,49 +167,28 @@ { "cell_type": "code", "source": [ - "public record Cashflow : IKeyed", + "public record Cashflow : IKeyed, IPartitioned, IWithPillar", "\n{", - "\n [Key]", - "\n [NotVisible] ", + "\n [Key][NotVisible] ", "\n public Guid Id { get; init; }", "\n", - "\n [IdentityProperty]", - "\n [Dimension(typeof(string), nameof(DataNode))]", + "\n [NotVisible][PartitionKey(typeof(PartitionByReportingNodeAndPeriod))]", + "\n public Guid Partition { get; init; }", + "\n", + "\n [IdentityProperty][Dimension(typeof(string), nameof(DataNode))]", "\n public string DataNode { get; init; }", "\n", - "\n [IdentityProperty]", - "\n [Dimension(typeof(AmountType))] ", + "\n [IdentityProperty][Dimension(typeof(AmountType))] ", "\n public string AmountType { get; init; }", "\n", - "\n [NoArithmetics(ArithmeticOperation.Scale)]", - "\n [Dimension(typeof(int), nameof(AccidentYear))]", - "\n [IdentityProperty]", + "\n [IdentityProperty][NoArithmetics(ArithmeticOperation.Scale)][Dimension(typeof(int), nameof(AccidentYear))]", "\n public int? AccidentYear { get; init; }", "\n", - "\n [IdentityProperty]", - "\n [Dimension(typeof(Shape), nameof(Shape))] // TODO find a better name", - "\n public Shape Shape { get; init; }", - "\n ", - "\n [Dimension(typeof(double), nameof(Values))]", - "\n public double[] Values { get; init; }", - "\n", - "\n}" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "public record SimpleCashflow : Cashflow", - "\n{", - "\n public double InitialValue { get; init; }", - "\n", - "\n public int Length { get; init; }", - "\n", - "\n public int Shift { get; init; }", + "\n [IdentityProperty]//[Dimension(typeof(Pillar))] ", + "\n public string Pillar { get; init; }", "\n", + "\n //[Dimension(typeof(double), nameof(Values))]", + "\n public double[] Values { get; init; }", "\n}" ], "metadata": {}, @@ -151,7 +207,14 @@ { "cell_type": "code", "source": [ - "public record BasicCashflow : SimpleCashflow {}" + "public record BasicCashflow : Cashflow, IBased", + "\n{", + "\n [IdentityProperty][Dimension(typeof(Shape), nameof(Shape))]", + "\n public Shape Shape { get; init; }", + "\n public double InitialValue { get; init; }", + "\n public int Length { get; init; }", + "\n public int Shift { get; init; }", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -169,10 +232,14 @@ { "cell_type": "code", "source": [ - "public record BoundedCashflow : SimpleCashflow", + "public record BoundedCashflow : Cashflow, IBased, IBounded", "\n{", + "\n [IdentityProperty][Dimension(typeof(Shape), nameof(Shape))]", + "\n public Shape Shape { get; init; }", + "\n public double InitialValue { get; init; }", + "\n public int Length { get; init; }", + "\n public int Shift { get; init; }", "\n public double Minimum { get; init; }", - "\n", "\n public double Maximum { get; init; }", "\n}" ], @@ -192,8 +259,14 @@ { "cell_type": "code", "source": [ - "public record ReferencedCashflow : SimpleCashflow", + "public record ReferencedCashflow : Cashflow, IBased, IReferenced //TODO should it inherit also from IBounded?", "\n{", + "\n [IdentityProperty][Dimension(typeof(Shape), nameof(Shape))]", + "\n public Shape Shape { get; init; }", + "\n public double InitialValue { get; init; }", + "\n public int Length { get; init; }", + "\n public int Shift { get; init; }", + "\n", "\n [IdentityProperty]", "\n [Dimension(typeof(AmountType))] ", "\n public string ReferenceAmountType { get; init; }", @@ -203,12 +276,20 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Fpa Variable" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ "public record FpaVariable : RawVariable", "\n{", - "\n //AocType => the minimum set for ifrs17 would be CL,C. (Projection of NB is included in CL,C)", "\n [Dimension(typeof(string))] ", "\n public string Layer { get; init; }", "\n}" @@ -217,6 +298,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Fpa Report Variable" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -236,10 +326,11 @@ "\n ", "\n [NotVisible] /*[Dimension(typeof(EstimateType))]*/", "\n public string EstimateType { get; init; }", - "\n ", + "\n", "\n [NotVisible] [Dimension(typeof(string), nameof(Layer))] ", "\n public string Layer { get; init; }", "\n", + "\n [NoArithmetics(ArithmeticOperation.Scale)]", "\n [NotVisible] [Dimension(typeof(int), nameof(Projection))]", "\n public int Projection { get; init; }", "\n", diff --git a/fpna/Import/Importers.ipynb b/fpna/Import/Importers.ipynb index c079f458..ba6089f9 100644 --- a/fpna/Import/Importers.ipynb +++ b/fpna/Import/Importers.ipynb @@ -24,6 +24,259 @@ "metadata": {}, "execution_count": 0, "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Other Methods" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "//output ComputeSolvency(inputs){", + "\n// var SolvencyStorage =", + "\n// SolvencyStorage.InitializeAsync();", + "\n// var solvencyUniverse = ", + "\n// var identites = ", + "\n// solvencyUniverse.GetsScope(identities);", + "\n// }" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public async Task ComputePillarAsync(ImportArgs args, IWorkspace workspace, IWorkspace workspaceToCompute, bool saveRawVariables, string pillar)", + "\n{", + "\n // //switch pillar {", + "\n // \"Solvency\" => ComputeSolvency(),", + "\n // }", + "\n return null;", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Generic Cashflow Defition" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public async Task ParseCashflowsDefinitionToWorkspaceAsync(IDataSet dataSet, ImportArgs args, IWorkspace workspace, IDataSource targetDataSource, string pillar = null)", + "\n{", + "\n workspace.Reset(x => x.ResetInitializationRules().ResetCurrentPartitions());", + "\n workspace.Initialize(x => x.FromSource(targetDataSource).DisableInitialization().DisableInitialization());", + "\n ", + "\n Activity.Start();", + "\n // var parsingStorage = new ParsingStorage(args, targetDataSource, workspace); //", + "\n // await parsingStorage.InitializeAsync();", + "\n ", + "\n var TargetPartitionByReportingNodeAndPeriod = (await workspace.Query()", + "\n .Where(p => p.ReportingNode == args.ReportingNode && p.Year == args.Year && p.Month == args.Month && p.Scenario == args.Scenario).ToArrayAsync())", + "\n .SingleOrDefault();", + "\n ", + "\n if(Activity.HasErrors()) return Activity.Finish();", + "\n ", + "\n var hasAccidentYearColumn = new Dictionary(){};", + "\n if(dataSet.Tables.Contains(nameof(BasicCashflow))) hasAccidentYearColumn[nameof(BasicCashflow)] = dataSet.Tables[nameof(BasicCashflow)].Columns.Any(x => x.ColumnName == nameof(Cashflow.AccidentYear));", + "\n if(dataSet.Tables.Contains(nameof(BoundedCashflow))) hasAccidentYearColumn[nameof(BoundedCashflow)] = dataSet.Tables[nameof(BoundedCashflow)].Columns.Any(x => x.ColumnName == nameof(Cashflow.AccidentYear));", + "\n if(dataSet.Tables.Contains(nameof(ReferencedCashflow))) hasAccidentYearColumn[nameof(ReferencedCashflow)] = dataSet.Tables[nameof(ReferencedCashflow)].Columns.Any(x => x.ColumnName == nameof(Cashflow.AccidentYear));", + "\n", + "\n var importLog = await Import.FromDataSet(dataSet)", + "\n .WithType ( (dataset, datarow) => {", + "\n //TODO extract these codes to small modular functions", + "\n int? accidentYear = default;", + "\n if(hasAccidentYearColumn[nameof(BasicCashflow)] && datarow.Field(nameof(Cashflow.AccidentYear)) != null)", + "\n if(Int32.TryParse(datarow.Field(nameof(Cashflow.AccidentYear)), out var parsedAccidentYear))", + "\n accidentYear = (int?)parsedAccidentYear;", + "\n ", + "\n var values = datarow.Table.Columns.Where(c => c.ColumnName.StartsWith(nameof(Cashflow.Values))).OrderBy(c => c.ColumnName.Length).ThenBy(c => c.ColumnName)", + "\n .Select(x => datarow.Field(x.ColumnName).CheckStringForExponentialAndConvertToDouble()).ToArray();", + "\n ", + "\n if (!Enum.TryParse(datarow.Field(nameof(BasicCashflow.Shape)), out Shape shape)) { ApplicationMessage.Log(Error.Generic, \"Wrong Shape - Add error message\"); return null; };", + "\n ", + "\n var item = new BasicCashflow {", + "\n DataNode = datarow.Field(nameof(Cashflow.DataNode)),", + "\n AmountType = datarow.Field(nameof(Cashflow.AmountType)),", + "\n AccidentYear = accidentYear,", + "\n Shape = shape,", + "\n Pillar = pillar,", + "\n Partition = TargetPartitionByReportingNodeAndPeriod.Id,", + "\n Values = values,", + "\n InitialValue = datarow.Field(nameof(BasicCashflow.InitialValue)).CheckStringForExponentialAndConvertToDouble(),", + "\n Length = Int32.TryParse(datarow.Field(nameof(BasicCashflow.Length)), out var length) ? length : default, //TODO Add ParseAndValidateIntProperty", + "\n Shift = Int32.TryParse(datarow.Field(nameof(BasicCashflow.Shift)), out var shift) ? shift : default,", + "\n };", + "\n return item;", + "\n }, nameof(BasicCashflow))", + "\n ", + "\n .WithType ( (dataset, datarow) => {", + "\n int? accidentYear = default;", + "\n if(hasAccidentYearColumn[nameof(BoundedCashflow)] && datarow.Field(nameof(Cashflow.AccidentYear)) != null)", + "\n if(Int32.TryParse(datarow.Field(nameof(Cashflow.AccidentYear)), out var parsedAccidentYear))", + "\n accidentYear = (int?)parsedAccidentYear;", + "\n ", + "\n var values = datarow.Table.Columns.Where(c => c.ColumnName.StartsWith(nameof(Cashflow.Values))).OrderBy(c => c.ColumnName.Length).ThenBy(c => c.ColumnName)", + "\n .Select(x => datarow.Field(x.ColumnName).CheckStringForExponentialAndConvertToDouble()).ToArray();", + "\n ", + "\n if (!Enum.TryParse(datarow.Field(nameof(BoundedCashflow.Shape)), out Shape shape)) { ApplicationMessage.Log(Error.Generic, \"Wrong Shape - Add error message\"); return null; };", + "\n var item = new BoundedCashflow {", + "\n DataNode = datarow.Field(nameof(Cashflow.DataNode)),", + "\n AmountType = datarow.Field(nameof(Cashflow.AmountType)),", + "\n AccidentYear = accidentYear,", + "\n Shape = shape,", + "\n Pillar = pillar,", + "\n Partition = TargetPartitionByReportingNodeAndPeriod.Id,", + "\n Values = values,", + "\n InitialValue = datarow.Field(nameof(BasicCashflow.InitialValue)).CheckStringForExponentialAndConvertToDouble(),", + "\n Length = Int32.TryParse(datarow.Field(nameof(BasicCashflow.Length)), out var length) ? length : default, //TODO Add ParseAndValidateIntProperty", + "\n Shift = Int32.TryParse(datarow.Field(nameof(BasicCashflow.Shift)), out var shift) ? shift : default,", + "\n Minimum = datarow.Field(nameof(BoundedCashflow.Minimum)).CheckStringForExponentialAndConvertToDouble(),", + "\n Maximum = datarow.Field(nameof(BoundedCashflow.Maximum)).CheckStringForExponentialAndConvertToDouble(),", + "\n };", + "\n return item;", + "\n }, nameof(BoundedCashflow))", + "\n ", + "\n .WithType ( (dataset, datarow) => {", + "\n int? accidentYear = default;", + "\n if(hasAccidentYearColumn[nameof(ReferencedCashflow)] && datarow.Field(nameof(Cashflow.AccidentYear)) != null)", + "\n if(Int32.TryParse(datarow.Field(nameof(Cashflow.AccidentYear)), out var parsedAccidentYear))", + "\n accidentYear = (int?)parsedAccidentYear;", + "\n ", + "\n var values = datarow.Table.Columns.Where(c => c.ColumnName.StartsWith(nameof(Cashflow.Values))).OrderBy(c => c.ColumnName.Length).ThenBy(c => c.ColumnName)", + "\n .Select(x => datarow.Field(x.ColumnName).CheckStringForExponentialAndConvertToDouble()).ToArray();", + "\n ", + "\n if (!Enum.TryParse(datarow.Field(nameof(ReferencedCashflow.Shape)), out Shape shape)) { ApplicationMessage.Log(Error.Generic, \"Wrong Shape - Add error message\"); return null; };", + "\n var item = new ReferencedCashflow {", + "\n DataNode = datarow.Field(nameof(Cashflow.DataNode)),", + "\n AmountType = datarow.Field(nameof(Cashflow.AmountType)),", + "\n AccidentYear = accidentYear,", + "\n Shape = shape,", + "\n Pillar = pillar,", + "\n Partition = TargetPartitionByReportingNodeAndPeriod.Id,", + "\n Values = values,", + "\n InitialValue = datarow.Field(nameof(BasicCashflow.InitialValue)).CheckStringForExponentialAndConvertToDouble(),", + "\n Length = Int32.TryParse(datarow.Field(nameof(BasicCashflow.Length)), out var length) ? length : default,//TODO Add ParseAndValidateIntProperty", + "\n Shift = Int32.TryParse(datarow.Field(nameof(BasicCashflow.Shift)), out var shift) ? shift : default,", + "\n ReferenceAmountType = datarow.Field(nameof(ReferencedCashflow.ReferenceAmountType)),", + "\n };", + "\n return item;", + "\n }, nameof(ReferencedCashflow))", + "\n ", + "\n .WithTarget(workspace).ExecuteAsync();", + "\n //await workspace.ExtendParsedVariables(parsingStorage.HierarchyCache);", + "\n //await workspace.ValidateForMandatoryAocSteps(dataSet, parsingStorage.MandatoryAocSteps);", + "\n //await workspace.ValidateForDataNodeStateActiveAsync(parsingStorage.DataNodeDataBySystemName);", + "\n return Activity.Finish().Merge(importLog);", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public async Task CashflowsDefinitionImporterAsync(IDataSet dataSet, IDataSource targetDataSource, string importFormat, string pillar = null)", + "\n{", + "\n Activity.Start();", + "\n var primaryArgs = await GetArgsAndCommitPartitionAsync(dataSet, targetDataSource) with {ImportFormat = importFormat};", + "\n if(Activity.HasErrors()) return Activity.Finish();", + "\n", + "\n var allArgs = await GetAllArgsAsync(primaryArgs, targetDataSource, importFormat);", + "\n", + "\n if(dataSet.Tables.Contains(nameof(BasicCashflow))) await DataNodeFactoryAsync(dataSet, nameof(BasicCashflow), primaryArgs, targetDataSource);", + "\n if(dataSet.Tables.Contains(nameof(BoundedCashflow))) await DataNodeFactoryAsync(dataSet, nameof(BoundedCashflow), primaryArgs, targetDataSource);", + "\n if(dataSet.Tables.Contains(nameof(ReferencedCashflow))) await DataNodeFactoryAsync(dataSet, nameof(ReferencedCashflow), primaryArgs, targetDataSource);", + "\n if(Activity.HasErrors()) return Activity.Finish();", + "\n", + "\n var workspace = Workspace.CreateNew();", + "\n var log = await ParseCashflowsDefinitionToWorkspaceAsync(dataSet, primaryArgs, workspace, targetDataSource, pillar);", + "\n if(log.Errors.Any()) return Activity.Finish().Merge(log);", + "\n", + "\n var workspaceToCompute = Workspace.CreateNew();", + "\n workspaceToCompute.Initialize(x => x.FromSource(targetDataSource));", + "\n // if (Debug.Enable) ", + "\n // {", + "\n // if(primaryArgs.Scenario == null) //What is the type here?", + "\n // await workspace.DeleteAsync(await workspace.Query().Where(rv => rv.Values.Sum(x => Math.Abs(x)) < Precision).ToArrayAsync());", + "\n // var partition = (Guid)(await workspace.Partition.GetKeyForInstanceAsync(primaryArgs));", + "\n // await workspace.CommitToAsync(workspaceToCompute, partition, snapshot : true);", + "\n // } ", + "\n // else ", + "\n // foreach (var args in allArgs) {", + "\n // log = log.Merge(await ComputeAsync(args, workspace, workspaceToCompute, args == primaryArgs, pillar));", + "\n // if(log.Errors.Any()) return Activity.Finish().Merge(log);", + "\n // }", + "\n", + "\n //await workspaceToCompute.CommitToTargetAsync(targetDataSource); //original code", + "\n await workspace.CommitToTargetAsync(targetDataSource);", + "\n workspace.Dispose();", + "\n workspaceToCompute.Dispose();", + "\n return Activity.Finish().Merge(log); ", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "Import.DefineFormat(\"CashflowDefinition\", async (options, dataSet) => {", + "\n Activity.Start();", + "\n var log = await CashflowsDefinitionImporterAsync(dataSet, options.TargetDataSource, \"CashflowDefinition\");", + "\n return Activity.Finish().Merge(log); ", + "\n});" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Solvency Cashflow Defition" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "Import.DefineFormat(SolvencyImportFormats.SolvencyCashflowDefinition, async (options, dataSet) => {", + "\n Activity.Start();", + "\n var log = await CashflowsDefinitionImporterAsync(dataSet, options.TargetDataSource, SolvencyImportFormats.SolvencyCashflowDefinition, \"Solvency\");", + "\n return Activity.Finish().Merge(log); ", + "\n});" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file diff --git a/fpna/Import/Scopes.ipynb b/fpna/Import/Scopes.ipynb index 6fe3c48c..c2b40c36 100644 --- a/fpna/Import/Scopes.ipynb +++ b/fpna/Import/Scopes.ipynb @@ -72,10 +72,10 @@ { "cell_type": "code", "source": [ - "public interface BasicCashflows : IScope", + "public interface BasicCashflows : IScope", "\n{", - "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetBasicCashflows(Identity)", - "\n .Select((cd,i) => (\"Basic\" + i.ToString(), GetScope(cd).Values));", + "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetCashflow(Identity)", + "\n .Select((cd,i) => (\"Basic-\" + cd.Shape + i.ToString(), GetScope(cd).Values));", "\n}" ], "metadata": {}, @@ -105,7 +105,7 @@ "\n}", "\n", "\npublic interface RandomShape: ComputeBoundedCashflows{", - "\n double[] ComputeBoundedCashflows.Values => GetBoundedCashflow(Identity.Shift, Identity.Length, Identity.Maximum, Identity.Minimum);", + "\n double[] ComputeBoundedCashflows.Values => GetBoundedRandomCashflow(Identity.Shift, Identity.Length, Identity.Maximum, Identity.Minimum);", "\n}" ], "metadata": {}, @@ -115,10 +115,10 @@ { "cell_type": "code", "source": [ - "public interface BoundedCashflows : IScope", + "public interface BoundedCashflows : IScope", "\n{", - "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetBoundedCashflows(Identity)", - "\n .Select((cd,i) => (\"Bounded\" + i.ToString(), GetScope(cd).Values)); ", + "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetCashflow(Identity)", + "\n .Select((cd,i) => (\"Bounded-\" + cd.Shape + i.ToString(), GetScope(cd).Values)); ", "\n}" ], "metadata": {}, @@ -137,7 +137,7 @@ { "cell_type": "code", "source": [ - "public interface ReferencedCashflows : IScope<(CashflowIdentity Id, string OriginalAmountType), CashflowStorage>{", + "public interface ReferencedCashflows : IScope<(CashflowIdentity Id, string OriginalAmountType), ICashflowStorage>{", "\n static ApplicabilityBuilder ScopeApplicabilityBuilder(ApplicabilityBuilder builder) =>", "\n builder.ForScope(s => s", "\n .WithApplicability(x => x.Identity.OriginalAmountType == x.Identity.Id.AmountType));", @@ -153,26 +153,36 @@ "\n double[] ReferencedCashflows.Values => LayerValues.Select(x => x.Values).AggregateDoubleArray();", "\n}", "\n", - "\npublic interface WeightedReference : IScope<(CashflowIdentity Id, string ReferenceAmountType), CashflowStorage>{", + "\n//Use functions where the reference can be provided externally", + "\npublic interface WeightedReference : IScope<(CashflowIdentity Id, string ReferenceAmountType), ICashflowStorage>{", "\n static ApplicabilityBuilder ScopeApplicabilityBuilder(ApplicabilityBuilder builder) =>", "\n builder.ForScope(s => s", - "\n .WithApplicability(x => x.Identity.ReferenceAmountType == x.Identity.Id.AmountType));", + "\n .WithApplicability(x => x.Identity.ReferenceAmountType == x.Identity.Id.AmountType)", + "\n //.WithApplicability(x => x.GetStorage().IsExternalReference(x.Identity.ReferenceAmountType) ) //wip", + "\n );", "\n", - "\n //private", - "\n double[] referencedValues => GetScope((Identity.Id with {AmountType = Identity.ReferenceAmountType}, Identity.Id.AmountType)).Values;", - "\n double[] weights => GetStorage().GetReferencedCashflow(Identity.Id).Single(cd => cd.ReferenceAmountType == Identity.ReferenceAmountType).Values;", + "\n protected double[] referencedValues => GetScope((Identity.Id with {AmountType = Identity.ReferenceAmountType}, Identity.Id.AmountType)).Values;", + "\n // double[] referencedValues => GetStorage().GetPillar() switch { //HERE the class method is used!", + "\n // \"Solvency\" => new []{1d,1d,1d,1d,1d},", + "\n // _ => new []{1000d,1000d,1000d,1000d,1000d},", + "\n // };", + "\n protected double[] weights => GetStorage().GetCashflow(Identity.Id).Single(cd => cd.ReferenceAmountType == Identity.ReferenceAmountType).Values;", "\n ", "\n double[] Values => weights.Zip(referencedValues, (weight, referencedValue) => weight * referencedValue).ToArray();", "\n}", + "\npublic interface ExternalReference : WeightedReference{", + "\n double[] WeightedReference.referencedValues => GetStorage().GetValues(Identity.ReferenceAmountType);", + "\n}", "\n", - "\npublic interface WeightedReferenceSameAt : WeightedReference{", + "\npublic interface WeightedSelfReference : WeightedReference{", "\n double[] WeightedReference.Values => weights.Zip(referencedValues, (weight, referencedValue) => ( weight - 1d ) * referencedValue).ToArray();", "\n}", "\n", - "\npublic interface ReferenceCashflows : IScope", + "\npublic interface ReferenceCashflows : IScope", "\n{", - "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetReferencedCashflow(Identity)", - "\n .Select((cd,i) => (\"Reference\" + i.ToString(), GetScope((Identity, cd.ReferenceAmountType)).Values));", + "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetCashflow(Identity)", + "\n .Select( (cd,i) => (\"Reference\" + cd.Shape + i.ToString(), ", + "\n GetScope((Identity, cd.ReferenceAmountType)).Values));", "\n}" ], "metadata": {}, @@ -182,7 +192,7 @@ { "cell_type": "markdown", "source": [ - "# Compute all Cashflows" + "# Compute all Cashflows (no pillar storage - debug -)" ], "metadata": {}, "execution_count": 0, @@ -191,19 +201,22 @@ { "cell_type": "code", "source": [ - "public interface Compute : IScope{", - "\n public FpaVariable[] FpaVariables => GetScope(Identity).LayerValues.Concat(", - "\n GetScope(Identity).LayerValues).Concat(", - "\n GetScope(Identity).LayerValues).Select(v => ", - "\n new FpaVariable() {", + "public interface Compute : IScope", + "\n{", + "\n public FpaVariable[] FpaVariables => (Identity.Type.Name switch {", + "\n nameof(BasicCashflow) => GetScope(Identity).LayerValues,", + "\n nameof(BoundedCashflow) => GetScope(Identity).LayerValues,", + "\n nameof(ReferencedCashflow) => GetScope(Identity).LayerValues,", + "\n _ => Enumerable.Empty<(string Layer, double[] Values)>(),", + "\n }).Select(v => new FpaVariable() {", "\n Layer = v.Layer,", "\n Values = v.Values,", "\n DataNode = Identity.DataNode,", "\n AmountType = Identity.AmountType,", "\n AccidentYear = Identity.AccidentYear,", - "\n EstimateType = EstimateTypes.BE,", - "\n Novelty = Novelties.C,", - "\n AocType = AocTypes.CL", + "\n EstimateType = EstimateTypes.BE, //hardcodedValue", + "\n Novelty = Novelties.C, //hardcodedValue", + "\n AocType = AocTypes.CL //hardcodedValue", "\n }).ToArray();", "\n}" ], @@ -211,10 +224,36 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "# Solvency Scopes" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ - "" + "public interface ComputeSolvency : IScope", + "\n{", + "\n public FpaVariable[] FpaVariables => (Identity.Type.Name switch {", + "\n nameof(BasicCashflow) => GetScope(Identity).LayerValues,", + "\n nameof(BoundedCashflow) => GetScope(Identity).LayerValues,", + "\n nameof(ReferencedCashflow) => GetScope(Identity).LayerValues,", + "\n _ => Enumerable.Empty<(string Layer, double[] Values)>(),", + "\n }).Select(v => new FpaVariable() {", + "\n Layer = v.Layer,", + "\n Values = v.Values,", + "\n DataNode = Identity.DataNode,", + "\n AmountType = Identity.AmountType,", + "\n AccidentYear = Identity.AccidentYear,", + "\n EstimateType = EstimateTypes.BE, //hardcodedValue", + "\n Novelty = Novelties.C, //hardcodedValue", + "\n AocType = AocTypes.CL //hardcodedValue", + "\n }).ToArray();", + "\n}" ], "metadata": {}, "execution_count": 0, diff --git a/fpna/Import/Storage.ipynb b/fpna/Import/Storage.ipynb index 4e089cd1..5548bb05 100644 --- a/fpna/Import/Storage.ipynb +++ b/fpna/Import/Storage.ipynb @@ -19,7 +19,27 @@ { "cell_type": "code", "source": [ - "#!import \"../DataModel/DataStructure\"" + "#!import \"../DataModel/DataStructure\"", + "\n#!import \"../Utils/Queries\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Cashflower" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Identity ", + "\nIdentity is used in the calculation (performed by Scopes) during import. " ], "metadata": {}, "execution_count": 0, @@ -32,6 +52,7 @@ "\n public string DataNode {get; set;}", "\n public string AmountType {get; set;}", "\n public int? AccidentYear {get; set;}", + "\n public Type Type {get; set;}", "\n}" ], "metadata": {}, @@ -41,7 +62,18 @@ { "cell_type": "markdown", "source": [ - "# Storage" + "# Storage", + "\n", + "\nThe Storage, preload and prepare the data that are required during the calculation. The Storage is consumed by the Scopes. " + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Interface" ], "metadata": {}, "execution_count": 0, @@ -50,76 +82,175 @@ { "cell_type": "code", "source": [ - "public class CashflowStorage", + "public interface ICashflowStorage", "\n{ ", - "\n private readonly IDataSource querySource; ", - "\n private readonly IWorkspace workspace;", - "\n private readonly Systemorph.Vertex.Hierarchies.IHierarchicalDimensionCache hierarchyCache;", - "\n private readonly ImportArgs args;", - "\n", - "\n //Dimensions", - "\n public HashSet EarningsAmountTypes { get; private set; }", - "\n public HashSet SolvencyBsAmountTypes { get; private set; }", - "\n public HashSet IfrsBsAmountTypes { get; private set; }", - "\n", - "\n public Cashflow[] CashflowsByIdentity { get; private set; }", - "\n public BasicCashflow[] BasicCashflowsByIdentity { get; private set; }", - "\n public BoundedCashflow[] BoundedCashflowsByIdentity { get; private set; }", - "\n public ReferencedCashflow[] ReferencedCashflowsByIdentity { get; private set; }", - "\n", - "\n ", - "\n public CashflowIdentity[] Identities {get; set;}", + "\n protected IDataSource querySource {get;} ", + "\n protected IWorkspace workspace {get;}", + "\n protected Systemorph.Vertex.Hierarchies.IHierarchicalDimensionCache hierarchyCache {get;}", + "\n protected ImportArgs args {get;}", "\n", - "\n // Constructor", - "\n public CashflowStorage(ImportArgs args, IDataSource querySource, IWorkspace workspace)", - "\n {", - "\n this.querySource = querySource;", - "\n this.workspace = workspace;", - "\n hierarchyCache = workspace.ToHierarchicalDimensionCache();", - "\n this.args = args;", - "\n }", + "\n public string pillar {get; set; }", + "\n public Cashflow[] CashflowDefinitions { get; set; }", + "\n public CashflowIdentity[] Identities {get; set;} //TODO Should not come from the storage but be computed in a scope to add those that are not input explicitly", + "\n public Dictionary> AmountTypesByPillar {get; set;}", "\n ", + "\n //From ImportStorage", + "\n //Time Periods", + "\n public (int Year, int Month) CurrentReportingPeriod => (args.Year, args.Month);", + "\n public (int Year, int Month) PreviousReportingPeriod => (args.Year - 1, MonthInAYear); // YTD Logic", + "\n //Partitions", + "\n public Guid PartitionByRn {get; set; }", + "\n public Guid TargetPartition {get; set; }", + "\n public Guid DefaultPartition {get; set; }", + "\n", "\n // Initialize", - "\n public async Task InitializeAsync()", - "\n { ", - "\n // Dimensions", - "\n EarningsAmountTypes = (await workspace.Query().ToArrayAsync()).Select(x => x.SystemName).ToHashSet();", - "\n SolvencyBsAmountTypes = (await workspace.Query().ToArrayAsync()).Select(x => x.SystemName).ToHashSet();", - "\n IfrsBsAmountTypes = (await workspace.Query().ToArrayAsync()).Select(x => x.SystemName).ToHashSet();", + "\n public async Task BaseInitializeAsync()", + "\n {", + "\n // Hierarchical Dimensions", + "\n await hierarchyCache.InitializeAsync();", + "\n await hierarchyCache.InitializeAsync();", + "\n await hierarchyCache.InitializeAsync(); //Should RiskDriver be an AmountType", + "\n await hierarchyCache.InitializeAsync();", + "\n await hierarchyCache.InitializeAsync();", + "\n", + "\n //Query Dimensions", + "\n AmountTypesByPillar = new Dictionary>(){", + "\n {\"Solvency\", await querySource.Query().Select(x => x.SystemName).ToArrayAsync()},", + "\n };", + "\n", + "\n //Get Partitions", + "\n PartitionByRn = (await querySource.Query().Where(p => p.ReportingNode == args.ReportingNode).ToArrayAsync()).Single().Id;", + "\n TargetPartition = (await querySource.Query().Where(p => p.ReportingNode == args.ReportingNode &&", + "\n p.Year == CurrentReportingPeriod.Year &&", + "\n p.Month == CurrentReportingPeriod.Month &&", + "\n p.Scenario == args.Scenario).ToArrayAsync()).Single().Id;", + "\n DefaultPartition = (await querySource.Query().Where(p => p.ReportingNode == args.ReportingNode &&", + "\n p.Year == CurrentReportingPeriod.Year &&", + "\n p.Month == CurrentReportingPeriod.Month &&", + "\n p.Scenario == null).ToArrayAsync()).Single().Id;", + "\n //Cashflow Definition", + "\n CashflowDefinitions = await workspace.QueryCashflowAsync(querySource, TargetPartition, DefaultPartition, args, pillar);", "\n ", "\n // Identities", - "\n CashflowsByIdentity = await workspace.Query().ToArrayAsync();", - "\n BasicCashflowsByIdentity = await workspace.Query().ToArrayAsync();", - "\n BoundedCashflowsByIdentity = await workspace.Query().ToArrayAsync();", - "\n ReferencedCashflowsByIdentity = await workspace.Query().ToArrayAsync();", - "\n", - "\n Identities = CashflowsByIdentity.Select(x => new CashflowIdentity() ", + "\n Identities = CashflowDefinitions.Select(x => new CashflowIdentity() ", "\n { ", "\n DataNode = x.DataNode,", "\n AmountType = x.AmountType,", - "\n AccidentYear = x.AccidentYear", + "\n AccidentYear = x.AccidentYear,", + "\n Type = x.GetType(),", "\n }", "\n ).Distinct().ToArray();", - "\n", "\n }", "\n", + "\n protected static async Task DefaultInitializationAsync(ICashflowStorage iCashflowStorage) => await iCashflowStorage.BaseInitializeAsync();", "\n ", - "\n // Getters", - "\n public IEnumerable GetBasicCashflows(CashflowIdentity identity) => ", - "\n BasicCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", + "\n // Getters", + "\n public IEnumerable GetCashflow(CashflowIdentity identity) where T : Cashflow =>", + "\n CashflowDefinitions.Where(x => x.GetType() == typeof(T) && x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear)", + "\n .Select(x => (T)x);", + "\n", + "\n public string GetPillar() => \"Pillar not set\";", + "\n", + "\n public bool IsExternalReference(string amountType) => false;", + "\n", + "\n public double[] GetValues(string amountType) => Enumerable.Empty().ToArray();", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public class CashflowStorage : ICashflowStorage", + "\n{", + "\n public IDataSource querySource {get;} ", + "\n public IWorkspace workspace {get;}", + "\n public Systemorph.Vertex.Hierarchies.IHierarchicalDimensionCache hierarchyCache {get;}", + "\n public ImportArgs args {get;}", + "\n", + "\n public string pillar {get; set; }", + "\n public Cashflow[] CashflowDefinitions { get; set; }", + "\n public CashflowIdentity[] Identities {get; set;}", + "\n public Dictionary> AmountTypesByPillar {get; set;}", + "\n ", + "\n //Partitions", + "\n public Guid PartitionByRn {get; set; }", + "\n public Guid TargetPartition {get; set; }", + "\n public Guid DefaultPartition {get; set; }", "\n", + "\n // Constructor", + "\n public CashflowStorage(ImportArgs args, IDataSource querySource, IWorkspace workspace)", + "\n {", + "\n this.querySource = querySource;", + "\n this.workspace = workspace;", + "\n hierarchyCache = workspace.ToHierarchicalDimensionCache();", + "\n this.args = args;", + "\n }", "\n", - "\n public IEnumerable GetBoundedCashflows(CashflowIdentity identity) => ", - "\n BoundedCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", + "\n public async Task InitializeAsync() { //Can't be called like the method in Interface ", + "\n await ICashflowStorage.DefaultInitializationAsync(this);", + "\n }", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public class SolvencyStorage : ICashflowStorage", + "\n{", + "\n public IDataSource querySource {get;} ", + "\n public IWorkspace workspace {get;}", + "\n public Systemorph.Vertex.Hierarchies.IHierarchicalDimensionCache hierarchyCache {get;}", + "\n public ImportArgs args {get;}", + "\n", + "\n public string pillar {get; set; }", + "\n public Cashflow[] CashflowDefinitions { get; set; }", + "\n public CashflowIdentity[] Identities {get; set;}", + "\n public Dictionary> AmountTypesByPillar {get; set;}", + "\n ", + "\n //From ImportStorage", + "\n //Partitions", + "\n public Guid PartitionByRn {get; set; }", + "\n public Guid TargetPartition {get; set; }", + "\n public Guid DefaultPartition {get; set; }", "\n ", - "\n public IEnumerable GetReferencedCashflow(CashflowIdentity identity) => ", - "\n ReferencedCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", + "\n // Constructor", + "\n public SolvencyStorage(ImportArgs args, IDataSource querySource, IWorkspace workspace)", + "\n {", + "\n this.querySource = querySource;", + "\n this.workspace = workspace;", + "\n hierarchyCache = workspace.ToHierarchicalDimensionCache();", + "\n this.args = args;", + "\n }", "\n", + "\n public async Task InitializeAsync() { // Can't be called like the method in Interface", + "\n pillar = \"Solvency\";", + "\n await ICashflowStorage.DefaultInitializationAsync(this);", + "\n }", + "\n", + "\n //Getters", + "\n public string GetPillar() => pillar;", + "\n", + "\n public bool IsExternalReference(string amountType) => !AmountTypesByPillar[pillar].Contains(amountType);", "\n}" ], "metadata": {}, "execution_count": 0, "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file diff --git a/fpna/Report/ReportConfigurationAndUtils.ipynb b/fpna/Report/ReportConfigurationAndUtils.ipynb index 5fe6104d..f20e3c9b 100644 --- a/fpna/Report/ReportConfigurationAndUtils.ipynb +++ b/fpna/Report/ReportConfigurationAndUtils.ipynb @@ -39,9 +39,17 @@ "source": [ "public static IDataCube ToReportType (this IDataCube fpaVariable)", "\n => fpaVariable.SelectMany(rv => rv.Values.Select((x,i) => new FpaReportVariable {", - "\n EstimateType = rv.EstimateType, AmountType = rv.AmountType, DataNode = rv.DataNode, AocType = rv.AocType, Novelty = rv.Novelty, ", - "\n Projection = i, Value = x, Layer = rv.Layer }))", - "\n .ToDataCube();" + "\n EstimateType = rv.EstimateType, ", + "\n AmountType = rv.AmountType, ", + "\n DataNode = rv.DataNode, ", + "\n AocType = rv.AocType,", + "\n Novelty = rv.Novelty,", + "\n // RiskDriver = rv.RiskDriver,", + "\n Projection = i,", + "\n Value = x,", + "\n Layer = rv.Layer", + "\n }))", + "\n .ToDataCube();" ], "metadata": {}, "execution_count": 0, diff --git a/fpna/Solvency.ipynb b/fpna/Solvency.ipynb index 4230299e..85cc76cd 100644 --- a/fpna/Solvency.ipynb +++ b/fpna/Solvency.ipynb @@ -65,11 +65,17 @@ "\n- Morbidity Risk", "\n- Operational Risk", "\n- Asset Risk", + "\n- - Equity", + "\n- - Property", + "\n- - Hedge Funds", + "\n- - Loans", + "\n- - Cash", + "\n- - Other Assets", "\n- Insurance Risk", "\n- Credit Risk", "\n- etc", "\n", - "\nFor example, Market Risk can be composed of correlations between Interest Rate, Interest Rate Sensitivity, Spread Risk, Volatility Risk, Inflation Risk, and Assets risks. Asset Risks can be related to various types of Assets, like Equity, Property, Hedge Funds, Lonas, Cash etc. Note the above list is not exhaustive.", + "\nFor example, Market Risk can be composed of correlations between Interest Rate, Interest Rate Sensitivity, Spread Risk, Volatility Risk, Inflation Risk, and Assets risks. Asset Risks can be related to various types of Assets, like Equity, Property, Hedge Funds, Loans, Cash etc. Note the above list is not exhaustive.", "\n", "\n# Scale risks up or down", "\nFor each SolvencyRegime, obtain estimates of current volume of Risk, depending on given Risk Exposure to given Assets, Liabilities (Reserves), Earnings or Premium volume", diff --git a/fpna/Utils/ExtentionMethods.ipynb b/fpna/Utils/ExtentionMethods.ipynb index 9d74bc8a..d2b03db6 100644 --- a/fpna/Utils/ExtentionMethods.ipynb +++ b/fpna/Utils/ExtentionMethods.ipynb @@ -19,7 +19,7 @@ { "cell_type": "markdown", "source": [ - "# Cashflow Computation" + "# Basic Cashflow Computation" ], "metadata": {}, "execution_count": 0, @@ -28,7 +28,9 @@ { "cell_type": "markdown", "source": [ - "## Compute Linear Cashflow" + "## Compute Linear Cashflow", + "\n", + "\nTODO: Add Short description" ], "metadata": {}, "execution_count": 0, @@ -56,7 +58,9 @@ { "cell_type": "markdown", "source": [ - "## Compute Compounded Cashflow" + "## Compute Compounded Cashflow", + "\n", + "\nTODO: Add Short description" ], "metadata": {}, "execution_count": 0, @@ -84,7 +88,18 @@ { "cell_type": "markdown", "source": [ - "## Compute " + "# Bounded Cashflow Computation" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Compute Bounded Random", + "\n", + "\nTODO: Add Short description" ], "metadata": {}, "execution_count": 0, @@ -93,7 +108,7 @@ { "cell_type": "code", "source": [ - "public static double[] GetBoundedCashflow(this int shift, int length, double max, double min){", + "public static double[] GetBoundedRandomCashflow(this int shift, int length, double max, double min){", "\n var bounded = new double[length];", "\n var random = new Random(); // is this random enough?", "\n", diff --git a/fpna/Utils/Queries.ipynb b/fpna/Utils/Queries.ipynb new file mode 100644 index 00000000..8a486d9c --- /dev/null +++ b/fpna/Utils/Queries.ipynb @@ -0,0 +1,82 @@ +{ + "metadata": { + "authors": [], + "id": "p8IcPf21F0SvkDjESdB1bw", + "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": [ + "# Cashflow Definition", + "\nCashflow Defintion query takes as input a string identifying the pillar: IFRS 17, Solvency, ...", + "\n", + "\nThis query has the default logic necessary to provide a fall back (Best Estimate) in case of Scenario calculation. " + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "// Should be required only to compile this NB in isolation (debugging)", + "\n#!import \"../Constants/Ifrs17CalculationEngine\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#!import \"../DataModel/DataStructure\"" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public static async Task QueryCashflowAsync(this IWorkspace workspace, IDataSource dataSource, ", + "\n Guid targetPartition, Guid defaultPartition,", + "\n ImportArgs args,", + "\n string pillar) //pillar should be contained in the args(.ImportFormat?)", + "\n where T : IPartitioned, IWithPillar", + "\n where P : IPartition", + "\n{", + "\n //FilterExpression", + "\n Expression> filterExpression = x => x.Pillar == pillar;", + "\n ", + "\n var isRelaxed = args.Scenario == null;", + "\n", + "\n var variablesFromWorkspace = await workspace.LoadPartitionedDataAsync(targetPartition, filterExpression);", + "\n if( !isRelaxed || variablesFromWorkspace.Any())", + "\n return variablesFromWorkspace;", + "\n ", + "\n var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(targetPartition, filterExpression);", + "\n if(variablesFromDataSource.Any()) return variablesFromDataSource;", + "\n", + "\n //var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar);", + "\n", + "\n return variablesFromWorkspace.Union(await dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar), EqualityComparer.Instance).ToArray();", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/ifrs17-template/PracticalUseCases/CompareReinsuranceContracts/Reports.ipynb b/ifrs17-template/PracticalUseCases/CompareReinsuranceContracts/Reports.ipynb index db07a7b7..0b3cc1c2 100644 --- a/ifrs17-template/PracticalUseCases/CompareReinsuranceContracts/Reports.ipynb +++ b/ifrs17-template/PracticalUseCases/CompareReinsuranceContracts/Reports.ipynb @@ -389,7 +389,7 @@ "source": [ "await Report.ForDataCube(pnl.ToDataCube()).WithQuerySource(Workspace)", "\n .SliceRowsBy(\"Scenario\")", - "\n .SliceColumnsBy(new[]{\"Currency\",\"GroupOfContract\"}) //\"GroupOfContract\"", + "\n .SliceColumnsBy(new[]{\"Currency\",\"GroupOfContract\"})", "\n .ReportGridOptions().ExecuteAsync()" ], "metadata": {}, diff --git a/ifrs17/Import/ImportStorage.ipynb b/ifrs17/Import/ImportStorage.ipynb index 88d2dfc8..8c7f3796 100644 --- a/ifrs17/Import/ImportStorage.ipynb +++ b/ifrs17/Import/ImportStorage.ipynb @@ -182,8 +182,10 @@ "\n await workspace.Partition.SetAsync(TargetPartition);", "\n ", "\n //Get data from Workspace (result of parsing)", - "\n var parsedRawVariables = await workspace.QueryPartitionedDataAsync(querySource, TargetPartition, DefaultPartition, ImportFormat);", - "\n var parsedIfrsVariables = await workspace.QueryPartitionedDataAsync(querySource, TargetPartition, DefaultPartition, ImportFormat);", + "\n var parsedRawVariables = await workspace.QueryPartitionedDataAsync", + "\n (querySource, TargetPartition, DefaultPartition, GetIsRelaxed());", + "\n var parsedIfrsVariables = await workspace.QueryPartitionedDataAsync", + "\n (querySource, TargetPartition, DefaultPartition, GetIsRelaxed());", "\n ", "\n //DataNodes", "\n DataNodeDataBySystemName = await workspace.LoadDataNodesAsync(args);", @@ -280,7 +282,7 @@ "\n }", "\n ", "\n //Variables", - "\n var rawVariables = parsedRawVariables.Concat(openingRawVariables)", + "\n var rawVariables = parsedRawVariables.Concat(openingRawVariables) //Concat might introduce duplicates", "\n .Concat(await querySource.Query().Where(rv => rv.Partition == TargetPartition)", "\n .Where(rv => addedToPrimaryScope.Contains(rv.DataNode)).ToArrayAsync());", "\n ", @@ -301,8 +303,12 @@ "\n IfrsVariablesByImportIdentity = (IDictionary>)ifrsVariables.ToDictionaryGrouped(v => v.DataNode, v => (ICollection)v.ToArray());", "\n }", "\n ", - "\n //Getters", + "\n //private methods", + "\n private bool GetIsRelaxed() where T : IPartitioned =>", + "\n (ImportFormat != ImportFormats.Cashflow && typeof(T).Name == nameof(IfrsVariable)) || ", + "\n (ImportFormat == ImportFormats.Cashflow && typeof(T).Name == nameof(RawVariable));", "\n ", + "\n //Getters", "\n //Periods", "\n public ValuationPeriod GetValuationPeriod(ImportIdentity id) => AocConfigurationByAocStep[new AocStep(id.AocType, id.Novelty)].ValuationPeriod;", "\n public PeriodType GetYieldCurvePeriod(ImportIdentity id) => AocConfigurationByAocStep[new AocStep(id.AocType, id.Novelty)].YcPeriod;", diff --git a/ifrs17/Import/Importers.ipynb b/ifrs17/Import/Importers.ipynb index 6ee027c0..c6b5b36e 100644 --- a/ifrs17/Import/Importers.ipynb +++ b/ifrs17/Import/Importers.ipynb @@ -489,9 +489,12 @@ "source": [ "public async Task DataNodeFactoryAsync(IDataSet dataSet, string tableName, ImportArgs args, IDataSource targetDataSource)", "\n{", - "\n var partition = (await DataSource.Query().Where(p => p.ReportingNode == args.ReportingNode).ToArrayAsync()).FirstOrDefault();", - "\n if(partition == null) { ApplicationMessage.Log(Error.ParsedPartitionNotFound, args.ReportingNode); return; }", - "\n ", + "\n var partition = (await targetDataSource.Query().Where(p => p.ReportingNode == args.ReportingNode).ToArrayAsync()).FirstOrDefault();", + "\n if(partition == null) {", + "\n await CommitPartitionAsync(args, targetDataSource);", + "\n partition = (await targetDataSource.Query().Where(p => p.ReportingNode == args.ReportingNode).ToArrayAsync()).FirstOrDefault();", + "\n }", + "\n ", "\n var table = dataSet.Tables[tableName];", "\n ", "\n var dataNodesImported = table.Rows.Select(x => x.Field(nameof(RawVariable.DataNode))).ToHashSet();", @@ -508,7 +511,8 @@ "\n Year = args.Year, ", "\n Month = DefaultDataNodeActivationMonth, ", "\n State = State.Active, ", - "\n Partition = partition.Id})", + "\n Partition = partition.Id,", + "\n Scenario = args.Scenario})", "\n .ToArray() );", "\n if ((dataNodeSingleParametersUndefined?.Any() ?? false))", "\n await targetDataSource.UpdateAsync( dataNodeSingleParametersUndefined.Select(x => ", @@ -518,7 +522,8 @@ "\n PremiumAllocation = DefaultPremiumExperienceAdjustmentFactor, ", "\n EconomicBasisDriver = GetDefaultEconomicBasisDriver(dataNodesDefined.SingleOrDefault(y => y.SystemName == x)?.ValuationApproach, ", "\n dataNodesDefined.SingleOrDefault(y => y.SystemName == x)?.LiabilityType),", - "\n Partition = partition.Id})", + "\n Partition = partition.Id,", + "\n Scenario = args.Scenario})", "\n .ToArray() );", "\n await targetDataSource.CommitAsync();", "\n}" @@ -644,7 +649,7 @@ "\n await workspace.CommitToAsync(workspaceToCompute, storage.TargetPartition, snapshot : true, ", "\n filter : x => storage.EstimateTypesByImportFormat[args.ImportFormat].Contains(x.EstimateType) ", "\n && storage.DataNodesByImportScope[ImportScope.Primary].Contains(x.DataNode));", - "\n if(saveRawVariables) {", + "\n if(saveRawVariables) { //Only for primaryArgs from CashflowFormat: Remove from inputs", "\n if(args.Scenario == null) await workspace.DeleteAsync(await workspace.Query().Where(rv => rv.Values.All(x => Math.Abs(x) < Precision)).ToArrayAsync());", "\n await workspace.CommitToAsync(workspaceToCompute, storage.TargetPartition, snapshot : true, ", "\n filter : x => storage.DataNodesByImportScope[ImportScope.Primary].Except(storage.DataNodesByImportScope[ImportScope.AddedToPrimary]).Contains(x.DataNode));", @@ -1506,12 +1511,12 @@ "\n await workspace.CommitToAsync(workspaceToCompute, partition, snapshot : true);", "\n } ", "\n else ", + "\n //TODO compute primaryArgs (extract primaryScope) then loop over secondaryArgs (taking the primaryScope)", "\n foreach (var args in allArgs) {", "\n log = log.Merge(await ComputeAsync(args, workspace, workspaceToCompute, args == primaryArgs));", "\n if(log.Errors.Any()) return Activity.Finish().Merge(log);", "\n }", "\n", - "\n", "\n await workspaceToCompute.CommitToTargetAsync(options.TargetDataSource);", "\n return Activity.Finish().Merge(log); ", "\n});" diff --git a/ifrs17/Report/ReportStorage.ipynb b/ifrs17/Report/ReportStorage.ipynb index 8c8da917..64bf0239 100644 --- a/ifrs17/Report/ReportStorage.ipynb +++ b/ifrs17/Report/ReportStorage.ipynb @@ -73,7 +73,6 @@ "\n [Dimension(typeof(Currency), nameof(FunctionalCurrency))]", "\n public string FunctionalCurrency { get; init; }", "\n", - "\n [NotAggregated]", "\n [Dimension(typeof(ProjectionConfiguration), nameof(Projection))]", "\n public string Projection { get; init; }", "\n", diff --git a/ifrs17/Test/AocConfigurationTest.ipynb b/ifrs17/Test/AocConfigurationTest.ipynb index 94469926..b2008499 100644 --- a/ifrs17/Test/AocConfigurationTest.ipynb +++ b/ifrs17/Test/AocConfigurationTest.ipynb @@ -43,6 +43,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ diff --git a/ifrs17/Test/AocStructureTest.ipynb b/ifrs17/Test/AocStructureTest.ipynb index 5d7e0d27..b1b440c9 100644 --- a/ifrs17/Test/AocStructureTest.ipynb +++ b/ifrs17/Test/AocStructureTest.ipynb @@ -52,6 +52,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ diff --git a/ifrs17/Test/DataNodeParameterTest.ipynb b/ifrs17/Test/DataNodeParameterTest.ipynb index ed0263c0..54b55227 100644 --- a/ifrs17/Test/DataNodeParameterTest.ipynb +++ b/ifrs17/Test/DataNodeParameterTest.ipynb @@ -52,6 +52,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -114,15 +123,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "code", - "source": [ - "Workspace.Initialize(x => x.FromSource(DataSource).DisableInitialization().DisableInitialization());" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "source": [ diff --git a/ifrs17/Test/ExtentionsTest.ipynb b/ifrs17/Test/ExtensionsTest.ipynb similarity index 100% rename from ifrs17/Test/ExtentionsTest.ipynb rename to ifrs17/Test/ExtensionsTest.ipynb diff --git a/ifrs17/Test/ImportStorageTest.ipynb b/ifrs17/Test/ImportStorageTest.ipynb index 5e8c52d3..978ef397 100644 --- a/ifrs17/Test/ImportStorageTest.ipynb +++ b/ifrs17/Test/ImportStorageTest.ipynb @@ -47,7 +47,8 @@ { "cell_type": "code", "source": [ - "await DataSource.SetAsync();", + "DataSource.Reset();", + "\nWorkspace.Reset();", "\nDataSource.Reset(x => x.ResetCurrentPartitions());", "\nWorkspace.Reset(x => x.ResetCurrentPartitions());" ], diff --git a/ifrs17/Test/QueriesScenarioDataTest.ipynb b/ifrs17/Test/QueriesScenarioDataTest.ipynb index 9cf0d481..45721f77 100644 --- a/ifrs17/Test/QueriesScenarioDataTest.ipynb +++ b/ifrs17/Test/QueriesScenarioDataTest.ipynb @@ -37,6 +37,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -51,6 +60,17 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "public bool IsRelaxed(string importFormat) where T : IPartitioned =>", + "\n (importFormat != ImportFormats.Cashflow && typeof(T).Name == nameof(IfrsVariable)) || ", + "\n (importFormat == ImportFormats.Cashflow && typeof(T).Name == nameof(RawVariable));" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -143,7 +163,8 @@ { "cell_type": "code", "source": [ - "var queriedRawVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Cashflow);" + "var queriedRawVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Cashflow));" ], "metadata": {}, "execution_count": 0, @@ -186,7 +207,8 @@ { "cell_type": "code", "source": [ - "var queriedRawVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Actual);" + "var queriedRawVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Actual));" ], "metadata": {}, "execution_count": 0, @@ -229,7 +251,8 @@ { "cell_type": "code", "source": [ - "var queriedRawVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Cashflow);" + "var queriedRawVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Cashflow));" ], "metadata": {}, "execution_count": 0, @@ -272,7 +295,8 @@ { "cell_type": "code", "source": [ - "var queriedRawVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Cashflow);" + "var queriedRawVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Cashflow));" ], "metadata": {}, "execution_count": 0, @@ -315,7 +339,8 @@ { "cell_type": "code", "source": [ - "var queriedRawVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Cashflow);" + "var queriedRawVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Cashflow));" ], "metadata": {}, "execution_count": 0, @@ -421,7 +446,8 @@ { "cell_type": "code", "source": [ - "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Actual);" + "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Actual));" ], "metadata": {}, "execution_count": 0, @@ -464,7 +490,8 @@ { "cell_type": "code", "source": [ - "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Cashflow);" + "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Cashflow));" ], "metadata": {}, "execution_count": 0, @@ -507,7 +534,8 @@ { "cell_type": "code", "source": [ - "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Actual);" + "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Actual));" ], "metadata": {}, "execution_count": 0, @@ -550,7 +578,8 @@ { "cell_type": "code", "source": [ - "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Actual);" + "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Actual));" ], "metadata": {}, "execution_count": 0, @@ -593,7 +622,8 @@ { "cell_type": "code", "source": [ - "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(DataSource, partitionScenarioMTUP.Id, partition.Id, ImportFormats.Actual);" + "var queriedIfrsVars = await ws.QueryPartitionedDataAsync(", + "\n DataSource, partitionScenarioMTUP.Id, partition.Id, IsRelaxed(ImportFormats.Actual));" ], "metadata": {}, "execution_count": 0, @@ -695,7 +725,8 @@ { "cell_type": "code", "source": [ - "(await ws.QueryReportVariablesAsync((args.Year, args.Month, args.ReportingNode, args.Scenario), projectionConfigurations)).Select(x => x.Value).Sum().Should().Be(21);" + "(await ws.QueryReportVariablesAsync((args.Year, args.Month, args.ReportingNode, args.Scenario), projectionConfigurations))", + "\n .Select(x => x.Value).Sum().Should().Be(21);" ], "metadata": {}, "execution_count": 0, @@ -746,7 +777,8 @@ { "cell_type": "code", "source": [ - "(await ws.QueryReportVariablesAsync((args.Year, args.Month, args.ReportingNode, argsScenarioMTUP.Scenario), projectionConfigurations)).Select(x => x.Value).Sum().Should().Be(21.2);" + "(await ws.QueryReportVariablesAsync((args.Year, args.Month, args.ReportingNode, argsScenarioMTUP.Scenario), projectionConfigurations))", + "\n .Select(x => x.Value).Sum().Should().Be(21.2);" ], "metadata": {}, "execution_count": 0, diff --git a/ifrs17/Test/QueriesTest.ipynb b/ifrs17/Test/QueriesTest.ipynb index 7ec96feb..289a0941 100644 --- a/ifrs17/Test/QueriesTest.ipynb +++ b/ifrs17/Test/QueriesTest.ipynb @@ -44,6 +44,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ diff --git a/ifrs17/Test/ReportStorageTest.ipynb b/ifrs17/Test/ReportStorageTest.ipynb index fdd50a01..439f0edd 100644 --- a/ifrs17/Test/ReportStorageTest.ipynb +++ b/ifrs17/Test/ReportStorageTest.ipynb @@ -35,6 +35,18 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset();", + "\nWorkspace.Reset();", + "\nDataSource.Reset(x => x.ResetCurrentPartitions());", + "\nWorkspace.Reset(x => x.ResetCurrentPartitions());" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ diff --git a/ifrs17/Test/TechnicalMarginTest.ipynb b/ifrs17/Test/TechnicalMarginTest.ipynb index 43235171..310cd312 100644 --- a/ifrs17/Test/TechnicalMarginTest.ipynb +++ b/ifrs17/Test/TechnicalMarginTest.ipynb @@ -44,6 +44,16 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "DataSource.Reset();", + "\nWorkspace.Reset();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ diff --git a/ifrs17/Test/Tests.ipynb b/ifrs17/Test/Tests.ipynb index 987eae5f..f4d3e088 100644 --- a/ifrs17/Test/Tests.ipynb +++ b/ifrs17/Test/Tests.ipynb @@ -57,7 +57,7 @@ { "cell_type": "code", "source": [ - "#!eval-notebook \"ExtentionsTest\"" + "#!eval-notebook \"ExtensionsTest\"" ], "metadata": {}, "execution_count": 0, diff --git a/ifrs17/Utils/Queries.ipynb b/ifrs17/Utils/Queries.ipynb index 5f340cc4..4e2ac9dc 100644 --- a/ifrs17/Utils/Queries.ipynb +++ b/ifrs17/Utils/Queries.ipynb @@ -422,14 +422,14 @@ { "cell_type": "code", "source": [ - "public static async Task LoadPartitionedDataAsync(this IDataSource querySource, Guid partition)", + "public static async Task LoadPartitionedDataAsync(this IDataSource querySource, Guid partition, Expression> filterExpression = null)", "\n where T : IPartitioned", "\n where P : IPartition", "\n{", "\n var partitionBackup = (Guid)(querySource.Partition.GetCurrent(typeof(P).Name)?? default(Guid));", "\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 var data = await querySource.Query().Where(x => x.Partition == partition).Where(filterExpression?? (Expression>)(x => true)).ToArrayAsync();", "\n if(partitionBackup == default(Guid)) await querySource.Partition.SetAsync

(null);", "\n else await querySource.Partition.SetAsync

(partitionBackup);", "\n return data;", @@ -442,23 +442,23 @@ { "cell_type": "code", "source": [ - "public static async Task QueryPartitionedDataAsync(this IWorkspace workspace, IDataSource dataSource, Guid targetPartition, Guid defaultPartition, string format)", + "public static async Task QueryPartitionedDataAsync(this IWorkspace workspace, IDataSource dataSource, ", + "\n Guid targetPartition, Guid defaultPartition, ", + "\n bool isRelaxed,", + "\n Expression> filterExpression = null)", "\n where T : IPartitioned", "\n where P : IPartition", "\n{", - "\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 var variablesFromWorkspace = await workspace.LoadPartitionedDataAsync(targetPartition, filterExpression);", "\n if(!isRelaxed || variablesFromWorkspace.Any()) return variablesFromWorkspace;", "\n", "\n // This is for scenario re-calculation", - "\n var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(targetPartition);", + "\n var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(targetPartition, filterExpression);", "\n if(variablesFromDataSource.Any()) return variablesFromDataSource;", "\n", "\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 return await dataSource.LoadPartitionedDataAsync(defaultPartition, filterExpression);", "\n}" ], "metadata": {},