% Filename: SleepingPillEfficacy.m
% M-file to estimate probability of sleeping better given that you take
%  a sleeping pill.

% Set how many times to run the experiment
N = 1000;

% Set variables to sum up the probabilities of events
A = 0;      % probability took sleeping pill
B = 0;      % probability took sleeping pill + had improved sleep

% Run set of N experiments N times
for i = 1:N
   % Set counters
   a = 0;   % took sleeping pill counter
   b = 0;   % took sleeping pill + had improved sleep counter
   
   % Run experiment N timess
   for j = 1:N
       % Select one participant
       x = DrugTesting;
       
       % If participant took sleeping pill
       if x(1,1)==1
           a = a + 1;
           
           % If participant also had improved sleep
           if x(1,2)==1
               b = b + 1;
           end
       end
   end
   
   % Add the probabilities for this set of N experiments
   % to the running sum
   A = A + a/N;
   B = B + b/N;
end

% Print out result
fprintf('P(improved sleep|took sleeping pill)=%6.4f\n',(B/N)/(A/N));
