ABalytics 1.2.1__tar.gz → 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: abalytics
3
- Version: 1.2.1
3
+ Version: 2.0.0
4
4
  Summary: Advanced A/B Testing Statistical Analytics
5
5
  Author: Janis Berneker
6
6
  License: LICENSE
@@ -42,23 +42,23 @@ import abalytics
42
42
 
43
43
  ### Analyzing Results
44
44
 
45
- To analyze your A/B testing results, you can use the `get_results` function. This function takes a pandas DataFrame, the name of the column containing the variable to analyze, the name of the column containing the grouping variable, and an optional p-value threshold (default is 0.05).
45
+ To analyze your A/B testing results, you can use the two functions.
46
+ `analyze_independent_groups` takes a pandas DataFrame, the name of the column containing the variable to analyze, the name of the column containing the grouping variable, and an optional p-value threshold (default is 0.05) and min_sample_size (default is 25).
47
+ `analyze_dependent_groups` takes a pandas DataFrame, the names of the columns to compare, and an optional p-value threshold (default is 0.05) and min_sample_size (default is 25).
46
48
 
47
- Here's an example of how to use `get_results`:
49
+ Here's an example of how to use `analyze_independent_groups`:
48
50
  ```python
49
- from abalytics.analysis import get_results
51
+ import abalytics
50
52
  import pandas as pd
51
53
 
52
54
  #Load your data into a pandas DataFrame
53
55
  df = pd.read_csv('your_data.csv')
54
56
 
55
57
  #Analyze the results
56
- analysis_results = get_results(
58
+ analysis_results = abalytics.analyze_independent_groups(
57
59
  df,
58
60
  variable_to_analyze = "order_value",
59
61
  group_column = "ab_test_group",
60
- p_value_threshold=0.05,
61
- min_sample_size=25,
62
62
  )
63
63
  ```
64
64
  The `get_results` function will return an `AnalysisResults` object containing the following attributes:
@@ -71,21 +71,26 @@ The `get_results` function will return an `AnalysisResults` object containing th
71
71
 
72
72
  ### Generating Pretty Text Output
73
73
 
74
- For a formatted text output of your results, you can use the `get_results_pretty_text` function. This function takes the same parameters as `get_results` and additionally allows for identifiers to be included in the output and whether to include a header.
74
+ To get a formatted text output of your results, you can use the `utils.format_results_as_table` function.
75
75
 
76
- Here's an example of how to use `get_results_pretty_text`:
76
+ Here's an example of how to use format_results_as_table`:
77
77
  ```python
78
- from abalytics.analysis import get_results_pretty_text
78
+ from abalytics
79
79
 
80
- # Assuming 'df' is your pandas DataFrame
81
- pretty_text = get_results_pretty_text(
80
+ #Load your data into a pandas DataFrame
81
+ df = pd.read_csv('your_data.csv')
82
+
83
+ #Analyze the results
84
+ analysis_results = abalytics.analyze_independent_groups(
82
85
  df,
83
86
  variable_to_analyze = "order_value",
84
87
  group_column = "ab_test_group",
85
- p_value_threshold=0.05,
86
- min_sample_size=25,
87
- header=True,
88
- identifiers=["A/B Test 1", "Mobile"],
88
+ )
89
+
90
+ # Assuming 'df' is your pandas DataFrame
91
+ pretty_text = abalytics.utils.format_results_as_table(
92
+ abalytics_results=[analysis_results],
93
+ identifiers_list=[["A/B Test 1", "Mobile"]],
89
94
  )
90
95
 
91
96
  print(pretty_text)
@@ -26,23 +26,23 @@ import abalytics
26
26
 
27
27
  ### Analyzing Results
28
28
 
29
- To analyze your A/B testing results, you can use the `get_results` function. This function takes a pandas DataFrame, the name of the column containing the variable to analyze, the name of the column containing the grouping variable, and an optional p-value threshold (default is 0.05).
29
+ To analyze your A/B testing results, you can use the two functions.
30
+ `analyze_independent_groups` takes a pandas DataFrame, the name of the column containing the variable to analyze, the name of the column containing the grouping variable, and an optional p-value threshold (default is 0.05) and min_sample_size (default is 25).
31
+ `analyze_dependent_groups` takes a pandas DataFrame, the names of the columns to compare, and an optional p-value threshold (default is 0.05) and min_sample_size (default is 25).
30
32
 
31
- Here's an example of how to use `get_results`:
33
+ Here's an example of how to use `analyze_independent_groups`:
32
34
  ```python
33
- from abalytics.analysis import get_results
35
+ import abalytics
34
36
  import pandas as pd
35
37
 
36
38
  #Load your data into a pandas DataFrame
37
39
  df = pd.read_csv('your_data.csv')
38
40
 
39
41
  #Analyze the results
40
- analysis_results = get_results(
42
+ analysis_results = abalytics.analyze_independent_groups(
41
43
  df,
42
44
  variable_to_analyze = "order_value",
43
45
  group_column = "ab_test_group",
44
- p_value_threshold=0.05,
45
- min_sample_size=25,
46
46
  )
47
47
  ```
48
48
  The `get_results` function will return an `AnalysisResults` object containing the following attributes:
@@ -55,21 +55,26 @@ The `get_results` function will return an `AnalysisResults` object containing th
55
55
 
56
56
  ### Generating Pretty Text Output
57
57
 
58
- For a formatted text output of your results, you can use the `get_results_pretty_text` function. This function takes the same parameters as `get_results` and additionally allows for identifiers to be included in the output and whether to include a header.
58
+ To get a formatted text output of your results, you can use the `utils.format_results_as_table` function.
59
59
 
60
- Here's an example of how to use `get_results_pretty_text`:
60
+ Here's an example of how to use format_results_as_table`:
61
61
  ```python
62
- from abalytics.analysis import get_results_pretty_text
62
+ from abalytics
63
63
 
64
- # Assuming 'df' is your pandas DataFrame
65
- pretty_text = get_results_pretty_text(
64
+ #Load your data into a pandas DataFrame
65
+ df = pd.read_csv('your_data.csv')
66
+
67
+ #Analyze the results
68
+ analysis_results = abalytics.analyze_independent_groups(
66
69
  df,
67
70
  variable_to_analyze = "order_value",
68
71
  group_column = "ab_test_group",
69
- p_value_threshold=0.05,
70
- min_sample_size=25,
71
- header=True,
72
- identifiers=["A/B Test 1", "Mobile"],
72
+ )
73
+
74
+ # Assuming 'df' is your pandas DataFrame
75
+ pretty_text = abalytics.utils.format_results_as_table(
76
+ abalytics_results=[analysis_results],
77
+ identifiers_list=[["A/B Test 1", "Mobile"]],
73
78
  )
74
79
 
75
80
  print(pretty_text)
@@ -0,0 +1,10 @@
1
+ from .analysis import analyze_independent_groups, analyze_dependent_groups
2
+ from . import significance_tests
3
+ from . import utils
4
+
5
+ __all__ = [
6
+ 'analyze_dependent_groups',
7
+ 'analyze_independent_groups',
8
+ 'significance_tests',
9
+ 'utils'
10
+ ]
@@ -0,0 +1,271 @@
1
+ from typing import Optional, List
2
+
3
+ import pandas as pd
4
+ from pydantic import BaseModel, Field
5
+
6
+ from .significance_tests import (
7
+ is_dichotomous,
8
+ is_numeric,
9
+ is_levene_significant,
10
+ is_gaussian,
11
+ get_chi_square_significance,
12
+ get_chi_square_posthoc_results,
13
+ get_welch_anova_significance,
14
+ get_oneway_anova_significance,
15
+ get_crushal_wallis_significance,
16
+ get_games_howell_posthoc_results,
17
+ get_tukeyhsd_posthoc_results,
18
+ get_dunn_posthoc_results,
19
+ get_mcnemar_results,
20
+ get_repeated_measures_anova_significance,
21
+ get_repeated_measures_anova_posthoc_results,
22
+ get_wilcoxon_results,
23
+ get_friedman_significance,
24
+ get_nemenyi_results,
25
+ )
26
+
27
+
28
+ class AnalysisResults(BaseModel):
29
+ significant_results: Optional[List] = Field(
30
+ default=[],
31
+ description="A list of significant results from the statistical tests.",
32
+ )
33
+ info: Optional[str] = Field(
34
+ default=None,
35
+ description="Additional information or notes regarding the analysis.",
36
+ )
37
+ sample_size: Optional[int] = Field(
38
+ default=None, description="The sample size used in the analysis."
39
+ )
40
+ dichotomous_flag: Optional[bool] = Field(
41
+ default=None,
42
+ description="Indicates if the data analyzed is dichotomous.",
43
+ )
44
+ levene_flag: Optional[bool] = Field(
45
+ default=None,
46
+ description="Indicates if Levene's test for homogeneity of variances is significant.",
47
+ )
48
+ gaussian_flag: Optional[bool] = Field(
49
+ default=None,
50
+ description="Indicates if the data follows a Gaussian distribution.",
51
+ )
52
+
53
+
54
+ def analyze_independent_groups(
55
+ df: pd.DataFrame,
56
+ variable_to_analyze: str,
57
+ group_column: str,
58
+ p_value_threshold: float = 0.05,
59
+ min_sample_size: int = 25,
60
+ ) -> Optional[AnalysisResults]:
61
+ """
62
+ Returns a text with the results of the statistical significance tests.
63
+
64
+ Parameters:
65
+ df (pandas.DataFrame): The dataframe containing the data to analyze.
66
+ variable_to_analyze (str): The column name in df that is to be analyzed for statistical significance.
67
+ group_column (str): The name of the column in df that contains the grouping variable.
68
+ p_value_threshold (float, optional): The threshold for determining statistical significance. Defaults to 0.05.
69
+ min_sample_size (int, optional): The minimum sample size for the data. Defaults to 25.
70
+
71
+ Returns:
72
+ AnalysisResults: An instance of the AnalysisResults class containing the following attributes:
73
+ - significant_results: A list of results of the statistical significance tests.
74
+ - info: A string containing information about the data if no results were found.
75
+ - sample_size: The sample size of the data.
76
+ - dichotomous_flag: A boolean flag indicating if the data is dichotomous (e.g. boolean).
77
+ - levene_flag: A boolean flag indicating if Levene's test for homogeneity of variances is significant.
78
+ - gaussian_flag: A boolean flag indicating if the data has a Gaussian distribution.
79
+ """
80
+ if p_value_threshold > 0.05:
81
+ print(
82
+ "Warning: p_value_threshold is set to a value higher than the conventional alpha level of 0.05."
83
+ )
84
+
85
+ if variable_to_analyze not in df.columns or group_column not in df.columns:
86
+ print(
87
+ f"Both columns {variable_to_analyze} and {group_column} have to be present in the DataFrame."
88
+ )
89
+ return None
90
+
91
+ df = df.dropna(subset=[variable_to_analyze])
92
+ sample_size = len(df)
93
+ pvalue = None
94
+ info = None
95
+ dichotomous_flag = False
96
+ levene_flag = False
97
+ gaussian_flag = False
98
+ # Check if any group has less than the minimum sample size
99
+ if (
100
+ df.groupby(group_column)[variable_to_analyze]
101
+ .apply(lambda x: len(x) < min_sample_size)
102
+ .any()
103
+ ):
104
+ info = "not enough data"
105
+ # Check if the variable is dichotomous (e.g. boolean)
106
+ elif dichotomous_flag := is_dichotomous(df, variable_to_analyze):
107
+ # Perform chi-square test
108
+ pvalue = get_chi_square_significance(
109
+ df, group_column, variable_to_analyze
110
+ )
111
+ if pvalue < p_value_threshold:
112
+ results = get_chi_square_posthoc_results(
113
+ df, group_column, variable_to_analyze, p_value_threshold
114
+ )
115
+ elif is_numeric(df, variable_to_analyze):
116
+ # Check homogeneity of variances. If significant, use non-parametric tests
117
+ if levene_flag := is_levene_significant(
118
+ df, group_column, variable_to_analyze, p_value_threshold
119
+ ):
120
+ pvalue = get_welch_anova_significance(
121
+ df, group_column, variable_to_analyze
122
+ )
123
+ if pvalue < p_value_threshold:
124
+ results = get_games_howell_posthoc_results(
125
+ df, group_column, variable_to_analyze, p_value_threshold
126
+ )
127
+ # Check if the data has Gaussian distribution, if not use non-parametric tests
128
+ elif gaussian_flag := is_gaussian(
129
+ df, variable_to_analyze, p_value_threshold, group_column
130
+ ):
131
+ pvalue = get_oneway_anova_significance(
132
+ df, group_column, variable_to_analyze
133
+ )
134
+ if pvalue < p_value_threshold:
135
+ results = get_tukeyhsd_posthoc_results(
136
+ df, group_column, variable_to_analyze, p_value_threshold
137
+ )
138
+ else:
139
+ pvalue = get_crushal_wallis_significance(
140
+ df, group_column, variable_to_analyze
141
+ )
142
+ if pvalue < p_value_threshold:
143
+ results = get_dunn_posthoc_results(
144
+ df, group_column, variable_to_analyze, p_value_threshold
145
+ )
146
+ else:
147
+ print("Variable has to be dichotomous (e.g. boolean) or numeric")
148
+ info = "Variable has to be dichotomous (e.g. boolean) or numeric"
149
+
150
+ # If no results were found, return a message
151
+ if not "results" in locals() or len(results.significant_results) == 0:
152
+ if pvalue is not None:
153
+ info = f"not significant (p={pvalue:.3f})"
154
+ # If results were found, return them
155
+ if "results" in locals():
156
+ significant_results = results.significant_results
157
+ else:
158
+ significant_results = []
159
+
160
+ return AnalysisResults(
161
+ significant_results=significant_results,
162
+ info=info,
163
+ sample_size=sample_size,
164
+ dichotomous_flag=dichotomous_flag,
165
+ levene_flag=levene_flag,
166
+ gaussian_flag=gaussian_flag,
167
+ )
168
+
169
+
170
+ def analyze_dependent_groups(
171
+ df: pd.DataFrame,
172
+ variables_to_compare: List[str],
173
+ p_value_threshold: float = 0.05,
174
+ min_sample_size: int = 25,
175
+ ) -> Optional[AnalysisResults]:
176
+ """
177
+ Analyzes dependent groups using appropriate statistical tests and returns an AnalysisResults object.
178
+
179
+ Parameters:
180
+ df (pandas.DataFrame): The dataframe containing the data to analyze.
181
+ variables_to_compare (List[str]): The column names in df that are to be analyzed for statistical significance.
182
+ p_value_threshold (float, optional): The threshold for determining statistical significance. Defaults to 0.05.
183
+ min_sample_size (int, optional): The minimum sample size for the data. Defaults to 25.
184
+
185
+ Returns:
186
+ AnalysisResults: An instance of the AnalysisResults class containing the following attributes:
187
+ - significant_results: A list of results of the statistical significance tests.
188
+ - info: A string containing information about the data if no results were found.
189
+ - sample_size: The sample size of the data.
190
+ - dichotomous_flag: A boolean flag indicating if the data is dichotomous (e.g. boolean).
191
+ - levene_flag: A boolean flag indicating if Levene's test for homogeneity of variances is significant.
192
+ - gaussian_flag: A boolean flag indicating if the data has a Gaussian distribution.
193
+ """
194
+ if p_value_threshold > 0.05:
195
+ print(
196
+ "Warning: p_value_threshold is set to a value higher than the conventional alpha level of 0.05."
197
+ )
198
+
199
+ missing_columns = [
200
+ variable
201
+ for variable in variables_to_compare
202
+ if variable not in df.columns
203
+ ]
204
+ if missing_columns:
205
+ print(f"Columns not found in DataFrame: {', '.join(missing_columns)}")
206
+ return None
207
+
208
+ df = df.dropna(subset=variables_to_compare)
209
+ sample_size = len(df)
210
+ pvalue = None
211
+ info = None
212
+ dichotomous_flag = False
213
+ levene_flag = False
214
+ gaussian_flag = False
215
+ results = None
216
+
217
+ # Check if any variable has less than the minimum sample size
218
+ if any(
219
+ df[variable].count() < min_sample_size
220
+ for variable in variables_to_compare
221
+ ):
222
+ info = "not enough data"
223
+ # Check if the variables are dichotomous (e.g. boolean)
224
+ elif all(is_dichotomous(df, variable) for variable in variables_to_compare):
225
+ dichotomous_flag = True
226
+ # Perform McNemar's test or its extension for more than two groups
227
+ pvalue, results = get_mcnemar_results(
228
+ df, variables_to_compare, p_value_threshold
229
+ )
230
+ elif all(is_numeric(df, variable) for variable in variables_to_compare):
231
+ # Check if the data has Gaussian distribution, if not use non-parametric tests
232
+ if gaussian_flag := all(
233
+ is_gaussian(df, variable, p_value_threshold)
234
+ for variable in variables_to_compare
235
+ ):
236
+ pvalue = get_repeated_measures_anova_significance(
237
+ df, variables_to_compare
238
+ )
239
+ if pvalue < p_value_threshold:
240
+ results = get_repeated_measures_anova_posthoc_results(
241
+ df, variables_to_compare, p_value_threshold
242
+ )
243
+ elif len(variables_to_compare) == 2:
244
+ pvalue, results = get_wilcoxon_results(
245
+ df, variables_to_compare, p_value_threshold
246
+ )
247
+ else:
248
+ pvalue = get_friedman_significance(df, variables_to_compare)
249
+ if pvalue < p_value_threshold:
250
+ results = get_nemenyi_results(
251
+ df, variables_to_compare, p_value_threshold
252
+ )
253
+ else:
254
+ print("All variables have to be dichotomous (e.g. boolean) or numeric")
255
+ info = "All variables have to be dichotomous (e.g. boolean) or numeric"
256
+
257
+ # If no results were found, return a message
258
+ if not results or len(results.significant_results) == 0:
259
+ if pvalue is not None:
260
+ info = f"not significant (p={pvalue:.3f})"
261
+ # If results were found, return them
262
+ significant_results = results.significant_results if results else []
263
+
264
+ return AnalysisResults(
265
+ significant_results=significant_results,
266
+ info=info,
267
+ sample_size=sample_size,
268
+ dichotomous_flag=dichotomous_flag,
269
+ levene_flag=levene_flag,
270
+ gaussian_flag=gaussian_flag,
271
+ )