I have as input two jagged arrays: The first is an array of arrays of values and the second is an array of arrays of probabilities corresponding to the values of the former array. For each row of values I would like to draw one value according to the corresponding array of probabilities.
Below is an example code of a non-vectorized version:
values = [[1, 2, 4], [19, 8], [7, 6, 1, 2], [5, 0]]
probabilities = [[0.1, 0.1, 0.8], [0.5, 0.5], [1, 0, 0, 0], [0.25, 0.75]]
output = [np.random.choice(values, p=probs) for values, probs in zip(values, probabilities)]
print(output)
Output:
[4, 19, 7, 0]
I tried using np.vectorize for each pair of values and probabilities but it provided no speedup. Is there a way to vectorize this type of random choice, either by using np.vectorize or not?