BWS_TEST
Overview
The BWS_TEST
function performs the Baumgartner-Weiss-Schindler (BWS) test to compare two independent samples. The BWS test is a nonparametric statistical test that evaluates whether two samples come from the same distribution, with particular sensitivity to differences in the tails of the distributions. Unlike parametric tests such as the t-test, the BWS test does not assume normality or equal variances, making it suitable for a wider range of data types. The test statistic is calculated as:
where and are the empirical distribution functions of samples and , and are the ranks of the combined samples. For more details, see the scipy.stats.bws_test documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=BWS_TEST(x, y, [alternative])
x
(2D list, required): First sample. Must be a 2D array (column vector) with at least two rows, containing numeric values.y
(2D list, required): Second sample. Must be a 2D array (column vector) with at least two rows, containing numeric values.alternative
(string, optional, default=‘two-sided’): Defines the alternative hypothesis. Must be one oftwo-sided
,less
, orgreater
.
The function returns a single-row 2D array: [statistic, pvalue]
, where statistic
is the BWS test statistic and pvalue
is the p-value for the test. If the input is invalid, an error message (string) is returned instead.
Examples
Example 1: Basic Case
Inputs:
x | y |
---|---|
1.2 | 2.1 |
2.3 | 2.9 |
3.1 | 3.7 |
4.0 | 4.5 |
Excel formula:
=BWS_TEST({1.2;2.3;3.1;4.0}, {2.1;2.9;3.7;4.5})
Expected output:
statistic | pvalue |
---|---|
0.326 | 1.000 |
Example 2: With alternative = “less”
Inputs:
x | y | alternative |
---|---|---|
1.2 | 2.1 | less |
2.3 | 2.9 | |
3.1 | 3.7 | |
4.0 | 4.5 |
Excel formula:
=BWS_TEST({1.2;2.3;3.1;4.0}, {2.1;2.9;3.7;4.5}, "less")
Expected output:
statistic | pvalue |
---|---|
-0.326 | 0.357 |
Example 3: With alternative = “greater”
Inputs:
x | y | alternative |
---|---|---|
1.2 | 2.1 | greater |
2.3 | 2.9 | |
3.1 | 3.7 | |
4.0 | 4.5 |
Excel formula:
=BWS_TEST({1.2;2.3;3.1;4.0}, {2.1;2.9;3.7;4.5}, "greater")
Expected output:
statistic | pvalue |
---|---|
-0.326 | 0.657 |
Example 4: Different Samples
Inputs:
x | y |
---|---|
10.0 | 2.0 |
12.0 | 3.0 |
13.0 | 4.0 |
15.0 | 5.0 |
Excel formula:
=BWS_TEST({10.0;12.0;13.0;15.0}, {2.0;3.0;4.0;5.0})
Expected output:
statistic | pvalue |
---|---|
3.711 | 0.029 |
Python Code
from scipy.stats import bws_test as scipy_bws_test
from typing import List, Union
def bws_test(x: List[List[float]], y: List[List[float]], alternative: str = 'two-sided') -> Union[List[List[float]], str]:
"""
Performs the Baumgartner-Weiss-Schindler test on two independent samples.
Args:
x: 2D list of float values. First sample.
y: 2D list of float values. Second sample.
alternative: Defines the alternative hypothesis ('two-sided', 'less', 'greater'). Default is 'two-sided'.
Returns:
2D list with a single row: [statistic, pvalue], or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate x and y
if not (isinstance(x, list) and all(isinstance(row, list) for row in x)) or len(x) < 2:
return "Invalid input: x must be a 2D list with at least two rows."
if not (isinstance(y, list) and all(isinstance(row, list) for row in y)) or len(y) < 2:
return "Invalid input: y must be a 2D list with at least two rows."
try:
x_flat = [float(item) for row in x for item in row]
y_flat = [float(item) for row in y for item in row]
except Exception:
return "Invalid input: x and y must contain only numeric values."
if alternative not in ['two-sided', 'less', 'greater']:
return "Invalid input: alternative must be 'two-sided', 'less', or 'greater'."
try:
result = scipy_bws_test(x_flat, y_flat, alternative=alternative)
except Exception as e:
return f"scipy.stats.bws_test error: {e}"
stat, pvalue = result.statistic, result.pvalue
# Convert to native float if needed
try:
stat = float(stat)
pvalue = float(pvalue)
except Exception:
return "Invalid output: statistic or pvalue could not be converted to float."
# Disallow nan/inf
if any([isinstance(val, float) and (val != val or val in [float('inf'), float('-inf')]) for val in [stat, pvalue]]):
return "Invalid output: statistic or pvalue is nan or inf."
return [[stat, pvalue]]