Skip to content

Commit

Permalink
Reporting scenarios (#202)
Browse files Browse the repository at this point in the history
* version 1 with everything

* much better performance

* Including filters to cache datacubes efficiently

* implement Daniel feedback

---------

Co-authored-by: Andrea Muolo <[email protected]>
  • Loading branch information
amuolo and Andrea Muolo authored Feb 9, 2023
1 parent 6443143 commit 0adf954
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 70 deletions.
3 changes: 1 addition & 2 deletions ifrs17-template/Test/Data/ExportReportVariables.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@
"\n ((2020, 12), \"CH\", null, CurrencyType.Group), ",
"\n ((2021, 3), \"CH\", null, CurrencyType.Contractual),",
"\n ((2021, 3), \"CH\", null, CurrencyType.Functional),",
"\n ((2021, 3), \"CH\", null, CurrencyType.Group),",
"\n ((2020, 12), \"CH\", nameof(Scenarios.MTUP10pct), CurrencyType.Contractual)",
"\n ((2021, 3), \"CH\", null, CurrencyType.Group)",
"\n};"
],
"metadata": {},
Expand Down
26 changes: 0 additions & 26 deletions ifrs17/Constants/Consts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -361,32 +361,6 @@
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"source": [
"public static class Scenarios{",
"\n public const string YCUP1pct = nameof(YCUP1pct); // Yield Curve Up 1.0pct",
"\n public const string YCDW1pct = nameof(YCDW1pct); // Yield Curve Down 1.0pct",
"\n public const string SRUP1pct = nameof(SRUP1pct); // Spread Rate Up 1.0pct",
"\n public const string SRDW1pct = nameof(SRDW1pct); // Spread Rate Down 1.0pct",
"\n public const string EUP1pct = nameof(EUP1pct); // Equity Up 1.0pct",
"\n public const string EDW1pct = nameof(EDW1pct); // Equity Down 1.0pct",
"\n public const string FXUP1pct = nameof(FXUP1pct); // Exchange Rate Up 1.0pct",
"\n public const string FXDW1pct = nameof(FXDW1pct); // Exchange Rate Down 1.0pct",
"\n public const string MTUP10pct = nameof(MTUP10pct); // Mortality Up 10pct",
"\n public const string MTDW10pct = nameof(MTDW10pct); // Mortality Down 10pct",
"\n public const string LUP10pct = nameof(LUP10pct); // Longevity Up 10pct",
"\n public const string LDW10pct = nameof(LDW10pct); // Longevity Down 10pct",
"\n public const string DUP10pct = nameof(DUP10pct); // Disability Up 10pct",
"\n public const string DDW10pct = nameof(DDW10pct); // Disability Down 10pct",
"\n public const string LICUP10pct = nameof(LICUP10pct); // Lic Up 10pct",
"\n public const string LICDW10pct = nameof(LICDW10pct); // Lic Down 10pct",
"\n}"
],
"metadata": {},
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
Expand Down
10 changes: 5 additions & 5 deletions ifrs17/Report/ReportConfigurationAndUtils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@
"cell_type": "code",
"source": [
"public static async Task<ICollection<ReportVariable>> QueryReportVariablesAsync(this IWorkspace workspace, (int Year, int Month, string ReportingNode, string Scenario) args ) {",
"\n ReportVariable[] reportVariables = new ReportVariable[0];",
"\n if(args.Scenario != null) ",
"\n reportVariables = (await workspace.QueryReportVariablesSingleScenarioAsync((args.Year, args.Month, args.ReportingNode, null)));",
"\n return (await workspace.QueryReportVariablesSingleScenarioAsync((args.Year, args.Month, args.ReportingNode, args.Scenario)))",
"\n .Union(reportVariables.Select(x => x with {Scenario = args.Scenario}), EqualityComparer<ReportVariable>.Instance).ToArray();",
"\n var bestEstimate = (await workspace.QueryReportVariablesSingleScenarioAsync((args.Year, args.Month, args.ReportingNode, null)));",
"\n return (args.Scenario == null) ",
"\n ? bestEstimate",
"\n : (await workspace.QueryReportVariablesSingleScenarioAsync((args.Year, args.Month, args.ReportingNode, args.Scenario)))",
"\n .Union(bestEstimate.Select(x => x with {Scenario = args.Scenario}), EqualityComparer<ReportVariable>.Instance).ToArray();",
"\n}"
],
"metadata": {},
Expand Down
41 changes: 30 additions & 11 deletions ifrs17/Report/ReportMutableScopes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
"\n int Year => ReportingPeriod.Year;",
"\n int Month => ReportingPeriod.Month;",
"\n string ReportingNode { get; set; }",
"\n string Scenario { get; set; }",
"\n string Scenario { get; set; } // TODO: Enable dropdown selection including All and Delta",
"\n string Comparison { get; set; } // TODO: only for scenario at the beginning, meant to enable general purpose comparisons ",
"\n CurrencyType CurrencyType { get; set; }",
"\n ",
"\n ((int Year, int Month) ReportingPeriod, string ReportingNode, string Scenario, CurrencyType) ShowSettings => (ReportingPeriod, ReportingNode, Scenario, CurrencyType);",
Expand All @@ -124,8 +125,15 @@
"\n protected string[] rowSlices => RowSlices is null ? defaultRowSlices : defaultRowSlices.Concat(RowSlices).Where(x => !forbiddenSlices.Contains(x)).ToArray();",
"\n",
"\n IEnumerable<string> ColumnSlices { get; set; }",
"\n protected string[] defaultColumnSlices => new string[] { };",
"\n protected string[] columnSlices => ColumnSlices is null ? defaultColumnSlices : defaultColumnSlices.Where(cs => !ColumnSlices.Contains(cs)).Concat(ColumnSlices).Where(x => !forbiddenSlices.Contains(x)).ToArray();",
"\n protected string[] defaultColumnSlices => new string[] { };",
"\n protected string[] columnSlices { get{",
"\n var slices = ColumnSlices is null ? defaultColumnSlices : defaultColumnSlices.Where(cs => !ColumnSlices.Contains(cs)).Concat(ColumnSlices).Where(x => !forbiddenSlices.Contains(x)).ToArray();",
"\n return Scenario == \"All\" || Scenario == \"Delta\"",
"\n ? slices.Concat(nameof(Scenario).RepeatOnce()).ToArray() ",
"\n : Scenario is null ? slices : nameof(Scenario).RepeatOnce().Concat(slices).ToArray();",
"\n }}",
"\n",
"\n // Identities",
"\n protected HashSet<(ReportIdentity, CurrencyType)> GetIdentities() => GetStorage().GetIdentities(ReportingPeriod, ReportingNode, Scenario, CurrencyType);",
"\n ",
"\n // Filter",
Expand All @@ -135,27 +143,26 @@
"\n // Scope Initialization",
"\n async Task InitAsync() {",
"\n var mostRecentPartition = (await workspace.Query<PartitionByReportingNodeAndPeriod>().Where(x => x.Scenario == null).OrderBy(x => x.Year).ThenBy(x => x.Month).ToArrayAsync()).Last(); ",
"\n ",
"\n ReportingPeriod = (mostRecentPartition.Year, mostRecentPartition.Month);",
"\n ReportingNode = (await workspace.Query<ReportingNode>().Where(x => x.Parent == null).ToArrayAsync()).First().SystemName; // TODO: change once user permissions are available",
"\n Scenario = null;",
"\n CurrencyType = CurrencyType.Contractual;",
"\n await GetStorage().InitializeReportIndependentCacheAsync();",
"\n }",
"\n } ",
"\n",
"\n //Report",
"\n public IDataCube<ReportVariable> GetDataCube() => default;",
"\n protected int headerColumnWidthValue => 250;",
"\n public Task<GridOptions> ToReportAsync => GetReportTaskAsync();",
"\n private async Task<GridOptions> GetReportTaskAsync() {",
"\n await GetStorage().InitializeAsync(ReportingPeriod, ReportingNode, Scenario, CurrencyType);",
"\n ",
"\n return await report.ForDataCube(GetDataCube())",
"\n await GetStorage().InitializeAsync(ReportingPeriod, ReportingNode, Scenario, CurrencyType); ",
"\n return await report.ForDataCube(GetScope<Data>((ReportingPeriod, ReportingNode, Scenario, CurrencyType, Identity, dataFilter)).Cube)",
"\n .WithQuerySource(workspace)",
"\n .SliceRowsBy(rowSlices)",
"\n .SliceColumnsBy(columnSlices)",
"\n .ReportGridOptions(headerColumnWidth: headerColumnWidthValue)",
"\n .ExecuteAsync();",
"\n }",
"\n",
"\n Task<GridOptions> ToReportAsync => GetReportTaskAsync();",
"\n}",
"\n",
"\npublic interface PvReport : IIfrs17Report {",
Expand Down Expand Up @@ -279,7 +286,19 @@
"\n IDataCube<ReportVariable> IIfrs17Report.GetDataCube() => DataFilter == null ",
"\n ? GetScopes<FinancialPerformance>(GetIdentities()).Aggregate().FinancialPerformance",
"\n : GetScopes<FinancialPerformance>(GetIdentities()).Aggregate().FinancialPerformance.Filter(dataFilter);",
"\n}"
"\n}",
"\n",
"\npublic interface Data : IMutableScope<((int year, int month) period, string reportingNode, string scenario, CurrencyType currencyType, ",
"\n string reportType, (string filterName, object filterValue)[] dataFilter)> ",
"\n{",
"\n IDataCube<ReportVariable> Cube { get{",
"\n var dataCube = GetScope<IIfrs17Report>(Identity.reportType).GetDataCube();",
"\n // TODO: suggestion to place the filter here instead of having it in every applicability scope",
"\n if(Identity.scenario != \"Delta\") return dataCube;",
"\n var bestEstimateById = dataCube.Where(x => x.Scenario == null).ToDictionary(x => x.ToIdentityString());",
"\n return dataCube.Select(x => x.Scenario == null ? x : x with { Value = x.Value - (bestEstimateById.TryGetValue((x with {Scenario = null}).ToIdentityString(), out var be)? be.Value : 0.0) }).ToDataCube();",
"\n }}",
"\n} "
],
"metadata": {},
"execution_count": 0,
Expand Down
Loading

0 comments on commit 0adf954

Please sign in to comment.