QUANTILE_TEST

Overview

The QUANTILE_TEST function performs a non-parametric hypothesis test to determine whether a specific quantile of a population equals a hypothesized value. This test is commonly used to assess whether the median (or any other quantile) of a dataset differs significantly from a reference value, making it valuable for quality control, distribution analysis, and exploratory data analysis.

The implementation uses the SciPy library’s quantile_test function, which follows the methodology described in W. J. Conover’s Practical Nonparametric Statistics (3rd Ed., 1999). For complete details, see the SciPy quantile_test documentation.

Quantiles represent values below which a specified proportion of the data falls. Common examples include the median (50th percentile, p = 0.5), the first quartile (25th percentile, p = 0.25), and the third quartile (75th percentile, p = 0.75). The test evaluates the null hypothesis:

H_0: \text{The } p\text{th population quantile equals } q

The test statistic is based on counting observations relative to the hypothesized quantile value. Two statistics are computed internally:

  • T_1: The number of observations less than or equal to q
  • T_2: The number of observations strictly less than q

Under the null hypothesis, these statistics follow a binomial distribution with parameters n (sample size) and p (the probability associated with the quantile):

Y \sim \text{Binomial}(n, p)

The p-value is calculated by comparing the observed test statistic to this binomial distribution. A small p-value (typically < 0.05) indicates evidence against the null hypothesis, suggesting the population quantile differs from the hypothesized value.

This non-parametric approach requires only that observations be independent and identically distributed (i.i.d.), making it applicable to data from any continuous, discrete, or mixed distribution without assumptions about the underlying distribution shape.

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

Excel Usage

=QUANTILE_TEST(x, q, p)
  • x (list[list], required): Sample data as a 2D array (column or row vector with at least two elements).
  • q (float, optional, default: 0): Hypothesized value of the quantile to test against.
  • p (float, optional, default: 0.5): Probability associated with the quantile (e.g., 0.5 for median, 0.25 for first quartile).

Returns (list[list]): 2D list [[statistic, p_value]], or error message string.

Examples

Example 1: Median test with hypothesized value 0

Inputs:

x q p
1 0 0.5
2
3
4
5

Excel formula:

=QUANTILE_TEST({1;2;3;4;5}, 0, 0.5)

Expected output:

Result
0 0.0625

Example 2: Median test with hypothesized value 3

Inputs:

x q p
1 3 0.5
2
3
4
5

Excel formula:

=QUANTILE_TEST({1;2;3;4;5}, 3, 0.5)

Expected output:

Result
3 1

Example 3: First quartile (p=0.25) test

Inputs:

x q p
1 2 0.25
2
3
4
5

Excel formula:

=QUANTILE_TEST({1;2;3;4;5}, 2, 0.25)

Expected output:

Result
1 1

Example 4: Third quartile (p=0.75) test

Inputs:

x q p
1 4 0.75
2
3
4
5

Excel formula:

=QUANTILE_TEST({1;2;3;4;5}, 4, 0.75)

Expected output:

Result
4 1

Python Code

from scipy.stats import quantile_test as scipy_quantile_test

def quantile_test(x, q=0, p=0.5):
    """
    Perform a quantile test to determine if a population quantile equals a hypothesized value.

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

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

    Args:
        x (list[list]): Sample data as a 2D array (column or row vector with at least two elements).
        q (float, optional): Hypothesized value of the quantile to test against. Default is 0.
        p (float, optional): Probability associated with the quantile (e.g., 0.5 for median, 0.25 for first quartile). Default is 0.5.

    Returns:
        list[list]: 2D list [[statistic, p_value]], or error message string.
    """
    def to2d(val):
        return [[val]] if not isinstance(val, list) else val

    x = to2d(x)

    # Flatten x to 1D list
    try:
        flat_x = []
        for row in x:
            if isinstance(row, list):
                flat_x.extend(row)
            else:
                flat_x.append(row)
        if len(flat_x) < 2:
            return "Invalid input: x must contain at least two elements."
        flat_x = [float(val) for val in flat_x]
    except (TypeError, ValueError):
        return "Invalid input: x must be a 2D list of numbers."

    try:
        q_val = float(q)
    except (TypeError, ValueError):
        return "Invalid input: q must be a number."

    try:
        p_val = float(p)
    except (TypeError, ValueError):
        return "Invalid input: p must be a number."

    if not (0 < p_val < 1):
        return "Invalid input: p must be between 0 and 1 (exclusive)."

    try:
        result = scipy_quantile_test(flat_x, q=q_val, p=p_val)
        return [[float(result.statistic), float(result.pvalue)]]
    except Exception as e:
        return f"Error in quantile_test: {e}"

Online Calculator