Is there a reason why you start off with numpy, then switch to lists and a for loop?
If you are given lists, you could convert them to numpy arrays and refactor your sampleValue() function to be done entirely in numpy. Otherwise, keep your generated values as numpy arrays instead of converting them to list and refactor your sampleValue() function to be done with numpy anyway.
probabilities = np.array(np.random.dirichlet(np.ones(N),size=1).tolist()[0])
def sampleValue2(possible_values, probabilities):
U = random.random()
return possible_values[np.argmax(np.cumsum(probabilities)>U)]
This reduces function simulation time from
1.18 s ± 9.87 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
to
486 ms ± 8.59 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Then if you know how many random #s you want you can ditch the list comprehension by creating a probability matrix:
cum_probs = np.tile(np.cumsum(probabilities),(num_test,1))
Apply the function we rewrote over the entire matrix instead of by row:
indices = np.argmax(U<cum_probs,axis=1)
return np.array(possible_values)[indices]
to write a new function:
def sampleValue3(possible_values, probabilities):
U = np.random.random(size=(num_test,1))
cum_probs = np.tile(np.cumsum(probabilities),(num_test,1))
indices = np.argmax(U<cum_probs,axis=1)
return np.array(possible_values)[indices]
And that gives me a simulation time of:
65.9 ms ± 502 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Since you are just doing a lot of numeric operations, your algorithm could be sped up a lot just by keeping everything within numpy arrays/matrices.
Alternatively, from your problem definition, I don't see any reason why you can't just use numpy's random choice:
def sampleValue5(possible_values, probabilities):
return np.random.choice(possible_values,p=probabilities,size=100000)
which is the fastest of these at:
36.8 ms ± 315 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)