-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmodels.py
More file actions
275 lines (218 loc) · 7.85 KB
/
models.py
File metadata and controls
275 lines (218 loc) · 7.85 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*-
"""
flask_split.models
~~~~~~~~~~~~~~~~~~
This module provides the models for experiments and alternatives.
:copyright: (c) 2012-2015 by Janne Vanhala.
:license: MIT, see LICENSE for more details.
"""
from datetime import datetime
from math import sqrt
from random import random
class Alternative(object):
def __init__(self, redis, name, experiment_name):
self.redis = redis
self.experiment_name = experiment_name
if isinstance(name, tuple):
self.name, self.weight = name
else:
self.name = name
self.weight = 1
def _get_participant_count(self):
return int(self.redis.hget(self.key, 'participant_count') or 0)
def _set_participant_count(self, count):
self.redis.hset(self.key, 'participant_count', int(count))
participant_count = property(
_get_participant_count,
_set_participant_count
)
def _get_completed_count(self):
return int(self.redis.hget(self.key, 'completed_count') or 0)
def _set_completed_count(self, count):
self.redis.hset(self.key, 'completed_count', int(count))
completed_count = property(
_get_completed_count,
_set_completed_count
)
def increment_participation(self):
self.redis.hincrby(self.key, 'participant_count', 1)
def increment_completion(self):
self.redis.hincrby(self.key, 'completed_count', 1)
@property
def is_control(self):
return self.experiment.control.name == self.name
@property
def conversion_rate(self):
if self.participant_count == 0:
return 0
return float(self.completed_count) / float(self.participant_count)
@property
def experiment(self):
return Experiment.find(self.redis, self.experiment_name)
def save(self):
self.redis.hsetnx(self.key, 'participant_count', 0)
self.redis.hsetnx(self.key, 'completed_count', 0)
def reset(self):
self.redis.hmset(self.key, {
'participant_count': 0,
'completed_count': 0
})
def delete(self):
self.redis.delete(self.key)
@property
def key(self):
return '%s:%s' % (self.experiment_name, self.name)
@property
def z_score(self):
control = self.experiment.control
alternative = self
if control.name == alternative.name:
return None
cr = alternative.conversion_rate
crc = control.conversion_rate
n = alternative.participant_count
nc = control.participant_count
if n == 0 or nc == 0:
return None
mean = cr - crc
var_cr = cr * (1 - cr) / float(n)
var_crc = crc * (1 - crc) / float(nc)
if var_cr + var_crc == 0:
return None
return mean / sqrt(var_cr + var_crc)
@property
def confidence_level(self):
z = self.z_score
if z is None:
return 'N/A'
z = abs(round(z, 3))
if z == 0:
return 'no change'
elif z < 1.64:
return 'no confidence'
elif z < 1.96:
return '90% confidence'
elif z < 2.57:
return '95% confidence'
elif z < 3.29:
return '99% confidence'
else:
return '99.9% confidence'
class Experiment(object):
def __init__(self, redis, name, *alternative_names):
self.redis = redis
self.name = name
self.alternatives = [
Alternative(redis, alternative, name)
for alternative in alternative_names
]
@property
def control(self):
return self.alternatives[0]
def _get_winner(self):
winner = self.redis.hget('experiment_winner', self.name)
if winner:
return Alternative(self.redis, winner, self.name)
def _set_winner(self, winner_name):
self.redis.hset('experiment_winner', self.name, winner_name)
winner = property(
_get_winner,
_set_winner
)
def reset_winner(self):
"""Reset the winner of this experiment."""
self.redis.hdel('experiment_winner', self.name)
@property
def start_time(self):
"""The start time of this experiment."""
t = self.redis.hget('experiment_start_times', self.name)
if t:
return datetime.strptime(t, '%Y-%m-%dT%H:%M:%S')
@property
def total_participants(self):
"""The total number of participants in this experiment."""
return sum(a.participant_count for a in self.alternatives)
@property
def total_completed(self):
"""The total number of users who completed this experiment."""
return sum(a.completed_count for a in self.alternatives)
@property
def alternative_names(self):
"""A list of alternative names. in this experiment."""
return [alternative.name for alternative in self.alternatives]
def next_alternative(self):
"""Return the winner of the experiment if set, or a random
alternative."""
return self.winner or self.random_alternative()
def random_alternative(self):
total = sum(alternative.weight for alternative in self.alternatives)
point = random() * total
for alternative in self.alternatives:
if alternative.weight >= point:
return alternative
point -= alternative.weight
@property
def version(self):
return int(self.redis.get('%s:version' % self.name) or 0)
def increment_version(self):
self.redis.incr('%s:version' % self.name)
@property
def key(self):
if self.version > 0:
return "%s:%s" % (self.name, self.version)
else:
return self.name
def reset(self):
"""Delete all data for this experiment."""
for alternative in self.alternatives:
alternative.reset()
self.reset_winner()
self.increment_version()
def delete(self):
"""Delete this experiment and all its data."""
for alternative in self.alternatives:
alternative.delete()
self.reset_winner()
self.redis.srem('experiments', self.name)
self.redis.delete(self.name)
self.increment_version()
@property
def is_new_record(self):
return self.name not in self.redis
def save(self):
if self.is_new_record:
start_time = self._get_time().isoformat()[:19]
self.redis.sadd('experiments', self.name)
self.redis.hset('experiment_start_times', self.name, start_time)
for alternative in reversed(self.alternatives):
self.redis.lpush(self.name, alternative.name)
@classmethod
def load_alternatives_for(cls, redis, name):
return redis.lrange(name, 0, -1)
@classmethod
def all(cls, redis):
return [cls.find(redis, e) for e in redis.smembers('experiments')]
@classmethod
def find(cls, redis, name):
if name in redis:
return cls(redis, name, *cls.load_alternatives_for(redis, name))
@classmethod
def find_or_create(cls, redis, key, *alternatives):
name = key.split(':')[0]
if len(alternatives) < 2:
raise TypeError('You must declare at least 2 alternatives.')
experiment = cls.find(redis, name)
if experiment:
alts = [a[0] if isinstance(a, tuple) else a for a in alternatives]
if [a.name for a in experiment.alternatives] != alts:
experiment.reset()
for alternative in experiment.alternatives:
alternative.delete()
experiment = cls(redis, name, *alternatives)
experiment.save()
else:
experiment = cls(redis, name, *alternatives)
experiment.save()
return experiment
def _get_time(self):
return datetime.now()