Exemple #1
0
def ranksums_test(data1, data2, alpha, flag):
    """
        Compute the Wilcoxon rank-sum statistic for two samples.

        The Wilcoxon rank-sum test tests the null hypothesis that two sets of measurements are drawn from the same distribution.
        The alternative hypothesis is that values in one sample are more likely to be larger than the values in the other sample.
        This test should be used to compare two samples from continuous distributions. It does not handle ties between measurements in x and y.
        For tie-handling and an optional continuity correction see scipy.stats.mannwhitneyu.
       """

    import file_operations as fo
    import pandas as pd
    import numpy as np
    from pandas import DataFrame as df
    from scipy import stats as st
    from scipy.stats import friedmanchisquare

    data_columns = []
    alpha = float(alpha)

    array1 = fo.loading_file(data1)
    array2 = fo.loading_file(data2)

    _rows, cols = array1.shape[:2]
    data_columns = array1.columns.values

    array1 = fo.transform2array(array1)
    array2 = fo.transform2array(array2)

    if flag == True:
        with open('database/ranksums-test-results.txt', 'w') as f:
            f.write('   --- ranksums test results --- \n\n')

    for col in range(cols):

        newarray1 = np.asarray(array1[:, col], np.float)
        newarray2 = np.asarray(array2[:, col], np.float)

        newarray1 = ((newarray1.transpose()))
        newarray2 = ((newarray2.transpose()))

        newarray1 = np.float64(newarray1)
        newarray2 = np.float64(newarray2)

        newarray1[newarray1 > 100] = 100
        newarray2[newarray2 > 100] = 100
        newarray1[newarray1 < 0] = 0
        newarray2[newarray2 < 0] = 0

        # newarray1 = newarray1.reshape(-1)
        # newarray2 = newarray2.reshape(-1)

        stat, p = st.ranksums(newarray1, newarray2)

        if (p * 100) > alpha:
            print(
                'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] tem a mesma distribuição'
                .format(p, data_columns[col]))
        else:
            print(
                'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] tem a mesma distribuição'
                .format(p, data_columns[col]))

        # Save results
        if flag == True:
            with open('database/ranksums-test-results.txt', 'a') as f:
                if (stat * 100) > p:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] tem a mesma distribuição'
                        .format(p, data_columns[col]))
                else:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] tem a mesma distribuição'
                        .format(p, data_columns[col]))
                f.write('\n')
Exemple #2
0
def studentst(data1, data2, alpha, flag):
    """
    Student’s t-test

    data1: 
    data2: 
    alpha: 
    flag: Receives a boolean value for saving or not saving the results to a text file.

    """

    import file_operations as fo
    import pandas as pd
    import numpy as np
    from pandas import DataFrame as df
    from scipy import stats as st
    from scipy.stats import ttest_ind

    data_columns = []
    alpha = float(alpha)

    array1 = fo.loading_file(data1)
    array2 = fo.loading_file(data2)

    _rows, cols = array1.shape[:2]
    data_columns = array1.columns.values

    array1 = fo.transform2array(array1)
    array2 = fo.transform2array(array2)

    if flag == True:
        with open('database/Students-t-test-results.txt', 'w') as f:
            f.write('   --- Student’s t-test results --- \n\n')

    for col in range(cols):

        newarray1 = np.asarray(array1[:, col], np.float)
        newarray2 = np.asarray(array2[:, col], np.float)

        newarray1 = ((newarray1.transpose()))
        newarray2 = ((newarray2.transpose()) * 100)

        newarray1 = np.float64(newarray1)
        newarray2 = np.float64(newarray2)

        newarray1[newarray1 > 100] = 100
        newarray2[newarray2 > 100] = 100

        # newarray1 = newarray1.reshape(-1)
        # newarray2 = newarray2.reshape(-1)

        # newarray1=np.asarray([635,704,662,560,603,745,698,575,633,669],np.uint8)
        # newarray2=np.asarray([640,712,681,558,610,740,707,585,635,682],np.uint8)

        _stat, p = ttest_ind(newarray1, newarray2)

        if (p * 100) > alpha:
            print(
                'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                .format(p, data_columns[col]))
        else:
            print(
                'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                .format(p, data_columns[col]))

        # Save results
        if flag == True:
            with open('database/Students-t-test-results.txt', 'a') as f:
                if (p * 100) > alpha:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                        .format(p, data_columns[col]))
                else:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                        .format(p, data_columns[col]))
                f.write('\n')
Exemple #3
0
def Mann_Whitney_test(data1, data2, alpha, flag):
    """
    Compute the Mann-Whitney rank test on samples x and y.
    for more informations see https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mannwhitneyu.html#scipy.stats.mannwhitneyu
    """

    import file_operations as fo
    import pandas as pd
    import numpy as np
    from pandas import DataFrame as df
    from scipy import stats as st
    from scipy.stats import friedmanchisquare

    data_columns = []
    alpha = float(alpha)

    array1 = fo.loading_file(data1)
    array2 = fo.loading_file(data2)

    _rows, cols = array1.shape[:2]
    data_columns = array1.columns.values

    array1 = fo.transform2array(array1)
    array2 = fo.transform2array(array2)

    if flag == True:
        with open('database/Mann_Whitney-test-results.txt', 'w') as f:
            f.write('   --- Mann_Whitney test results --- \n\n')

    for col in range(cols):

        newarray1 = np.asarray(array1[:, col], np.float)
        newarray2 = np.asarray(array2[:, col], np.float)

        newarray1 = ((newarray1.transpose()))
        newarray2 = ((newarray2.transpose()))

        newarray1 = np.float64(newarray1)
        newarray2 = np.float64(newarray2)

        newarray1[newarray1 > 100] = 100
        newarray2[newarray2 > 100] = 100
        newarray1[newarray1 < 0] = 0
        newarray2[newarray2 < 0] = 0

        # newarray1 = newarray1.reshape(-1)
        # newarray2 = newarray2.reshape(-1)

        stat, p = friedmanchisquare(newarray1, newarray1, newarray2, newarray2)

        if (p * 100) > alpha:
            print(
                'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                .format(p, data_columns[col]))
        else:
            print(
                'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                .format(p, data_columns[col]))

        # Save results
        if flag == True:
            with open('database/Mann_Whitney-test-results.txt', 'a') as f:
                if (stat * 100) > p:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                        .format(p, data_columns[col]))
                else:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                        .format(p, data_columns[col]))
                f.write('\n')
Exemple #4
0
def normal_test(data1, alpha, flag):
    """
        This function tests the null hypothesis that a sample comes from a normal distribution.
         It is based on D’Agostino and Pearson’s [1], [2] test that combines skew and kurtosis to produce an omnibus test of normality.

        """

    import scipy.stats as st
    import file_operations as fo
    import pandas as pd
    import numpy as np
    from pandas import DataFrame as df

    data_columns = []
    alpha = float(alpha)

    array1 = fo.loading_file(data1)
    # array2 = fo.loading_file(data2)

    _rows, cols = array1.shape[:2]
    data_columns = array1.columns.values

    array1 = fo.transform2array(array1)
    # array2 = fo.transform2array(array2)

    if flag == True:
        with open('database/normal test-results.txt', 'w') as f:
            f.write('   --- normal test results --- \n\n')

    for col in range(cols):

        newarray1 = np.asarray(array1[:, col], np.float)
        # newarray2 = np.asarray(array2[:, col], np.float)

        newarray1 = ((newarray1.transpose()))
        # newarray2 = ((newarray2.transpose()))

        newarray1 = np.float64(newarray1)
        # newarray2 = np.float64(newarray2)

        newarray1[newarray1 > 100] = 100
        newarray1[newarray1 < 0] = 0
        # newarray2[newarray2 < 0] = 0
        # newarray2[newarray2 > 100] = 100

        # newarray1 = newarray1.reshape(-1)
        # newarray2 = newarray2.reshape(-1)

        _, P_value = st.normaltest(newarray1)

        if (P_value * 100) > alpha:
            print(
                'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] possuem distribuição normal'
                .format(P_value, data_columns[col]))
        else:
            print(
                'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] possuem distribuição normal'
                .format(P_value, data_columns[col]))

        # Save results
        if flag == True:
            with open('database/normal-results.txt', 'a') as f:
                if (P_value * 100) > alpha:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] possuem distribuição normal'
                        .format(P_value, data_columns[col]))
                else:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] possuem distribuição normal'
                        .format(P_value, data_columns[col]))
                f.write('\n')
Exemple #5
0
def levene_test(data1, data2, alpha, flag):
    """
    The Levene test tests the null hypothesis that all input samples are from populations with equal variances.
     Levene’s test is an alternative to Bartlett’s test bartlett in the case where there are significant deviations from normality.

    """

    import scipy.stats as st
    import file_operations as fo
    import pandas as pd
    import numpy as np
    from pandas import DataFrame as df

    data_columns = []
    alpha = float(alpha)

    array1 = fo.loading_file(data1)
    array2 = fo.loading_file(data2)

    _rows, cols = array1.shape[:2]
    data_columns = array1.columns.values

    array1 = fo.transform2array(array1)
    array2 = fo.transform2array(array2)

    if flag == True:
        with open('database/levene test-results.txt', 'w') as f:
            f.write('   --- levene test results --- \n\n')

    for col in range(cols):

        newarray1 = np.asarray(array1[:, col], np.float)
        newarray2 = np.asarray(array2[:, col], np.float)

        newarray1 = ((newarray1.transpose()))
        newarray2 = ((newarray2.transpose()))

        newarray1 = np.float64(newarray1)
        newarray2 = np.float64(newarray2)

        newarray1[newarray1 > 100] = 100
        newarray2[newarray2 > 100] = 100
        newarray1[newarray1 < 0] = 0
        newarray2[newarray2 < 0] = 0

        # newarray1 = newarray1.reshape(-1)
        # newarray2 = newarray2.reshape(-1)

        _, P_value = st.bartlett(newarray1, newarray2)

        if (P_value * 100) > alpha:
            print(
                'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] possuem a variâncias iguais'
                .format(P_value, data_columns[col]))
        else:
            print(
                'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] possuem a variâncias iguais'
                .format(P_value, data_columns[col]))

        # Save results
        if flag == True:
            with open('database/levene-results.txt', 'a') as f:
                if (P_value * 100) > alpha:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] possuem a variâncias iguais'
                        .format(P_value, data_columns[col]))
                else:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] possuem a variâncias iguais'
                        .format(P_value, data_columns[col]))
                f.write('\n')
Exemple #6
0
def friedman(data1, data2, alpha, flag):
    """
    The Friedman test tests the null hypothesis that repeated measurements of the same individuals have the same distribution.
    It is often used to test for consistency among measurements obtained in different ways.
    For example, if two measurement techniques are used on the same set of individuals, the Friedman test can be used
    to determine if the two measurement techniques are consistent.
    """

    import file_operations as fo
    import pandas as pd
    import numpy as np
    from pandas import DataFrame as df
    from scipy import stats as st
    from scipy.stats import friedmanchisquare

    data_columns = []
    alpha = float(alpha)

    array1 = fo.loading_file(data1)
    array2 = fo.loading_file(data2)

    _rows, cols = array1.shape[:2]
    data_columns = array1.columns.values

    array1 = fo.transform2array(array1)
    array2 = fo.transform2array(array2)

    if flag == True:
        with open('database/Friedman-test-results.txt', 'w') as f:
            f.write('   --- Friedman test results --- \n\n')

    for col in range(cols):

        newarray1 = np.asarray(array1[:, col], np.float)
        newarray2 = np.asarray(array2[:, col], np.float)

        newarray1 = ((newarray1.transpose()))
        newarray2 = ((newarray2.transpose()))

        newarray1 = np.float64(newarray1)
        newarray2 = np.float64(newarray2)

        newarray1[newarray1 > 100] = 100
        newarray2[newarray2 > 100] = 100
        newarray1[newarray1 < 0] = 0
        newarray2[newarray2 < 0] = 0

        # newarray1 = newarray1.reshape(-1)
        # newarray2 = newarray2.reshape(-1)

        stat, p = friedmanchisquare(newarray1, newarray1, newarray2, newarray2)

        if (p * 100) > alpha:
            print(
                'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                .format(p, data_columns[col]))
        else:
            print(
                'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                .format(p, data_columns[col]))

        # Save results
        if flag == True:
            with open('database/Friedman-test-results.txt', 'a') as f:
                if (p * 100) > alpha:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                        .format(p, data_columns[col]))
                else:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                        .format(p, data_columns[col]))
                f.write('\n')
Exemple #7
0
def kolmogorov(data1, data2, alpha, flag):
    """
    Kolmogorov-Smirnov test

    data1: Receive the file to be compared.
    data2: Receive file for comparison with main file.
    alpha: Threshold value for hypothesis testing.
    flag: Receives a boolean value for saving or not saving the results to a text file.

    """

    import scipy.stats as st
    import file_operations as fo
    import pandas as pd
    import numpy as np
    from pandas import DataFrame as df

    data_columns = []
    alpha = float(alpha)

    array1 = fo.loading_file(data1)
    array2 = fo.loading_file(data2)

    _rows, cols = array1.shape[:2]
    data_columns = array1.columns.values

    array1 = fo.transform2array(array1)
    array2 = fo.transform2array(array2)

    if flag == True:
        with open('database/Kolmogorov-Smirnov-results.txt', 'w') as f:
            f.write('   --- Kolmogorov-Smirnov test results --- \n\n')

    for col in range(cols):

        newarray1 = np.asarray(array1[:, col], np.float)
        newarray2 = np.asarray(array2[:, col], np.float)

        newarray1 = ((newarray1.transpose()))
        newarray2 = ((newarray2.transpose()))

        newarray1 = np.float64(newarray1)
        newarray2 = np.float64(newarray2)

        newarray1[newarray1 > 100] = 100
        newarray2[newarray2 > 100] = 100
        newarray1[newarray1 < 0] = 0
        newarray2[newarray2 < 0] = 0

        # newarray1 = newarray1.reshape(-1)
        # newarray2 = newarray2.reshape(-1)
        # np.random.seed(12345678)
        # newarray1 = st.norm.rvs(size=200, loc=0., scale=1)
        # newarray2 = st.norm.rvs(size=300, loc=0.5, scale=1.5)

        _, ks_value = st.ks_2samp(newarray1, newarray2)

        if (ks_value * 100) > alpha:
            print(
                'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                .format(ks_value, data_columns[col]))
        else:
            print(
                'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                .format(ks_value, data_columns[col]))

        # Save results
        if flag == True:
            with open('database/Kolmogorov-Smirnov-results.txt', 'a') as f:
                if (ks_value * 100) > alpha:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para confirmar a hipótese de que os dados para [{}] são similares'
                        .format(ks_value, data_columns[col]))
                else:
                    f.write(
                        'Com um p de {:.4f}, há evidências suficientes para negar a hipótese de que os dados para [{}] são similares'
                        .format(ks_value, data_columns[col]))
                f.write('\n')