📚 The CoCalc Library - books, templates and other resources
License: OTHER
import numpy as np1import pandas as pd23names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank', 'George',4'Hannah', 'Ingrid', 'Jerry', 'Kevin', 'Laura', 'Michael', 'Norbert', 'Oliver',5'Patricia', 'Quinn', 'Ray', 'Sarah', 'Tim', 'Ursula', 'Victor', 'Wendy',6'Xavier', 'Yvonne', 'Zelda']78k = 10091011def account_params(k):12ids = np.arange(k, dtype=int)13names2 = np.random.choice(names, size=k, replace=True)14wealth_mag = np.random.exponential(100, size=k)15wealth_trend = np.random.normal(10, 10, size=k)16freq = np.random.exponential(size=k)17freq /= freq.sum()1819return ids, names2, wealth_mag, wealth_trend, freq2021def account_entries(n, ids, names, wealth_mag, wealth_trend, freq):22indices = np.random.choice(ids, size=n, replace=True, p=freq)23amounts = ((np.random.normal(size=n) + wealth_trend[indices])24* wealth_mag[indices])2526return pd.DataFrame({'id': indices,27'names': names[indices],28'amount': amounts.astype('i4')},29columns=['id', 'names', 'amount'])303132def accounts(n, k):33ids, names, wealth_mag, wealth_trend, freq = account_params(k)34df = account_entries(n, ids, names, wealth_mag, wealth_trend, freq)35return df363738def json_entries(n, *args):39df = account_entries(n, *args)40g = df.groupby(df.id).groups4142data = []43for k in g:44sub = df.iloc[g[k]]45d = dict(id=int(k), name=sub['names'].iloc[0],46transactions=[{'transaction-id': int(i), 'amount': int(a)}47for i, a in list(zip(sub.index, sub.amount))])48data.append(d)4950return data5152def accounts_json(n, k):53args = account_params(k)54return json_entries(n, *args)555657