% MS-E2170 Simulation
% Exercise 1.4: Chi-squared test
%
% From the second lecture:
% "k should be at least 100"
% "N/k should be at least 5"
% => if k = 100, N > 500.
%
% Created: 2018-02-19 Heikki Puustinen
%% Initialization
% Sample size for sequence of random numbers.
N = 50;
% Number of sub intervals.
k = 9;
% Number of independent sequences of random numbers.
M = 100;
% Initialize variable where the test results are stored.
p = zeros(M,1);
%% Simulation
for ii = 1:M
% Create random numbers for the ii:th case.
r = rand(N,1);
% Run the test.
p(ii) = chitest(r,k);
end
% Calculate the proportion of rejected cases.
y = sum(p < 0.05) / M;
%% Results
fprintf('Proportion of rejected cases: %.3f\n',y)