Skip to content

Instantly share code, notes, and snippets.

@Subho07
Forked from wladston/distcorr.py
Created November 27, 2021 10:14
Show Gist options
  • Save Subho07/d56b0cf09a675533618a2f4d71ad06b0 to your computer and use it in GitHub Desktop.
Save Subho07/d56b0cf09a675533618a2f4d71ad06b0 to your computer and use it in GitHub Desktop.

Revisions

  1. @wladston wladston revised this gist Jun 26, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions distcorr.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ def distcorr(Xval, Yval, pval=True, nruns=500):
    >>> a = [1,2,3,4,5]
    >>> b = np.array([1,2,9,4,4])
    >>> distcorr(a, b)
    (0.76267624241686671, 0.404)
    (0.76267624241686671, 0.266)
    """
    X = np.atleast_1d(Xval)
    Y = np.atleast_1d(Yval)
    @@ -38,7 +38,7 @@ def distcorr(Xval, Yval, pval=True, nruns=500):
    for i in range(nruns):
    Y_r = copy.copy(Yval)
    np.random.shuffle(Y_r)
    if distcorr(Xval, Y_r, pval=False) >= dcor:
    if distcorr(Xval, Y_r, pval=False) > dcor:
    greater += 1
    return (dcor, greater / float(nruns))
    else:
  2. @wladston wladston revised this gist Aug 12, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion distcorr.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ def distcorr(Xval, Yval, pval=True, nruns=500):
    >>> a = [1,2,3,4,5]
    >>> b = np.array([1,2,9,4,4])
    >>> distcorr(a, b)
    (0.76267624241686671, 0.268)
    (0.76267624241686671, 0.404)
    """
    X = np.atleast_1d(Xval)
    Y = np.atleast_1d(Yval)
  3. @wladston wladston revised this gist Aug 12, 2018. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions distcorr.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    from scipy.spatial.distance import pdist, squareform
    import numpy as np
    import random
    import copy


    @@ -29,18 +28,18 @@ def distcorr(Xval, Yval, pval=True, nruns=500):
    A = a - a.mean(axis=0)[None, :] - a.mean(axis=1)[:, None] + a.mean()
    B = b - b.mean(axis=0)[None, :] - b.mean(axis=1)[:, None] + b.mean()

    dcov2_xy = (A * B).sum()/float(n * n)
    dcov2_xx = (A * A).sum()/float(n * n)
    dcov2_yy = (B * B).sum()/float(n * n)
    dcor = np.sqrt(dcov2_xy)/np.sqrt(np.sqrt(dcov2_xx) * np.sqrt(dcov2_yy))
    dcov2_xy = (A * B).sum() / float(n * n)
    dcov2_xx = (A * A).sum() / float(n * n)
    dcov2_yy = (B * B).sum() / float(n * n)
    dcor = np.sqrt(dcov2_xy) / np.sqrt(np.sqrt(dcov2_xx) * np.sqrt(dcov2_yy))

    if pval:
    greater = 0
    for i in range(nruns):
    Y_r = copy.copy(Yval)
    random.shuffle(Y_r)
    if distcorr(Xval, Y_r, pval=False) > dcor:
    np.random.shuffle(Y_r)
    if distcorr(Xval, Y_r, pval=False) >= dcor:
    greater += 1
    return (dcor, greater/float(n))
    return (dcor, greater / float(nruns))
    else:
    return dcor
  4. Wladston Viana Ferreira Filho revised this gist Jan 29, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion distcorr.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    from scipy.spatial.distance import pdist, squareform
    import numpy as np
    import random
    import copy


    def distcorr(Xval, Yval, pval=True, nruns=500):
    @@ -36,7 +37,7 @@ def distcorr(Xval, Yval, pval=True, nruns=500):
    if pval:
    greater = 0
    for i in range(nruns):
    Y_r = Yval.copy()
    Y_r = copy.copy(Yval)
    random.shuffle(Y_r)
    if distcorr(Xval, Y_r, pval=False) > dcor:
    greater += 1
  5. Wladston Viana Ferreira Filho created this gist Jan 29, 2015.
    45 changes: 45 additions & 0 deletions distcorr.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    from scipy.spatial.distance import pdist, squareform
    import numpy as np
    import random


    def distcorr(Xval, Yval, pval=True, nruns=500):
    """ Compute the distance correlation function, returning the p-value.
    Based on Satra/distcorr.py (gist aa3d19a12b74e9ab7941)
    >>> a = [1,2,3,4,5]
    >>> b = np.array([1,2,9,4,4])
    >>> distcorr(a, b)
    (0.76267624241686671, 0.268)
    """
    X = np.atleast_1d(Xval)
    Y = np.atleast_1d(Yval)
    if np.prod(X.shape) == len(X):
    X = X[:, None]
    if np.prod(Y.shape) == len(Y):
    Y = Y[:, None]
    X = np.atleast_2d(X)
    Y = np.atleast_2d(Y)
    n = X.shape[0]
    if Y.shape[0] != X.shape[0]:
    raise ValueError('Number of samples must match')
    a = squareform(pdist(X))
    b = squareform(pdist(Y))
    A = a - a.mean(axis=0)[None, :] - a.mean(axis=1)[:, None] + a.mean()
    B = b - b.mean(axis=0)[None, :] - b.mean(axis=1)[:, None] + b.mean()

    dcov2_xy = (A * B).sum()/float(n * n)
    dcov2_xx = (A * A).sum()/float(n * n)
    dcov2_yy = (B * B).sum()/float(n * n)
    dcor = np.sqrt(dcov2_xy)/np.sqrt(np.sqrt(dcov2_xx) * np.sqrt(dcov2_yy))

    if pval:
    greater = 0
    for i in range(nruns):
    Y_r = Yval.copy()
    random.shuffle(Y_r)
    if distcorr(Xval, Y_r, pval=False) > dcor:
    greater += 1
    return (dcor, greater/float(n))
    else:
    return dcor