dimanche 12 janvier 2020

Does the LCG fail the Kolmogorov-Smirnov test as badly as my code suggests?

I use the following Python code to illustrate the generation of random variables to students:

import numpy as np
import scipy.stats as stats

def lcg(n, x0, M=2**32, a=1103515245, c=12345):
    result = np.zeros(n)
    for i in range(n):
        result[i] = (a*x0 + c) % M
        x0 = result[i]

    return np.array([x/M for x in result])

x = lcg(10**6, 3)
print(stats.kstest(x, 'uniform'))

The default parameters are the ones used by glibc, according to Wikipedia. The last line of the code prints

KstestResult(statistic=0.043427751892089805, pvalue=0.0)

The pvalue of 0.0 indicates that the observation would basically never occur if the elements of x were truly distributed according to a uniform distribution. My question is: is there a bug in my code, or does the LCG with the parameters given not pass the Kolmogorov-Smirnov test with 10**6 replicas?




Aucun commentaire:

Enregistrer un commentaire