Drawing samples from numerical probability density

Published January 27, 2018

Quite often it happens in science that the probability distribution is set only numerically at some grid. In this case, the fastest option is to prepare the numerical cumulative distribution by summing each element of the array. The cumulative distribution is inverted by means of a spline fitting procedure where the arguments are rearranged. The x-axis becomes the cumulative probability and y-axis is the actual random variable. After these steps, a sample with a uniform distribution between zero and one is easily converted by means of the fitted function to a distribution with the required probability density.