Skip to content

Commit 5f41cd3

Browse files
return to old sampling when sampling without replacement
1 parent 16b0ec9 commit 5f41cd3

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

climada/util/yearsets.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,20 @@ def sample_events(events_per_year, freqs_orig, with_replacement=True, seed=None)
226226
"with_replacement=True if you want to sample with replacmeent."
227227
)
228228

229-
# if not enough events remaining, use original events
229+
# if not enough events remaining, append original events
230230
if len(indices) < amount_events or len(indices) == 0:
231-
indices = indices_orig
232-
freqs = freqs_orig
231+
indices = np.append(indices, indices_orig)
232+
freqs = np.append(freqs, freqs_orig)
233+
234+
# ensure that each event only occurs once per sampled year
235+
unique_events = np.unique(indices, return_index=True)[0]
236+
probab_dis = freqs[np.unique(indices, return_index=True)[1]] / (
237+
np.sum(freqs[np.unique(indices, return_index=True)[1]])
238+
)
233239

234240
# sample events
235-
probab_dis = freqs / sum(freqs)
236241
selected_events = rng.choice(
237-
indices, size=amount_events, replace=False, p=probab_dis
242+
unique_events, size=amount_events, replace=False, p=probab_dis
238243
).astype("int")
239244

240245
# determine used events to remove them from sampling pool

0 commit comments

Comments
 (0)