Skip to content

Commit e06a3e7

Browse files
committed
doc(risk_traj): adds some docstrings
1 parent 20befdf commit e06a3e7

File tree

1 file changed

+145
-11
lines changed

1 file changed

+145
-11
lines changed

climada/trajectories/risk_trajectory.py

Lines changed: 145 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ def _reset_metrics(self):
106106

107107
@property
108108
def default_rp(self):
109+
"""The default return period values to use when computing risk period metrics.
110+
111+
Notes
112+
-----
113+
114+
Changing its value resets the corresponding metric.
115+
"""
109116
return self._default_rp
110117

111118
@default_rp.setter
@@ -120,7 +127,14 @@ def default_rp(self, value):
120127

121128
@property
122129
def risk_transf_cover(self):
123-
"""The risk transfer coverage."""
130+
"""The risk transfer coverage.
131+
132+
Notes
133+
-----
134+
135+
Changing its value resets the risk metrics.
136+
"""
137+
124138
return self._risk_transf_cover
125139

126140
@risk_transf_cover.setter
@@ -131,7 +145,14 @@ def risk_transf_cover(self, value):
131145

132146
@property
133147
def risk_transf_attach(self):
134-
"""The risk transfer attachment."""
148+
"""The risk transfer attachment.
149+
150+
Notes
151+
-----
152+
153+
Changing its value resets the risk metrics.
154+
"""
155+
135156
return self._risk_transf_attach
136157

137158
@risk_transf_attach.setter
@@ -190,11 +211,27 @@ def pairwise(container: list):
190211
]
191212

192213
@classmethod
193-
def npv_transform(cls, df: pd.DataFrame, risk_disc) -> pd.DataFrame:
214+
def npv_transform(cls, df: pd.DataFrame, risk_disc: DiscRates) -> pd.DataFrame:
215+
"""Apply discount rate to a metric `DataFrame`.
216+
217+
Parameters
218+
----------
219+
df : pd.DataFrame
220+
The `DataFrame` of the metric to discount.
221+
risk_disc : DiscRate
222+
The discount rate to apply.
223+
224+
Returns
225+
-------
226+
pd.DataFrame
227+
The discounted risk metric.
228+
229+
230+
"""
231+
194232
def _npv_group(group, disc):
195233
start_date = group.index.get_level_values("date").min()
196-
end_date = group.index.get_level_values("date").max()
197-
return calc_npv_cash_flows(group, start_date, end_date, disc)
234+
return calc_npv_cash_flows(group, start_date, disc)
198235

199236
df = df.set_index("date")
200237
grouper = cls._grouper
@@ -272,11 +309,39 @@ def _compute_metrics(
272309
return df
273310

274311
def eai_metrics(self, npv: bool = True, **kwargs):
312+
"""Return the estimatated annual impacts at each exposure point for each date.
313+
314+
This method computes and return a `GeoDataFrame` with eai metric
315+
(for each exposure point) for each date.
316+
317+
Parameters
318+
----------
319+
npv : bool
320+
Whether to apply the (risk) discount rate if it is defined.
321+
Defaults to `True`.
322+
323+
Notes
324+
-----
325+
326+
This computation may become quite expensive for big areas with high resolution.
327+
328+
"""
275329
return self._compute_metrics(
276330
npv=npv, metric_name="eai", metric_meth="calc_eai_gdf", **kwargs
277331
)
278332

279333
def aai_metrics(self, npv: bool = True, **kwargs):
334+
"""Return the average annual impacts for each date.
335+
336+
This method computes and return a `DataFrame` with aai metric for each date.
337+
338+
Parameters
339+
----------
340+
npv : bool
341+
Whether to apply the (risk) discount rate if it is defined.
342+
Defaults to `True`.
343+
"""
344+
280345
return self._compute_metrics(
281346
npv=npv, metric_name="aai", metric_meth="calc_aai_metric", **kwargs
282347
)
@@ -291,6 +356,18 @@ def return_periods_metrics(self, return_periods, npv: bool = True, **kwargs):
291356
)
292357

293358
def aai_per_group_metrics(self, npv: bool = True, **kwargs):
359+
"""Return the average annual impacts for each exposure group ID.
360+
361+
This method computes and return a `DataFrame` with aai metric for each
362+
of the exposure group defined by a group id, for each date.
363+
364+
Parameters
365+
----------
366+
npv : bool
367+
Whether to apply the (risk) discount rate if it is defined.
368+
Defaults to `True`.
369+
"""
370+
294371
return self._compute_metrics(
295372
npv=npv,
296373
metric_name="aai_per_group",
@@ -299,6 +376,24 @@ def aai_per_group_metrics(self, npv: bool = True, **kwargs):
299376
)
300377

301378
def risk_components_metrics(self, npv: bool = True, **kwargs):
379+
"""Return the "components" of change in future risk (Exposure and Hazard)
380+
381+
This method returns the components of the change in risk at each date:
382+
383+
- The base risk, i.e., the risk without change in hazard or exposure, compared to trajectory earliest date.
384+
- The "delta from exposure", i.e., the additional risks that come with change in exposure
385+
- The "delta from hazard", i.e., the additional risks that come with change in hazard
386+
387+
Due to how computations are being done the "delta from exposure" corresponds to the change of risk due to change in exposure while hazard remains constant to "baseline hazard", while "delta from hazard" corresponds to the change of risk due to change in hazard, while exposure remains constant to **future** exposure.
388+
389+
Parameters
390+
----------
391+
npv : bool
392+
Whether to apply the (risk) discount rate if it is defined.
393+
Defaults to `True`.
394+
395+
"""
396+
302397
return self._compute_metrics(
303398
npv=npv,
304399
metric_name="risk_components",
@@ -312,6 +407,30 @@ def per_date_risk_metrics(
312407
return_periods: list[int] | None = None,
313408
npv: bool = True,
314409
) -> pd.DataFrame | pd.Series:
410+
"""Returns a DataFrame of risk metrics for each dates
411+
412+
This methods collects (and if needed computes) the `metrics`
413+
(Defaulting to "aai", "return_periods" and "aai_per_group").
414+
415+
Parameters
416+
----------
417+
metrics : list[str], optional
418+
The list of metrics to return (defaults to
419+
["aai","return_periods","aai_per_group"])
420+
return_periods : list[int], optional
421+
The return periods to consider for the return periods metric
422+
(default to the value of the `.default_rp` attribute)
423+
npv : bool
424+
Whether to apply the (risk) discount rate if it was defined
425+
when instantiating the trajectory. Defaults to `True`.
426+
427+
Returns
428+
-------
429+
pd.DataFrame | pd.Series
430+
A tidy DataFrame with metrics value for all possible dates.
431+
432+
"""
433+
315434
metrics_df = []
316435
metrics = (
317436
["aai", "return_periods", "aai_per_group"] if metrics is None else metrics
@@ -578,20 +697,35 @@ def plot_waterfall(
578697
def calc_npv_cash_flows(
579698
cash_flows: pd.DataFrame,
580699
start_date: datetime.date,
581-
end_date: datetime.date | None = None,
582700
disc: DiscRates | None = None,
583701
):
584-
# If no discount rates are provided, return the cash flows as is
702+
"""Apply discount rate to cash flows
703+
704+
If it is defined, applies a discount rate `disc` to a given cash flow
705+
`cash_flows` assuming present year corresponds to `start_date`.
706+
707+
Parameters
708+
----------
709+
cash_flows : pd.DataFrame
710+
The cash flow to apply the discount rate to
711+
start_date : datetime.date
712+
The date representing the present
713+
end_date : datetime.date, optional
714+
disc : DiscRates, optional
715+
The discount rate to apply
716+
717+
Returns
718+
-------
719+
720+
A dataframe (copy) of `cash_flows` where values are discounted according to `disc`
721+
"""
722+
585723
if not disc:
586724
return cash_flows
587725

588726
if not isinstance(cash_flows.index, pd.DatetimeIndex):
589727
raise ValueError("cash_flows must be a pandas Series with a datetime index")
590728

591-
# Determine the end date if not provided
592-
if end_date is None:
593-
end_date = cash_flows.index[-1]
594-
595729
df = cash_flows.to_frame(name="cash_flow")
596730
df["year"] = df.index.year
597731

0 commit comments

Comments
 (0)