netneurotools.stats.make_correlated_xy

netneurotools.stats.make_correlated_xy(corr=0.85, size=10000, seed=None, tol=0.001)[source]

Generate random vectors that are correlated to approximately corr.

Parameters:
  • corr ([-1, 1] float or (N, N) numpy.ndarray, optional) – The approximate correlation desired. If a float is provided, two vectors with the specified level of correlation will be generated. If an array is provided, it is assumed to be a symmetrical correlation matrix and len(corr) vectors with the specified levels of correlation will be generated. Default: 0.85

  • size (int or tuple, optional) – Desired size of the generated vectors. Default: 1000

  • seed ({int, np.random.RandomState instance, None}, optional) – Seed for random number generation. Default: None

  • tol ([0, 1] float, optional) – Tolerance of correlation between generated vectors and specified corr. Default: 0.001

Returns:

vectors – Random vectors of size size with correlation specified by corr

Return type:

numpy.ndarray

Examples

>>> from netneurotools import stats

By default two vectors are generated with specified correlation

>>> x, y = stats.make_correlated_xy()
>>> np.corrcoef(x, y)  
array([[1.        , 0.85083661],
       [0.85083661, 1.        ]])
>>> x, y = stats.make_correlated_xy(corr=0.2)
>>> np.corrcoef(x, y)  
array([[1.        , 0.20069953],
       [0.20069953, 1.        ]])

You can also provide correlation matrices to generate more than two vectors if desired. Note that this makes it more difficult to ensure the actual correlations are close to the desired values:

>>> corr = [[1, 0.5, 0.3], [0.5, 1, 0], [0.3, 0, 1]]
>>> out = stats.make_correlated_xy(corr=corr)
>>> out.shape
(3, 10000)
>>> np.corrcoef(out)  
array([[1.        , 0.50965273, 0.30235686],
       [0.50965273, 1.        , 0.01089107],
       [0.30235686, 0.01089107, 1.        ]])

Examples using netneurotools.stats.make_correlated_xy

Non-parametric significance testing with permutations

Non-parametric significance testing with permutations