-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101_Chi_Square_Test.py
More file actions
103 lines (41 loc) · 2.32 KB
/
101_Chi_Square_Test.py
File metadata and controls
103 lines (41 loc) · 2.32 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 2 06:54:29 2025
@author: kingsleylee
"""
# AB Testing - Our Task for ABC Grocery
import pandas as pd
from scipy.stats import chi2_contingency, chi2
# IMPORT DATA
campaign_data = pd.read_excel("grocery_database.xlsx", sheet_name = "campaign_data")
# FILTER OUR DATA
campaign_data = campaign_data.loc[campaign_data["mailer_type"] != "Control"]
# SUMMARISE TO GET OUR OBSERED FREQUENCIES
observed_values = pd.crosstab(campaign_data["mailer_type"], campaign_data["signup_flag"]).values
# To see in table format with labels:
observed_values = pd.crosstab(campaign_data["mailer_type"], campaign_data["signup_flag"])
print(observed_values)
mailer1_signup_rate = 123/ (252 + 123)
mailer2_signup_rate = 127/ (209 + 127)
print(mailer1_signup_rate, mailer2_signup_rate)
# STATE HYPOTHESIS & SET ACCEPTANCE CRITERIA
null_hypothesis = "There is no relationship between mailer type and signup rate. They are independent."
alternate_hypothesis = "There is a relationship between mailer type and signup rate. They are not independent."
acceptance_criteria = 0.05
# CALCULATE EXPECTED FREQUENCIES & CHI SQUARE STATISTIC
chi2_statistic, p_value, dof, expected_values = chi2_contingency(observed_values, correction = False)
print(chi2_statistic, p_value)
# FIND THE CRITICAL VALUE FOR OUR TEST
critical_value = chi2.ppf(1 - acceptance_criteria, dof)
print(critical_value)
# PRINT THE RESULTS (Chi Square Statistic)
if chi2_statistic >= critical_value:
print(f"As our chi-square statistic of {chi2_statistic} is higher than our critical value of {critical_value} - we reject the null hypothesis, and conclude that: {alternate_hypothesis}")
else:
print(f"As our chi-square statistic of {chi2_statistic} is lower than our critical value of {critical_value} - we retain the null hypothesis, and conclude that: {null_hypothesis}")
# PRINT THE RESULTS (p-value)
if p_value <= acceptance_criteria:
print(f"As our p-value of {p_value} is lower than our acceptance criteria of {acceptance_criteria} - we reject the null hypothesis, and conclude that: {alternate_hypothesis}")
else:
print(f"As our p-value of {p_value} is higher than our acceptance criteria of {acceptance_criteria} - we retain the null hypothesis, and conclude that: {null_hypothesis}")