Skip to content

Commit 8dae2fe

Browse files
authored
Merge pull request #405 from ClimateImpactLab/quantreg_sccs
Add quantreg full cloud of points across batches
2 parents 3b30261 + 901feab commit 8dae2fe

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/dscim/menu/main_recipe.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MainRecipe(StackedDamages, ABC):
2626
Parameters
2727
----------
2828
discounting_type : str
29-
Choice of discounting: ``euler_gwr``, ``euler_ramsey``, ``constant``, ``naive_ramsey``,
29+
Choice of discounting: ``euler_gwr``, ``euler_ramsey``, ``constant``, ``constant_gwr``, ``naive_ramsey``,
3030
``naive_gwr``, ``gwr_gwr``.
3131
discrete_discounting: boolean
3232
Discounting is discrete if ``True``, else continuous (default is ``False``).
@@ -52,6 +52,7 @@ class MainRecipe(StackedDamages, ABC):
5252
DISCOUNT_TYPES = [
5353
"constant",
5454
"constant_model_collapsed",
55+
"constant_gwr",
5556
"naive_ramsey",
5657
"euler_ramsey",
5758
"naive_gwr",
@@ -253,7 +254,11 @@ def __init__(
253254
# 'constant_model_collapsed' should be here except that we allow
254255
# for a collapsed-model Ramsey rate to be calculated (for labour
255256
# and energy purposes)
256-
if self.discounting_type in ["constant", "constant_model_collapsed"]:
257+
if self.discounting_type in [
258+
"constant",
259+
"constant_model_collapsed",
260+
"constant_gwr",
261+
]:
257262
self.stream_discount_factors = None
258263

259264
# assert formulas for which clip_gmsl is implemented
@@ -318,8 +323,10 @@ def scc():
318323
self.logger.info("Processing SCC calculation ...")
319324
if self.fit_type == "quantreg":
320325
self.full_uncertainty_iqr
321-
self.calculate_scc
322-
self.stat_uncertainty_iqr
326+
# stat_uncertainty_iqr function expects collapsed SCCs, so a fair aggregation is required
327+
if len(self.fair_aggregation) > 0:
328+
self.calculate_scc
329+
self.stat_uncertainty_iqr
323330
else:
324331
if len(self.fair_aggregation) > 0:
325332
self.stream_discount_factors
@@ -1093,7 +1100,7 @@ def discounted_damages(self, damages, discrate):
10931100
xr.Dataset
10941101
"""
10951102

1096-
if discrate in ["constant", "constant_model_collapsed"]:
1103+
if discrate in ["constant", "constant_model_collapsed", "constant_gwr"]:
10971104
if self.discrete_discounting:
10981105
discrate_damages = [
10991106
damages * (1 / (1 + r)) ** (damages.year - self.climate.pulse_year)

src/dscim/preprocessing/preprocessing.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def ce_from_chunk(
2121
zero,
2222
socioec,
2323
ce_batch_coords,
24+
quantreg=False,
2425
):
2526
year = chunk.year.values
2627
ssp = chunk.ssp.values
@@ -44,13 +45,19 @@ def ce_from_chunk(
4445
raise NotImplementedError("Pass 'cc' or 'no_cc' to reduction.")
4546

4647
if recipe == "adding_up":
47-
result = mean_func(
48-
np.maximum(
48+
if not quantreg:
49+
result = mean_func(
50+
np.maximum(
51+
calculation,
52+
bottom_code,
53+
),
54+
"batch",
55+
)
56+
else:
57+
result = np.maximum(
4958
calculation,
5059
bottom_code,
51-
),
52-
"batch",
53-
)
60+
)
5461
elif recipe == "risk_aversion":
5562
result = ce_func(
5663
np.maximum(
@@ -73,13 +80,17 @@ def reduce_damages(
7380
socioec,
7481
bottom_coding_gdppc=39.39265060424805,
7582
zero=False,
83+
quantreg=False,
7684
):
7785
if recipe == "adding_up":
7886
assert (
7987
eta is None
8088
), "Adding up does not take an eta argument. Please set to None."
8189
# client = Client(n_workers=35, memory_limit="9G", threads_per_worker=1)
8290

91+
if recipe == "risk_aversion":
92+
assert not quantreg, "Quantile regression is not compatible with risk aversion. Please set quantreg to False."
93+
8394
with open(config) as stream:
8495
c = yaml.safe_load(stream)
8596
params = c["sectors"][sector]
@@ -112,10 +123,15 @@ def reduce_damages(
112123
"model": 1,
113124
"ssp": 1,
114125
}
115-
116-
ce_batch_dims = [i for i in gdppc.dims] + [
117-
i for i in ds.dims if i not in gdppc.dims and i != "batch"
118-
]
126+
if quantreg:
127+
chunkies["batch"] = 1
128+
ce_batch_dims = [i for i in gdppc.dims] + [
129+
i for i in ds.dims if i not in gdppc.dims
130+
]
131+
else:
132+
ce_batch_dims = [i for i in gdppc.dims] + [
133+
i for i in ds.dims if i not in gdppc.dims and i != "batch"
134+
]
119135
ce_batch_coords = {c: ds[c].values for c in ce_batch_dims}
120136
ce_batch_coords["region"] = [
121137
i for i in gdppc.region.values if i in ce_batch_coords["region"]
@@ -143,6 +159,7 @@ def reduce_damages(
143159
zero=zero,
144160
socioec=socioec,
145161
ce_batch_coords=ce_batch_coords,
162+
quantreg=quantreg,
146163
),
147164
template=template,
148165
)

0 commit comments

Comments
 (0)