-
Notifications
You must be signed in to change notification settings - Fork 0
/
stan_test.py
146 lines (117 loc) · 4.44 KB
/
stan_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from email.utils import parsedate_tz, mktime_tz
import pandas as pd
import numpy as np
from json import dump
from utils import split_by, partition_by
def fails(df):
cardIdToFailrate = dict()
cardId_group = df.groupby('cardId')
for cardId, group in (cardId_group):
failrate = (group.ease <= 1).sum() / len(group)
cardIdToFailrate[cardId] = failrate
return cardIdToFailrate
# print(np.sort(list(cardIdToFailrate.values())))
def dfToVariables(df):
g = df.copy().sort_values('timestamp')
g.timestamp -= g.iloc[0].timestamp
hour_per_second = 1 / 3600
# drop the first
tnows_hours = np.diff(g.timestamp.values) * hour_per_second
results = g.ease.values[1:] > 1
ret = []
for partition in partition_by(lambda tnow_res: tnow_res[1],
zip(tnows_hours, results)):
# `partition` is all True or all False
first_result = partition[0][1]
if first_result or len(partition) <= 1:
ret.extend(partition)
else:
# failure, and more than one of them in a row. Group them within a time period
splits = split_by(lambda v, vs: (v[0] - vs[0][0]) >= 0.5,
partition)
# Then for each group of timed-clusters, pick the last one
ret.extend([split[-1] for split in splits])
tnows_hours, results = zip(*ret)
return tnows_hours, results, g
def fitVariables(deltas, results, viz=False, msg=''):
t = np.cumsum(np.hstack([[0], deltas[:-1]])).tolist()
data = {
"T": len(results),
"quiz": [int(x) for x in results],
"delta": deltas,
"time": t
}
dump(data, open('data.json', 'w'))
model = open('model.stan', 'r').read()
import stan
posterior = stan.build(model, data=data, random_seed=1)
fit = posterior.sample(num_chains=4, num_samples=10_000)
print(fit)
fitdf = fit.to_frame()
fitdf = fitdf[[c for c in fitdf.columns if not c.endswith("_")]]
if viz:
import pylab as plt
plt.ion()
import pandas as pd
dfplot = fitdf[[c for c in fitdf.columns if "." not in c]]
p = pd.plotting.scatter_matrix(dfplot)
if len(msg):
p[0, 0].get_figure().suptitle(msg)
p[0, 0].get_figure().set_tight_layout(True)
return fitdf, fit
def fitCardid(df, cid, viz=True):
deltas, results, g = dfToVariables(df[df.cardId == cid])
months = sum(deltas) / 24 / 365 * 12
percentRight = 100 * np.mean(results)
return fitVariables(
deltas,
results,
viz=viz,
msg=
f'card {int(cid)}: {len(results)} quizzes, {percentRight:.3}%, {months:.1f} months'
)
def demo():
model = open('model.stan', 'r').read()
posterior = stan.build(model,
data={
"T": 4,
"quiz": [1, 1, 0, 1],
"delta": [50, 50, 100, 100]
},
random_seed=1)
fit = posterior.sample(num_chains=4, num_samples=10_000)
fitdf = fit.to_frame()
fitdf = fitdf[[c for c in fitdf.columns if not c.endswith("_")]]
def analyzeDf(df, viz=False):
cardId_group = df.groupby('cardId')
cids = []
lens = []
passRates = []
times = []
for cardId, group in cardId_group:
if len(group) < 4: continue
deltas, results, _ = dfToVariables(group)
passRates.append(np.mean(results))
lens.append(len(group))
times.append(np.sum(deltas) / 24 / 365 * 12)
cids.append(cardId)
summary = pd.DataFrame(zip(cids, lens, passRates, times),
columns='cardid,len,passRate,months'.split(','))
if viz: pd.plotting.scatter_matrix(summary[summary.columns[1:]])
return summary
# summary = analyzeDf(df)
# [fitCardid(df, c) for c in summary[summary['len'] > 40]['cardid']]
def csv2df(csvfile):
df = pd.read_csv(csvfile, parse_dates=True)
# via https://stackoverflow.com/a/31905585
df['timestamp'] = df.dateString.map(
lambda s: mktime_tz(parsedate_tz(s.replace("GMT", ""))))
return df
if __name__ == '__main__':
df = csv2df("fuzzy-anki.csv")
# 85% pass rate, 20 quizzes
# deltas, results, g = dfToVariables(df[df.cardId == 1300038030510.0])
# fitdf, fit = fitCardid(df, 1300038030510.0, viz=False)
import pylab as plt
plt.ion()
fitCardid(df, 1300038030580.0) # 90% pass rate, 30 quizzes