From 5f0d4c8b138f29d5654f95d4e927bc2748b41747 Mon Sep 17 00:00:00 2001 From: Slavomir Batka Date: Mon, 30 Oct 2023 09:05:02 +0100 Subject: [PATCH 01/17] add RiskDriver --- fpna-template/Files/RiskDriver.ipynb | 77 +++++++++++++++++++ .../InitSystemorphToMemory.ipynb | 36 +++++++++ fpna/Solvency.ipynb | 8 +- 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 fpna-template/Files/RiskDriver.ipynb diff --git a/fpna-template/Files/RiskDriver.ipynb b/fpna-template/Files/RiskDriver.ipynb new file mode 100644 index 00000000..c945c08a --- /dev/null +++ b/fpna-template/Files/RiskDriver.ipynb @@ -0,0 +1,77 @@ +{ + "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", + "\nLOR,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": [] + } + ] +} \ No newline at end of file diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index 1d2026e2..6fbf9761 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -83,6 +83,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Amount Types" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -106,6 +115,33 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Risk Driver" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#!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": [ 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", From c0696e56f5c10da702a20b42b5e5dfd8af3bf9de Mon Sep 17 00:00:00 2001 From: Slavomir Batka Date: Thu, 2 Nov 2023 12:56:39 +0100 Subject: [PATCH 02/17] refactor AmountTypes, remove AssetAmountType add RiskDriver to DataModel --- fpna-template/Files/AmountType.ipynb | 57 +++--- fpna-template/Files/Cashflows.ipynb | 6 +- .../InitSystemorphToMemory.ipynb | 1 - fpna/DataModel/DataStructure.ipynb | 165 ++++++++++++++++-- 4 files changed, 179 insertions(+), 50 deletions(-) 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..e0c5af0c 100644 --- a/fpna-template/Files/Cashflows.ipynb +++ b/fpna-template/Files/Cashflows.ipynb @@ -50,9 +50,9 @@ "\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" + "\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": {}, "execution_count": 0, diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index 6fbf9761..39665d8d 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -107,7 +107,6 @@ "await Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\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();" ], diff --git a/fpna/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index 2ff9a4b5..c5370241 100644 --- a/fpna/DataModel/DataStructure.ipynb +++ b/fpna/DataModel/DataStructure.ipynb @@ -46,6 +46,77 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "# Interfaces" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface IBased", + "\n{", + "\n public double InitialValue { get; init; }", + "\n", + "\n public int Length { get; init; }", + "\n", + "\n public int Shift { get; init; }", + "\n", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface IBounded", + "\n{", + "\n public double Minimum { get; init; }", + "\n", + "\n public double Maximum { get; init; }", + "\n", + "\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 IWithRiskDriver", + "\n{", + "\n [IdentityProperty]", + "\n [Dimension(typeof(RiskDriver))] ", + "\n public string RiskDriver { get; init; }", + "\n", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -70,7 +141,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 {}" ], @@ -94,7 +164,7 @@ "\n{", "\n [Key]", "\n [NotVisible] ", - "\n public Guid Id { get; init; }", + "\n public Guid Id { get; init; } // TODO should go to db?", "\n", "\n [IdentityProperty]", "\n [Dimension(typeof(string), nameof(DataNode))]", @@ -112,7 +182,7 @@ "\n [IdentityProperty]", "\n [Dimension(typeof(Shape), nameof(Shape))] // TODO find a better name", "\n public Shape Shape { get; init; }", - "\n ", + "\n", "\n [Dimension(typeof(double), nameof(Values))]", "\n public double[] Values { get; init; }", "\n", @@ -122,11 +192,20 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "### Basic" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ - "public record SimpleCashflow : Cashflow", - "\n{", + "public record BasicCashflow : Cashflow, IBased {", + "\n ", "\n public double InitialValue { get; init; }", "\n", "\n public int Length { get; init; }", @@ -142,7 +221,7 @@ { "cell_type": "markdown", "source": [ - "### Basic" + "##### Bounded" ], "metadata": {}, "execution_count": 0, @@ -151,7 +230,12 @@ { "cell_type": "code", "source": [ - "public record BasicCashflow : SimpleCashflow {}" + "public record BoundedCashflow : BasicCashflow, IBounded", + "\n{", + "\n public double Minimum { get; init; }", + "\n", + "\n public double Maximum { get; init; }", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -160,7 +244,7 @@ { "cell_type": "markdown", "source": [ - "#### Bounded" + "#### Referenced" ], "metadata": {}, "execution_count": 0, @@ -169,11 +253,11 @@ { "cell_type": "code", "source": [ - "public record BoundedCashflow : SimpleCashflow", + "public record ReferencedCashflow : Cashflow, IReferenced", "\n{", - "\n public double Minimum { get; init; }", - "\n", - "\n public double Maximum { get; init; }", + "\n [IdentityProperty]", + "\n [Dimension(typeof(AmountType))] ", + "\n public string ReferenceAmountType { get; init; }", "\n}" ], "metadata": {}, @@ -183,7 +267,7 @@ { "cell_type": "markdown", "source": [ - "#### Referenced" + "#### Solvency" ], "metadata": {}, "execution_count": 0, @@ -192,17 +276,26 @@ { "cell_type": "code", "source": [ - "public record ReferencedCashflow : SimpleCashflow", + "public record SolvencyCashflow : BoundedCashflow, IWithRiskDriver", "\n{", "\n [IdentityProperty]", - "\n [Dimension(typeof(AmountType))] ", - "\n public string ReferenceAmountType { get; init; }", + "\n [Dimension(typeof(RiskDriver))] ", + "\n public string RiskDriver { get; init; }", "\n}" ], "metadata": {}, "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Fpa Variable" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -211,12 +304,36 @@ "\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", "\n}" ], "metadata": {}, "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "public record SolvencyVariable : FpaVariable", + "\n{", + "\n [Dimension(typeof(RiskDriver), nameof(RiskDriver))] ", + "\n public string RiskDriver { get; init; }", + "\n", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Fpa Report Variable" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -236,10 +353,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", @@ -249,6 +367,19 @@ "metadata": {}, "execution_count": 0, "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public record SolvencyReportVariable : FpaReportVariable", + "\n{", + "\n [Dimension(typeof(RiskDriver), nameof(RiskDriver))]", + "\n public string RiskDriver { get; init; }", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file From dfa75f2177024eba64dd44e47acad53ba9bf44bc Mon Sep 17 00:00:00 2001 From: Slavomir Batka Date: Thu, 2 Nov 2023 20:46:24 +0100 Subject: [PATCH 03/17] Solvency variable in storage --- fpna-template/Files/Cashflows.ipynb | 14 +++++++++++++ fpna-template/Files/RiskDriver.ipynb | 2 +- fpna/DataModel/DataStructure.ipynb | 20 ++++++++++++++++++- fpna/Import/Storage.ipynb | 5 +++++ fpna/Report/ReportConfigurationAndUtils.ipynb | 14 ++++++++++--- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/fpna-template/Files/Cashflows.ipynb b/fpna-template/Files/Cashflows.ipynb index e0c5af0c..0f38714b 100644 --- a/fpna-template/Files/Cashflows.ipynb +++ b/fpna-template/Files/Cashflows.ipynb @@ -57,6 +57,20 @@ "metadata": {}, "execution_count": 0, "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var solvencyCashflows = @\"@@SolvencyCashflow", + "\nDataNode,AmountType,Shape,RiskDriver,InitialValue,Length,Shift,Minimum,Maximum", + "\nG.BBA.LRC.2020.P,IFASH,Constant,EQR,0.75,5,0,-1,0", + "\nG.BBA.LRC.2020.P,IFASH,Constant,SPR,0.6,5,0,-1,0", + "\nG.BBA.LRC.2020.P,IFLTP,Constant,LMOR,0.5,5,0,-1,0", + "\n\";" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file diff --git a/fpna-template/Files/RiskDriver.ipynb b/fpna-template/Files/RiskDriver.ipynb index c945c08a..e466d49f 100644 --- a/fpna-template/Files/RiskDriver.ipynb +++ b/fpna-template/Files/RiskDriver.ipynb @@ -38,7 +38,7 @@ "\nEQR,Equity,MR", "\nPPR,Property,MR", "\nIRR,Interest Rate,MR", - "\nLOR,Spread,MR", + "\nSPR,Spread,MR", "\nCUR,Currency,MR", "\nCOR,Concentration,MR", "\nHR,Health Insurance Risk,BR", diff --git a/fpna/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index c5370241..f839c85f 100644 --- a/fpna/DataModel/DataStructure.ipynb +++ b/fpna/DataModel/DataStructure.ipynb @@ -221,7 +221,7 @@ { "cell_type": "markdown", "source": [ - "##### Bounded" + "#### Bounded" ], "metadata": {}, "execution_count": 0, @@ -311,6 +311,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Solvency Variable" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ @@ -368,6 +377,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Solvency Report Variable" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ diff --git a/fpna/Import/Storage.ipynb b/fpna/Import/Storage.ipynb index 4e089cd1..98ba8e8f 100644 --- a/fpna/Import/Storage.ipynb +++ b/fpna/Import/Storage.ipynb @@ -66,6 +66,7 @@ "\n public BasicCashflow[] BasicCashflowsByIdentity { get; private set; }", "\n public BoundedCashflow[] BoundedCashflowsByIdentity { get; private set; }", "\n public ReferencedCashflow[] ReferencedCashflowsByIdentity { get; private set; }", + "\n public SolvencyCashflow[] SolvencyCashflowsByIdentity { get; private set; }", "\n", "\n ", "\n public CashflowIdentity[] Identities {get; set;}", @@ -92,6 +93,7 @@ "\n BasicCashflowsByIdentity = await workspace.Query().ToArrayAsync();", "\n BoundedCashflowsByIdentity = await workspace.Query().ToArrayAsync();", "\n ReferencedCashflowsByIdentity = await workspace.Query().ToArrayAsync();", + "\n SolvencyCashflowsByIdentity = await workspace.Query().ToArrayAsync();", "\n", "\n Identities = CashflowsByIdentity.Select(x => new CashflowIdentity() ", "\n { ", @@ -115,6 +117,9 @@ "\n public IEnumerable GetReferencedCashflow(CashflowIdentity identity) => ", "\n ReferencedCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", "\n", + "\n public IEnumerable GetSolvencyCashflow(CashflowIdentity identity) => ", + "\n SolvencyCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", + "\n", "\n}" ], "metadata": {}, 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, From fc471be950698b13635d70d4e67834bdf6f6df39 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Fri, 3 Nov 2023 14:45:44 +0100 Subject: [PATCH 04/17] init --- .../InitSystemorphToMemory.ipynb | 35 ++++++++++++++++--- fpna-template/Report/Earnings.ipynb | 16 +++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index 39665d8d..b265fe4d 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -29,7 +29,35 @@ { "cell_type": "markdown", "source": [ - "# Set default DataSource" + "# Initialize DataSource" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## New DataSource" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "// await DataSource.SetAsync();", + "\n// DataSource.Reset(x => x.ResetCurrentPartitions());" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Initialize from calculation engine template" ], "metadata": {}, "execution_count": 0, @@ -38,8 +66,7 @@ { "cell_type": "code", "source": [ - "await DataSource.SetAsync();", - "\nDataSource.Reset(x => x.ResetCurrentPartitions());" + "#!import \"../../ifrs17-template/Initialization/InitSystemorphBaseToMemory\"" ], "metadata": {}, "execution_count": 0, @@ -104,7 +131,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", + "//await Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(ratioAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(patternAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(solvencyBsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", 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, From 5680de6d0710294c2b4cbd82f928ea1a62368145 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Fri, 3 Nov 2023 14:49:04 +0100 Subject: [PATCH 05/17] wip --- .../InitSystemorphToMemory.ipynb | 12 ++++++- fpna-template/Report/Earnings.ipynb | 35 ++++++++++++++++--- fpna/DataModel/DataStructure.ipynb | 1 - ifrs17/Import/Importers.ipynb | 6 ++-- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index b265fe4d..9c7f60e5 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -66,7 +66,8 @@ { "cell_type": "code", "source": [ - "#!import \"../../ifrs17-template/Initialization/InitSystemorphBaseToMemory\"" + "//#!import \"../../ifrs17-template/Initialization/InitSystemorphToMemory\"", + "\n#!import \"../../ifrs17-template/Initialization/InitSystemorphBaseToMemory\"" ], "metadata": {}, "execution_count": 0, @@ -101,6 +102,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "# Import IFRS17 Initialization" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ diff --git a/fpna-template/Report/Earnings.ipynb b/fpna-template/Report/Earnings.ipynb index 02bd06da..47b47414 100644 --- a/fpna-template/Report/Earnings.ipynb +++ b/fpna-template/Report/Earnings.ipynb @@ -28,7 +28,7 @@ { "cell_type": "markdown", "source": [ - "- Forecast earnings under IFRS17 or any other regimes.", + "- Forecast earnings under IFRS 17 or any other regimes.", "\n- Integrate bottom-up and top-down approaches to earnings planning. ", "\n- Scale your projections by estimated volumes of business", "\n- Split figures by components, analyze by source and compare actual to expected for any of your KPI's" @@ -64,6 +64,33 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "# Data" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var dataCube = cashflowVariables.ToDataCube().ToReportType();" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "cashflowVariables.AggregateBy(\"AmountType\")" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -77,11 +104,11 @@ "cell_type": "code", "source": [ "await Report.ForDataCube(", - "\n cashflowVariables.ToDataCube().ToReportType()", + "\n dataCube//", "\n //.Filter(x => storage.EarningsAmountTypes.Contains(x.AmountType))", "\n )", - "\n.SliceRowsBy(\"AmountType\", \"Layer\")", - "\n.SliceColumnsBy(\"Projection\")", + "\n .SliceRowsBy(\"AmountType\", \"Layer\")", + "\n .SliceColumnsBy(\"Projection\")", "\n.WithQuerySource(DataSource)", "\n.ToTable()", "\n .WithOptions(go => go", diff --git a/fpna/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index f839c85f..6d1ff02a 100644 --- a/fpna/DataModel/DataStructure.ipynb +++ b/fpna/DataModel/DataStructure.ipynb @@ -301,7 +301,6 @@ "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", diff --git a/ifrs17/Import/Importers.ipynb b/ifrs17/Import/Importers.ipynb index 1a741ea1..2f09e50f 100644 --- a/ifrs17/Import/Importers.ipynb +++ b/ifrs17/Import/Importers.ipynb @@ -508,7 +508,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 +519,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}" From a7a0400f82382b790d14fb40d7bd0eb1ff7444f5 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Fri, 3 Nov 2023 18:02:44 +0100 Subject: [PATCH 06/17] use generics in the storage and scope to fix an issue with the calculation --- .../InitSystemorphToMemory.ipynb | 15 ++---- fpna-template/Report/Earnings.ipynb | 35 ++------------ fpna-template/Report/Solvency.ipynb | 6 ++- fpna/DataModel/DataStructure.ipynb | 11 +++-- fpna/Import/Scopes.ipynb | 46 ++++++++---------- fpna/Import/Storage.ipynb | 48 ++++--------------- 6 files changed, 45 insertions(+), 116 deletions(-) diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index 9c7f60e5..dc28ba87 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -66,8 +66,7 @@ { "cell_type": "code", "source": [ - "//#!import \"../../ifrs17-template/Initialization/InitSystemorphToMemory\"", - "\n#!import \"../../ifrs17-template/Initialization/InitSystemorphBaseToMemory\"" + "#!import \"../../ifrs17-template/Initialization/InitSystemorphBaseToMemory\"" ], "metadata": {}, "execution_count": 0, @@ -102,15 +101,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "markdown", - "source": [ - "# Import IFRS17 Initialization" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "source": [ @@ -141,7 +131,8 @@ { "cell_type": "code", "source": [ - "//await Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", + "// EarningsAmountType overlaps with the IFRS17 => clean up the data", + "\n//await Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(ratioAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(patternAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", "\nawait Import.FromString(solvencyBsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", diff --git a/fpna-template/Report/Earnings.ipynb b/fpna-template/Report/Earnings.ipynb index 47b47414..02bd06da 100644 --- a/fpna-template/Report/Earnings.ipynb +++ b/fpna-template/Report/Earnings.ipynb @@ -28,7 +28,7 @@ { "cell_type": "markdown", "source": [ - "- Forecast earnings under IFRS 17 or any other regimes.", + "- Forecast earnings under IFRS17 or any other regimes.", "\n- Integrate bottom-up and top-down approaches to earnings planning. ", "\n- Scale your projections by estimated volumes of business", "\n- Split figures by components, analyze by source and compare actual to expected for any of your KPI's" @@ -64,33 +64,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "markdown", - "source": [ - "# Data" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "var dataCube = cashflowVariables.ToDataCube().ToReportType();" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "cashflowVariables.AggregateBy(\"AmountType\")" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "source": [ @@ -104,11 +77,11 @@ "cell_type": "code", "source": [ "await Report.ForDataCube(", - "\n dataCube//", + "\n cashflowVariables.ToDataCube().ToReportType()", "\n //.Filter(x => storage.EarningsAmountTypes.Contains(x.AmountType))", "\n )", - "\n .SliceRowsBy(\"AmountType\", \"Layer\")", - "\n .SliceColumnsBy(\"Projection\")", + "\n.SliceRowsBy(\"AmountType\", \"Layer\")", + "\n.SliceColumnsBy(\"Projection\")", "\n.WithQuerySource(DataSource)", "\n.ToTable()", "\n .WithOptions(go => go", 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/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index 6d1ff02a..4a5bbb4d 100644 --- a/fpna/DataModel/DataStructure.ipynb +++ b/fpna/DataModel/DataStructure.ipynb @@ -276,7 +276,8 @@ { "cell_type": "code", "source": [ - "public record SolvencyCashflow : BoundedCashflow, IWithRiskDriver", + "//Remove ", + "\npublic record SolvencyCashflow : BoundedCashflow, IWithRiskDriver", "\n{", "\n [IdentityProperty]", "\n [Dimension(typeof(RiskDriver))] ", @@ -303,7 +304,6 @@ "\n{", "\n [Dimension(typeof(string))] ", "\n public string Layer { get; init; }", - "\n", "\n}" ], "metadata": {}, @@ -322,11 +322,11 @@ { "cell_type": "code", "source": [ - "public record SolvencyVariable : FpaVariable", + "//Remove", + "\npublic record SolvencyVariable : FpaVariable", "\n{", "\n [Dimension(typeof(RiskDriver), nameof(RiskDriver))] ", "\n public string RiskDriver { get; init; }", - "\n", "\n}" ], "metadata": {}, @@ -388,7 +388,8 @@ { "cell_type": "code", "source": [ - "public record SolvencyReportVariable : FpaReportVariable", + "//Remove", + "\npublic record SolvencyReportVariable : FpaReportVariable", "\n{", "\n [Dimension(typeof(RiskDriver), nameof(RiskDriver))]", "\n public string RiskDriver { get; init; }", diff --git a/fpna/Import/Scopes.ipynb b/fpna/Import/Scopes.ipynb index 6fe3c48c..ba5bdd94 100644 --- a/fpna/Import/Scopes.ipynb +++ b/fpna/Import/Scopes.ipynb @@ -74,8 +74,8 @@ "source": [ "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": {}, @@ -117,8 +117,8 @@ "source": [ "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": {}, @@ -158,9 +158,8 @@ "\n builder.ForScope(s => s", "\n .WithApplicability(x => x.Identity.ReferenceAmountType == x.Identity.Id.AmountType));", "\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 protected double[] weights => GetStorage().GetCashflow(Identity.Id).Single(cd => ((ReferencedCashflow)cd).ReferenceAmountType == Identity.ReferenceAmountType).Values;//Cast is not nice", "\n ", "\n double[] Values => weights.Zip(referencedValues, (weight, referencedValue) => weight * referencedValue).ToArray();", "\n}", @@ -171,8 +170,9 @@ "\n", "\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, ((ReferencedCashflow)cd).ReferenceAmountType)).Values)); //Cast is not nice", "\n}" ], "metadata": {}, @@ -191,34 +191,28 @@ { "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}" ], "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/Storage.ipynb b/fpna/Import/Storage.ipynb index 98ba8e8f..a12f83f4 100644 --- a/fpna/Import/Storage.ipynb +++ b/fpna/Import/Storage.ipynb @@ -32,6 +32,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": {}, @@ -57,18 +58,7 @@ "\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 public SolvencyCashflow[] SolvencyCashflowsByIdentity { get; private set; }", - "\n", - "\n ", "\n public CashflowIdentity[] Identities {get; set;}", "\n", "\n // Constructor", @@ -82,44 +72,22 @@ "\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 ", - "\n // Identities", + "\n {", "\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 SolvencyCashflowsByIdentity = await workspace.Query().ToArrayAsync();", - "\n", + "\n // Identities", "\n Identities = CashflowsByIdentity.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 ", - "\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", - "\n", - "\n public IEnumerable GetBoundedCashflows(CashflowIdentity identity) => ", - "\n BoundedCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", - "\n ", - "\n public IEnumerable GetReferencedCashflow(CashflowIdentity identity) => ", - "\n ReferencedCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", - "\n", - "\n public IEnumerable GetSolvencyCashflow(CashflowIdentity identity) => ", - "\n SolvencyCashflowsByIdentity.Where(x => x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", - "\n", + "\n // Getters", + "\n public IEnumerable GetCashflow(CashflowIdentity identity) where T : Cashflow =>", + "\n CashflowsByIdentity.Where(x => x.GetType() == typeof(T) && x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", "\n}" ], "metadata": {}, From f604451fb52aeb64344e70459845c587c7bc029d Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Fri, 3 Nov 2023 18:15:29 +0100 Subject: [PATCH 07/17] chashflower test --- fpna-template/Test/CHashFlowerTest.ipynb | 288 +++++++++++++++++++++++ fpna-template/Test/Tests.ipynb | 38 +++ 2 files changed, 326 insertions(+) create mode 100644 fpna-template/Test/CHashFlowerTest.ipynb create mode 100644 fpna-template/Test/Tests.ipynb diff --git a/fpna-template/Test/CHashFlowerTest.ipynb b/fpna-template/Test/CHashFlowerTest.ipynb new file mode 100644 index 00000000..01170043 --- /dev/null +++ b/fpna-template/Test/CHashFlowerTest.ipynb @@ -0,0 +1,288 @@ +{ + "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 = \"DE\";", + "\nvar year = 2022;", + "\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 = @\"@@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).WithType().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": [ + "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 = @\"@@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).WithType().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": [ + "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..9cb7827e --- /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 From 4ad7384222c5edd6d97c6311fe6e565fc2190185 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Mon, 6 Nov 2023 09:49:10 +0100 Subject: [PATCH 08/17] clean up --- fpna/DataModel/DataStructure.ipynb | 87 +----------------------------- 1 file changed, 1 insertion(+), 86 deletions(-) diff --git a/fpna/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index 4a5bbb4d..91a4c26a 100644 --- a/fpna/DataModel/DataStructure.ipynb +++ b/fpna/DataModel/DataStructure.ipynb @@ -102,21 +102,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "code", - "source": [ - "public interface IWithRiskDriver", - "\n{", - "\n [IdentityProperty]", - "\n [Dimension(typeof(RiskDriver))] ", - "\n public string RiskDriver { get; init; }", - "\n", - "\n}" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "source": [ @@ -164,7 +149,7 @@ "\n{", "\n [Key]", "\n [NotVisible] ", - "\n public Guid Id { get; init; } // TODO should go to db?", + "\n public Guid Id { get; init; } // TODO should go to db? YES", "\n", "\n [IdentityProperty]", "\n [Dimension(typeof(string), nameof(DataNode))]", @@ -264,30 +249,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "markdown", - "source": [ - "#### Solvency" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "//Remove ", - "\npublic record SolvencyCashflow : BoundedCashflow, IWithRiskDriver", - "\n{", - "\n [IdentityProperty]", - "\n [Dimension(typeof(RiskDriver))] ", - "\n public string RiskDriver { get; init; }", - "\n}" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "source": [ @@ -310,29 +271,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "markdown", - "source": [ - "## Solvency Variable" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "//Remove", - "\npublic record SolvencyVariable : FpaVariable", - "\n{", - "\n [Dimension(typeof(RiskDriver), nameof(RiskDriver))] ", - "\n public string RiskDriver { get; init; }", - "\n}" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "source": [ @@ -375,29 +313,6 @@ "metadata": {}, "execution_count": 0, "outputs": [] - }, - { - "cell_type": "markdown", - "source": [ - "## Solvency Report Variable" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "//Remove", - "\npublic record SolvencyReportVariable : FpaReportVariable", - "\n{", - "\n [Dimension(typeof(RiskDriver), nameof(RiskDriver))]", - "\n public string RiskDriver { get; init; }", - "\n}" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] } ] } \ No newline at end of file From ded1b512971da82711f19d2f5194d47267bd84a5 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Thu, 9 Nov 2023 08:48:24 +0100 Subject: [PATCH 09/17] wip --- fpna-template/Files/Cashflows.ipynb | 58 ++-- fpna-template/Files/RiskDriver.ipynb | 26 ++ ...te.ipynb => FpnaCloseImportTemplate.ipynb} | 72 ++++- fpna-template/Test/CHashFlowerTest.ipynb | 30 +- fpna/Constants/Consts.ipynb | 31 ++ fpna/DataModel/DataStructure.ipynb | 72 +++-- fpna/Import/Importers.ipynb | 248 ++++++++++++++++ fpna/Import/Scopes.ipynb | 68 ++++- fpna/Import/Storage.ipynb | 276 ++++++++++++++++-- fpna/Utils/ExtentionMethods.ipynb | 25 +- fpna/Utils/Queries.ipynb | 78 +++++ .../CompareReinsuranceContracts/Reports.ipynb | 2 +- ifrs17/Import/Importers.ipynb | 2 +- ifrs17/Utils/Queries.ipynb | 39 ++- 14 files changed, 931 insertions(+), 96 deletions(-) rename fpna-template/Import/{CloseImportTemplate.ipynb => FpnaCloseImportTemplate.ipynb} (72%) create mode 100644 fpna/Constants/Consts.ipynb create mode 100644 fpna/Utils/Queries.ipynb diff --git a/fpna-template/Files/Cashflows.ipynb b/fpna-template/Files/Cashflows.ipynb index 0f38714b..6ffd4a46 100644 --- a/fpna-template/Files/Cashflows.ipynb +++ b/fpna-template/Files/Cashflows.ipynb @@ -17,25 +17,18 @@ "nbformat_minor": 5, "cells": [ { - "cell_type": "code", + "cell_type": "markdown", "source": [ - "var basicCashflows = @\"@@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\";" + "# Cashflow Definition Input" ], "metadata": {}, "execution_count": 0, "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\";" + "## Generic" ], "metadata": {}, "execution_count": 0, @@ -44,7 +37,18 @@ { "cell_type": "code", "source": [ - "var referencedCashflows = @\"@@ReferencedCashflow", + "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", @@ -58,14 +62,34 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "markdown", + "source": [ + "## Solvency" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ - "var solvencyCashflows = @\"@@SolvencyCashflow", - "\nDataNode,AmountType,Shape,RiskDriver,InitialValue,Length,Shift,Minimum,Maximum", - "\nG.BBA.LRC.2020.P,IFASH,Constant,EQR,0.75,5,0,-1,0", - "\nG.BBA.LRC.2020.P,IFASH,Constant,SPR,0.6,5,0,-1,0", - "\nG.BBA.LRC.2020.P,IFLTP,Constant,LMOR,0.5,5,0,-1,0", + "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,S2ASH,,Reference,,,,PR,1,1,1,1,1", + "\nG.BBA.LRC.2020.P,S2LTP,,Reference,,,,PR,3,3,3,4,5", "\n\";" ], "metadata": {}, diff --git a/fpna-template/Files/RiskDriver.ipynb b/fpna-template/Files/RiskDriver.ipynb index e466d49f..0bb13862 100644 --- a/fpna-template/Files/RiskDriver.ipynb +++ b/fpna-template/Files/RiskDriver.ipynb @@ -72,6 +72,32 @@ "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 72% rename from fpna-template/Import/CloseImportTemplate.ipynb rename to fpna-template/Import/FpnaCloseImportTemplate.ipynb index f007d5e7..962a0f3b 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.CashflowsByIdentity" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "markdown", "source": [ @@ -212,7 +221,64 @@ { "cell_type": "code", "source": [ - "" + "storage.CashflowsByIdentity.Where(x => x.type == typeof(ReferenceCashflow))" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# debug Solvency" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "var solvencystorage = new SolvencyStorage(args, DataSource, Workspace);", + "\nawait solvencystorage.InitializeAsync();" + ], + "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": [ + "(solvencystorage.GetPillar(), solvencystorage.TargetPartition)" + ], + "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/Test/CHashFlowerTest.ipynb b/fpna-template/Test/CHashFlowerTest.ipynb index 01170043..a91bd3a8 100644 --- a/fpna-template/Test/CHashFlowerTest.ipynb +++ b/fpna-template/Test/CHashFlowerTest.ipynb @@ -46,8 +46,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;", @@ -81,7 +81,11 @@ { "cell_type": "code", "source": [ - "var basicCashflows = @\"@@BasicCashflow", + "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", @@ -94,7 +98,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(basicCashflows).WithType().WithTarget(workspace).ExecuteAsync()" + "await Import.FromString(basicCashflows).WithFormat(\"CashflowDefinition\").WithTarget(workspace).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -105,6 +109,7 @@ "source": [ "var storage = new CashflowStorage(args, DataSource, workspace);", "\nawait storage.InitializeAsync();", + "\n", "\nvar identities = storage.Identities;", "\n", "\npublic interface IModel : IMutableScopeWithStorage{}", @@ -187,7 +192,11 @@ { "cell_type": "code", "source": [ - "var boundedCashflows = @\"@@BoundedCashflow ", + "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\";" @@ -199,7 +208,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(boundedCashflows).WithType().WithTarget(workspace).ExecuteAsync()" + "await Import.FromString(boundedCashflows).WithFormat(\"CashflowDefinition\").WithTarget(workspace).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -219,6 +228,15 @@ "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "source": [ + "((ICashflowStorage)storage).GetCashflow(identities.First())" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, { "cell_type": "code", "source": [ 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/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index 91a4c26a..6c3ea0ed 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": [ @@ -61,11 +71,8 @@ "public interface IBased", "\n{", "\n public double InitialValue { get; init; }", - "\n", "\n public int Length { get; init; }", - "\n", "\n public int Shift { get; init; }", - "\n", "\n}" ], "metadata": {}, @@ -78,9 +85,7 @@ "public interface IBounded", "\n{", "\n public double Minimum { get; init; }", - "\n", "\n public double Maximum { get; init; }", - "\n", "\n}" ], "metadata": {}, @@ -102,6 +107,21 @@ "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": [ @@ -145,32 +165,31 @@ { "cell_type": "code", "source": [ - "public record Cashflow : IKeyed", + "public record Cashflow : IKeyed, IPartitioned, IWithPillar", "\n{", - "\n [Key]", - "\n [NotVisible] ", - "\n public Guid Id { get; init; } // TODO should go to db? YES", + "\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 [IdentityProperty][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 [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": {}, @@ -189,14 +208,11 @@ { "cell_type": "code", "source": [ - "public record BasicCashflow : Cashflow, IBased {", - "\n ", + "public record BasicCashflow : Cashflow, IBased", + "\n{", "\n public double InitialValue { get; init; }", - "\n", "\n public int Length { get; init; }", - "\n", "\n public int Shift { get; init; }", - "\n", "\n}" ], "metadata": {}, @@ -215,10 +231,12 @@ { "cell_type": "code", "source": [ - "public record BoundedCashflow : BasicCashflow, IBounded", + "public record BoundedCashflow : Cashflow, IBased, IBounded", "\n{", + "\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}" ], diff --git a/fpna/Import/Importers.ipynb b/fpna/Import/Importers.ipynb index c079f458..63a8cf39 100644 --- a/fpna/Import/Importers.ipynb +++ b/fpna/Import/Importers.ipynb @@ -24,6 +24,254 @@ "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(Cashflow.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, //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(Cashflow.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(BoundedCashflow.InitialValue)).CheckStringForExponentialAndConvertToDouble(),", + "\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(Cashflow.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 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 ba5bdd94..38f263a1 100644 --- a/fpna/Import/Scopes.ipynb +++ b/fpna/Import/Scopes.ipynb @@ -72,7 +72,20 @@ { "cell_type": "code", "source": [ - "public interface BasicCashflows : IScope", + "public interface BasicCashflows : IScope", + "\n{", + "\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": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public interface BasicCashflows2 : IScope", "\n{", "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetCashflow(Identity)", "\n .Select((cd,i) => (\"Basic-\" + cd.Shape + i.ToString(), GetScope(cd).Values));", @@ -105,7 +118,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,7 +128,7 @@ { "cell_type": "code", "source": [ - "public interface BoundedCashflows : IScope", + "public interface BoundedCashflows : IScope", "\n{", "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetCashflow(Identity)", "\n .Select((cd,i) => (\"Bounded-\" + cd.Shape + i.ToString(), GetScope(cd).Values)); ", @@ -137,7 +150,8 @@ { "cell_type": "code", "source": [ - "public interface ReferencedCashflows : IScope<(CashflowIdentity Id, string OriginalAmountType), CashflowStorage>{", + "", + "\npublic 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,10 +167,11 @@ "\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", "\n protected double[] referencedValues => GetScope((Identity.Id with {AmountType = Identity.ReferenceAmountType}, Identity.Id.AmountType)).Values;", "\n protected double[] weights => GetStorage().GetCashflow(Identity.Id).Single(cd => ((ReferencedCashflow)cd).ReferenceAmountType == Identity.ReferenceAmountType).Values;//Cast is not nice", @@ -164,11 +179,11 @@ "\n double[] Values => weights.Zip(referencedValues, (weight, referencedValue) => weight * referencedValue).ToArray();", "\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().GetCashflow(Identity)", "\n .Select( (cd,i) => (\"Reference\" + cd.Shape + i.ToString(), ", @@ -182,7 +197,7 @@ { "cell_type": "markdown", "source": [ - "# Compute all Cashflows" + "# Compute all Cashflows (no pillar storage - debug -)" ], "metadata": {}, "execution_count": 0, @@ -213,6 +228,41 @@ "metadata": {}, "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, + "outputs": [] } ] } \ No newline at end of file diff --git a/fpna/Import/Storage.ipynb b/fpna/Import/Storage.ipynb index a12f83f4..86a7218a 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, @@ -42,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, @@ -51,29 +82,42 @@ { "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 protected IDataSource querySource {get;} ", + "\n protected IWorkspace workspace {get;}", + "\n protected Systemorph.Vertex.Hierarchies.IHierarchicalDimensionCache hierarchyCache {get;}", + "\n protected ImportArgs args {get;}", "\n", - "\n public Cashflow[] CashflowsByIdentity { get; private set; }", + "\n public string pillar {get; set; }", + "\n public Cashflow[] CashflowsByIdentity { get; set; } //rename to CashflowDefinitions", "\n public CashflowIdentity[] Identities {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 //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 public async Task BaseInitializeAsync()", "\n {", - "\n CashflowsByIdentity = await workspace.Query().ToArrayAsync();", + "\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 CashflowsByIdentity = await workspace.QueryCashflowAsync(querySource, TargetPartition, DefaultPartition, args, pillar);", + "\n ", "\n // Identities", "\n Identities = CashflowsByIdentity.Select(x => new CashflowIdentity() ", "\n { ", @@ -84,15 +128,209 @@ "\n }", "\n ).Distinct().ToArray();", "\n }", + "\n", + "\n protected static async Task DefaultInitializationAsync(ICashflowStorage iCashflowStorage) => await iCashflowStorage.BaseInitializeAsync();", "\n ", "\n // Getters", "\n public IEnumerable GetCashflow(CashflowIdentity identity) where T : Cashflow =>", "\n CashflowsByIdentity.Where(x => x.GetType() == typeof(T) && x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", + "\n", + "\n public string GetPillar() => \"Pillar not set\";", "\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[] CashflowsByIdentity { get; set; }", + "\n public CashflowIdentity[] Identities {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 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[] CashflowsByIdentity { get; set; }", + "\n public CashflowIdentity[] Identities {get; set;}", + "\n ", + "\n //From ImportStorage", + "\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 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}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Class" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "// public class CashflowStorage", + "\n// { ", + "\n// IDataSource querySource; ", + "\n// IWorkspace workspace;", + "\n// Systemorph.Vertex.Hierarchies.IHierarchicalDimensionCache hierarchyCache;", + "\n// ImportArgs args;", + "\n", + "\n// public string pillar = null;", + "\n// public Cashflow[] CashflowsByIdentity { get; set; }", + "\n// public CashflowIdentity[] Identities {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;", + "\n// public Guid TargetPartition;", + "\n// public Guid DefaultPartition;", + "\n", + "\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// // Initialize", + "\n// public async Task InitializeAsync()", + "\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// CashflowsByIdentity = await workspace.QueryCashflowAsync(querySource, TargetPartition, DefaultPartition, args, pillar);", + "\n ", + "\n// // Identities", + "\n// Identities = CashflowsByIdentity.Select(x => new CashflowIdentity() ", + "\n// { ", + "\n// DataNode = x.DataNode,", + "\n// AmountType = x.AmountType,", + "\n// AccidentYear = x.AccidentYear,", + "\n// Type = x.GetType(),", + "\n// }", + "\n// ).Distinct().ToArray();", + "\n// }", + "\n ", + "\n// // Getters", + "\n// public IEnumerable GetCashflow(CashflowIdentity identity) where T : Cashflow =>", + "\n// CashflowsByIdentity.Where(x => x.GetType() == typeof(T) && x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", + "\n", + "\n// public string GetPillar() => \"Pillar not set\";", + "\n// }" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Solvency" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "// public class SolvencyStorage : CashflowStorage", + "\n// {", + "\n// // Constructor", + "\n// public SolvencyStorage(ImportArgs args, IDataSource querySource, IWorkspace workspace) : base(args, querySource, workspace){}", + "\n", + "\n// public async Task InitializeAsync() {", + "\n// pillar = \"Solvency\";", + "\n// await base.InitializeAsync();", + "\n// }", + "\n", + "\n// //Getters", + "\n// public string GetPillar() => pillar;", + "\n// }" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file 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..ac6ffb92 --- /dev/null +++ b/fpna/Utils/Queries.ipynb @@ -0,0 +1,78 @@ +{ + "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 idenfied from the args.ImportFormat", + "\n where T : IPartitioned, IWithPillar", + "\n where P : IPartition", + "\n{", + "\n //FilterExpression", + "\n //Expression> filterExpression = pillar == null ? (Expression>)(x => true) : x => x.Pillar == pillar; // Is it ever needed to query all pillars?", + "\n Expression> filterExpression = x => x.Pillar == pillar;", + "\n ", + "\n var variablesFromWorkspace = await workspace.LoadPartitionedDataAsync(targetPartition, filterExpression);", + "\n if(args.Scenario == null)", + "\n return variablesFromWorkspace;", + "\n", + "\n var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar); //TODO what about the partition? Should be set to the correct one here?", + "\n", + "\n return variablesFromWorkspace.Union(variablesFromWorkspace, 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/Importers.ipynb b/ifrs17/Import/Importers.ipynb index d212d51c..bdf9b9c0 100644 --- a/ifrs17/Import/Importers.ipynb +++ b/ifrs17/Import/Importers.ipynb @@ -508,7 +508,7 @@ "\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))", diff --git a/ifrs17/Utils/Queries.ipynb b/ifrs17/Utils/Queries.ipynb index 593b57bc..266d90bc 100644 --- a/ifrs17/Utils/Queries.ipynb +++ b/ifrs17/Utils/Queries.ipynb @@ -430,14 +430,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;", @@ -450,23 +450,46 @@ { "cell_type": "code", "source": [ - "public static async Task QueryPartitionedDataAsync(this IWorkspace workspace, IDataSource dataSource, Guid targetPartition, Guid defaultPartition, string format)", + "public static bool GetIsRelaxed(string format)", + "\n where T : IPartitioned", + "\n{", + "\n if(typeof(ImportFormats).GetFields().Select(x => x.Name).Contains(format)) //ifrs17 ImportFormats", + "\n return ((format != ImportFormats.Cashflow && typeof(T).Name == nameof(IfrsVariable)) ||", + "\n (format == ImportFormats.Cashflow && typeof(T).Name == nameof(RawVariable)));", + "\n ", + "\n //Override or simple expansion allows to extend this concept to other Formats", + "\n return true;", + "\n //Scenario == null (BE) => workspace", + "\n //Scenario == ", + "\n}" + ], + "metadata": {}, + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "public static async Task QueryPartitionedDataAsync(this IWorkspace workspace, IDataSource dataSource, ", + "\n Guid targetPartition, Guid defaultPartition, ", + "\n string format,", + "\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 //If Ifrs17 format", + "\n var isRelaxed = GetIsRelaxed(format);", "\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": {}, From 868b0fb53639adcece1a93af636edd3f4b1f79ff Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Thu, 9 Nov 2023 16:54:05 +0100 Subject: [PATCH 10/17] init using Import.WithFormat --- .../Initialization/InitSystemorphToMemory.ipynb | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index dc28ba87..035a0f33 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -190,7 +190,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(basicCashflows).WithType().WithTarget(Workspace).ExecuteAsync()" + "Debug.Enable = true;" ], "metadata": {}, "execution_count": 0, @@ -199,7 +199,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(boundedCashflows).WithType().WithTarget(Workspace).ExecuteAsync()" + "await Import.FromString(CashflowsDefInput).WithFormat(\"CashflowDefinition\").WithTarget(Workspace).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, @@ -208,16 +208,7 @@ { "cell_type": "code", "source": [ - "await Import.FromString(referencedCashflows).WithType().WithTarget(Workspace).ExecuteAsync()" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "" + "await Import.FromString(SolvencyCashflowsDefInput).WithFormat(SolvencyImportFormats.SolvencyCashflowDefinition).WithTarget(Workspace).ExecuteAsync()" ], "metadata": {}, "execution_count": 0, From c8a6a8f1548b83234f60212aee02fcf672890375 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Fri, 10 Nov 2023 16:13:16 +0100 Subject: [PATCH 11/17] some clean up --- fpna-template/Files/Cashflows.ipynb | 2 - .../Import/FpnaCloseImportTemplate.ipynb | 23 +--- fpna/Import/Scopes.ipynb | 33 ++--- fpna/Import/Storage.ipynb | 127 ++++-------------- 4 files changed, 40 insertions(+), 145 deletions(-) diff --git a/fpna-template/Files/Cashflows.ipynb b/fpna-template/Files/Cashflows.ipynb index 6ffd4a46..312fdffa 100644 --- a/fpna-template/Files/Cashflows.ipynb +++ b/fpna-template/Files/Cashflows.ipynb @@ -88,8 +88,6 @@ "\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": {}, diff --git a/fpna-template/Import/FpnaCloseImportTemplate.ipynb b/fpna-template/Import/FpnaCloseImportTemplate.ipynb index 962a0f3b..a3322b1c 100644 --- a/fpna-template/Import/FpnaCloseImportTemplate.ipynb +++ b/fpna-template/Import/FpnaCloseImportTemplate.ipynb @@ -212,16 +212,7 @@ "cell_type": "code", "source": [ "var referenced = cashflowVariables.Where(x => x.Layer.Contains(\"Reference\"));", - "\nreferenced.AggregateOver()" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "storage.CashflowsByIdentity.Where(x => x.type == typeof(ReferenceCashflow))" + "\nreferenced" ], "metadata": {}, "execution_count": 0, @@ -240,7 +231,8 @@ "cell_type": "code", "source": [ "var solvencystorage = new SolvencyStorage(args, DataSource, Workspace);", - "\nawait solvencystorage.InitializeAsync();" + "\nawait solvencystorage.InitializeAsync();", + "\nsolvencystorage.CashflowsByIdentity" ], "metadata": {}, "execution_count": 0, @@ -265,15 +257,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "code", - "source": [ - "(solvencystorage.GetPillar(), solvencystorage.TargetPartition)" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "code", "source": [ diff --git a/fpna/Import/Scopes.ipynb b/fpna/Import/Scopes.ipynb index 38f263a1..a0b0e993 100644 --- a/fpna/Import/Scopes.ipynb +++ b/fpna/Import/Scopes.ipynb @@ -82,19 +82,6 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "code", - "source": [ - "public interface BasicCashflows2 : IScope", - "\n{", - "\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": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "markdown", "source": [ @@ -150,8 +137,7 @@ { "cell_type": "code", "source": [ - "", - "\npublic interface ReferencedCashflows : IScope<(CashflowIdentity Id, string OriginalAmountType), ICashflowStorage>{", + "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));", @@ -171,13 +157,22 @@ "\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 protected double[] referencedValues => GetScope((Identity.Id with {AmountType = Identity.ReferenceAmountType}, Identity.Id.AmountType)).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 => ((ReferencedCashflow)cd).ReferenceAmountType == Identity.ReferenceAmountType).Values;//Cast is not nice", "\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 WeightedSelfReference : WeightedReference{", "\n double[] WeightedReference.Values => weights.Zip(referencedValues, (weight, referencedValue) => ( weight - 1d ) * referencedValue).ToArray();", @@ -245,8 +240,8 @@ "\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 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,", diff --git a/fpna/Import/Storage.ipynb b/fpna/Import/Storage.ipynb index 86a7218a..ffb19e0e 100644 --- a/fpna/Import/Storage.ipynb +++ b/fpna/Import/Storage.ipynb @@ -91,7 +91,8 @@ "\n", "\n public string pillar {get; set; }", "\n public Cashflow[] CashflowsByIdentity { get; set; } //rename to CashflowDefinitions", - "\n public CashflowIdentity[] Identities {get; set;}", + "\n public CashflowIdentity[] Identities {get; set;} //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", @@ -105,6 +106,18 @@ "\n // Initialize", "\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 &&", @@ -136,6 +149,10 @@ "\n CashflowsByIdentity.Where(x => x.GetType() == typeof(T) && x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", "\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": {}, @@ -155,6 +172,7 @@ "\n public string pillar {get; set; }", "\n public Cashflow[] CashflowsByIdentity { get; set; }", "\n public CashflowIdentity[] Identities {get; set;}", + "\n public Dictionary> AmountTypesByPillar {get; set;}", "\n ", "\n //Partitions", "\n public Guid PartitionByRn {get; set; }", @@ -192,9 +210,9 @@ "\n public string pillar {get; set; }", "\n public Cashflow[] CashflowsByIdentity { get; set; }", "\n public CashflowIdentity[] Identities {get; set;}", + "\n public Dictionary> AmountTypesByPillar {get; set;}", "\n ", "\n //From ImportStorage", - "\n", "\n //Partitions", "\n public Guid PartitionByRn {get; set; }", "\n public Guid TargetPartition {get; set; }", @@ -216,96 +234,9 @@ "\n", "\n //Getters", "\n public string GetPillar() => pillar;", - "\n}" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "source": [ - "# Class" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "// public class CashflowStorage", - "\n// { ", - "\n// IDataSource querySource; ", - "\n// IWorkspace workspace;", - "\n// Systemorph.Vertex.Hierarchies.IHierarchicalDimensionCache hierarchyCache;", - "\n// ImportArgs args;", - "\n", - "\n// public string pillar = null;", - "\n// public Cashflow[] CashflowsByIdentity { get; set; }", - "\n// public CashflowIdentity[] Identities {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;", - "\n// public Guid TargetPartition;", - "\n// public Guid DefaultPartition;", - "\n", "\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// // Initialize", - "\n// public async Task InitializeAsync()", - "\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// CashflowsByIdentity = await workspace.QueryCashflowAsync(querySource, TargetPartition, DefaultPartition, args, pillar);", - "\n ", - "\n// // Identities", - "\n// Identities = CashflowsByIdentity.Select(x => new CashflowIdentity() ", - "\n// { ", - "\n// DataNode = x.DataNode,", - "\n// AmountType = x.AmountType,", - "\n// AccidentYear = x.AccidentYear,", - "\n// Type = x.GetType(),", - "\n// }", - "\n// ).Distinct().ToArray();", - "\n// }", - "\n ", - "\n// // Getters", - "\n// public IEnumerable GetCashflow(CashflowIdentity identity) where T : Cashflow =>", - "\n// CashflowsByIdentity.Where(x => x.GetType() == typeof(T) && x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", - "\n", - "\n// public string GetPillar() => \"Pillar not set\";", - "\n// }" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "source": [ - "# Solvency" + "\n public bool IsExternalReference(string amountType) => !AmountTypesByPillar[pillar].Contains(amountType);", + "\n}" ], "metadata": {}, "execution_count": 0, @@ -314,19 +245,7 @@ { "cell_type": "code", "source": [ - "// public class SolvencyStorage : CashflowStorage", - "\n// {", - "\n// // Constructor", - "\n// public SolvencyStorage(ImportArgs args, IDataSource querySource, IWorkspace workspace) : base(args, querySource, workspace){}", - "\n", - "\n// public async Task InitializeAsync() {", - "\n// pillar = \"Solvency\";", - "\n// await base.InitializeAsync();", - "\n// }", - "\n", - "\n// //Getters", - "\n// public string GetPillar() => pillar;", - "\n// }" + "" ], "metadata": {}, "execution_count": 0, From 5139c614bf26b8d4f5bf78fa4e739a279bb16c41 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Tue, 14 Nov 2023 09:16:25 +0100 Subject: [PATCH 12/17] some clean up --- fpna/Utils/Queries.ipynb | 14 +++++++++----- ifrs17/Import/ImportStorage.ipynb | 14 ++++++++++---- ifrs17/Import/Importers.ipynb | 4 ++-- ifrs17/Report/ReportStorage.ipynb | 1 - ifrs17/Utils/Queries.ipynb | 25 +------------------------ 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/fpna/Utils/Queries.ipynb b/fpna/Utils/Queries.ipynb index ac6ffb92..e4cecdd3 100644 --- a/fpna/Utils/Queries.ipynb +++ b/fpna/Utils/Queries.ipynb @@ -53,21 +53,25 @@ "public static async Task QueryCashflowAsync(this IWorkspace workspace, IDataSource dataSource, ", "\n Guid targetPartition, Guid defaultPartition,", "\n ImportArgs args,", - "\n string pillar) //pillar should be idenfied from the args.ImportFormat", + "\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 = pillar == null ? (Expression>)(x => true) : x => x.Pillar == pillar; // Is it ever needed to query all pillars?", "\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(args.Scenario == null)", + "\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); //TODO what about the partition? Should be set to the correct one here?", + "\n //var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar);", "\n", - "\n return variablesFromWorkspace.Union(variablesFromWorkspace, EqualityComparer.Instance).ToArray();", + "\n return variablesFromWorkspace.Union(dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar), EqualityComparer.Instance).ToArray();", "\n}" ], "metadata": {}, diff --git a/ifrs17/Import/ImportStorage.ipynb b/ifrs17/Import/ImportStorage.ipynb index 85802c4c..1fb63313 100644 --- a/ifrs17/Import/ImportStorage.ipynb +++ b/ifrs17/Import/ImportStorage.ipynb @@ -181,8 +181,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);", @@ -279,7 +281,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 ", @@ -300,8 +302,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 bdf9b9c0..f7bd16cd 100644 --- a/ifrs17/Import/Importers.ipynb +++ b/ifrs17/Import/Importers.ipynb @@ -646,7 +646,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));", @@ -1508,12 +1508,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/Utils/Queries.ipynb b/ifrs17/Utils/Queries.ipynb index 266d90bc..6a7640d8 100644 --- a/ifrs17/Utils/Queries.ipynb +++ b/ifrs17/Utils/Queries.ipynb @@ -447,39 +447,16 @@ "execution_count": 0, "outputs": [] }, - { - "cell_type": "code", - "source": [ - "public static bool GetIsRelaxed(string format)", - "\n where T : IPartitioned", - "\n{", - "\n if(typeof(ImportFormats).GetFields().Select(x => x.Name).Contains(format)) //ifrs17 ImportFormats", - "\n return ((format != ImportFormats.Cashflow && typeof(T).Name == nameof(IfrsVariable)) ||", - "\n (format == ImportFormats.Cashflow && typeof(T).Name == nameof(RawVariable)));", - "\n ", - "\n //Override or simple expansion allows to extend this concept to other Formats", - "\n return true;", - "\n //Scenario == null (BE) => workspace", - "\n //Scenario == ", - "\n}" - ], - "metadata": {}, - "execution_count": 0, - "outputs": [] - }, { "cell_type": "code", "source": [ "public static async Task QueryPartitionedDataAsync(this IWorkspace workspace, IDataSource dataSource, ", "\n Guid targetPartition, Guid defaultPartition, ", - "\n string format,", + "\n bool isRelaxed,", "\n Expression> filterExpression = null)", "\n where T : IPartitioned", "\n where P : IPartition", "\n{", - "\n //If Ifrs17 format", - "\n var isRelaxed = GetIsRelaxed(format);", - "\n", "\n var variablesFromWorkspace = await workspace.LoadPartitionedDataAsync(targetPartition, filterExpression);", "\n if(!isRelaxed || variablesFromWorkspace.Any()) return variablesFromWorkspace;", "\n", From cb02cc7b84d0026be80ce49623ad70bf213d1812 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Tue, 14 Nov 2023 16:39:12 +0100 Subject: [PATCH 13/17] fix test and clean up --- .../InitSystemorphToMemory.ipynb | 7 ++- fpna-template/Test/Tests.ipynb | 2 +- fpna/Import/Scopes.ipynb | 14 +++--- fpna/Import/Storage.ipynb | 17 +++---- fpna/Utils/Queries.ipynb | 2 +- ifrs17/Test/QueriesScenarioDataTest.ipynb | 47 ++++++++++++++----- 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index 035a0f33..33f6b253 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -57,7 +57,7 @@ { "cell_type": "markdown", "source": [ - "## Initialize from calculation engine template" + "## Initialize from IFRS 17 calculation engine template" ], "metadata": {}, "execution_count": 0, @@ -66,7 +66,10 @@ { "cell_type": "code", "source": [ - "#!import \"../../ifrs17-template/Initialization/InitSystemorphBaseToMemory\"" + "var ifrs17templateInit = $\"#!import \\\"//{projectTemplateName}/{environmentTemplateName}/Initialization/InitSystemorphBaseToMemory\\\"\";", + "\nifrs17templateInit = $\"#!import \\\"../../{projectTemplateName}/Initialization/InitSystemorphBaseToMemory\\\"\";", + "\n", + "\n#!eval ifrs17templateInit" ], "metadata": {}, "execution_count": 0, diff --git a/fpna-template/Test/Tests.ipynb b/fpna-template/Test/Tests.ipynb index 9cb7827e..ee26f27f 100644 --- a/fpna-template/Test/Tests.ipynb +++ b/fpna-template/Test/Tests.ipynb @@ -28,7 +28,7 @@ { "cell_type": "code", "source": [ - "#!Import \"CHashFlowerTest\"" + "#!import \"CHashFlowerTest\"" ], "metadata": {}, "execution_count": 0, diff --git a/fpna/Import/Scopes.ipynb b/fpna/Import/Scopes.ipynb index a0b0e993..c2b40c36 100644 --- a/fpna/Import/Scopes.ipynb +++ b/fpna/Import/Scopes.ipynb @@ -161,12 +161,12 @@ "\n //.WithApplicability(x => x.GetStorage().IsExternalReference(x.Identity.ReferenceAmountType) ) //wip", "\n );", "\n", - "\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 => ((ReferencedCashflow)cd).ReferenceAmountType == Identity.ReferenceAmountType).Values;//Cast is not nice", + "\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}", @@ -182,7 +182,7 @@ "\n{", "\n public IEnumerable<(string Layer, double[] Values)> LayerValues => GetStorage().GetCashflow(Identity)", "\n .Select( (cd,i) => (\"Reference\" + cd.Shape + i.ToString(), ", - "\n GetScope((Identity, ((ReferencedCashflow)cd).ReferenceAmountType)).Values)); //Cast is not nice", + "\n GetScope((Identity, cd.ReferenceAmountType)).Values));", "\n}" ], "metadata": {}, diff --git a/fpna/Import/Storage.ipynb b/fpna/Import/Storage.ipynb index ffb19e0e..5548bb05 100644 --- a/fpna/Import/Storage.ipynb +++ b/fpna/Import/Storage.ipynb @@ -90,8 +90,8 @@ "\n protected ImportArgs args {get;}", "\n", "\n public string pillar {get; set; }", - "\n public Cashflow[] CashflowsByIdentity { get; set; } //rename to CashflowDefinitions", - "\n public CashflowIdentity[] Identities {get; set;} //Should not come from the storage but be computed in a scope to add those that are not input explicitly", + "\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", @@ -129,10 +129,10 @@ "\n p.Month == CurrentReportingPeriod.Month &&", "\n p.Scenario == null).ToArrayAsync()).Single().Id;", "\n //Cashflow Definition", - "\n CashflowsByIdentity = await workspace.QueryCashflowAsync(querySource, TargetPartition, DefaultPartition, args, pillar);", + "\n CashflowDefinitions = await workspace.QueryCashflowAsync(querySource, TargetPartition, DefaultPartition, args, pillar);", "\n ", "\n // Identities", - "\n Identities = CashflowsByIdentity.Select(x => new CashflowIdentity() ", + "\n Identities = CashflowDefinitions.Select(x => new CashflowIdentity() ", "\n { ", "\n DataNode = x.DataNode,", "\n AmountType = x.AmountType,", @@ -145,8 +145,9 @@ "\n protected static async Task DefaultInitializationAsync(ICashflowStorage iCashflowStorage) => await iCashflowStorage.BaseInitializeAsync();", "\n ", "\n // Getters", - "\n public IEnumerable GetCashflow(CashflowIdentity identity) where T : Cashflow =>", - "\n CashflowsByIdentity.Where(x => x.GetType() == typeof(T) && x.DataNode == identity.DataNode && x.AmountType == identity.AmountType && x.AccidentYear == identity.AccidentYear);", + "\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", @@ -170,7 +171,7 @@ "\n public ImportArgs args {get;}", "\n", "\n public string pillar {get; set; }", - "\n public Cashflow[] CashflowsByIdentity { get; set; }", + "\n public Cashflow[] CashflowDefinitions { get; set; }", "\n public CashflowIdentity[] Identities {get; set;}", "\n public Dictionary> AmountTypesByPillar {get; set;}", "\n ", @@ -208,7 +209,7 @@ "\n public ImportArgs args {get;}", "\n", "\n public string pillar {get; set; }", - "\n public Cashflow[] CashflowsByIdentity { get; set; }", + "\n public Cashflow[] CashflowDefinitions { get; set; }", "\n public CashflowIdentity[] Identities {get; set;}", "\n public Dictionary> AmountTypesByPillar {get; set;}", "\n ", diff --git a/fpna/Utils/Queries.ipynb b/fpna/Utils/Queries.ipynb index e4cecdd3..8a486d9c 100644 --- a/fpna/Utils/Queries.ipynb +++ b/fpna/Utils/Queries.ipynb @@ -71,7 +71,7 @@ "\n", "\n //var variablesFromDataSource = await dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar);", "\n", - "\n return variablesFromWorkspace.Union(dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar), EqualityComparer.Instance).ToArray();", + "\n return variablesFromWorkspace.Union(await dataSource.LoadPartitionedDataAsync(defaultPartition, x => x.Pillar == pillar), EqualityComparer.Instance).ToArray();", "\n}" ], "metadata": {}, diff --git a/ifrs17/Test/QueriesScenarioDataTest.ipynb b/ifrs17/Test/QueriesScenarioDataTest.ipynb index 9cf0d481..8207fb6a 100644 --- a/ifrs17/Test/QueriesScenarioDataTest.ipynb +++ b/ifrs17/Test/QueriesScenarioDataTest.ipynb @@ -51,6 +51,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 +154,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 +198,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 +242,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 +286,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 +330,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 +437,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 +481,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 +525,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 +569,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 +613,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 +716,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 +768,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, From 05231ecfa73b20dd6cc612da1db88ff03c4b6d9a Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Tue, 14 Nov 2023 16:39:54 +0100 Subject: [PATCH 14/17] add reference to template --- fpna/Constants/Ifrs17CalculationEngine.ipynb | 22 +++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/fpna/Constants/Ifrs17CalculationEngine.ipynb b/fpna/Constants/Ifrs17CalculationEngine.ipynb index cfa2868b..58d09ebf 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}\\\"\";" + "\n//var calculationEngine = $\"#!import \\\"//{projectName}/{environmentName}/{notebookName}\\\"\";", + "\nvar 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 From 3b27edbe82f090cbbd9ee2ce5b0a453458ca868b Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Wed, 15 Nov 2023 11:27:52 +0100 Subject: [PATCH 15/17] reset dataSource in ifrs17 tests --- .../Import/FpnaCloseImportTemplate.ipynb | 4 ++-- ifrs17/Test/AocConfigurationTest.ipynb | 9 +++++++++ ifrs17/Test/AocStructureTest.ipynb | 9 +++++++++ ifrs17/Test/DataNodeParameterTest.ipynb | 18 +++++++++--------- ...tentionsTest.ipynb => ExtensionsTest.ipynb} | 0 ifrs17/Test/ImportStorageTest.ipynb | 3 ++- ifrs17/Test/QueriesScenarioDataTest.ipynb | 9 +++++++++ ifrs17/Test/QueriesTest.ipynb | 9 +++++++++ ifrs17/Test/ReportStorageTest.ipynb | 12 ++++++++++++ ifrs17/Test/TechnicalMarginTest.ipynb | 10 ++++++++++ ifrs17/Test/Tests.ipynb | 2 +- 11 files changed, 72 insertions(+), 13 deletions(-) rename ifrs17/Test/{ExtentionsTest.ipynb => ExtensionsTest.ipynb} (100%) diff --git a/fpna-template/Import/FpnaCloseImportTemplate.ipynb b/fpna-template/Import/FpnaCloseImportTemplate.ipynb index a3322b1c..78339755 100644 --- a/fpna-template/Import/FpnaCloseImportTemplate.ipynb +++ b/fpna-template/Import/FpnaCloseImportTemplate.ipynb @@ -127,7 +127,7 @@ { "cell_type": "code", "source": [ - "storage.CashflowsByIdentity" + "storage.CashflowDefinitions" ], "metadata": {}, "execution_count": 0, @@ -232,7 +232,7 @@ "source": [ "var solvencystorage = new SolvencyStorage(args, DataSource, Workspace);", "\nawait solvencystorage.InitializeAsync();", - "\nsolvencystorage.CashflowsByIdentity" + "\nsolvencystorage.CashflowDefinitions" ], "metadata": {}, "execution_count": 0, 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 8207fb6a..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": [ 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, From a7f2e2151d56492b2f005d7e3b68154cf739da9f Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Wed, 15 Nov 2023 12:37:22 +0100 Subject: [PATCH 16/17] data model and init --- .../InitSystemorphToMemory.ipynb | 23 +++++++++++++------ fpna/DataModel/DataStructure.ipynb | 17 ++++++++++---- fpna/Import/Importers.ipynb | 15 ++++++++---- ifrs17/Import/Importers.ipynb | 9 +++++--- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/fpna-template/Initialization/InitSystemorphToMemory.ipynb b/fpna-template/Initialization/InitSystemorphToMemory.ipynb index 33f6b253..72f581b4 100644 --- a/fpna-template/Initialization/InitSystemorphToMemory.ipynb +++ b/fpna-template/Initialization/InitSystemorphToMemory.ipynb @@ -47,8 +47,8 @@ { "cell_type": "code", "source": [ - "// await DataSource.SetAsync();", - "\n// DataSource.Reset(x => x.ResetCurrentPartitions());" + "await DataSource.SetAsync();", + "\nDataSource.Reset(x => x.ResetCurrentPartitions());" ], "metadata": {}, "execution_count": 0, @@ -66,10 +66,19 @@ { "cell_type": "code", "source": [ - "var ifrs17templateInit = $\"#!import \\\"//{projectTemplateName}/{environmentTemplateName}/Initialization/InitSystemorphBaseToMemory\\\"\";", - "\nifrs17templateInit = $\"#!import \\\"../../{projectTemplateName}/Initialization/InitSystemorphBaseToMemory\\\"\";", - "\n", - "\n#!eval ifrs17templateInit" + "// 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, @@ -135,7 +144,7 @@ "cell_type": "code", "source": [ "// EarningsAmountType overlaps with the IFRS17 => clean up the data", - "\n//await Import.FromString(earningsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", + "\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(solvencyBsAmountTypes).WithType().WithTarget(DataSource).ExecuteAsync();", diff --git a/fpna/DataModel/DataStructure.ipynb b/fpna/DataModel/DataStructure.ipynb index 6c3ea0ed..d570da03 100644 --- a/fpna/DataModel/DataStructure.ipynb +++ b/fpna/DataModel/DataStructure.ipynb @@ -70,6 +70,8 @@ "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; }", @@ -182,9 +184,6 @@ "\n [IdentityProperty][NoArithmetics(ArithmeticOperation.Scale)][Dimension(typeof(int), nameof(AccidentYear))]", "\n public int? AccidentYear { get; init; }", "\n", - "\n [IdentityProperty][Dimension(typeof(Shape), nameof(Shape))] // TODO find a better name", - "\n public Shape Shape { get; init; }", - "\n", "\n [IdentityProperty]//[Dimension(typeof(Pillar))] ", "\n public string Pillar { get; init; }", "\n", @@ -210,6 +209,8 @@ "source": [ "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; }", @@ -233,6 +234,8 @@ "source": [ "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; }", @@ -256,8 +259,14 @@ { "cell_type": "code", "source": [ - "public record ReferencedCashflow : Cashflow, IReferenced", + "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; }", diff --git a/fpna/Import/Importers.ipynb b/fpna/Import/Importers.ipynb index 63a8cf39..ba6089f9 100644 --- a/fpna/Import/Importers.ipynb +++ b/fpna/Import/Importers.ipynb @@ -107,7 +107,7 @@ "\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(Cashflow.Shape)), out Shape shape)) { ApplicationMessage.Log(Error.Generic, \"Wrong Shape - Add error message\"); return null; };", + "\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)),", @@ -118,7 +118,7 @@ "\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, //ParseAndValidateIntProperty", + "\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;", @@ -133,7 +133,7 @@ "\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(Cashflow.Shape)), out Shape shape)) { ApplicationMessage.Log(Error.Generic, \"Wrong Shape - Add error message\"); return null; };", + "\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)),", @@ -142,7 +142,9 @@ "\n Pillar = pillar,", "\n Partition = TargetPartitionByReportingNodeAndPeriod.Id,", "\n Values = values,", - "\n InitialValue = datarow.Field(nameof(BoundedCashflow.InitialValue)).CheckStringForExponentialAndConvertToDouble(),", + "\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 };", @@ -158,7 +160,7 @@ "\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(Cashflow.Shape)), out Shape shape)) { ApplicationMessage.Log(Error.Generic, \"Wrong Shape - Add error message\"); return null; };", + "\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)),", @@ -167,6 +169,9 @@ "\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;", diff --git a/ifrs17/Import/Importers.ipynb b/ifrs17/Import/Importers.ipynb index a496ffae..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();", From 9ccada36a2384a171851a4d9b9edf3037f083382 Mon Sep 17 00:00:00 2001 From: Davide Colleoni Date: Wed, 15 Nov 2023 12:49:52 +0100 Subject: [PATCH 17/17] reset link to ifrs17ce --- fpna/Constants/Ifrs17CalculationEngine.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fpna/Constants/Ifrs17CalculationEngine.ipynb b/fpna/Constants/Ifrs17CalculationEngine.ipynb index 58d09ebf..b23fe482 100644 --- a/fpna/Constants/Ifrs17CalculationEngine.ipynb +++ b/fpna/Constants/Ifrs17CalculationEngine.ipynb @@ -31,8 +31,8 @@ "var projectName = \"ifrs17\";", "\nvar environmentName = \"dev\";", "\nvar notebookName = \"CalculationEngine\";", - "\n//var calculationEngine = $\"#!import \\\"//{projectName}/{environmentName}/{notebookName}\\\"\";", - "\nvar calculationEngine = $\"#!import \\\"../../{projectName}/{notebookName}\\\"\";" + "\nvar calculationEngine = $\"#!import \\\"//{projectName}/{environmentName}/{notebookName}\\\"\";", + "\n//var calculationEngine = $\"#!import \\\"../../{projectName}/{notebookName}\\\"\";" ], "metadata": {}, "execution_count": 0,