ANSARI

Overview

The ANSARI function performs the Ansari-Bradley test, a non-parametric statistical test used to determine whether two independent samples have equal scale parameters (dispersion). Unlike tests for central tendency, this test specifically assesses whether the variability or spread of two distributions differs significantly.

The Ansari-Bradley test was introduced by Ansari and Bradley in 1960 and is particularly useful when the underlying distributions are unknown or non-normal. The test operates by ranking the combined samples and assigning scores based on the distance of each observation from the center of the combined distribution. Under the null hypothesis, both samples are drawn from distributions with identical scale parameters, meaning the ratio of scales equals 1.

The test statistic is computed by summing the Ansari-Bradley scores for one of the samples. For the alternative hypothesis options:

  • two-sided: tests whether the ratio of scales is not equal to 1
  • less: tests whether the ratio of scales (x to y) is less than 1
  • greater: tests whether the ratio of scales (x to y) is greater than 1

This implementation uses scipy.stats.ansari from the SciPy library. The p-value returned is exact when both sample sizes are less than 55 and there are no ties; otherwise, a normal approximation is used. For related non-parametric scale tests, see also the Mood test and Fligner-Killeen test.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=ANSARI(x, y, ansari_alternative)
  • x (list[list], required): First sample group (numeric values)
  • y (list[list], required): Second sample group (numeric values)
  • ansari_alternative (str, optional, default: “two-sided”): Defines the alternative hypothesis

Returns (list[list]): 2D list with two float values [[statistic, pvalue]], where statistic is the Ansari-Bradley test statistic and pvalue is the two-sided p-value for the test. str: Error message if input is invalid.

Examples

Example 1: Demo case 1

Inputs:

x y
1 2 5 6
3 4 7 8

Excel formula:

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

Expected output:

Result
10 1

Example 2: Demo case 2

Inputs:

x y ansari_alternative
1 2 5 6 less
3 4 7 8

Excel formula:

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

Expected output:

Result
10 0.6286

Example 3: Demo case 3

Inputs:

x y ansari_alternative
1 2 5 6 greater
3 4 7 8

Excel formula:

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

Expected output:

Result
10 0.6286

Example 4: Demo case 4

Inputs:

x y
10 20 50 60
30 40 70 80

Excel formula:

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

Expected output:

Result
10 1

Python Code

import math
from scipy.stats import ansari as scipy_ansari

def ansari(x, y, ansari_alternative='two-sided'):
    """
    Performs the Ansari-Bradley test for equal scale parameters (non-parametric) using scipy.stats.ansari.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ansari.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        x (list[list]): First sample group (numeric values)
        y (list[list]): Second sample group (numeric values)
        ansari_alternative (str, optional): Defines the alternative hypothesis Valid options: Two-sided, Less, Greater. Default is 'two-sided'.

    Returns:
        list[list]: 2D list with two float values [[statistic, pvalue]], where statistic is the Ansari-Bradley test statistic and pvalue is the two-sided p-value for the test. str: Error message if input is invalid.
    """
    def to2d(val):
        """Normalize input to 2D list format."""
        return [[val]] if not isinstance(val, list) else val

    # Normalize inputs
    x = to2d(x)
    y = to2d(y)

    # Validate inputs are 2D lists
    if not (isinstance(x, list) and all(isinstance(row, list) for row in x)):
        return "Invalid input: x must be a 2D list."
    if not (isinstance(y, list) and all(isinstance(row, list) for row in y)):
        return "Invalid input: y must be a 2D list."

    # 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 (TypeError, ValueError) as e:
        return f"Invalid input: x and y must contain only numeric values. {e}"

    # Validate sample sizes
    if len(x_flat) < 2:
        return "Invalid input: x must contain at least two values."
    if len(y_flat) < 2:
        return "Invalid input: y must contain at least two values."

    # Validate alternative parameter
    valid_alternatives = ['two-sided', 'less', 'greater']
    if ansari_alternative not in valid_alternatives:
        return f"Invalid input: ansari_alternative must be one of {valid_alternatives}."

    # Call scipy.stats.ansari
    try:
        stat, pvalue = scipy_ansari(x_flat, y_flat, alternative=ansari_alternative)
    except ValueError as e:
        return f"Calculation error: {e}"
    except Exception as e:
        return f"Unexpected error in scipy.stats.ansari: {e}"

    # Convert numpy types to Python float
    try:
        stat = float(stat)
        pvalue = float(pvalue)
    except (TypeError, ValueError) as e:
        return f"Invalid result: could not convert output to float. {e}"

    # Check for nan/inf
    if math.isnan(stat) or math.isinf(stat):
        return "Invalid result: statistic is NaN or infinite."
    if math.isnan(pvalue) or math.isinf(pvalue):
        return "Invalid result: pvalue is NaN or infinite."

    return [[stat, pvalue]]

Online Calculator