-
Notifications
You must be signed in to change notification settings - Fork 2
/
A-B_Testing.py
183 lines (142 loc) · 3.71 KB
/
A-B_Testing.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
import matplotlib.pyplot as plt
import pandas as pd
market_data = pd.read_csv('Marketing Campaign.csv')
#DATA ANALYSIS
print(market_data.shape,market_data.head())
#Total Sales
market_data['SalesInThousands'].describe()
ax = market_data.groupby(
'Promotion'
).sum()[
'SalesInThousands'
].plot.pie(
figsize=(7, 7),
autopct='%1.0f%%'
)
ax.set_ylabel('')
ax.set_title('sales distribution across different promotions')
plt.show()
#Market Size
market_data.groupby('MarketSize').count()['MarketID']
ax = market_data.groupby([
'Promotion', 'MarketSize'
]).count()[
'MarketID'
].unstack(
'MarketSize'
).plot(
kind='bar',
figsize=(12,10),
grid=True,
)
ax.set_ylabel('count')
ax.set_title('breakdowns of market sizes across different promotions')
plt.show()
ax = market_data.groupby([
'Promotion', 'MarketSize'
]).sum()[
'SalesInThousands'
].unstack(
'MarketSize'
).plot(
kind='bar',
figsize=(12,10),
grid=True,
stacked=True
)
ax.set_ylabel('Sales (in Thousands)')
ax.set_title('breakdowns of market sizes across different promotions')
plt.show()
#Age of Store
ax = market_data.groupby(
'AgeOfStore'
).count()[
'MarketID'
].plot(
kind='bar',
color='skyblue',
figsize=(10,7),
grid=True
)
ax.set_xlabel('age')
ax.set_ylabel('count')
ax.set_title('overall distributions of age of store')
plt.show()
ax = market_data.groupby(
['AgeOfStore', 'Promotion']
).count()[
'MarketID'
].unstack(
'Promotion'
).iloc[::-1].plot(
kind='barh',
figsize=(12,15),
grid=True
)
ax.set_ylabel('age')
ax.set_xlabel('count')
ax.set_title('overall distributions of age of store')
plt.show()
market_data.groupby('Promotion').describe()['AgeOfStore']
#Week Number
market_data.groupby('week').count()['MarketID']
market_data.groupby(['Promotion', 'week']).count()['MarketID']
ax1, ax2, ax3 = market_data.groupby(
['week', 'Promotion']
).count()[
'MarketID'
].unstack('Promotion').plot.pie(
subplots=True,
figsize=(24, 8),
autopct='%1.0f%%'
)
ax1.set_ylabel('Promotion #1')
ax2.set_ylabel('Promotion #2')
ax3.set_ylabel('Promotion #3')
ax1.set_xlabel('distribution across different weeks')
ax2.set_xlabel('distribution across different weeks')
ax3.set_xlabel('distribution across different weeks')
plt.show()
#STATISTICAL SIGNIFICANCE
import numpy as np
from scipy import stats
mean = market_data.groupby('Promotion').mean()['SalesInThousands']
print(mean)
std = market_data.groupby('Promotion').std()['SalesInThousands']
print(std)
ns = market_data.groupby('Promotion').count()['SalesInThousands']
print(ns)
#Promotion 1 VS 2
t_1_vs_2 = (
mean.iloc[0] - mean.iloc[1]
)/ np.sqrt(
(std.iloc[0]**2/ns.iloc[0]) + (std.iloc[1]**2/ns.iloc[1])
)
d_1_vs_1 = ns.iloc[0] + ns.iloc[1] - 2
p_1_vs_2 = (1 - stats.t.cdf(t_1_vs_2, df=d_1_vs_1))*2
print(t_1_vs_2)
print(p_1_vs_2)
t, p = stats.ttest_ind(
market_data.loc[market_data['Promotion'] == 1, 'SalesInThousands'].values,
market_data.loc[market_data['Promotion'] == 2, 'SalesInThousands'].values,
equal_var=False
)
print(t)
print(p)
#Promotion 1 vs 3
t_1_vs_3 = (
mean.iloc[0] - mean.iloc[2]
)/ np.sqrt(
(std.iloc[0]**2/ns.iloc[0]) + (std.iloc[2]**2/ns.iloc[2])
)
d_1_vs_3 = ns.iloc[0] + ns.iloc[1] - 2
p_1_vs_3 = (1 - stats.t.cdf(t_1_vs_3, df=d_1_vs_3))*2
print(t_1_vs_3)
print(p_1_vs_3)
t, p = stats.ttest_ind(
market_data.loc[market_data['Promotion'] == 1, 'SalesInThousands'].values,
market_data.loc[market_data['Promotion'] == 3, 'SalesInThousands'].values,
equal_var=False
)
print(t)
print(p)