Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96107
License: OTHER
1
import numpy as np
2
import pandas as pd
3
4
names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank', 'George',
5
'Hannah', 'Ingrid', 'Jerry', 'Kevin', 'Laura', 'Michael', 'Norbert', 'Oliver',
6
'Patricia', 'Quinn', 'Ray', 'Sarah', 'Tim', 'Ursula', 'Victor', 'Wendy',
7
'Xavier', 'Yvonne', 'Zelda']
8
9
k = 100
10
11
12
def account_params(k):
13
ids = np.arange(k, dtype=int)
14
names2 = np.random.choice(names, size=k, replace=True)
15
wealth_mag = np.random.exponential(100, size=k)
16
wealth_trend = np.random.normal(10, 10, size=k)
17
freq = np.random.exponential(size=k)
18
freq /= freq.sum()
19
20
return ids, names2, wealth_mag, wealth_trend, freq
21
22
def account_entries(n, ids, names, wealth_mag, wealth_trend, freq):
23
indices = np.random.choice(ids, size=n, replace=True, p=freq)
24
amounts = ((np.random.normal(size=n) + wealth_trend[indices])
25
* wealth_mag[indices])
26
27
return pd.DataFrame({'id': indices,
28
'names': names[indices],
29
'amount': amounts.astype('i4')},
30
columns=['id', 'names', 'amount'])
31
32
33
def accounts(n, k):
34
ids, names, wealth_mag, wealth_trend, freq = account_params(k)
35
df = account_entries(n, ids, names, wealth_mag, wealth_trend, freq)
36
return df
37
38
39
def json_entries(n, *args):
40
df = account_entries(n, *args)
41
g = df.groupby(df.id).groups
42
43
data = []
44
for k in g:
45
sub = df.iloc[g[k]]
46
d = dict(id=int(k), name=sub['names'].iloc[0],
47
transactions=[{'transaction-id': int(i), 'amount': int(a)}
48
for i, a in list(zip(sub.index, sub.amount))])
49
data.append(d)
50
51
return data
52
53
def accounts_json(n, k):
54
args = account_params(k)
55
return json_entries(n, *args)
56
57