SHARE
TWEET

Birthday simulation

a guest Jun 29th, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from __future__ import division
  2. import random
  3. import math
  4.  
  5. def generateBirthdayCounts(numberBirthdays):
  6.     birthdays = []
  7.     for i in range(numberBirthdays):
  8.         birthdays.append(random.randint(1,365))
  9.     # Find counts for each birthday
  10.     dayCounts = [birthdays.count(day) for day in range(1,366)]
  11.     return dayCounts
  12.  
  13. def optimumBirthdayChoice(dayCounts, numberBirthdays):
  14.     # Find how many days need to be chosen to cover half of the birthdays.
  15.     birthdaySum = 0
  16.     numberDays = 0
  17.     k = math.ceil(numberBirthdays*0.5)
  18.     while (birthdaySum < k):
  19.         # Add maximum day count to cumulative sum of birthdays.
  20.         maxCount = max(dayCounts)
  21.         maxCountIndex = dayCounts.index(maxCount)
  22.         dayCounts[maxCountIndex] = 0
  23.         birthdaySum += maxCount
  24.         numberDays += 1
  25.     return numberDays
  26.  
  27. def numberDaysSample(numberBirthdays, sampleSize):
  28.     # Sample many times and return a list of required number of days.
  29.     daysSamples = []
  30.     for i in range(sampleSize):
  31.         dayCounts = generateBirthdayCounts(numberBirthdays)
  32.         daysRequired = optimumBirthdayChoice(dayCounts, numberBirthdays)
  33.         daysSamples.append(daysRequired)
  34.     return daysSamples    
  35.  
  36. def daysRequiredStatistics(numberBirthdays, sampleSize):
  37.     # Get the sample mean and variance of days required.
  38.     daysRequiredSamples = numberDaysSample(numberBirthdays, sampleSize)
  39.     daysSum = sum(daysRequiredSamples)
  40.     meanDays = daysSum/sampleSize
  41.     squaredDays = [x*x for x in daysRequiredSamples]
  42.     sumSq = sum(squaredDays)
  43.     averageSquaredDays = sumSq/sampleSize
  44.     variance = averageSquaredDays - (meanDays**2)
  45.     statistics = [meanDays, variance]
  46.     return statistics
  47.  
  48. def makeConfidenceInterval(mean, variance, sampleSize):
  49.     # Construct a 95% confidence interval.
  50.     intervalMin = mean - 1.96*((variance/sampleSize)**0.5)
  51.     intervalMax = mean + 1.96*((variance/sampleSize)**0.5)
  52.     interval = [intervalMin, intervalMax]
  53.     return interval
  54.    
  55. numberBirthdays = 2000
  56. sampleSize = 1000
  57. birthdayStatistics = daysRequiredStatistics(numberBirthdays, sampleSize)
  58. mean = birthdayStatistics[0]
  59. variance = birthdayStatistics[1]
  60. confInt = makeConfidenceInterval(mean, variance, sampleSize)
  61. print("mean days is " + str(mean))
  62. print("variance days is " + str(variance))
  63. print("95% confidence interval is " + str(confInt))
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