简体   繁体   中英

Use the scipy.stats.kstest to see if the randomly generated numbers follow a specified distribution

I am trying to generate random numbers from a chosen distribution with specified parameters and then see if the numbers are indeed following that distribution using the Kolmogorov-Smirnov test.

import matplotlib.pyplot as plt
from scipy.stats import johnsonsu
values = johnsonsu.rvs(0.4, 1.27, loc = 3.50, scale = 5.97, size = 10000)
plt.hist(values, bins = 25)
plt.show()

在此处输入图像描述

Dstat, Pvalue = scipy.stats.kstest(values, 'johnsonsu', args = (0.4, 1.27))
print(Dstat)
0.48575579351993264
print(Pvalue)
0.0

I believe that the null-hypothesis of the KS test is that the sample data follows the specified distribution (johnson su, in this example). So the p-value being less than 0.05 rejects the null hypothesis and we conclude that the data is not following the distribution? Shouldn't it be the opposite or am I missing something?

If I am passing the full list of distribution parameters to the arg parameter I get what you expect, namely:

import scipy.stats as stats

n = 10_000
values = stats.johnsonsu.rvs(0.4, 1.27, loc=3.50, scale=5.97, size=n)

print(stats.kstest(values, 'johnsonsu', N=n, args=(0.4, 1.27, 3.5, 5.97)))
KstestResult(statistic=0.007110068990121343, pvalue=0.6928424801510613)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM