dcor.double_centered

double_centered(a, *, out=None)[source]

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

A matrix is double centered if both the sum of its columns and the sum of its rows are 0.

In order to do that, for every element its row and column averages are subtracted, and the total average is added.

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

\[\tilde{a}_{i, j} = a_{i,j} - \frac{1}{N} \sum_{l=1}^N a_{il} - \frac{1}{N}\sum_{k=1}^N a_{kj} + \frac{1}{N^2}\sum_{k=1}^N a_{kj}.\]
Parameters
  • a (T) – Original square matrix.

  • out (T | None) – If not None, specifies where to return the resulting array. This array should allow non integer numbers.

Returns

Double centered matrix.

Return type

T

See also

u_centered

Examples

>>> import numpy as np
>>> import dcor
>>> a = np.array([[1, 2], [3, 4]])
>>> dcor.double_centered(a)
array([[0., 0.],
       [0., 0.]])
>>> b = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 6]])
>>> dcor.double_centered(b)
array([[ 0.44444444, -0.22222222, -0.22222222],
       [-0.22222222,  0.11111111,  0.11111111],
       [-0.22222222,  0.11111111,  0.11111111]])
>>> c = np.array([[1., 2., 3.], [2., 4., 5.], [3., 5., 6.]])
>>> dcor.double_centered(c, out=c)
array([[ 0.44444444, -0.22222222, -0.22222222],
       [-0.22222222,  0.11111111,  0.11111111],
       [-0.22222222,  0.11111111,  0.11111111]])
>>> c
array([[ 0.44444444, -0.22222222, -0.22222222],
       [-0.22222222,  0.11111111,  0.11111111],
       [-0.22222222,  0.11111111,  0.11111111]])