Skip to Content

ANSARI

Overview

The ANSARI function performs the Ansari-Bradley test, a non-parametric statistical test for comparing the scale (dispersion) of two independent samples. Unlike parametric tests such as the F-test, the Ansari-Bradley test does not assume normality and is robust to outliers. It is useful for determining whether two groups have similar variability, regardless of their means. The test statistic is based on the ranks of the combined samples:

W=i=1n1RiW = \sum_{i=1}^{n_1} R_i

where RiR_i are the ranks assigned to the first sample after combining both samples. For more details, see the scipy.stats.ansari documentation.

This wrapper exposes only the most commonly used parameters: the two sample arrays and the alternative hypothesis. Parameters related to axis selection, NaN handling, and dimensionality reduction are omitted for Excel compatibility. This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=ANSARI(x, y, [alternative])
  • x (2D list, required): First sample group. Must be a 2D array (table) with at least two rows.
  • y (2D list, required): Second sample group. Must be a 2D array (table) with at least two rows.
  • alternative (string, optional, default='two-sided'): Defines the alternative hypothesis. Allowed values: 'two-sided', 'less', 'greater'.

The function returns a 2D array with two float values: [[statistic, pvalue]], or an error message (string) if the input is invalid. The statistic measures the difference in scale between the two samples; the p-value indicates the probability of observing such a difference under the null hypothesis.

Examples

Example 1: Basic Case

Inputs:

xy
1.02.05.06.0
3.04.07.08.0

Excel formula:

=ANSARI({1,2;3,4}, {5,6;7,8})

Expected output:

statisticpvalue
10.0001.000

Example 2: With Optional Argument

Inputs:

xyalternative
1.02.05.06.0less
3.04.07.08.0

Excel formula:

=ANSARI({1,2;3,4}, {5,6;7,8}, "less")

Expected output:

statisticpvalue
10.0000.629

Example 3: All Arguments Specified

Inputs:

xyalternative
1.02.05.06.0greater
3.04.07.08.0

Excel formula:

=ANSARI({1,2;3,4}, {5,6;7,8}, "greater")

Expected output:

statisticpvalue
10.0000.629

Example 4: Different Values

Inputs:

xy
10.020.050.060.0
30.040.070.080.0

Excel formula:

=ANSARI({10,20;30,40}, {50,60;70,80})

Expected output:

statisticpvalue
10.0001.000

Python Code

from scipy.stats import ansari as scipy_ansari from typing import List, Union def ansari(x: List[List[float]], y: List[List[float]], alternative: str = 'two-sided') -> Union[List[List[float]], str]: """ Performs the Ansari-Bradley test for equal scale parameters (non-parametric). Args: x: 2D list of float values. First sample group. y: 2D list of float values. Second sample group. alternative: Defines the alternative hypothesis ('two-sided', 'less', 'greater'). Default is 'two-sided'. Returns: 2D list with two float values: [[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 are 2D lists with at least two rows if not (isinstance(x, list) and all(isinstance(row, list) for row in x) and 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) and len(y) >= 2): return "Invalid input: y must be a 2D list with at least two rows." # Flatten x and y 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 len(x_flat) < 2 or len(y_flat) < 2: return "Invalid input: each sample must contain at least two values." # Validate alternative if alternative not in ['two-sided', 'less', 'greater']: return "Invalid input: alternative must be 'two-sided', 'less', or 'greater'." # Call scipy.stats.ansari try: stat, pvalue = scipy_ansari(x_flat, y_flat, alternative=alternative) except Exception as e: return f"scipy.stats.ansari error: {e}" # Convert numpy types to Python float try: stat = float(stat) pvalue = float(pvalue) except Exception: return "Invalid result: could not convert output to float." # Check for nan/inf if any([isinstance(val, float) and (val != val or val in [float('inf'), float('-inf')]) for val in [stat, pvalue]]): return "Invalid result: statistic or pvalue is nan or inf." return [[stat, pvalue]]

Example Workbook

Link to Workbook

Last updated on