Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix yearsets.sample_from_poisson #823

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Code freeze date: YYYY-MM-DD

- `Hazard.from_xarray_raster` now stores strings as default values for `Hazard.event_name` [#795](https://github.com/CLIMADA-project/climada_python/pull/795)
- 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)
- `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)]

### Deprecated

Expand Down
13 changes: 8 additions & 5 deletions climada/util/test/test_yearsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ def test_impact_yearset_sampling_vect(self):
def test_sample_from_poisson(self):
"""Test sampling amount of events per year."""
n_sample_years = 1000
lam = np.sum(IMP.frequency)
events_per_year = yearsets.sample_from_poisson(n_sample_years, lam)

self.assertEqual(events_per_year.size, n_sample_years)
self.assertAlmostEqual(np.round(np.mean(events_per_year)), 2)
for lam in [0, 1, 2.5]:
events_per_year = yearsets.sample_from_poisson(n_sample_years, lam, seed=1)

self.assertEqual(events_per_year.size, n_sample_years)
self.assertAlmostEqual(np.mean(events_per_year), lam, places=1)

self.assertRaises(TypeError, yearsets.sample_from_poisson, n_sample_years, None)
self.assertRaises(ValueError, yearsets.sample_from_poisson, n_sample_years, -1)

def test_sample_events(self):
"""Test the sampling of 34 events out of a pool of 20 events."""
Expand Down
10 changes: 2 additions & 8 deletions climada/util/yearsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def sample_from_poisson(n_sampled_years, lam, seed=None):
-----------
n_sampled_years : int
The target number of years the impact yearset shall contain.
lam: int
lam: float
the applied Poisson distribution is centered around lambda events per year
seed : int, optional
seed for numpy.random, will be set if not None
Expand All @@ -165,14 +165,8 @@ def sample_from_poisson(n_sampled_years, lam, seed=None):
"""
if seed is not None:
np.random.seed(seed)
if lam != 1:
events_per_year = np.round(np.random.poisson(lam=lam,
size=n_sampled_years)).astype('int')
else:
events_per_year = np.ones(len(n_sampled_years))

return np.round(np.random.poisson(lam=lam, size=n_sampled_years)).astype('int')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure to understand. This changes the behaviour of the method. Is this desired?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. The original behavior at lam = 1.0 is inconsistent, see discussion in #820.


return events_per_year

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