# Kendall's Tau-b

The correlation between two variables, $\displaystyle{ x }$ and $\displaystyle{ y }$, is:

$\displaystyle{ \tau_b = \frac{n_c-n_d}{\sqrt{(n_t-n_x)(n_t-n_y)}} }$

where

$\displaystyle{ n_c = \sum^n_{i=1} \sum^n_{j=1} w_i (I_{x_i\gt x_j,y_i\gt y_j}+I_{x_i\gt x_j,y_i\gt y_j} ) }$,
$\displaystyle{ n_d = \sum^n_{i=1} \sum^n_{j=1} w_i ( I_{x_i\lt x_j,y_i\gt y_j}+I_{x_i\gt x_j,y_i\lt y_j}) }$,
$\displaystyle{ n_w = \sum^n_{i=1} w_i }$,
$\displaystyle{ n_t =\frac{n_w(n_w-1)}{2} }$,
$\displaystyle{ n_x = \sum^t_{j=1} \sum^n_{i=n} w_i I_{x_i=j} }$,
$\displaystyle{ n_y = \sum^r_{j=1} \sum^n_{i=n} w_i I_{y_i=j} }$,
$\displaystyle{ w_i }$ is the Calibrated Weight for the $\displaystyle{ i }$th of $\displaystyle{ n }$ is the number of observations,
$\displaystyle{ x }$ is a variable with $\displaystyle{ t }$ unique values, categorised in the range $\displaystyle{ {{1,2,..,t}} }$,
$\displaystyle{ y }$ is a variable with $\displaystyle{ r }$ unique values, categorised in the range $\displaystyle{ {{1,2,..,r}} }$,

The tests statistic is:

$\displaystyle{ z = {n_c - n_d \over \sqrt{ v } } }$

where

$\displaystyle{ v = (v_0 - v_x - v_y)/18 + v_1 + v_2 }$,
$\displaystyle{ v_0 = n (n-1) (2n+5) }$,
$\displaystyle{ v_x = \sum_j t_{xj} (t_{xj}-1) (2 t_{xj} + 5) }$,
$\displaystyle{ v_y = \sum_j t_{yj} t_{yj}-1) (2 t_{yj} + 5) }$,
$\displaystyle{ v_1 = \sum^r_{j=1} t_{xj}(t_{xj}-1)(t_{xj}-2) }$,
$\displaystyle{ v_2 = \sum^t_{j=1} t_{yj}(t_{yj}-1)(t_{yj}-2) }$,
$\displaystyle{ v_3 = (v1 v2) / (9 n_w (n_w - 1) (n_w - 2)) }$,
$\displaystyle{ v_4 = \sum^r_{j=1} t_{xj}(t_{xj}-1) }$,
$\displaystyle{ v_5 = \sum^t_{j=1} t_{yj}(t_{yj}-1) }$,
$\displaystyle{ v_6 = (v_4 v_5) / (2 n_w (n_w - 1)) }$,
$\displaystyle{ \hat{\sigma} = (v_0 - v_x - v_y) / 18 + v3 + v6 }$,
$\displaystyle{ z = \frac{n_c - n_d}{\hat{\sigma}} }$,
$\displaystyle{ p \approx 2(1-\Phi(|z|)) }$