User Tools

Site Tools


gibson:teaching:fall-2012:math445:sickcat2.m
% Suppose my cat gets sick one day out of seven. 
% How likely is she to get sick two days in one week?

% Here's a compact but tricky way to solve the problem

Ntrials = 10000;   % number of trials
probsick = 1/7;    % prob of being sick on a given day
count = 0;         % number of weeks with two sick days

probsick_vector = probsick*ones(7,1); % auxiliary variable

for n = 1:Ntrials;
  
  % sum(rand(7,1) < probsick_vector) counts the number of sick 
  % days in a given week. If that sum is 2, increment the count
  count = count + (sum(rand(7,1) < probsick_vector) == 2);
  
end
  
prob = count/Ntrials; 

fprintf('Prob of two sick days per week is %f\n', prob);
gibson/teaching/fall-2012/math445/sickcat2.m.txt · Last modified: 2012/09/27 13:45 by gibson