Calmcode - memo: memlist

How to use memlist from memo.

1 2 3 4 5 6

The base usage of memo is to use a decorator for data collection.

import numpy as np
from memo import memlist, memfile, memfunc, memweb

data = []

@memlist(data=data)
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}

for size in range(2, 100):
    birthday_experiment(class_size=size, n_sim=1_000)

This will log data into a list, which can then be imported in pandas for plotting.

import pandas as pd
import matplotlib.pylab as plt

df = pd.DataFrame(data)
plt.plot(df['class_size'], df['est_prob']);