-
Notifications
You must be signed in to change notification settings - Fork 19
/
Quantium - Module 2.py
605 lines (412 loc) · 27.4 KB
/
Quantium - Module 2.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env python
# coding: utf-8
# # Quantium - Module 2
#
# We will be examining the performance in trial vs control stores to provide a recommendation for each location based on our insight.
#
# - Select control stores – explore the data and define metrics for control store selection – "What would make them a control store?" Visualize the drivers to see suitability.
#
# - Assessment of the trial – get insights of each of the stores. Compare each trial store with ontrol store to get its overall performance. We want to know if the trial stores were successful or not.
#
# - Collate findings – summarise findings for each store and provide recommendations to share with client outlining the impact on sales during trial period.
# In[59]:
import pandas as pd
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
# In[60]:
qvi = pd.read_csv("QVI_data.csv")
qvi.head()
# In[61]:
# Checking for nulls
qvi.info()
# - Client has selected store numbers 77, 86 and 88 as trial stores.
# - Client wants control stores to be established stores that are operational for the entire observation period.
# - Trial period = 1 Feb 2019 to 30 April 2019.
# - Compare trial stores to control stores that are similar pre-trial. Similarity measurement:
# - Monthly overall sales revenue
# - Monthly number of customers
# - Monthly number of transactions per customer
#
# In[62]:
qvi["DATE"] = pd.to_datetime(qvi["DATE"])
qvi["YEARMONTH"] = qvi["DATE"].dt.strftime("%Y%m").astype("int")
# Compile each store's monthly:
# - Total sales
# - Number of customers,
# - Average transactions per customer
# - Average chips per customer
# - Average price per unit
# In[63]:
def monthly_store_metrics():
store_yrmo_group = qvi.groupby(["STORE_NBR", "YEARMONTH"])
total = store_yrmo_group["TOT_SALES"].sum()
num_cust = store_yrmo_group["LYLTY_CARD_NBR"].nunique()
trans_per_cust = store_yrmo_group.size() / num_cust
avg_chips_per_cust = store_yrmo_group["PROD_QTY"].sum() / num_cust
avg_chips_price = total / store_yrmo_group["PROD_QTY"].sum()
aggregates = [total, num_cust, trans_per_cust, avg_chips_per_cust, avg_chips_price]
metrics = pd.concat(aggregates, axis=1)
metrics.columns = ["TOT_SALES", "nCustomers", "nTxnPerCust", "nChipsPerTxn", "avgPricePerUnit"]
return metrics
# In[64]:
qvi_monthly_metrics = monthly_store_metrics().reset_index()
qvi_monthly_metrics.info()
# In[65]:
#pre trial observation
#filter only stores with full 12 months observation
observ_counts = qvi_monthly_metrics["STORE_NBR"].value_counts()
full_observ_index = observ_counts[observ_counts == 12].index
full_observ = qvi_monthly_metrics[qvi_monthly_metrics["STORE_NBR"].isin(full_observ_index)]
pretrial_full_observ = full_observ[full_observ["YEARMONTH"] < 201902]
pretrial_full_observ.head(8)
# In[66]:
def calcCorrTable(metricCol, storeComparison, inputTable=pretrial_full_observ):
"""Calculate correlation for a measure, looping through each control store.
Args:
metricCol (str): Name of column containing store's metric to perform correlation test on.
storeComparison (int): Trial store's number.
inputTable (dataframe): Metric table with potential comparison stores.
Returns:
DataFrame: Monthly correlation table between Trial and each Control stores.
"""
control_store_nbrs = inputTable[~inputTable["STORE_NBR"].isin([77, 86, 88])]["STORE_NBR"].unique()
corrs = pd.DataFrame(columns = ["YEARMONTH", "Trial_Str", "Ctrl_Str", "Corr_Score"])
trial_store = inputTable[inputTable["STORE_NBR"] == storeComparison][metricCol].reset_index()
for control in control_store_nbrs:
concat_df = pd.DataFrame(columns = ["YEARMONTH", "Trial_Str", "Ctrl_Str", "Corr_Score"])
control_store = inputTable[inputTable["STORE_NBR"] == control][metricCol].reset_index()
concat_df["Corr_Score"] = trial_store.corrwith(control_store, axis=1)
concat_df["Trial_Str"] = storeComparison
concat_df["Ctrl_Str"] = control
concat_df["YEARMONTH"] = list(inputTable[inputTable["STORE_NBR"] == storeComparison]["YEARMONTH"])
corrs = pd.concat([corrs, concat_df])
return corrs
# In[67]:
corr_table = pd.DataFrame()
for trial_num in [77, 86, 88]:
corr_table = pd.concat([corr_table, calcCorrTable(["TOT_SALES", "nCustomers", "nTxnPerCust", "nChipsPerTxn", "avgPricePerUnit"], trial_num)])
corr_table.head(8)
# In[68]:
def calculateMagnitudeDistance(metricCol, storeComparison, inputTable=pretrial_full_observ):
"""Calculate standardised magnitude distance for a measure, looping through each control store.
Args:
metricCol (str): Name of column containing store's metric to perform distance calculation on.
storeComparison (int): Trial store's number.
inputTable (dataframe): Metric table with potential comparison stores.
Returns:
DataFrame: Monthly magnitude-distance table between Trial and each Control stores.
"""
control_store_nbrs = inputTable[~inputTable["STORE_NBR"].isin([77, 86, 88])]["STORE_NBR"].unique()
dists = pd.DataFrame()
trial_store = inputTable[inputTable["STORE_NBR"] == storeComparison][metricCol]
for control in control_store_nbrs:
concat_df = abs(inputTable[inputTable["STORE_NBR"] == storeComparison].reset_index()[metricCol] - inputTable[inputTable["STORE_NBR"] == control].reset_index()[metricCol])
concat_df["YEARMONTH"] = list(inputTable[inputTable["STORE_NBR"] == storeComparison]["YEARMONTH"])
concat_df["Trial_Str"] = storeComparison
concat_df["Ctrl_Str"] = control
dists = pd.concat([dists, concat_df])
for col in metricCol:
dists[col] = 1 - ((dists[col] - dists[col].min()) / (dists[col].max() - dists[col].min()))
dists["magnitude"] = dists[metricCol].mean(axis=1)
return dists
# In[123]:
dist_table = pd.DataFrame()
for trial_num in [77, 86, 88]:
dist_table = pd.concat([dist_table, calculateMagnitudeDistance(["TOT_SALES", "nCustomers", "nTxnPerCust", "nChipsPerTxn", "avgPricePerUnit"], trial_num)])
dist_table.head(8)
dist_table
# We'll select control stores based on how similar monthly total sales in dollar amounts and monthly number of customers are to the trial stores by using correlation and magnitude distance.
# In[70]:
def combine_corr_dist(metricCol, storeComparison, inputTable=pretrial_full_observ):
corrs = calcCorrTable(metricCol, storeComparison, inputTable)
dists = calculateMagnitudeDistance(metricCol, storeComparison, inputTable)
dists = dists.drop(metricCol, axis=1)
combine = pd.merge(corrs, dists, on=["YEARMONTH", "Trial_Str", "Ctrl_Str"])
return combine
# In[71]:
compare_metrics_table1 = pd.DataFrame()
for trial_num in [77, 86, 88]:
compare_metrics_table1 = pd.concat([compare_metrics_table1, combine_corr_dist(["TOT_SALES"], trial_num)])
# In[72]:
corr_weight = 0.5
dist_weight = 1 - corr_weight
# In[73]:
#Top 5 highest Composite Score for each Trial Store based on TOT_SALES
grouped_comparison_table1 = compare_metrics_table1.groupby(["Trial_Str", "Ctrl_Str"]).mean().reset_index()
grouped_comparison_table1["CompScore"] = (corr_weight * grouped_comparison_table1["Corr_Score"]) + (dist_weight * grouped_comparison_table1["magnitude"])
for trial_num in compare_metrics_table1["Trial_Str"].unique():
print(grouped_comparison_table1[grouped_comparison_table1["Trial_Str"] == trial_num].sort_values(ascending=False, by="CompScore").head(), '\n')
# In[74]:
compare_metrics_table2 = pd.DataFrame()
for trial_num in [77, 86, 88]:
compare_metrics_table2 = pd.concat([compare_metrics_table2, combine_corr_dist(["nCustomers"], trial_num)])
# In[75]:
#Top 5 highest Composite Score for each Trial Store based on nCustomers
grouped_comparison_table2 = compare_metrics_table2.groupby(["Trial_Str", "Ctrl_Str"]).mean().reset_index()
grouped_comparison_table2["CompScore"] = (corr_weight * grouped_comparison_table2["Corr_Score"]) + (dist_weight * grouped_comparison_table2["magnitude"])
for trial_num in compare_metrics_table2["Trial_Str"].unique():
print(grouped_comparison_table2[grouped_comparison_table2["Trial_Str"] == trial_num].sort_values(ascending=False, by="CompScore").head(), '\n')
# In[121]:
for trial_num in compare_metrics_table2["Trial_Str"].unique():
a = grouped_comparison_table1[grouped_comparison_table1["Trial_Str"] == trial_num].sort_values(ascending=False, by="CompScore").set_index(["Trial_Str", "Ctrl_Str"])["CompScore"]
b = grouped_comparison_table2[grouped_comparison_table2["Trial_Str"] == trial_num].sort_values(ascending=False, by="CompScore").set_index(["Trial_Str", "Ctrl_Str"])["CompScore"]
print((pd.concat([a,b], axis=1).sum(axis=1)/2).sort_values(ascending=False).head(3), '\n')
# Top 3 similarity based on TOT_SALES:
# - Trial store 77: Store 233, 255, 188
# - Trial store 86: Store 109, 155, 222
# - Trial store 88: Store 40, 26, 72
#
# Top 3 similartiy based on nCustomers:
# - Trial store 77: Store 233, 41, 111
# - Trial store 86: Store 155, 225, 109
# - Trial store 88: Store 237, 203, 40
# Based on highest average of both features combined:
# - Trial store 77: Store 233
# - Trial store 86: Store 155
# - Trial store 88: Store 40
# In[76]:
trial_control_dic = {77:233, 86:155, 88:40}
for key, val in trial_control_dic.items():
pretrial_full_observ[pretrial_full_observ["STORE_NBR"].isin([key, val])].groupby(
["YEARMONTH", "STORE_NBR"]).sum()["TOT_SALES"].unstack().plot.bar()
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Trial Store "+str(key)+" and Control Store "+str(val)+" - TOT_SALES")
plt.show()
pretrial_full_observ[pretrial_full_observ["STORE_NBR"].isin([key, val])].groupby(
["YEARMONTH", "STORE_NBR"]).sum()["nCustomers"].unstack().plot.bar()
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Trial Store "+str(key)+" and Control Store "+str(val)+" - nCustomer")
plt.show()
print('\n')
# Next we'll compare the performance of Trial stores to Control stores during the trial period. To ensure their performance is comparable during Trial period, we need to scale (multiply to ratio of trial / control) all of Control stores' performance to Trial store's performance during pre-trial. Starting with TOT_SALES.
# In[77]:
#Ratio of Store 77 and its Control store.
sales_ratio_77 = pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 77]["TOT_SALES"].sum() / pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 233]["TOT_SALES"].sum()
#Ratio of Store 86 and its Control store.
sales_ratio_86 = pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 86]["TOT_SALES"].sum() / pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 155]["TOT_SALES"].sum()
#Ratio of Store 77 and its Control store.
sales_ratio_88 = pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 88]["TOT_SALES"].sum() / pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 40]["TOT_SALES"].sum()
# In[78]:
trial_full_observ = full_observ[(full_observ["YEARMONTH"] >= 201902) & (full_observ["YEARMONTH"] <= 201904)]
scaled_sales_control_stores = full_observ[full_observ["STORE_NBR"].isin([233, 155, 40])][["STORE_NBR", "YEARMONTH", "TOT_SALES"]]
def scaler(row):
if row["STORE_NBR"] == 233:
return row["TOT_SALES"] * sales_ratio_77
elif row["STORE_NBR"] == 155:
return row["TOT_SALES"] * sales_ratio_86
elif row["STORE_NBR"] == 40:
return row["TOT_SALES"] * sales_ratio_88
scaled_sales_control_stores["ScaledSales"] = scaled_sales_control_stores.apply(lambda row: scaler(row), axis=1)
trial_scaled_sales_control_stores = scaled_sales_control_stores[(scaled_sales_control_stores["YEARMONTH"] >= 201902) & (scaled_sales_control_stores["YEARMONTH"] <= 201904)]
pretrial_scaled_sales_control_stores = scaled_sales_control_stores[scaled_sales_control_stores["YEARMONTH"] < 201902]
# In[79]:
percentage_diff = {}
for trial, control in trial_control_dic.items():
a = trial_scaled_sales_control_stores[trial_scaled_sales_control_stores["STORE_NBR"] == control]
b = trial_full_observ[trial_full_observ["STORE_NBR"] == trial][["STORE_NBR", "YEARMONTH", "TOT_SALES"]]
percentage_diff[trial] = b["TOT_SALES"].sum() / a["ScaledSales"].sum()
b[["YEARMONTH", "TOT_SALES"]].merge(a[["YEARMONTH", "ScaledSales"]],on="YEARMONTH").set_index("YEARMONTH").rename(columns={"ScaledSales":"Scaled_Control_Sales", "TOT_SALES":"Trial_Sales"}).plot.bar()
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Trial Store "+str(trial)+" and Control Store "+str(control))
# In[80]:
percentage_diff
# In[81]:
#Creating a compiled percentage_difference table
temp1 = scaled_sales_control_stores.sort_values(by=["STORE_NBR", "YEARMONTH"], ascending=[False, True]).reset_index().drop(["TOT_SALES", "index"], axis=1)
temp2 = full_observ[full_observ["STORE_NBR"].isin([77,86,88])][["STORE_NBR", "YEARMONTH", "TOT_SALES"]].reset_index().drop(["index", "YEARMONTH"], axis=1)
scaledsales_vs_trial = pd.concat([temp1, temp2], axis=1)
scaledsales_vs_trial.columns = ["c_STORE_NBR", "YEARMONTH", "c_ScaledSales", "t_STORE_NBR", "t_TOT_SALES"]
scaledsales_vs_trial["Sales_Percentage_Diff"] = (scaledsales_vs_trial["t_TOT_SALES"] - scaledsales_vs_trial["c_ScaledSales"]) / (((scaledsales_vs_trial["t_TOT_SALES"] + scaledsales_vs_trial["c_ScaledSales"])/2))
def label_period(cell):
if cell < 201902:
return "pre"
elif cell > 201904:
return "post"
else:
return "trial"
scaledsales_vs_trial["trial_period"] = scaledsales_vs_trial["YEARMONTH"].apply(lambda cell: label_period(cell))
scaledsales_vs_trial[scaledsales_vs_trial["trial_period"] == "trial"]
# Check significance of Trial minus Control stores TOT_SALES Percentage Difference Pre-Trial vs Trial.
# - Step 1: Check null hypothesis of 0 difference between control store's Pre-Trial and Trial period performance.
#
# - Step 2: Proof control and trial stores are similar statistically
# - Check p-value of control store's Pre-Trial vs Trial store's Pre-Trial.
# - If <5%, it is significantly different. If >5%, it is not significantly different (similar).
#
# - Step 3: After checking Null Hypothesis of first 2 step to be true, we can check Null Hypothesis of Percentage Difference between Trial and Control stores during pre-trial is the same as during trial.
# - Check T-Value of Percentage Difference of each Trial month (Feb, March, April 2019).
# - Mean is mean of Percentage Difference during pre-trial.
# - Standard deviation is stdev of Percentage Difference during pre-trial.
# - Formula is Trial month's Percentage Difference minus Mean, divided by Standard deviation.
# - Compare each T-Value with 95% percentage significance critical t-value of 6 degrees of freedom (7 months of sample - 1)
# In[82]:
from scipy.stats import ttest_ind, t
# Step 1
for num in [40, 155, 233]:
print("Store", num)
print(ttest_ind(pretrial_scaled_sales_control_stores[pretrial_scaled_sales_control_stores["STORE_NBR"] == num]["ScaledSales"],
trial_scaled_sales_control_stores[trial_scaled_sales_control_stores["STORE_NBR"] == num]["ScaledSales"],
equal_var=False), '\n')
#print(len(pretrial_scaled_sales_control_stores[pretrial_scaled_sales_control_stores["STORE_NBR"] == num]["ScaledSales"]), len(trial_scaled_sales_control_stores[trial_scaled_sales_control_stores["STORE_NBR"] == num]["ScaledSales"]))
alpha = 0.05
print("Critical t-value for 95% confidence interval:")
print(t.ppf((alpha/2, 1-alpha/2), df=min([len(pretrial_scaled_sales_control_stores[pretrial_scaled_sales_control_stores["STORE_NBR"] == num]),
len(trial_scaled_sales_control_stores[trial_scaled_sales_control_stores["STORE_NBR"] == num])])-1))
# In[83]:
a = pretrial_scaled_sales_control_stores[pretrial_scaled_sales_control_stores["STORE_NBR"] == 40]["ScaledSales"]
b = trial_scaled_sales_control_stores[trial_scaled_sales_control_stores["STORE_NBR"] == 40]["ScaledSales"]
# Null hypothesis is true. There isn't any statistically significant difference between control store's scaled Pre-Trial and Trial period sales.
# In[84]:
# Step 2
for trial, cont in trial_control_dic.items():
print("Trial store:", trial, ", Control store:", cont)
print(ttest_ind(pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == trial]["TOT_SALES"],
pretrial_scaled_sales_control_stores[pretrial_scaled_sales_control_stores["STORE_NBR"] == cont]["ScaledSales"],
equal_var=True), '\n')
#print(len(pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == trial]["TOT_SALES"]),len(pretrial_scaled_sales_control_stores[pretrial_scaled_sales_control_stores["STORE_NBR"] == cont]["ScaledSales"]))
alpha = 0.05
print("Critical t-value for 95% confidence interval:")
print(t.ppf((alpha/2, 1-alpha/2), df=len(pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == trial])-1))
# Null hypothesis is true. There isn't any statistically significant difference between Trial store's sales and Control store's scaled-sales performance during pre-trial.
# In[85]:
# Step 3
for trial, cont in trial_control_dic.items():
print("Trial store:", trial, ", Control store:", cont)
temp_pre = scaledsales_vs_trial[(scaledsales_vs_trial["c_STORE_NBR"] == cont) & (scaledsales_vs_trial["trial_period"]=="pre")]
std = temp_pre["Sales_Percentage_Diff"].std()
mean = temp_pre["Sales_Percentage_Diff"].mean()
#print(std, mean)
for t_month in scaledsales_vs_trial[scaledsales_vs_trial["trial_period"] == "trial"]["YEARMONTH"].unique():
pdif = scaledsales_vs_trial[(scaledsales_vs_trial["YEARMONTH"] == t_month) & (scaledsales_vs_trial["t_STORE_NBR"] == trial)]["Sales_Percentage_Diff"]
print(t_month,":",(float(pdif)-mean)/std)
print('\n')
print("Critical t-value for 95% confidence interval:")
conf_intv_95 = t.ppf(0.95, df=len(temp_pre)-1)
print(conf_intv_95)
# There are 3 months' increase in performance that are statistically significant (Above the 95% confidence interval t-score):
# - March and April trial months for trial store 77
# - March trial months for trial store 86
# In[129]:
for trial, control in trial_control_dic.items():
a = trial_scaled_sales_control_stores[trial_scaled_sales_control_stores["STORE_NBR"] == control].rename(columns={"TOT_SALES": "control_TOT_SALES"})
b = trial_full_observ[trial_full_observ["STORE_NBR"] == trial][["STORE_NBR", "YEARMONTH", "TOT_SALES"]].rename(columns={"TOT_SALES": "trial_TOT_SALES"})
comb = b[["YEARMONTH", "trial_TOT_SALES"]].merge(a[["YEARMONTH", "control_TOT_SALES"]],on="YEARMONTH").set_index("YEARMONTH")
comb.plot.bar()
cont_sc_sales = trial_scaled_sales_control_stores[trial_scaled_sales_control_stores["STORE_NBR"] == control]["TOT_SALES"]
std = scaledsales_vs_trial[(scaledsales_vs_trial["c_STORE_NBR"] == control) & (scaledsales_vs_trial["trial_period"]=="pre")]["Sales_Percentage_Diff"].std()
thresh95 = cont_sc_sales.mean() + (cont_sc_sales.mean() * std * 2)
thresh5 = cont_sc_sales.mean() - (cont_sc_sales.mean() * std * 2)
plt.axhline(y=thresh95,linewidth=1, color='b', label="95% threshold")
plt.axhline(y=thresh5,linewidth=1, color='r', label="5% threshold")
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Trial Store "+str(trial)+" and Control Store "+str(control)+" - Total Sales")
plt.savefig("TS {} and CS {} - TOT_SALES.png".format(trial,control), bbox_inches="tight")
# We can see that Trial store 77 sales for March and April exceeds 95% threshold of control store. Same goes to store 86 sales for March.
#
# Next, we'll look into nCustomers.
# In[87]:
#Ratio of Store 77 and its Control store.
ncust_ratio_77 = pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 77]["nCustomers"].sum() / pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 233]["nCustomers"].sum()
#Ratio of Store 86 and its Control store.
ncust_ratio_86 = pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 86]["nCustomers"].sum() / pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 155]["nCustomers"].sum()
#Ratio of Store 77 and its Control store.
ncust_ratio_88 = pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 88]["nCustomers"].sum() / pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == 40]["nCustomers"].sum()
# In[88]:
#trial_full_observ = full_observ[(full_observ["YEARMONTH"] >= 201902) & (full_observ["YEARMONTH"] <= 201904)]
scaled_ncust_control_stores = full_observ[full_observ["STORE_NBR"].isin([233, 155, 40])][["STORE_NBR", "YEARMONTH", "nCustomers"]]
def scaler_c(row):
if row["STORE_NBR"] == 233:
return row["nCustomers"] * ncust_ratio_77
elif row["STORE_NBR"] == 155:
return row["nCustomers"] * ncust_ratio_86
elif row["STORE_NBR"] == 40:
return row["nCustomers"] * ncust_ratio_88
scaled_ncust_control_stores["ScaledNcust"] = scaled_ncust_control_stores.apply(lambda row: scaler_c(row), axis=1)
trial_scaled_ncust_control_stores = scaled_ncust_control_stores[(scaled_ncust_control_stores["YEARMONTH"] >= 201902) & (scaled_ncust_control_stores["YEARMONTH"] <= 201904)]
pretrial_scaled_ncust_control_stores = scaled_ncust_control_stores[scaled_ncust_control_stores["YEARMONTH"] < 201902]
# In[89]:
ncust_percentage_diff = {}
for trial, control in trial_control_dic.items():
a = trial_scaled_ncust_control_stores[trial_scaled_ncust_control_stores["STORE_NBR"] == control]
b = trial_full_observ[trial_full_observ["STORE_NBR"] == trial][["STORE_NBR", "YEARMONTH", "nCustomers"]]
ncust_percentage_diff[trial] = b["nCustomers"].sum() / a["ScaledNcust"].sum()
b[["YEARMONTH", "nCustomers"]].merge(a[["YEARMONTH", "ScaledNcust"]],on="YEARMONTH").set_index("YEARMONTH").rename(columns={"ScaledSales":"Scaled_Control_nCust", "TOT_SALES":"Trial_nCust"}).plot.bar()
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Trial Store "+str(trial)+" and Control Store "+str(control))
# In[90]:
ncust_percentage_diff
# In[91]:
#Creating a compiled ncust_percentage_difference table
temp1 = scaled_ncust_control_stores.sort_values(by=["STORE_NBR", "YEARMONTH"], ascending=[False, True]).reset_index().drop(["nCustomers", "index"], axis=1)
temp2 = full_observ[full_observ["STORE_NBR"].isin([77,86,88])][["STORE_NBR", "YEARMONTH", "nCustomers"]].reset_index().drop(["index", "YEARMONTH"], axis=1)
scaledncust_vs_trial = pd.concat([temp1, temp2], axis=1)
scaledncust_vs_trial.columns = ["c_STORE_NBR", "YEARMONTH", "c_ScaledNcust", "t_STORE_NBR", "t_nCustomers"]
scaledncust_vs_trial["nCust_Percentage_Diff"] = (scaledncust_vs_trial["t_nCustomers"] - scaledncust_vs_trial["c_ScaledNcust"]) / (((scaledncust_vs_trial["t_nCustomers"] + scaledncust_vs_trial["c_ScaledNcust"])/2))
scaledncust_vs_trial["trial_period"] = scaledncust_vs_trial["YEARMONTH"].apply(lambda cell: label_period(cell))
scaledncust_vs_trial[scaledncust_vs_trial["trial_period"] == "trial"]
# Check significance of Trial minus Control stores nCustomers Percentage Difference Pre-Trial vs Trial.
# - Step 1: Check null hypothesis of 0 difference between control store's Pre-Trial and Trial period performance.
# - Step 2: Proof control and trial stores are similar statistically
# - Step 3: After checking Null Hypothesis of first 2 step to be true, we can check Null Hypothesis of Percentage Difference between Trial and Control stores during pre-trial is the same as during trial.
# In[92]:
# Step 1
for num in [40, 155, 233]:
print("Store", num)
print(ttest_ind(pretrial_scaled_ncust_control_stores[pretrial_scaled_ncust_control_stores["STORE_NBR"] == num]["ScaledNcust"],
trial_scaled_ncust_control_stores[trial_scaled_ncust_control_stores["STORE_NBR"] == num]["ScaledNcust"],
equal_var=False), '\n')
alpha = 0.05
print("Critical t-value for 95% confidence interval:")
print(t.ppf((alpha/2, 1-alpha/2), df=min([len(pretrial_scaled_ncust_control_stores[pretrial_scaled_ncust_control_stores["STORE_NBR"] == num]),
len(trial_scaled_ncust_control_stores[trial_scaled_ncust_control_stores["STORE_NBR"] == num])])-1))
# In[93]:
# Step 2
for trial, cont in trial_control_dic.items():
print("Trial store:", trial, ", Control store:", cont)
print(ttest_ind(pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == trial]["nCustomers"],
pretrial_scaled_ncust_control_stores[pretrial_scaled_ncust_control_stores["STORE_NBR"] == cont]["ScaledNcust"],
equal_var=True), '\n')
alpha = 0.05
print("Critical t-value for 95% confidence interval:")
print(t.ppf((alpha/2, 1-alpha/2), df=len(pretrial_full_observ[pretrial_full_observ["STORE_NBR"] == trial])-1))
# In[94]:
# Step 3
for trial, cont in trial_control_dic.items():
print("Trial store:", trial, ", Control store:", cont)
temp_pre = scaledncust_vs_trial[(scaledncust_vs_trial["c_STORE_NBR"] == cont) & (scaledncust_vs_trial["trial_period"]=="pre")]
std = temp_pre["nCust_Percentage_Diff"].std()
mean = temp_pre["nCust_Percentage_Diff"].mean()
#print(std, mean)
for t_month in scaledncust_vs_trial[scaledncust_vs_trial["trial_period"] == "trial"]["YEARMONTH"].unique():
pdif = scaledncust_vs_trial[(scaledncust_vs_trial["YEARMONTH"] == t_month) & (scaledncust_vs_trial["t_STORE_NBR"] == trial)]["nCust_Percentage_Diff"]
print(t_month,":",(float(pdif)-mean)/std)
print('\n')
print("Critical t-value for 95% confidence interval:")
conf_intv_95 = t.ppf(0.95, df=len(temp_pre)-1)
print(conf_intv_95)
# There are 5 months' increase in performance that are statistically significant (Above the 95% confidence interval t-score):
# - March and April trial months for trial store 77
# - Feb, March and April trial months for trial store 86
# In[128]:
for trial, control in trial_control_dic.items():
a = trial_scaled_ncust_control_stores[trial_scaled_ncust_control_stores["STORE_NBR"] == control].rename(columns={"nCustomers": "control_nCustomers"})
b = trial_full_observ[trial_full_observ["STORE_NBR"] == trial][["STORE_NBR", "YEARMONTH", "nCustomers"]].rename(columns={"nCustomers": "trial_nCustomers"})
comb = b[["YEARMONTH", "trial_nCustomers"]].merge(a[["YEARMONTH", "control_nCustomers"]],on="YEARMONTH").set_index("YEARMONTH")
comb.plot.bar()
cont_sc_ncust = trial_scaled_ncust_control_stores[trial_scaled_ncust_control_stores["STORE_NBR"] == control]["nCustomers"]
std = scaledncust_vs_trial[(scaledncust_vs_trial["c_STORE_NBR"] == control) & (scaledncust_vs_trial["trial_period"]=="pre")]["nCust_Percentage_Diff"].std()
thresh95 = cont_sc_ncust.mean() + (cont_sc_ncust.mean() * std * 2)
thresh5 = cont_sc_ncust.mean() - (cont_sc_ncust.mean() * std * 2)
plt.axhline(y=thresh95,linewidth=1, color='b', label="95% threshold")
plt.axhline(y=thresh5,linewidth=1, color='r', label="5% threshold")
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Trial Store "+str(trial)+" and Control Store "+str(control)+" - Number of Customers")
plt.savefig("TS {} and CS {} - nCustomers.png".format(trial,control), bbox_inches="tight")
# We can see that Trial store 77 sales for Feb, March, and April exceeds 95% threshold of control store.
# Same goes to store 86 sales for all 3 trial months.
# - Trial store 77: Control store 233
# - Trial store 86: Control store 155
# - Trial store 88: Control store 40
# - Both trial store 77 and 86 showed significant increase in Total Sales and Number of Customers during trial period. But not for trial store 88. Perhaps the client knows if there's anything about trial 88 that differs it from the other two trial.
# - Overall the trial showed positive significant result.