clear all
close all
nsubjects = 10000;
prevalence = 0.2; 
se =0.8; sp = 0.7;

tests = [];
ss=[]; a=0; b=0; c=0; d=0;
for i = 1:nsubjects
   %simulate a subjest wp of disease equal to prevalence
   s = (rand < prevalence); 
   %test the subject
   test=s*(rand < se) + (1-s)*(rand>sp); %test result is 0 or 1
   ss=[ss s];            %save subject status
   tests = [tests test]; %save test result 
   %build the test table 
   a = a + test*s;          %TP
   b = b + test*(1-s);      %FP
   c = c + (1-test)*s;      %FN
   d = d + (1-test)*(1-s);  %TN
end
% estimate Se/Sp from the table...should be close to se, sp
sens = a/(a+c) %
spec = d/(b+d)
