Ejemplo n.º 1
0
def test_xii():
    # Pandas DataSeries integration with correlation_permutation_test.
    import pandas as pd
    import numpy as np
    from scipy.stats import kendalltau

    x = [4.02, 4.52, 4.79, 4.89, 5.27, 5.63, 5.89, 6.08, 6.13, 6.19, 6.47]
    y = [4.56, 2.92, 2.71, 3.34, 3.53, 3.47, 3.20, 4.51, 3.76, 3.77, 4.03]
    side = "both"

    df0 = pd.DataFrame(columns=["X", "Y"], data=zip(x, y))
    df = pd.DataFrame(columns=["X", "Y"], data=zip(x, y))

    def ktau(x, y):
        tau, _ = kendalltau(x, y)
        return tau

    expected = 0.1646

    result = mcpt.correlation_permutation_test(
        df["X"], df["Y"], f=ktau, side=side, seed=6919
    )
    # Check the result is correct.
    assert expected >= result.lower
    assert expected <= result.upper
    # Check the inputs haven't changed as a result of function.
    assert df0.equals(df)
Ejemplo n.º 2
0
def test_x():
    # Testing use of a custom function.
    from scipy.stats import kendalltau

    x0 = [4.02, 4.52, 4.79, 4.89, 5.27, 5.63, 5.89, 6.08, 6.13, 6.19, 6.47]
    y0 = [4.56, 2.92, 2.71, 3.34, 3.53, 3.47, 3.20, 4.51, 3.76, 3.77, 4.03]

    x = [4.02, 4.52, 4.79, 4.89, 5.27, 5.63, 5.89, 6.08, 6.13, 6.19, 6.47]
    y = [4.56, 2.92, 2.71, 3.34, 3.53, 3.47, 3.20, 4.51, 3.76, 3.77, 4.03]

    def ktau(x, y):
        tau, _ = kendalltau(x, y)
        return tau

    result = mcpt.correlation_permutation_test(x, y, side="both", f=ktau)
    # Check the result is correct.
    assert result.lower <= 0.1646
    assert result.upper >= 0.1646
    # Check the inputs haven't changed as a result of function.
    assert x0 == x
    assert y0 == y
Ejemplo n.º 3
0
def test_viii():
    # Testing that we obtain the correct result when cores are > 1.
    x0 = [-2.31, 1.06, 0.76, 1.38, -0.26, 1.29, -1.31, 0.41, -0.67, -0.58]
    y0 = [-1.08, 1.03, 0.90, 0.24, -0.24, 0.76, -0.57, -0.05, -1.28, 1.04]

    x = [-2.31, 1.06, 0.76, 1.38, -0.26, 1.29, -1.31, 0.41, -0.67, -0.58]
    y = [-1.08, 1.03, 0.90, 0.24, -0.24, 0.76, -0.57, -0.05, -1.28, 1.04]

    expected = 0.025554177689594355

    f = "pearsonr"
    n = 10000
    side = "both"
    result = mcpt.correlation_permutation_test(x, y, f, side, n=n, cores=2, seed=4919)

    # Check the result is correct.
    assert expected >= result.lower
    assert expected <= result.upper
    # Check the inputs haven't changed as a result of function.
    assert y0 == y
    assert x0 == x
Ejemplo n.º 4
0
def test_v():
    # Taken from http://biol09.biol.umontreal.ca/PLcourses/Statistical_tests.pdf
    # True p-value calculated through exhaustive permutation of y.
    x0 = [-2.31, 1.06, 0.76, 1.38, -0.26, 1.29, -1.31, 0.41, -0.67, -0.58]
    y0 = [-1.08, 1.03, 0.90, 0.24, -0.24, 0.76, -0.57, -0.05, -1.28, 1.04]

    x = [-2.31, 1.06, 0.76, 1.38, -0.26, 1.29, -1.31, 0.41, -0.67, -0.58]
    y = [-1.08, 1.03, 0.90, 0.24, -0.24, 0.76, -0.57, -0.05, -1.28, 1.04]

    expected = 0.0144144069664903

    f = "pearsonr"
    n = 10000
    side = "greater"
    result = mcpt.correlation_permutation_test(x, y, f, side, n=n, seed=4919)

    # Check the result is correct.
    assert expected >= result.lower
    assert expected <= result.upper
    # Check the inputs haven't changed as a result of function.
    assert y0 == y
    assert x0 == x
Ejemplo n.º 5
0
def test_ix():
    # Test that seeding works.
    x0 = [-2.31, 1.06, 0.76, 1.38, -0.26, 1.29, -1.31, 0.41, -0.67, -0.58]
    y0 = [-1.08, 1.03, 0.90, 0.24, -0.24, 0.76, -0.57, -0.05, -1.28, 1.04]

    x = [-2.31, 1.06, 0.76, 1.38, -0.26, 1.29, -1.31, 0.41, -0.67, -0.58]
    y = [-1.08, 1.03, 0.90, 0.24, -0.24, 0.76, -0.57, -0.05, -1.28, 1.04]

    seed = 4919
    n = 10000
    for side in ["greater", "lower", "both"]:
        for cores in [1, 2]:
            # Run two tests with the same seed.
            result_a = mcpt.permutation_test(
                x, y, "mean", side, n=n, cores=cores, seed=seed
            )
            result_b = mcpt.permutation_test(
                x, y, "mean", side, n=n, cores=cores, seed=seed
            )
            # Check that the seeded results are equivalent.
            assert result_a == result_b

            # Run up to ten unseeded permutations and ensure that it differs from the seeded.
            for _ in range(10):
                result_c = mcpt.permutation_test(x, y, "mean", side, n=n, cores=cores)

                if result_a != result_c:
                    break
            else:
                raise Exception("result_a always identical to result_c")

            # Run up to ten unseeded permutation tests and ensure that it differes from
            # the previously unseeded result.
            for _ in range(10):
                result_d = mcpt.permutation_test(x, y, "mean", side, n=n, cores=cores)
                if result_c != result_d:
                    break
            else:
                raise Exception("result_c always identical to result_d")

            # Check the inputs haven't changed as a result of function.
            assert x0 == x
            assert y0 == y

            # Run two tests with the same seed.
            result_a = mcpt.correlation_permutation_test(
                x, y, "pearsonr", side, n=n, cores=cores, seed=seed
            )
            result_b = mcpt.correlation_permutation_test(
                x, y, "pearsonr", side, n=n, cores=cores, seed=seed
            )
            # Check that the seeded results are equivalent.
            assert result_a == result_b

            # Run up to ten unseeded permutations and ensure that it differs from the seeded.
            for _ in range(10):
                result_c = mcpt.correlation_permutation_test(
                    x, y, "pearsonr", side, n=n, cores=cores
                )
                if result_a != result_c:
                    break
            else:
                raise Exception("result_a always identical to result_c")

            # Run up to ten unseeded permutation tests and ensure that it differes from
            # the previously unseeded result.
            for _ in range(10):
                result_d = mcpt.correlation_permutation_test(
                    x, y, "pearsonr", side, n=n, cores=cores
                )
                if result_c != result_d:
                    break
            else:
                raise Exception("result_c always identical to result_d")

            # Check the inputs haven't changed as a result of function.
            assert x0 == x
            assert y0 == y