resampling
get_resampling_fn(method)
Get a resampling function by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
One of 'systematic', 'stratified', 'residual', 'multinomial'. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
callable |
Resampling function that takes weights and returns indices. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If method is not recognized. |
Source code in llamppl/inference/resampling.py
multinomial_resample(weights)
Multinomial resampling: independent categorical draws.
Each of the N ancestor indices is drawn independently from the categorical distribution defined by the weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
array - like
|
Normalized probability weights summing to 1. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ndarray |
Integer array of ancestor indices. |
Source code in llamppl/inference/resampling.py
residual_resample(weights)
Residual resampling: deterministic floor copies + multinomial remainder.
Takes floor(N * w_i) copies of each particle deterministically, then resamples the remaining slots from the fractional residuals using multinomial resampling.
Adapted from FilterPy (R. Labbe): https://filterpy.readthedocs.io/en/latest/monte_carlo/resampling.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
array - like
|
Normalized probability weights summing to 1. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ndarray |
Integer array of ancestor indices. |
Source code in llamppl/inference/resampling.py
stratified_resample(weights)
Stratified resampling with one random draw per stratum.
Divides [0, 1] into N equal strata and draws one uniform point independently within each, so that consecutive points are between 0 and 2/N apart. Indices are derived by mapping these points through the inverse CDF of the categorical distribution defined by the weights.
Adapted from FilterPy (R. Labbe): https://filterpy.readthedocs.io/en/latest/monte_carlo/resampling.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
array - like
|
Normalized probability weights summing to 1. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ndarray |
Integer array of ancestor indices. |
Source code in llamppl/inference/resampling.py
systematic_resample(weights)
Systematic resampling with a single random offset.
Generates N equally spaced probe points in [0, 1] with a single random offset between 0 and 1/N. Indices are derived by mapping these points through the inverse CDF of the categorical distribution defined by the weights. Each index i is resampled exactly floor(N * w_i) or ceil(N * w_i) times.
Unlike stratified and residual resampling, systematic resampling is not provably lower-variance than multinomial in all cases; see Douc et al. (2005), Sec. 3.4: https://arxiv.org/abs/cs/0507025
Adapted from FilterPy (R. Labbe): https://filterpy.readthedocs.io/en/latest/monte_carlo/resampling.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
array - like
|
Normalized probability weights summing to 1. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ndarray |
Integer array of ancestor indices. |