To run, ray, you'll first need to make sure it is installed with pip. Once this is accounted for, you can start a ray service.
import ray
ray.init(num_cpus=2);
You can now run the function in parallel.
@ray.remote
def birthday_experiment(class_size, n_sim=1000):
"""Simulates the birthday paradox. Vectorized = Fast!"""
sims = np.random.randint(1, 365 + 1, (n_sim, class_size))
sort_sims = np.sort(sims, axis=1)
n_uniq = (sort_sims[:, 1:] != sort_sims[:, :-1]).sum(axis = 1) + 1
return {"est_prob": np.mean(n_uniq != class_size)}
futures = [birthday_experiment.remote(class_size=size, n_sim=10_000) for size in range(2, 10)]
You should notice a speedup now.
%%time
results = ray.get(futures)