-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreports.py
291 lines (255 loc) · 12.1 KB
/
reports.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- coding: utf-8 -*-
import logging
l = logging.getLogger(__name__)
from datetime import datetime, timedelta
from .models import (DailyEngagementReport,
DailyConversionReport,
DailyConversionReportGoalData,
Experiment, Participant,
GoalRecord, GoalType)
from .significance import chi_square_p_value
def calculate_participant_conversion(participant, goal_type, report_date):
"""
Determines whether a specific participant achieved a specific goal_type
between the participant's enrollment date and the report date.
If goal_type is None, then it determines the result for _any_ goal_type.
"""
if goal_type == None:
count = GoalRecord.objects.filter(
created__gte=participant.enrollment_date,
created__lt=(report_date + timedelta(days=1)),
anonymous_visitor=participant.anonymous_visitor).count()
else:
count = GoalRecord.objects.filter(
goal_type=goal_type,
created__gte=participant.enrollment_date,
created__lt=(report_date + timedelta(days=1)),
anonymous_visitor=participant.anonymous_visitor).count()
return count and 1 or 0
def calculate_goal_type_conversion(goal_type,
participants,
report_date,
participant_conversion_calculator=calculate_participant_conversion):
"""
Calculates the number of conversions for a specific goal type among the group of
participants between each participant's enrollment date and the given report date.
"""
count = 0
for participant in participants:
count += participant_conversion_calculator(participant, goal_type, report_date)
return count
def find_experiment_group_participants(group, experiment, report_date):
"""
Returns a collection of participants belonging to the specified group in the
given experiment. It only includes participants that were enrolled in the
given report date.
"""
return Participant.objects.filter(group=group,
enrollment_date__lte=report_date,
experiment=experiment,
anonymous_visitor__isnull=False)
def __rate(a, b):
if not b or a == None:
return None
return 100. * a / b
def __improvement(a, b):
if not b or not a:
return None
return (a - b) * 100. / b
def get_conversion_data(experiment, date):
"""
Returns (if report exists):
{
"date",
"test_group_size",
"control_group_size",
"goal_types": {
<goal_type_name>: {
"test_count",
"control_count",
"test_rate",
"control_rate",
"improvement",
"confidence"
}, ...
},
"totals": {
"test_count",
"control_count",
"test_rate",
"control_rate",
"improvement",
"confidence"
}
}
Otherwise, returns 'None'
<goal_type_name> will map to None if a report was generated for a given day, but no goal type report was generated for <goal_type_name>
"""
report_set = DailyConversionReport.objects.filter(experiment=experiment, date=date)
if report_set.count() != 1:
l.warn("No conversion report for date %s and experiment %s" %
(date, experiment.name))
return None
report = report_set[0]
test_rate = __rate(report.overall_test_conversion, report.test_group_size)
control_rate = __rate(report.overall_control_conversion, report.control_group_size)
improvement = __improvement(test_rate, control_rate)
all_goal_types = GoalType.objects.all()
goal_types_data = {}
for goal_type in all_goal_types:
goal_type_data_set = report.goal_data.filter(goal_type=goal_type)
if goal_type_data_set.count() != 1:
goal_data = None
else:
goal_type_data = goal_type_data_set[0]
goal_test_rate = __rate(goal_type_data.test_conversion, report.test_group_size)
goal_control_rate = __rate(goal_type_data.control_conversion, report.control_group_size)
goal_improvement = __improvement(goal_test_rate, goal_control_rate)
goal_data = {
"test_count": goal_type_data.test_conversion,
"control_count": goal_type_data.control_conversion,
"test_rate": goal_test_rate,
"control_rate": goal_control_rate,
"improvement": goal_improvement,
"confidence": goal_type_data.confidence
}
goal_types_data[goal_type.name] = goal_data
data = {
"date": report.date,
"test_group_size": report.test_group_size,
"control_group_size": report.control_group_size,
"goal_types": goal_types_data,
"totals": {
"test_count": report.overall_test_conversion,
"control_count": report.overall_control_conversion,
"test_rate": test_rate,
"control_rate": control_rate,
"improvement": improvement,
"confidence": report.confidence
}
}
return data
class BaseReportGenerator(object):
def __init__(self, report_model_class):
self.report_model_class = report_model_class
def generate_all_daily_reports(self, name=None):
""" Generates all missing reports up until yesterday """
if name:
experiments = Experiment.objects.filter(start_date__isnull=False, name=name)
else:
experiments = Experiment.objects.filter(start_date__isnull=False)
yesterday = (datetime.today() - timedelta(days=1)).date()
for experiment in experiments:
start_date = experiment.start_date
current_date = start_date
end_date = experiment.end_date or yesterday
end_date = min(end_date, yesterday)
# get or create the report for all the days of the experiment
while current_date <= end_date:
if (self.report_model_class.objects.filter(
experiment=experiment, date=current_date).count() == 0):
daily_report = self.generate_daily_report_for_experiment(
experiment=experiment, report_date=current_date)
current_date = current_date + timedelta(days=1)
class ConversionReportGenerator(BaseReportGenerator):
def __init__(self, goal_type_conversion_calculator=calculate_goal_type_conversion,
participant_finder=find_experiment_group_participants):
BaseReportGenerator.__init__(self, DailyConversionReport)
self.goal_type_conversion_calculator = goal_type_conversion_calculator
self.participant_finder = participant_finder
def __confidence(self, a_count, a_conversion, b_count, b_conversion):
contingency_table = [[a_count - a_conversion, a_conversion],
[b_count - b_conversion, b_conversion]]
chi_square, p_value = chi_square_p_value(contingency_table)
if p_value:
return (1 - p_value) * 100
else:
return None
def generate_daily_report_for_experiment(self, experiment, report_date):
""" Generates a single conversion report """
control_participants = self.participant_finder(Participant.CONTROL_GROUP,
experiment, report_date)
test_participants = self.participant_finder(Participant.TEST_GROUP,
experiment, report_date)
control_participant_count = control_participants.count()
test_participant_count = test_participants.count()
total_control_conversion = self.goal_type_conversion_calculator(
None, control_participants, report_date)
total_test_conversion = self.goal_type_conversion_calculator(
None, test_participants, report_date)
confidence = self.__confidence(test_participant_count, total_test_conversion,
control_participant_count, total_control_conversion)
report = DailyConversionReport.objects.create(
experiment=experiment,
date=report_date,
test_group_size=test_participant_count,
control_group_size=control_participant_count,
overall_test_conversion=total_test_conversion,
overall_control_conversion=total_control_conversion,
confidence=confidence)
for goal_type in GoalType.objects.all():
control_count = self.goal_type_conversion_calculator(goal_type,
control_participants,
report_date)
test_count = self.goal_type_conversion_calculator(goal_type,
test_participants,
report_date)
confidence = self.__confidence(test_participant_count, test_count,
control_participant_count, control_count)
DailyConversionReportGoalData.objects.create(
report=report, goal_type=goal_type,
test_conversion=test_count,
control_conversion=control_count,
confidence=confidence)
class EngagementReportGenerator(BaseReportGenerator):
def __init__(self, engagement_score_calculator):
BaseReportGenerator.__init__(self, DailyEngagementReport)
self.engagement_score_calculator = engagement_score_calculator
def __generate_scores(self, experiment, group, report_date):
"""
Returns an array of all scores for participants in the given group in the
given experiment, as of the specified report date.
"""
participants = Participant.objects.filter(
experiment=experiment,
group=group,
enrollment_date__lte=report_date).exclude(user=None)
scores = []
for participant in participants:
scores.append(self.engagement_score_calculator.
calculate_user_engagement_score(participant.user,
participant.enrollment_date,
report_date))
return scores
def generate_daily_report_for_experiment(self, experiment, report_date):
""" Generates a single engagement report """
try:
from numpy import mean, isnan
from scipy.stats import ttest_ind
except ImportError:
from .stats import mean, isnan, ttest_ind
test_group_scores = self.__generate_scores(
experiment, Participant.TEST_GROUP, report_date)
control_group_scores = self.__generate_scores(
experiment, Participant.CONTROL_GROUP, report_date)
test_group_mean = None
control_group_mean = None
confidence = None
if len(test_group_scores):
test_group_mean = mean(test_group_scores)
if len(control_group_scores):
control_group_mean = mean(control_group_scores)
if len(test_group_scores) and len(control_group_scores):
t_value, p_value = ttest_ind(test_group_scores, control_group_scores)
if isnan(p_value):
confidence = None
else:
confidence = (1 - p_value) * 100
DailyEngagementReport.objects.create(
experiment=experiment,
date=report_date,
test_score=test_group_mean,
control_score=control_group_mean,
test_group_size=len(test_group_scores),
control_group_size=len(control_group_scores),
confidence=confidence)