%% Demonstration of use of kstest in MATLAB
% From "help kstest" in Matlab:
% kstest Single sample Kolmogorov-Smirnov goodness-of-fit hypothesis test.
%     H = kstest(X) performs a Kolmogorov-Smirnov (K-S) test to determine if a random sample X could have come from a standard normal distribution, N(0,1). H indicates the result of the hypothesis test:
%        H = 0 => Do not reject the null hypothesis at the 5% significance level.
%        H = 1 => Reject the null hypothesis at the 5% significance level.
%
% Generate some normally distirbuted data around some nonzero mean with a standard deviation not equal to 1, i.e. generate data from a normal distribution but NOT the standard normal distribution N(0,1) with mean zero and standard deviation 1.

mu = 1.5;
sigma = 0.1;
N = 25;
data = normrnd(mu,sigma,1,N);
histogram(data)

%
% Do a hypothesis test on the data for the standard normal distribution. We expect it to fail pretty close to 100% of the time and for the p-values in general to be tiny.

[h,p] = kstest(data);
if h
    disp('Reject hypothesis of standard normal distribution')
    fprintf('p-value = %e\n\n',p)
end

%
% Now shift the data to the known mean and calculate the differences relative to the known standard deviation of the data. If we know the true mean and sigma we expect this to pass the kstest 95% of the time (the 5% rejection rate we expect is due to the default setting of 5% significance level for the test). In the large-sample limit we expect the p-values of the KS-test to be uniformly distributed bertween 0 and 1.

[h,p] = kstest((data-mu)/sigma);
if ~h
    disp('Do NOT reject hypothesis of known normal distribution')
    fprintf('p-value = %f\n',p)
end

%
% If we use the mean and sigma of the (simulated) data we expect the p-values to no longer be uniformaly distirbuted but to pile up toward 1 and the rejection rate to be less than the significance level of 5%.

[h,p] = kstest((data-mean(data))/std(data));
if ~h
    disp('Do NOT reject hypothesis of data-driven normal distribution')
    fprintf('p-value = %f\n',p)
end