WEIGHTEDTAU

Overview

The WEIGHTEDTAU function computes a weighted version of Kendall’s τ (tau) correlation coefficient, a non-parametric measure of ordinal association between two ranked variables. Unlike standard Kendall’s τ, which treats all element exchanges equally, weighted τ assigns greater influence to exchanges involving high-importance elements, making it particularly useful for evaluating ranking algorithms, information retrieval systems, and recommendation engines.

This implementation uses the SciPy library’s weightedtau function, which employs an efficient O(n \log n) mergesort-based algorithm developed by Sebastiano Vigna. For complete technical details, see the SciPy weightedtau documentation and the SciPy GitHub repository.

The algorithm extends Knight’s method for computing Kendall’s τ to incorporate element weights. Weights are assigned based on rank position using a hyperbolic weighting function, where rank r (zero-indexed, with 0 being the highest importance) maps to weight \frac{1}{r+1}. The weight of an exchange between elements at ranks r and s depends on the additive parameter:

  • Additive weighting (default): Exchange weight = \frac{1}{r+1} + \frac{1}{s+1}
  • Multiplicative weighting: Exchange weight = \frac{1}{r+1} \times \frac{1}{s+1}

The additive hyperbolic variant, denoted \tau_h, has been shown to provide the best balance between emphasizing important elements while still accounting for less important ones. The weighted τ coefficient ranges from −1 (complete disagreement) to +1 (complete agreement), with 0 indicating no association.

When the rank parameter is set to TRUE (default), the function averages the weighted τ values computed using the decreasing lexicographical ranks by (x, y) and by (y, x), providing a symmetric measure without requiring an external ranking criterion. Setting rank to FALSE uses element indices directly as ranks.

For theoretical background on Kendall’s τ and rank correlation methods, see the Wikipedia article on Kendall rank correlation coefficient. The weighted variant is described in Vigna’s 2015 paper “A weighted correlation index for rankings with ties” presented at the 24th International Conference on World Wide Web.

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

Excel Usage

=WEIGHTEDTAU(x, y, rank, additive)
  • x (list[list], required): First set of rankings or observations (column vector)
  • y (list[list], required): Second set of rankings or observations (column vector), same length as x
  • rank (bool, optional, default: true): If true, the input is treated as ranks; if false, values are ranked internally
  • additive (bool, optional, default: true): If true, uses additive hyperbolic weighting; if false, uses multiplicative weighting

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

Examples

Example 1: Demo case 1

Inputs:

x y rank additive
12 1 true true
2 4
1 7
12 1
2 0

Excel formula:

=WEIGHTEDTAU({12;2;1;12;2}, {1;4;7;1;0}, TRUE, TRUE)

Expected output:

Result
-0.5669

Example 2: Demo case 2

Inputs:

x y rank additive
12 1 true false
2 4
1 7
12 1
2 0

Excel formula:

=WEIGHTEDTAU({12;2;1;12;2}, {1;4;7;1;0}, TRUE, FALSE)

Expected output:

Result
-0.6221

Example 3: Demo case 3

Inputs:

x y rank additive
12 1 false true
2 4
1 7
12 1
2 0

Excel formula:

=WEIGHTEDTAU({12;2;1;12;2}, {1;4;7;1;0}, FALSE, TRUE)

Expected output:

Result
-0.516

Example 4: Demo case 4

Inputs:

x y rank additive
1 2 true true
2 3
3 4
4 5
5 6

Excel formula:

=WEIGHTEDTAU({1;2;3;4;5}, {2;3;4;5;6}, TRUE, TRUE)

Expected output:

Result
1

Python Code

from scipy.stats import weightedtau as scipy_weightedtau

def weightedtau(x, y, rank=True, additive=True):
    """
    Compute a weighted version of Kendall's tau correlation coefficient.

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

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

    Args:
        x (list[list]): First set of rankings or observations (column vector)
        y (list[list]): Second set of rankings or observations (column vector), same length as x
        rank (bool, optional): If true, the input is treated as ranks; if false, values are ranked internally Default is True.
        additive (bool, optional): If true, uses additive hyperbolic weighting; if false, uses multiplicative weighting Default is True.

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

    def flatten(arr):
        flat = []
        for row in arr:
            if isinstance(row, list):
                flat.extend(row)
            else:
                flat.append(row)
        return flat

    x = to2d(x)
    y = to2d(y)

    try:
        x_array = [float(val) for val in flatten(x)]
        y_array = [float(val) for val in flatten(y)]
    except (ValueError, TypeError):
        return "Invalid input: x and y must contain numeric values."

    if len(x_array) != len(y_array):
        return "Invalid input: x and y must have the same length."

    if len(x_array) < 2:
        return "Invalid input: x and y must have at least 2 elements."

    if not isinstance(rank, bool):
        return "Invalid input: rank must be a boolean value."
    if not isinstance(additive, bool):
        return "Invalid input: additive must be a boolean value."

    try:
        result = scipy_weightedtau(x_array, y_array, rank=rank, additive=additive)
        return [[float(result.statistic)]]
    except Exception as e:
        return f"Calculation error: {str(e)}"

Online Calculator