Skip to content

Commit 2feb7d2

Browse files
committed
Add quantiles and median for hazard forecast
1 parent 4a80344 commit 2feb7d2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

climada/hazard/forecast.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,55 @@ def mean(self):
238238
intensity=red_intensity,
239239
fraction=red_fraction,
240240
)
241+
242+
def quantile(self, q: float):
243+
"""
244+
Reduce the impact matrix and at_event of a HazardForecast to the quantile value.
245+
246+
The quantile value is computed by taking the quantile of the impact matrix
247+
along the event dimension axis (axis=0) and then taking the quantile of the
248+
resulting array.
249+
250+
Parameters
251+
----------
252+
q : float
253+
The quantile to compute, between 0 and 1.
254+
255+
Returns
256+
-------
257+
HazardForecast
258+
A HazardForecast object with the quantile intensity and fraction.
259+
"""
260+
red_intensity = sparse.csr_matrix(self.intensity.todense().quantile(q, axis=0))
261+
red_fraction = sparse.csr_matrix(self.fraction.todense().quantile(q, axis=0))
262+
reduced_attrs = self._reduce_attrs(f"quantile_{q}")
263+
return HazardForecast(
264+
lead_time=reduced_attrs["lead_time"],
265+
member=reduced_attrs["member"],
266+
haz_type=self.haz_type,
267+
pool=self.pool,
268+
units=self.units,
269+
centroids=self.centroids,
270+
event_id=reduced_attrs["event_id"],
271+
frequency=reduced_attrs["frequency"],
272+
frequency_unit=self.frequency_unit,
273+
event_name=reduced_attrs["event_name"],
274+
date=reduced_attrs["date"],
275+
orig=reduced_attrs["orig"],
276+
intensity=red_intensity,
277+
fraction=red_fraction,
278+
)
279+
280+
def median(self):
281+
"""
282+
Reduce the impact matrix and at_event of a HazardForecast to the median value.
283+
284+
The median value is computed by taking the median of the impact matrix along the
285+
event dimension axis (axis=0) and then taking the median of the resulting array.
286+
287+
Returns
288+
-------
289+
HazardForecast
290+
A HazardForecast object with the median intensity and fraction.
291+
"""
292+
return self.quantile(0.5)

0 commit comments

Comments
 (0)