resample_x_to_match_y¶
- halotools.utils.resample_x_to_match_y(x, y, bins, seed=None)[source]¶
Return the indices that resample
x
(with replacement) so that the resampled distribution matches the histogram ofy
. The returned indexing array will be sorted so that the i^th element of x[idx] is as close as possible to the i^th value of x, subject to the the constraint that x[idx] matches y.- Parameters:
- xndarray
Numpy array of shape (nx, )
- yndarray
Numpy array of shape (ny, )
- binsndarray
Numpy array of shape (nbins, ) defining how the distribution
y
will be binned to evaluate its PDF.- seedint, optional
Random number seed used to generate indices. Default is None for stochastic results.
- Returns:
- indicesndarray
Numpy array of shape (nx, )
Examples
>>> nx, ny = int(1e5), int(1e4) >>> x = np.random.normal(loc=0, size=nx, scale=1) >>> y = np.random.normal(loc=1, size=ny, scale=0.5) >>> bins = np.linspace(-5, 5, 100) >>> indices = resample_x_to_match_y(x, y, bins) >>> rescaled_x = x[indices]