pisces.math_utils.sampling.sample_from_pdf#

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

Sample particle positions from a probability density function (PDF) defined at discrete positions.

This function first constructs a normalized cumulative distribution function (CDF) from the input PDF using the trapezoidal rule, and then uses inverse transform sampling to generate samples.

Parameters:
  • x (np.ndarray) – A strictly increasing 1D array representing the domain of the PDF.

  • y (np.ndarray) – A 1D array of the same length as x, containing the PDF values at each position. Values must be non-negative.

  • 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 y contains negative values.

Notes

This function uses inverse transform sampling: it constructs a CDF via cumulative trapezoidal integration, normalizes it to [0, 1], and maps uniform random values through the inverse CDF (using linear interpolation).