The same units each have two ratings X and Y ordered from low to high. Measuring their association with Somers’ D, D(Y|X) and D(X|Y) can give different numbers. That is not an artifact of computation order: both directions compare the same concordant and discordant pairs, but they use different totals of pairs as the denominator.[1]

Pairwise comparisons first, then the denominator

Draw two units at random from independent units and compare their X and Y ratings. If the unit with higher X also has higher Y, the pair is concordant; if higher X pairs with lower Y, it is discordant. Let C be the number of concordant pairs and Q the number of discordant pairs. The difference C−Q says how many more pairs agree in order than disagree.

Ratings can also tie. Let T_X be pairs tied only on X, T_Y pairs tied only on Y, and T_b pairs tied on both. When computing D(Y|X), units are distinguished by X: all pairs tied on X are excluded, leaving C, Q, and T_Y. Pairs in T_Y, though tied on Y and contributing zero to the net concordant–discordant difference, remain in the denominator.[1]

D(Y∣X)=C−QC+Q+TY,D(X∣Y)=C−QC+Q+TX.D(Y\mid X)=\frac{C-Q}{C+Q+T_Y},\qquad D(X\mid Y)=\frac{C-Q}{C+Q+T_X}.

Computing D(X|Y) the other way excludes all pairs tied on Y, leaving C, Q, and T_X in the denominator. Pairs tied on both, T_b, are excluded in either direction. Swapping direction never changes whether a pair of units is concordant or discordant, so the numerator is the same; only when the two one-sided tie counts differ does the denominator differ. SciPy also writes the first form as D(Y|X)=τₐ(X,Y)/τₐ(X,X).[1]

A constructed example with 60 rating pairs

Construct 60 paired ratings with three levels of X and four of Y, rows and columns ordered from low to high. Each cell is the number of units with that row's X level and that column's Y level; the upper-left 12, for example, means 12 units sit at the lowest level on both ratings.

X / Y Y1 Y2 Y3 Y4 Row total
X1 12 3 1 0 16
X2 4 10 4 2 20
X3 0 3 8 13 24
Column total 16 16 13 15 60

Sixty units yield 60×59/2=1770 unordered pairs. Pairwise comparison gives C=934, Q=63, T_X=350, T_Y=187, T_b=236; the five classes sum to exactly 1770. In both directions the concordant–discordant difference is 934−63=871.

Three panels showing an ordinal rating frequency table, pair-classification counts, and association values in both directions

Sixty constructed ordinal rating pairs: left, contingency frequencies; middle, exact pairwise counts; right, different Somers’ D values after swapping direction. Numerators match; denominators exclude pairs tied on X or on Y, respectively. A recomputable teaching example.

D(Y∣X)=871934+63+187=8711184≈0.73564D(Y\mid X)=\frac{871}{934+63+187}=\frac{871}{1184}\approx0.73564 D(X∣Y)=871934+63+350=8711347≈0.64662D(X\mid Y)=\frac{871}{934+63+350}=\frac{871}{1347}\approx0.64662

In the first direction there are 1184 pairs with distinct X; in the second, 1347 pairs with distinct Y. The same net difference 871 divided by different pair counts yields different results. Here pairs tied only on X outnumber pairs tied only on Y, so D(X|Y) has the larger denominator and the smaller value.

How to read the number

Drawing a random pair among these 60 units, conditional on distinct X ratings, the concordant share minus the discordant share is about 0.73564. That is a difference of two proportions, not a 73.564% prediction accuracy. Positive values mean more concordant than discordant pairs; negative values mean the reverse; zero means the two counts cancel.

Direction should follow the research question: if the interest is how units that differ on X order on Y, report D(Y|X); if the interest is how units that differ on Y order on X, report D(X|Y). SciPy calls X the independent variable and Y the dependent variable for the first form; that names a statistical direction and does not itself mean X was manipulated or that X causes Y.[1]

Recomputing from a contingency table

SciPy's somersd accepts two equal-length rating columns or a two-way frequency table. Table rows correspond to X and columns to Y; transpose to compute the opposite direction. The returned object includes statistic, pvalue, and table.[1]

import numpy as np
from scipy.stats import somersd

table = np.array([[12, 3, 1, 0],
                  [4, 10, 4, 2],
                  [0, 3, 8, 13]])

forward = somersd(table)    # D(Y|X)
reverse = somersd(table.T)  # D(X|Y)
print(forward.statistic, forward.pvalue)
print(reverse.statistic, reverse.pvalue)

Output:

0.7356418918918919 9.460829619613381e-31
0.6466221232368226 9.460829619613381e-31

The default two-sided p-value targets population Somers’ D=0 with an asymptotic approximation; it is not an exact p-value for a given contingency table. In this constructed example the two directions share a p-value but not a coefficient: the test result and which denominator is used to report association strength are separate matters. Population D=0 is also not the same as independence of the two variables; failing to reject D=0 does not prove independence. SciPy's kendalltau and somersd may use different default nulls and approximations, so their p-values need not match exactly.[1]

Fix the order before input

The two ratings need only a clear order; levels need not be equally spaced. Contingency rows and columns must follow a predetermined natural order: arbitrarily permuting rows or columns changes which pairs count as concordant or discordant. Frequencies should be nonnegative integers, and two raw rating columns must keep the same unit pairing.

If every unit has the same X rating, no pairs differ on X, the denominator of D(Y|X) is zero, and that direction's coefficient is undefined—nan must not be read as zero association. When choosing a direction, first ask on which rating units must differ for the comparison, then check that the corresponding denominator exists.

References

  1. [1] SciPy community. scipy.stats.somersd. SciPy Manual.