Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python LCG的Kolmogorov-Smirnov测试是否如我的代码所示严重失败?_Python_Random_Statistics_Distribution - Fatal编程技术网

Python LCG的Kolmogorov-Smirnov测试是否如我的代码所示严重失败?

Python LCG的Kolmogorov-Smirnov测试是否如我的代码所示严重失败?,python,random,statistics,distribution,Python,Random,Statistics,Distribution,我使用以下Python代码向学生演示随机变量的生成: 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(

我使用以下
Python
代码向学生演示随机变量的生成:

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'))
根据维基百科,默认参数是glibc使用的参数。最后一行代码打印出来

KstestResult(statistic=0.043427751892089805, pvalue=0.0)
pvalue为0.0表明,如果
x
的元素确实按照均匀分布分布,则观察基本上不会发生。
我的问题是:我的代码中是否存在缺陷,或者具有给定参数的LCG是否未通过Kolmogorov-Smirnov测试(具有
10**6
副本?

您的代码有问题,它会使分布像

我对您的LCG实现做了一些更改,现在一切都好了(Python 3.7、Anaconda、Win10 x64)

哪张照片

KstestResult(statistic=0.0007238884545415214, pvalue=0.6711878724246786)
情节

更新

正如@pjs指出的,最好在循环中直接除以float(M),不需要 整个阵列的第二遍

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

    return result

您的代码有问题,它使均匀分布像

我对您的LCG实现做了一些更改,现在一切都好了(Python 3.7、Anaconda、Win10 x64)

哪张照片

KstestResult(statistic=0.0007238884545415214, pvalue=0.6711878724246786)
情节

更新

正如@pjs指出的,最好在循环中直接除以float(M),不需要 整个阵列的第二遍

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

    return result

为了补充Severin的回答,我的代码不能正常工作的原因是
result
是一个浮点数数组。 我们可以在第二次迭代中看到这两个实现之间的差异。 第一次迭代后,
x0=3310558080

In [9]: x0 = 3310558080

In [10]: float_x0 = float(x0)

In [11]: (a*x0 + c) % M
Out[11]: 465823161

In [12]: (a*float_x0 + c) % M
Out[12]: 465823232.0

In [13]: a*x0
Out[13]: 3653251310737929600

In [14]: a*float_x0
Out[14]: 3.6532513107379297e+18

因此,问题与浮点数的使用有关。

为了补充Severin的答案,我的代码不能正常工作的原因是,
result
是一个浮点数数组。 我们可以在第二次迭代中看到这两个实现之间的差异。 第一次迭代后,
x0=3310558080

In [9]: x0 = 3310558080

In [10]: float_x0 = float(x0)

In [11]: (a*x0 + c) % M
Out[11]: 465823161

In [12]: (a*float_x0 + c) % M
Out[12]: 465823232.0

In [13]: a*x0
Out[13]: 3653251310737929600

In [14]: a*float_x0
Out[14]: 3.6532513107379297e+18

因此,问题与浮点数的使用有关。

您使用哪一版本的Python?在Python2和Python3中,除法是不同的。您使用哪种版本的Python?除法在Python2和Python3中是不同的您的代码不起作用的原因是您使用了
x0=result[i]
而不是
result[i]=x0
,并且忽略了除以
float(M)
@pjs他除以float(M),但在最后,非常低效更新了我的答案,使用了更高效的实现除以
float(M)
M
并不重要,从我的实验来看。@Rastapopulos在python 3中是正确的,在python 2中不是正确的。当Severin询问时,您没有指定要使用哪一个,并且
float(M)
在这两种情况下都起作用。您的代码不起作用的原因是您使用了
x0=result[i]
而不是
result[i]=x0
,并且忽略了除以
float(M)
@pjs他除以float(M),但在最后,非常低效更新了我的答案,使用了更高效的实现除以
float(M)
M
并不重要,从我的实验来看。@Rastapopulos在python 3中是正确的,在python 2中不是正确的。当Severin询问时,您没有指定要使用哪个,并且
float(M)
在这两种情况下都有效。