2424LOGGER = logging .getLogger (__name__ )
2525
2626
27- def impact_yearset (imp , sampled_years , lam = None , correction_fac = True , seed = None ):
27+ def impact_yearset (
28+ imp , sampled_years , lam = None , correction_fac = True , with_replacement = False , seed = None
29+ ):
2830 """Create a yearset of impacts (yimp) containing a probabilistic impact for each year
2931 in the sampled_years list by sampling events from the impact received as input with a
3032 Poisson distribution centered around lam per year (lam = sum(imp.frequency)).
@@ -38,6 +40,10 @@ def impact_yearset(imp, sampled_years, lam=None, correction_fac=True, seed=None)
3840 impact object containing impacts per event
3941 sampled_years : list
4042 A list of years that shall be covered by the resulting yimp.
43+ with_replacement : bool, optional
44+ If False and all frequencies of freqs_orig are constant, events are sampled
45+ without replacement. Otherwise, events are sampled with replacement.
46+ Defaults to False.
4147 seed : Any, optional
4248 seed for the default bit generator
4349 default: None
@@ -68,27 +74,13 @@ def impact_yearset(imp, sampled_years, lam=None, correction_fac=True, seed=None)
6874 if not lam :
6975 lam = np .sum (imp .frequency )
7076 events_per_year = sample_from_poisson (n_sampled_years , lam , seed = seed )
71- sampling_vect = sample_events (events_per_year , imp .frequency , seed = seed )
77+ sampling_vect = sample_events (
78+ events_per_year , imp .frequency , with_replacement = with_replacement , seed = seed
79+ )
7280
7381 # compute impact per sampled_year
74- imp_per_year = compute_imp_per_year (imp , sampling_vect )
75-
76- # copy imp object as basis for the yimp object
77- yimp = copy .deepcopy (imp )
78-
79- # save imp_per_year in yimp
80- if correction_fac : # adjust for sampling error
81- yimp .at_event = imp_per_year / calculate_correction_fac (imp_per_year , imp )
82- else :
83- yimp .at_event = imp_per_year
84-
85- # save calculations in yimp
86- yimp .event_id = np .arange (1 , n_sampled_years + 1 )
87- yimp .date = u_dt .str_to_date ([str (date ) + "-01-01" for date in sampled_years ])
88- yimp .frequency = (
89- np .ones (n_sampled_years )
90- * sum (len (row ) for row in sampling_vect )
91- / n_sampled_years
82+ yimp = impact_yearset_from_sampling_vect (
83+ imp , sampled_years , sampling_vect , correction_fac = correction_fac
9284 )
9385
9486 return yimp , sampling_vect
@@ -139,18 +131,15 @@ def impact_yearset_from_sampling_vect(
139131 yimp = copy .deepcopy (imp )
140132
141133 if correction_fac : # adjust for sampling error
142- imp_per_year = imp_per_year / calculate_correction_fac (imp_per_year , imp )
134+ imp_per_year = imp_per_year .astype (float ) / calculate_correction_fac (
135+ imp_per_year , imp
136+ )
143137
144138 # save calculations in yimp
145139 yimp .at_event = imp_per_year
146- n_sampled_years = len (sampled_years )
147- yimp .event_id = np .arange (1 , n_sampled_years + 1 )
140+ yimp .event_id = np .arange (1 , len (sampled_years ) + 1 )
148141 yimp .date = u_dt .str_to_date ([str (date ) + "-01-01" for date in sampled_years ])
149- yimp .frequency = (
150- np .ones (n_sampled_years )
151- * sum (len (row ) for row in sampling_vect )
152- / n_sampled_years
153- )
142+ yimp .frequency = np .full_like (yimp .at_event , 1.0 / len (sampled_years ), dtype = float )
154143
155144 return yimp
156145
0 commit comments