Memo also has helpers that make it easier for you to add information to a simulation run.
You can add a time_taken
decorator to measure how long the simulation took to run.
import numpy as np
from memo import memlist, grid, Runner, time_taken
data = []
@memlist(data=data)
@time_taken()
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), "number": 42}
settings = grid(
class_size=range(2, 100),
n_sim=[1_000, 10_000, 100_000]
)
Runner(backend="threading", n_jobs=4).run(birthday_experiment, settings)
From here you typically move on to analysing the simulation results. We might recommend trying out the hiplot library in this scenario.
import hiplot as hip
hip.Experiment.from_iterable(data).display()
If you're interested to learn more, know that we have a full course on hiplot on calmcode.