A normally disturbed random number is generated using randn function in Python. The KS test and Chi-square test is performed to check if the generated numbers belong to a Gaussian (zero mean, unit variance) distribution based on p-value. Then, an offset of 0.01 is added to the generated random numbers and randomness test is performed. The idea is to find the difference between the correct randn and the offset ones using the KS and Chi^2 test.
Here is the code for the KS test. The bin size is fixed (10) for various size of random numbers and the offset is 0.01:
def rand_norm(n=1): x = nrand.normal(0.0,1,n); y = 0.01*ones(n) + x; return x,y n = arange(100,100001,1000); a = zeros(len(n)); b = zeros(len(n)); c = zeros(len(n)); d = zeros(len(n)); bin_value = 10; for i in range(len(n)): x,y = rand_norm(n[i]); hist1, bin_edge1 =histogram(x,bins = bin_value, density = 1); hist2, bin_edge2 =histogram(y,bins = bin_value, density = 1); a[i],b[i]=stats.kstest(hist1,'norm',N = len(hist1)); c[i],d[i]=stats.kstest(hist2,'norm',N = len(hist2)); figure(); semilogy(n,b) xlabel('Number of random variables') ylabel('p-value') title('KS Test for correct randn'); figure(); semilogy(n,d) xlabel('Number of random variables') ylabel('p-value') title('KS Test for offset randn');
The p value of the KS test for both the cases are plotted in semi-log scale.
There is no difference between the p-values for both the cases, verified by plotting them together. I repeated the same experiment with chi-square function in python. Even Chi-square is giving same p-values for both the case.
Not sure where I am going wrong. It would be a great if someone can help! Thanks in advance!