0

I have this code to check the Kolmogorov-Smirnov test. But it gives the wrong result. What is the problem?

import pandas
import random

import matplotlib
import numpy
import matplotlib.pyplot as plt
datas = [0.3877, 0.3878, 0.3877, 0.3877, 0.3877, 0.3879, 0.3899, 0.3892, 0.3881, 0.3873, 0.3886, 0.3875, 0.3876, 0.3893, 0.3888, 0.3886, 0.388, 0.3886, 0.387, 0.3872, 0.388, 0.3877, 0.3867, 0.3898, 0.3878, 0.3886, 0.3883, 0.388, 0.3877, 0.3886, 0.3877, 0.3879, 0.3883, 0.3878, 0.3882, 0.3878, 0.3883, 0.3867, 0.3881, 0.3883, 0.3882, 0.3884, 0.3876, 0.3878, 0.3886, 0.3885, 0.3865, 0.3874, 0.3878, 0.388]
from scipy.stats import kstest

print(kstest(datas, 'norm'))

Must be

KstestResult(statistic=0.11781653619879678, pvalue=0.45678095532064167

According to my data, this is coming out:

KstestResult(statistic=0.6504367986973135, pvalue=3.3658717487529006e-21
Barmar
  • 741,623
  • 53
  • 500
  • 612
Dani K
  • 1
  • What do all those other imports have to do with this question? You're not plotting anything, you don't use numpy, random, or pandas. – Barmar May 16 '23 at 20:29
  • Yes, it’s redundant, you’re right.. This refers to another piece of code that works – Dani K May 16 '23 at 20:33
  • I doubt that the code in scipy is wrong. Why do you think your result is correct? – Barmar May 16 '23 at 20:35
  • 1
    There are two different questions here: 1) How well does this data fit a normal distribution? and 2) How well does this data fit a normal distribution with mu=0 and sigma=1? Scipy is answering question #2, but I suspect you really want question #1. Is that right? – Nick ODell May 16 '23 at 20:37
  • e.g. if I subtract the mean and divide by the standard deviation of `datas`, I get this result: `KstestResult(statistic=0.11610318522195917, pvalue=0.4752759129992553)` which is closer to what you expected. – Nick ODell May 16 '23 at 20:39
  • @NickODell Can you explain how to do that? – Dani K May 16 '23 at 22:27
  • @DaniK `from scipy.stats import zscore; datas = zscore(datas)` – Nick ODell May 16 '23 at 22:31

0 Answers0