SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %Birthday problem simulation method
  2.  
  3. numPeople = 86;
  4.  
  5. %Let's ignore leap years
  6. numDays = 356;
  7. numTrials = 100000;
  8. cumTriples = zeros(numTrials, 1);
  9.  
  10. numPeople = round(numPeople);
  11.  
  12. for i = 1:numTrials
  13.     birthdays = round(numDays .* rand(numPeople, 1));
  14.     numTriples = sum(hist(birthdays, unique(birthdays)) > 2);
  15.     cumTriples(i) = numTriples;
  16. end
  17.  
  18. percent = sum(logical(cumTriples)) / numTrials;
  19.  
  20. % It seems as though 86 is the number of people one would expect to require
  21. % based on this simulation.
  22.  
  23.  
  24. % Birthday problem Poisson Approximation
  25.  
  26. probability = 1 - exp(-(nchoosek(numPeople, 3)) / 365^2);
  27.  
  28. fprintf('In Simulation 3 in %d people share a birthday with probability %f\n', numPeople, percent);
  29. fprintf('By a Poisson Approximation, 3 in %d people share a birthday with probability %f \n', numPeople, probability);
  30.  
  31. % It seems as though 84 is the number of people one would expect to require
  32. % if we were going on the Poisson approximation.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top