3131
3232DUMP = False
3333
34- InteractionSampleMethod = typing .Literal ["monte_carlo " , "eet" , "poisson" ]
34+ InteractionSampleMethod = typing .Literal ["inverse_cdf " , "eet" , "poisson" ]
3535
3636# Threshold on P0, the probability that a chooser's Poisson draw comes up empty, below
3737# which the fallback term is dropped from the reported inclusion probabilities. Choosers
@@ -82,7 +82,7 @@ def resolve_sample_method(
8282 sampling_method = state .settings .sample_method
8383 if sampling_method is None :
8484 sampling_method = (
85- "poisson" if state .settings .use_explicit_error_terms else "monte_carlo "
85+ "poisson" if state .settings .use_explicit_error_terms else "inverse_cdf "
8686 )
8787 if sampling_method not in typing .get_args (InteractionSampleMethod ):
8888 raise ValueError (
@@ -134,6 +134,10 @@ def _poisson_fallback_positions(
134134 alternatives for each row of `probs_values` (all of them if there are fewer
135135 than `sample_size` alternatives), with ties broken by column position.
136136
137+ A row with fewer than `sample_size` positive-probability alternatives gets
138+ zero-probability positions in its trailing columns; the caller drops those
139+ pairs so that an unavailable alternative can never enter the choice set.
140+
137141 This is deliberately *deterministic* and consumes no random numbers, so every
138142 chooser row advances its RNG channel by exactly the same amount whether or not
139143 the fallback fires. That keeps random number streams aligned across scenarios,
@@ -166,7 +170,7 @@ def make_sample_choices_eet(
166170
167171 Each chooser receives `sample_size` EV1 draw sets and the argmax-over-utility
168172 winner is recorded per draw, so duplicates are possible (same with-replacement
169- semantics as the Monte Carlo sampling path).
173+ semantics as the inverse-CDF sampling path).
170174
171175 `utilities` drives the Gumbel argmax. `probs` (the MNL choice probabilities
172176 computed from the same utilities by the caller) supplies the `prob` column
@@ -225,7 +229,7 @@ def make_sample_choices_poisson(
225229
226230 where `p_i` is the chooser's MNL choice probability for alternative `i`. That is the
227231 probability the alternative would have been drawn at least once across `sample_size`
228- Monte Carlo draws, which is what makes Poisson sampling interchangeable with the other
232+ inverse-CDF draws, which is what makes Poisson sampling interchangeable with the other
229233 sampling methods. `pick_count` is 1 by definition (the draw is a yes/no per
230234 alternative), so the standard sampling correction factor is recoverable in the usual
231235 way as `np.log(df.pick_count / df.prob)`.
@@ -238,8 +242,11 @@ def make_sample_choices_poisson(
238242 Since the probabilities sum to one and 1 - p <= exp(-p), this is bounded above by
239243 exp(-sample_size): very small at the sample sizes these models use (~1e-13
240244 for `sample_size=30`), but not negligible at small sample sizes or for a chooser whose
241- probability mass is spread thinly. Those choosers fall back to the `sample_size`
242- highest-probability alternatives (see `_poisson_fallback_positions`). The fallback is
245+ probability mass is spread thinly. Those choosers fall back to their
246+ `min(sample_size, n_available)` highest-probability available alternatives, where
247+ `n_available` counts the alternatives with non-zero probability (see
248+ `_poisson_fallback_positions`) -- an unavailable alternative can never enter the
249+ choice set through either branch. The fallback is
243250 deterministic and draws no random numbers, so every chooser advances its RNG channel by
244251 exactly the same amount whether or not it fires -- unlike a retry scheme, this cannot
245252 desynchronise random number streams between scenarios.
@@ -293,9 +300,9 @@ def make_sample_choices_poisson(
293300 if n_empty > 0 :
294301 logger .warning (
295302 f"Poisson sampling drew an empty choice set for { n_empty } of { len (probs )} "
296- f"chooser(s) in { trace_label } ; falling back to the "
297- f"{ min (sample_size , probs_values .shape [1 ])} highest-probability alternatives "
298- f"for those choosers. Highest empty-sample probability was "
303+ f"chooser(s) in { trace_label } ; falling back to (at most) the "
304+ f"{ min (sample_size , probs_values .shape [1 ])} highest-probability available "
305+ f"alternatives for those choosers. Highest empty-sample probability was "
299306 f"{ empty_sample_probs [empty_rows ].max ():.2g} against a requested sample size "
300307 f"of { sample_size } and a mean expected sample size of "
301308 f"{ inclusion_probs [empty_rows ].sum (axis = 1 ).mean ():.1f} ."
@@ -312,6 +319,17 @@ def make_sample_choices_poisson(
312319 )
313320 row_positions = np .repeat (fallback_rows , fallback_cols .shape [1 ])
314321 col_positions = fallback_cols .reshape (- 1 )
322+
323+ # drop zero-probability pairs: a chooser with fewer available (p > 0)
324+ # alternatives than the fallback window would otherwise get it padded with
325+ # unavailable alternatives, which would enter the final choice set carrying a
326+ # large positive correction term log(1/P0). The fallback set remains a
327+ # deterministic function of the probabilities, so the closed form for the
328+ # reported prob is unchanged.
329+ available = probs_values [row_positions , col_positions ] > 0.0
330+ row_positions = row_positions [available ]
331+ col_positions = col_positions [available ]
332+
315333 inclusion_probs [row_positions , col_positions ] += empty_sample_probs [
316334 row_positions
317335 ]
@@ -763,10 +781,10 @@ def _interaction_sample(
763781
764782 sampling_method = resolve_sample_method (state , compute_settings )
765783
766- # Estimation requires MC sampling and MC choice for now
767- if estimation .manager .enabled and sampling_method != "monte_carlo " :
784+ # Estimation requires inverse-CDF sampling and choice for now
785+ if estimation .manager .enabled and sampling_method != "inverse_cdf " :
768786 raise ValueError (
769- f"{ trace_label } : estimation requires monte_carlo sampling and choice. Set sample_method='monte_carlo '"
787+ f"{ trace_label } : estimation requires inverse_cdf sampling and choice. Set sample_method='inverse_cdf '"
770788 + " (or leave it unset) and use_explicit_error_terms=False for estimation runs."
771789 )
772790
@@ -817,7 +835,7 @@ def _interaction_sample(
817835 column_labels = ["alternative" , "probability" ],
818836 )
819837
820- if sampling_method == "monte_carlo " :
838+ if sampling_method == "inverse_cdf " :
821839 del utilities
822840 chunk_sizer .log_df (trace_label , "utilities" , None )
823841
@@ -872,8 +890,8 @@ def _interaction_sample(
872890 del probs
873891 chunk_sizer .log_df (trace_label , "probs" , None )
874892 else :
875- # eet and poisson: optionally trim choosers with all-zero probs. The MC
876- # path handles this inside make_sample_choices
893+ # eet and poisson: optionally trim choosers with all-zero probs. The
894+ # inverse-CDF path handles this inside make_sample_choices
877895 if allow_zero_probs :
878896 non_zero = probs .sum (axis = 1 ) != 0
879897 if not non_zero .any ():
@@ -1075,20 +1093,25 @@ def interaction_sample(
10751093 sampling_method = resolve_sample_method (state , compute_settings )
10761094 logger .debug (f" interaction_sample sample method = { sampling_method } " )
10771095
1078- if sampling_method == "monte_carlo" :
1079- # The MC sampling path (make_sample_choices) does not consume stable_alt_positions
1080- # or n_total_alts. Null them out so callers that conservatively pass values along
1081- # don't accidentally rely on them under MC sampling.
1096+ if sampling_method == "inverse_cdf" :
1097+ # The inverse-CDF sampling path (make_sample_choices) does not consume
1098+ # stable_alt_positions or n_total_alts. Null them out so callers that
1099+ # conservatively pass values along don't accidentally rely on them under
1100+ # inverse-CDF sampling.
10821101 stable_alt_positions = None
10831102 n_total_alts = None
10841103
1085- # FIXME - legacy logic - not sure this is needed or even correct?
10861104 if sampling_method != "poisson" :
1105+ # legacy clamp for the with-replacement methods; statistically harmless because
1106+ # the omitted log(sample_size) term in the correction is constant per chooser
10871107 sample_size = min (sample_size , len (alternatives .index ))
1088- # with poisson sampling, definitely don't want to reduce sample size - it's not a sample size but a number
1089- # of theoretical draws. Another options would be to disable sampling if # alts < sample size to ensure
1090- # all are included (but this wouldn't behave well if there were land use changes in the project case which
1091- # switched regimes)
1108+ # with poisson sampling the sample size must not be clamped: it is not a count of
1109+ # draws but the rate parameter of the inclusion probabilities. When a chooser has
1110+ # fewer available alternatives than sample_size, its inclusion probabilities
1111+ # saturate towards 1 and the final choice approaches exact MNL over its full
1112+ # availability set, which is the desired behavior. (Disabling sampling entirely
1113+ # in that regime would behave badly if a project-case land use change switched
1114+ # regimes.)
10921115
10931116 logger .debug (f" interaction_sample sample size = { sample_size } " )
10941117
0 commit comments