Skip to content

Commit a8cb0d2

Browse files
yearsets.sample_from_poisson skip distinction between lam 1 and not 1 (#823)
1 parent 3851c48 commit a8cb0d2

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Code freeze date: YYYY-MM-DD
2323

2424
- `Hazard.from_xarray_raster` now stores strings as default values for `Hazard.event_name` [#795](https://github.com/CLIMADA-project/climada_python/pull/795)
2525
- Fix the dist_approx util function when used with method="geosphere" and log=True and points that are very close. [#792](https://github.com/CLIMADA-project/climada_python/pull/792)
26+
- `climada.util.yearsets.sample_from_poisson`: fix a bug ([#819](https://github.com/CLIMADA-project/climada_python/issues/819)) and inconsistency that occurs when lambda events per year (`lam`) are set to 1. [[#823](https://github.com/CLIMADA-project/climada_python/pull/823)]
2627

2728
### Deprecated
2829

climada/util/test/test_yearsets.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ def test_impact_yearset_sampling_vect(self):
6060
def test_sample_from_poisson(self):
6161
"""Test sampling amount of events per year."""
6262
n_sample_years = 1000
63-
lam = np.sum(IMP.frequency)
64-
events_per_year = yearsets.sample_from_poisson(n_sample_years, lam)
65-
66-
self.assertEqual(events_per_year.size, n_sample_years)
67-
self.assertAlmostEqual(np.round(np.mean(events_per_year)), 2)
63+
for lam in [0, 1, 2.5]:
64+
events_per_year = yearsets.sample_from_poisson(n_sample_years, lam, seed=1)
65+
66+
self.assertEqual(events_per_year.size, n_sample_years)
67+
self.assertAlmostEqual(np.mean(events_per_year), lam, places=1)
68+
69+
self.assertRaises(TypeError, yearsets.sample_from_poisson, n_sample_years, None)
70+
self.assertRaises(ValueError, yearsets.sample_from_poisson, n_sample_years, -1)
6871

6972
def test_sample_events(self):
7073
"""Test the sampling of 34 events out of a pool of 20 events."""

climada/util/yearsets.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def sample_from_poisson(n_sampled_years, lam, seed=None):
152152
-----------
153153
n_sampled_years : int
154154
The target number of years the impact yearset shall contain.
155-
lam: int
155+
lam: float
156156
the applied Poisson distribution is centered around lambda events per year
157157
seed : int, optional
158158
seed for numpy.random, will be set if not None
@@ -165,14 +165,8 @@ def sample_from_poisson(n_sampled_years, lam, seed=None):
165165
"""
166166
if seed is not None:
167167
np.random.seed(seed)
168-
if lam != 1:
169-
events_per_year = np.round(np.random.poisson(lam=lam,
170-
size=n_sampled_years)).astype('int')
171-
else:
172-
events_per_year = np.ones(len(n_sampled_years))
173-
168+
return np.round(np.random.poisson(lam=lam, size=n_sampled_years)).astype('int')
174169

175-
return events_per_year
176170

177171
def sample_events(events_per_year, freqs_orig, seed=None):
178172
"""Sample events uniformely from an array (indices_orig) without replacement

0 commit comments

Comments
 (0)