Tools

Resample particles

pocomc.tools.systematic_resample(size: ndarray, weights: ndarray, random_state: int = None)

Resample a new set of points from the weighted set of inputs such that they all have equal weight.

Parameters:
  • size (int) – Number of samples to draw.

  • weights (~numpy.ndarray with shape (nsamples,)) – Corresponding weight of each sample.

  • random_state (int, optional) – Random seed.

Returns:

indeces – Indices of the resampled array.

Return type:

~numpy.ndarray with shape (nsamples,)

Examples

>>> x = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])
>>> w = np.array([0.6, 0.2, 0.15, 0.05])
>>> systematic_resample(4, w)
array([0, 0, 0, 2])

Notes

Implements the systematic resampling method.

Compute effective sample size

pocomc.tools.compute_ess(logw: ndarray)

Compute effective sample size (per centage).

Parameters:

logw (np.ndarray) – Log-weights.

Returns:

ess – Effective sample size divided by actual number of particles (between 0 and 1)

Return type:

float

pocomc.tools.effective_sample_size(weights)

Compute effective sample size (ESS).

Parameters:

weights (np.ndarray) – Weights.

Returns:

ess – Effective sample size.

Return type:

float

pocomc.tools.unique_sample_size(weights, k=None)

Compute unique sample size (ESS).

Parameters:
  • weights (np.ndarray) – Weights.

  • k (int) – Number of resampled samples.

Returns:

uss – Unique sample size.

Return type:

float

Progress bar

class pocomc.tools.ProgressBar(show: bool = True, initial=0)

Progress bar class.

Parameters:

show (bool) – Whether or not to show a progress bar. Default is True.

close()

Close progress bar.

update_iter()

Update iteration counter.

update_stats(info)

Update shown stats.

Parameters:

info (dict) – Dictionary with stats to show.

Function wrapper

class pocomc.tools.FunctionWrapper(f, args, kwargs)

Make the log-likelihood or log-prior function pickleable when args or kwargs are also included.

Parameters:
  • f (callable) – Log probability function.

  • args (list) – Extra positional arguments to be passed to f.

  • kwargs (dict) – Extra keyword arguments to be passed to f.