pisces.math_utils.sampling.sample_from_cdf#

pisces.math_utils.sampling.sample_from_cdf(x: ndarray, cdf: ndarray, num_particles: int) ndarray[source]#

Sample particle positions from a cumulative distribution function (CDF) defined at discrete positions.

This function implements inverse transform sampling using linear interpolation. It assumes the input CDF is monotonically increasing and defined on a 1D grid x.

Parameters:
  • x (np.ndarray) – A strictly increasing 1D array of positions where the CDF is defined.

  • cdf (np.ndarray) – A 1D array of the same length as x, containing the CDF values at each position. Values must be non-decreasing and bounded in [0, 1] (after normalization).

  • num_particles (int) – The number of samples to draw from the distribution.

Returns:

A 1D array of shape (num_particles,) containing particle positions sampled from the input distribution.

Return type:

np.ndarray

Raises:

ValueError – If inputs are not 1D arrays of the same length, if x is not strictly increasing, or if cdf is not non-decreasing.

Notes

This function uses inverse transform sampling: uniformly distributed random values on [0, 1] are mapped through the inverse CDF (using linear interpolation) to obtain samples from the target distribution.