From e84f1b99f5b2f12f824086dfab96b860d59cc1ea Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Mon, 9 Sep 2024 13:14:02 -0500 Subject: [PATCH 1/6] Add constant_gwr option --- src/dscim/menu/main_recipe.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/dscim/menu/main_recipe.py b/src/dscim/menu/main_recipe.py index 0ab531d7..564010dc 100644 --- a/src/dscim/menu/main_recipe.py +++ b/src/dscim/menu/main_recipe.py @@ -26,7 +26,7 @@ class MainRecipe(StackedDamages, ABC): Parameters ---------- discounting_type : str - Choice of discounting: ``euler_gwr``, ``euler_ramsey``, ``constant``, ``naive_ramsey``, + Choice of discounting: ``euler_gwr``, ``euler_ramsey``, ``constant``, ``constant_gwr``, ``naive_ramsey``, ``naive_gwr``, ``gwr_gwr``. discrete_discounting: boolean Discounting is discrete if ``True``, else continuous (default is ``False``). @@ -52,6 +52,7 @@ class MainRecipe(StackedDamages, ABC): DISCOUNT_TYPES = [ "constant", "constant_model_collapsed", + "constant_gwr", "naive_ramsey", "euler_ramsey", "naive_gwr", @@ -253,7 +254,7 @@ def __init__( # 'constant_model_collapsed' should be here except that we allow # for a collapsed-model Ramsey rate to be calculated (for labour # and energy purposes) - if self.discounting_type in ["constant", "constant_model_collapsed"]: + if self.discounting_type in ["constant", "constant_model_collapsed", "constant_gwr"]: self.stream_discount_factors = None # assert formulas for which clip_gmsl is implemented @@ -1093,7 +1094,7 @@ def discounted_damages(self, damages, discrate): xr.Dataset """ - if discrate in ["constant", "constant_model_collapsed"]: + if discrate in ["constant", "constant_model_collapsed", "constant_gwr"]: if self.discrete_discounting: discrate_damages = [ damages * (1 / (1 + r)) ** (damages.year - self.climate.pulse_year) From 6557cc55f592c0cd67840eb1fd28eec9590c1489 Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Tue, 17 Sep 2024 10:11:19 -0500 Subject: [PATCH 2/6] add quantreg option to reduce damages --- src/dscim/menu/main_recipe.py | 5 ++-- src/dscim/preprocessing/preprocessing.py | 36 ++++++++++++++++-------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/dscim/menu/main_recipe.py b/src/dscim/menu/main_recipe.py index 564010dc..97af7176 100644 --- a/src/dscim/menu/main_recipe.py +++ b/src/dscim/menu/main_recipe.py @@ -319,8 +319,9 @@ def scc(): self.logger.info("Processing SCC calculation ...") if self.fit_type == "quantreg": self.full_uncertainty_iqr - self.calculate_scc - self.stat_uncertainty_iqr + if len(self.fair_aggregation) > 0: + self.calculate_scc + self.stat_uncertainty_iqr else: if len(self.fair_aggregation) > 0: self.stream_discount_factors diff --git a/src/dscim/preprocessing/preprocessing.py b/src/dscim/preprocessing/preprocessing.py index 8ac0e10f..db812621 100644 --- a/src/dscim/preprocessing/preprocessing.py +++ b/src/dscim/preprocessing/preprocessing.py @@ -21,6 +21,7 @@ def ce_from_chunk( zero, socioec, ce_batch_coords, + quantreg, ): year = chunk.year.values ssp = chunk.ssp.values @@ -44,13 +45,19 @@ def ce_from_chunk( raise NotImplementedError("Pass 'cc' or 'no_cc' to reduction.") if recipe == "adding_up": - result = mean_func( - np.maximum( - calculation, - bottom_code, - ), - "batch", - ) + if not quantreg: + result = mean_func( + np.maximum( + calculation, + bottom_code, + ), + "batch", + ) + else: + result = np.maximum( + calculation, + bottom_code, + ) elif recipe == "risk_aversion": result = ce_func( np.maximum( @@ -73,6 +80,7 @@ def reduce_damages( socioec, bottom_coding_gdppc=39.39265060424805, zero=False, + quantreg=False, ): if recipe == "adding_up": assert ( @@ -112,10 +120,15 @@ def reduce_damages( "model": 1, "ssp": 1, } - - ce_batch_dims = [i for i in gdppc.dims] + [ - i for i in ds.dims if i not in gdppc.dims and i != "batch" - ] + if quantreg: + chunkies['batch'] = 1 + ce_batch_dims = [i for i in gdppc.dims] + [ + i for i in ds.dims if i not in gdppc.dims + ] + else: + ce_batch_dims = [i for i in gdppc.dims] + [ + i for i in ds.dims if i not in gdppc.dims and i != "batch" + ] ce_batch_coords = {c: ds[c].values for c in ce_batch_dims} ce_batch_coords["region"] = [ i for i in gdppc.region.values if i in ce_batch_coords["region"] @@ -143,6 +156,7 @@ def reduce_damages( zero=zero, socioec=socioec, ce_batch_coords=ce_batch_coords, + quantreg=quantreg, ), template=template, ) From b7165dbb6eed1afbce45aeaabe14f1b032b1b915 Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Fri, 30 May 2025 15:12:33 -0500 Subject: [PATCH 3/6] ruff --- src/dscim/menu/main_recipe.py | 6 +++++- src/dscim/preprocessing/preprocessing.py | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/dscim/menu/main_recipe.py b/src/dscim/menu/main_recipe.py index 97af7176..c77ba879 100644 --- a/src/dscim/menu/main_recipe.py +++ b/src/dscim/menu/main_recipe.py @@ -254,7 +254,11 @@ def __init__( # 'constant_model_collapsed' should be here except that we allow # for a collapsed-model Ramsey rate to be calculated (for labour # and energy purposes) - if self.discounting_type in ["constant", "constant_model_collapsed", "constant_gwr"]: + if self.discounting_type in [ + "constant", + "constant_model_collapsed", + "constant_gwr", + ]: self.stream_discount_factors = None # assert formulas for which clip_gmsl is implemented diff --git a/src/dscim/preprocessing/preprocessing.py b/src/dscim/preprocessing/preprocessing.py index db812621..e9e0bb6a 100644 --- a/src/dscim/preprocessing/preprocessing.py +++ b/src/dscim/preprocessing/preprocessing.py @@ -55,8 +55,8 @@ def ce_from_chunk( ) else: result = np.maximum( - calculation, - bottom_code, + calculation, + bottom_code, ) elif recipe == "risk_aversion": result = ce_func( @@ -121,7 +121,7 @@ def reduce_damages( "ssp": 1, } if quantreg: - chunkies['batch'] = 1 + chunkies["batch"] = 1 ce_batch_dims = [i for i in gdppc.dims] + [ i for i in ds.dims if i not in gdppc.dims ] From 76975611fc945e19c2b3269a9eee0e8a93daa742 Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Fri, 30 May 2025 15:23:48 -0500 Subject: [PATCH 4/6] Add quantreg default --- src/dscim/preprocessing/preprocessing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dscim/preprocessing/preprocessing.py b/src/dscim/preprocessing/preprocessing.py index e9e0bb6a..41b9aef1 100644 --- a/src/dscim/preprocessing/preprocessing.py +++ b/src/dscim/preprocessing/preprocessing.py @@ -21,7 +21,7 @@ def ce_from_chunk( zero, socioec, ce_batch_coords, - quantreg, + quantreg=False, ): year = chunk.year.values ssp = chunk.ssp.values From aaa79c3b2efb1bdabfd838a63608fd88d0ca5044 Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Tue, 17 Jun 2025 11:55:54 -0400 Subject: [PATCH 5/6] Throw error for uncompatible specs --- src/dscim/preprocessing/preprocessing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dscim/preprocessing/preprocessing.py b/src/dscim/preprocessing/preprocessing.py index 41b9aef1..53a56b3b 100644 --- a/src/dscim/preprocessing/preprocessing.py +++ b/src/dscim/preprocessing/preprocessing.py @@ -88,6 +88,9 @@ def reduce_damages( ), "Adding up does not take an eta argument. Please set to None." # client = Client(n_workers=35, memory_limit="9G", threads_per_worker=1) + if recipe == "risk_aversion": + assert not quantreg, "Quantile regression is not compatible with risk aversion. Please set quantreg to False." + with open(config) as stream: c = yaml.safe_load(stream) params = c["sectors"][sector] From 901feab04215f68ea8bb96973c903061a1da2c7e Mon Sep 17 00:00:00 2001 From: Kelly McCusker Date: Fri, 20 Jun 2025 22:14:48 -0700 Subject: [PATCH 6/6] Add comment about the need for checking if fair_aggregation is set --- src/dscim/menu/main_recipe.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dscim/menu/main_recipe.py b/src/dscim/menu/main_recipe.py index c77ba879..483c431f 100644 --- a/src/dscim/menu/main_recipe.py +++ b/src/dscim/menu/main_recipe.py @@ -323,6 +323,7 @@ def scc(): self.logger.info("Processing SCC calculation ...") if self.fit_type == "quantreg": self.full_uncertainty_iqr + # stat_uncertainty_iqr function expects collapsed SCCs, so a fair aggregation is required if len(self.fair_aggregation) > 0: self.calculate_scc self.stat_uncertainty_iqr