@@ -22,126 +22,6 @@ def mean_func(consumption, dims):
2222 return consumption .mean (dims )
2323
2424
25- def constant_equivalent_discount_rate (scc , marginal_damages ):
26- """
27- Calculate the constant equivalent discount rates given a series of damages
28- and the present discounted value (the SCC)
29- by solving for the r in this formula:
30- PDV = undiscountedcost (0) + undiscountedcost (1) / (1+r) + undiscountedcost (2) / (1+r)^2 …
31-
32- Parameters
33- ----------
34- scc: float
35- the PDV in the equation above
36-
37- marginal_damages: 1d array
38- the undiscountedcosts in the equation above
39-
40- Returns
41- -------
42- r: float
43- the r in the equation above
44-
45- """
46- # the marginal damages will be the coefficients
47- coeffs = marginal_damages .copy ()
48- # calculate the constant term
49- coeffs [0 ] = coeffs [0 ] - scc
50- p = np .polynomial .Polynomial (coeffs )
51- # get the roots
52- roots = p .roots ()
53- # get rid of the complex roots
54- real_roots = roots .real [abs (roots .imag ) < 1e-5 ]
55- # get rid of negative roots
56- positive_real_root = real_roots [real_roots > 0 ]
57- # if we're only left with one root we're good to go!
58- assert len (positive_real_root ) == 1
59- # root = 1/(1+r), calculate r by inversing it
60- r = 1 / positive_real_root - 1
61- return r
62-
63-
64- def calculate_constant_equivalent_discount_rate (
65- folder ,
66- recipe ,
67- disc ,
68- sel_dict ,
69- eta ,
70- rho ,
71- mean_dims = ("simulation" ,),
72- uncollapsed = False ,
73- ):
74- """
75- calls constant_equivalent_discount_rate on a set of marginal damages and sccs
76-
77- Parameters
78- ----------
79- folder: str
80- root directory to the menu results
81-
82- recipe: str
83- one of "adding_up", "risk_aversion", "equity"
84-
85- disc: str
86- discount type of the scc and marginal damages that we want to derive the
87- constant equivalent discount rate for
88-
89- sel_dict : dict
90- dictionary of subsetting options
91-
92- uncollapsed : boolean
93- if True, will use `mean` marginal damages combined with `uncollapsed_sccs` file
94-
95- Returns
96- -------
97-
98- """
99- # read files
100- if not uncollapsed :
101- sccs = xr .open_dataset (
102- f"{ folder } /{ recipe } _{ disc } _eta{ eta } _rho{ rho } _scc.nc4"
103- ).sel (sel_dict )
104- marginal_damages = xr .open_dataset (
105- f"{ folder } /{ recipe } _{ disc } _eta{ eta } _rho{ rho } _marginal_damages.nc4"
106- ).sel (sel_dict )
107- else :
108- sccs = (
109- xr .open_dataset (
110- f"{ folder } /{ recipe } _{ disc } _eta{ eta } _rho{ rho } _uncollapsed_sccs.nc4"
111- )
112- .sel (sel_dict )
113- .mean (mean_dims )
114- )
115- marginal_damages = (
116- xr .open_zarr (
117- f"{ folder } /{ recipe } _{ disc } _eta{ eta } _rho{ rho } _uncollapsed_marginal_damages.zarr"
118- )
119- .load ()
120- .expand_dims ({"fair_aggregation" : ["uncollapsed" ]})
121- .sel (sel_dict )
122- .mean (mean_dims )
123- )
124-
125- # do stuff to the datasets so that they can be passed as parameters to the apply_ufunc
126- sccs = (
127- sccs .to_array ().rename ("constant_discrate" ).to_dataset ().squeeze (dim = "variable" )
128- )
129- marginal_damages = (
130- marginal_damages .to_array ()
131- .rename ("constant_discrate" )
132- .to_dataset ()
133- .squeeze (dim = "variable" )
134- )
135- constant_discount_rates = xr .apply_ufunc (
136- constant_equivalent_discount_rate ,
137- sccs ,
138- marginal_damages ,
139- input_core_dims = [[], ["year" ]],
140- vectorize = True ,
141- )
142- return constant_discount_rates
143-
144-
14525def get_model_weights (rcp ):
14626 # clean weights
14727 WEIGHT_FILE = (
0 commit comments