예제 #1
0
def pruebaKS(muestra):
    # Prueba de Kolmogorov-Smirnov
    n = len(muestra)
    muestra_ordenada = muestra.copy()
    muestra_ordenada.sort()
    d_esperada = (ksone.ppf((1 - 0.05 / 2), n))
    # 1.36/math.sqrt(n)
    d_mas = []
    d_menos = []

    for i in range(1, n + 1):
        x = i / n - muestra_ordenada[i - 1]
        d_mas.append(x)

    for i in range(1, n + 1):
        y = (i - 1) / n
        y = muestra_ordenada[i - 1] - y
        d_menos.append(y)

    D = max(max(d_mas, d_menos))
    print("D calculada: " + str(D) + " , D esperada : " + str(d_esperada))
    if (D < d_esperada):
        print("Test aprobado. Muestra uniforme")
        print("")
        return True
    else:
        print("Test desaprobado. No implica que no sea uniforme")
        print("")
        return False
예제 #2
0
def D_distribution_table():
    alphaList = [0.2, 0.15, 0.10, 0.05, 0.01]
    table = [[0] * 5 for i in range(35)]
    for i in range(35):
        for j in range(5):
            table[i][j] = ksone.ppf(1 - alphaList[j] / 2, i + 1)

    return table
예제 #3
0
def Kolmogorov_test(variation, n):
    ay, by = find_borders()
    emp_theor_graphics(ay, by, variation)
    f_theor = [F(x) for x in variation]
    max_d = max([
        max(i / n - F(variation[i]),
            F(variation[i]) - (i - 1) / n) for i in range(n)
    ])
    lambda_crit = n**0.5 * max_d
    print("Kolmogorov test:")
    kolmogorov_function = n**0.5 * ksone.ppf(1 - 0.01 / 2, n)
    if lambda_crit < kolmogorov_function:
        print("True: ", lambda_crit, " < ", kolmogorov_function)
    else:
        print("False: ", lambda_crit, " > ", kolmogorov_function)
예제 #4
0
def Kolmogorov(numeros, alpha):

    #Ordenar los numeros en forma ascendente
    num_ordenados = sorted(numeros)
    tamaño = len(num_ordenados)

    #Calculo D+ Y D-
    D_Mas = max(((i + 1) / tamaño - num_ordenados[i]) for i in range(tamaño))
    D_Menos = min((num_ordenados[i] - (i - 1) / tamaño) for i in range(tamaño))

    #Obtengo el mas grande
    D = max(abs(D_Mas), abs(D_Menos))

    #Busco el valor critico de la tabla de kolmogorov para ese nivel de significancia y lo comparo con D
    #Si D es menor que el valor critico puedo decir que los datos pertenecen a una distribucion uniforme
    #Para buscar los datos uso la libreria de scipy
    valor_critico = (ksone.ppf((1 - alpha / 2), tamaño))
    if D < valor_critico:
        resultado = True
    else:
        resultado = False

    return num_ordenados
def kolmogorov(random_numbers):
    random_numbers.sort()

    dPlus = []
    dMinus = []
    i = 1
    N = len(random_numbers)

    for R in random_numbers:
        dPlus.append((i / N) - R)
        dMinus.append(R - ((i - 1) / N))
        i += 1

    dPlus = max(dPlus)
    dMinus = max(dMinus)

    D = max(dPlus, dMinus)
    DTable = ksone.ppf(1 - 0.05 / 2, N)

    result = "Kolmogorov:\nCon D = " + str(D) + "\nCon DTable = " + str(DTable)
    if D < DTable:
        return result + "\nSí es aceptado, la prueba es uniforme"
    else:
        return result + "\nNo es aceptado, la prueba no es uniforme"
예제 #6
0
plt.xticks(midpoints)
plt.title('ECDF')

tcdf = theor_cdf(x_tdf, rel_scale_coeff)
plt.plot(x_tdf, tcdf, ls='dashed', color='r')
#plt.savefig('ecdf.png', dpi = 900)
ct = np.array([
    x * (np.sum(hist) * bins_width)
    for x in theor_pdf(midpoints, rel_scale_coeff)
])

chi2_emp = np.sum(((hist - ct)**2) / ct)

chi2_crit = st.chi2.ppf(0.99, np.size(bin_edges) - 2)
check_hip = chi2_crit > chi2_emp
print('chi2 crit: {}, chi2 emp: {}'.format(chi2_crit, chi2_emp))
data2 = np.array(read_unix_csv('9_data2.csv'))

dmin, dmax = d_stat(data1, data2)
dstat_emp = np.max([dmin, dmax])
ks_crit = ksone.ppf(0.6)

n, m = np.size(data1), np.size(data2)
print('Dm,n : {}, Kcrit: {}'.format(dstat_emp, ks_crit))
print('D * sqrt [mn / (m + n)] : {}'.format(dstat_emp * np.sqrt(m * n /
                                                                (m + n))))

dstat, pval = ks_2samp(data1, data2)
print(dstat)
print('d1 and d2 is same with prob: ', pval)
예제 #7
0
 def ks_critical_value(self, n_trials, alpha):
     return ksone.ppf(1 - alpha / 2, n_trials)
from scipy.stats import ksone
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:

n = 1e+03
mean, var, skew, kurt = ksone.stats(n, moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(ksone.ppf(0.01, n), ksone.ppf(0.99, n), 100)
ax.plot(x, ksone.pdf(x, n), 'r-', lw=5, alpha=0.6, label='ksone pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = ksone(n)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = ksone.ppf([0.001, 0.5, 0.999], n)
np.allclose([0.001, 0.5, 0.999], ksone.cdf(vals, n))
# True

# Generate random numbers:
예제 #9
0
 def valor_critico(self):
     if self.metodo == 'KS':
         return ksone.ppf(1 - self.nivel_de_significancia / 2, self.v)
     else:
         return chi2.ppf(1 - self.nivel_de_significancia, self.v)
예제 #10
0
def critical_value(lev_of_sig, df):
    return ksone.ppf(1 - lev_of_sig / 2, df)
from scipy.stats import chi2, ks_2samp, kstat, kstatvar, kstest, ksone, anderson_ksamp
# Devuelve el valor critico de la tabla

v = 1
alpha = 0.001

for v in range(1, 11):
    print("v", v,  ksone.ppf(1 - alpha / 2, v))

prob = 1 - 0.99