u_centered#

u_centered(a, *, out=None)[source]#

Return a copy of the matrix \(a\) which is \(U\)-centered.

If the element of the i-th row and j-th column of the original matrix \(a\) is \(a_{i,j}\), then the new element will be

\[\begin{split}\tilde{a}_{i, j} = \begin{cases} a_{i,j} - \frac{1}{n-2} \sum_{l=1}^n a_{il} - \frac{1}{n-2} \sum_{k=1}^n a_{kj} + \frac{1}{(n-1)(n-2)}\sum_{k=1}^n a_{kj}, &\text{if } i \neq j, \\ 0, &\text{if } i = j. \end{cases}\end{split}\]
Parameters
  • a (Array) – Original symmetric square matrix.

  • out (Optional[Array]) – If not None, specifies where to return the resulting array. This array should allow non integer numbers.

Returns

\(U\)-centered matrix.

Return type

Array

See also

double_centered

Examples

>>> import numpy as np
>>> import dcor
>>> a = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 6]])
>>> dcor.u_centered(a)
array([[ 0. ,  0.5, -1.5],
       [ 0.5,  0. , -4.5],
       [-1.5, -4.5,  0. ]])
>>> b = np.array([[1., 2., 3.], [2., 4., 5.], [3., 5., 6.]])
>>> dcor.u_centered(b, out=b)
array([[ 0. ,  0.5, -1.5],
       [ 0.5,  0. , -4.5],
       [-1.5, -4.5,  0. ]])
>>> b
array([[ 0. ,  0.5, -1.5],
       [ 0.5,  0. , -4.5],
       [-1.5, -4.5,  0. ]])

Note that when the matrix is 1x1 or 2x2, the formula performs a division by 0

>>> import warnings
>>> b = np.array([[1, 2], [3, 4]])
>>> with warnings.catch_warnings():
...     warnings.simplefilter("ignore")
...     dcor.u_centered(b)
array([[ 0., nan],
       [nan,  0.]])