Beispiel #1
0
def compute_corr(x, y, xlim=None, ylim=None, coeff=None):
    """
    Wrapper function to compute the correct correlation coefficient.
    """

    # set up the correct function for computing the requested correlation
    # coefficient
    if coeff == 'spearmanr':
        # pass to scipy function
        return _spearmanr(x, y)
    elif coeff == 'kendallt':
        # pass to our kendall tau function, in case we need to handle
        # censored data
        return kendall(x, y, xlim=xlim, ylim=ylim)
    elif coeff == 'pearsonr':
        # pass to scipy function
        return _pearsonr(x, y)
def correlation(x, y):
    rho = _pearsonr(x, y)[0]
    return rho
Beispiel #3
0
def run_tests():
    """
    Test output of pymcspearman against tabulated values from MCSpearman
    """

    from tempfile import NamedTemporaryFile as ntf
    from urllib.request import urlretrieve

    # get test data
    tfile = ntf()
    urlretrieve(
        "https://raw.githubusercontent.com/PACurran/MCSpearman/master/test.data",
        tfile.name)
    # open temporary file
    data = _np.genfromtxt(tfile,
                          usecols=(0, 1, 2, 3),
                          dtype=[('x', float), ('dx', float), ('y', float),
                                 ('dy', float)])

    # tabulated results from a MCSpearman run with 10000 iterations
    MCSres = [
        (0.8308, 0.001),  # spearman only
        (0.8213, 0.0470),  # bootstrap only
        (0.7764, 0.0356),  # perturbation only
        (0.7654, 0.0584)
    ]  # bootstrapping and perturbation

    # spearman only
    res = pymccorrelation(data['x'],
                          data['y'],
                          dx=data['dx'],
                          dy=data['dy'],
                          coeff='spearmanr',
                          Nboot=None,
                          Nperturb=None,
                          return_dist=True)
    try:
        assert _np.isclose(MCSres[0][0], res[0], atol=MCSres[0][1])
        _sys.stdout.write("Passed spearman check.\n")
    except AssertionError:
        _sys.stderr.write("Spearman comparison failed.\n")

    # bootstrap only
    res = pymccorrelation(data['x'],
                          data['y'],
                          dx=data['dx'],
                          dy=data['dy'],
                          Nboot=10000,
                          coeff='spearmanr',
                          Nperturb=None,
                          return_dist=True)
    try:
        assert _np.isclose(MCSres[1][0], _np.mean(res[2]), atol=MCSres[1][1])
        _sys.stdout.write("Passed bootstrap only method check.\n")
    except AssertionError:
        _sys.stderr.write("Bootstrap only method comparison failed.\n")

    # perturbation only
    res = pymccorrelation(data['x'],
                          data['y'],
                          dx=data['dx'],
                          dy=data['dy'],
                          coeff='spearmanr',
                          Nboot=None,
                          Nperturb=10000,
                          return_dist=True)
    try:
        assert _np.isclose(MCSres[2][0], _np.mean(res[2]), atol=MCSres[2][1])
        _sys.stdout.write("Passed perturbation only method check.\n")
    except AssertionError:
        _sys.stderr.write("Perturbation only method comparison failed.\n")

    # composite method
    res = pymccorrelation(data['x'],
                          data['y'],
                          dx=data['dx'],
                          dy=data['dy'],
                          coeff='spearmanr',
                          Nboot=10000,
                          Nperturb=10000,
                          return_dist=True)
    try:
        assert _np.isclose(MCSres[3][0], _np.mean(res[2]), atol=MCSres[3][1])
        _sys.stdout.write("Passed composite method check.\n")
    except AssertionError:
        _sys.stderr.write("Composite method comparison failed.\n")

    # test Kendall tau IFN86 for consistency with scipy
    sres = _kendalltau(data['x'], data['y'])
    IFN86res = kendall_IFN86(data['x'],
                             data['y'],
                             xlim=_np.zeros(len(data)),
                             ylim=_np.zeros(len(data)))
    kt_wrap_res = pymccorrelation(data['x'],
                                  data['y'],
                                  xlim=_np.zeros(len(data)),
                                  ylim=_np.zeros(len(data)),
                                  coeff='kendallt')
    try:
        assert _np.isclose(sres[0], IFN86res[0])
        assert _np.isclose(sres[1], IFN86res[1])
        _sys.stdout.write("Passed Kendall tau comparison with scipy.\n")
    except AssertionError:
        _sys.stderr.write("Kendall tau comparison with scipy failed.\n")
    try:
        assert _np.isclose(kt_wrap_res[0], IFN86res[0])
        assert _np.isclose(kt_wrap_res[1], IFN86res[1])
        _sys.stdout.write("Passed internal Kendall tau comparison.\n")
    except AssertionError:
        _sys.stderr.write("Internal Kendall tau comparison failed.\n")

    # test pearson r wrapper
    wrap_res = pymccorrelation(data['x'],
                               data['y'],
                               coeff='pearsonr',
                               return_dist=False)
    res = _pearsonr(data['x'], data['y'])
    try:
        assert _np.isclose(wrap_res[0], res[0])
        assert _np.isclose(wrap_res[1], res[1])
        _sys.stdout.write("Passed Pearson r wrapper check.\n")
    except AssertionError:
        _sys.stderr.write("Pearson r wrapper check failed.\n")
Beispiel #4
0
def pearsonr(y_true, y_pred, multioutput="uniform_average"):
    if multioutput == "uniform_average":
        return _pearsonr(y_true, y_pred)[0]
    else:
        return np.array(
            [_pearsonr(yt, yp)[0] for yt, yp in zip(y_true.T, y_pred.T)])
Beispiel #5
0
def correlation(x, y):
    rho = _pearsonr(x, y)[0]
    if _np.isnan(rho):
        rho = 0.
    return rho