Advertisement
elena1234

random sampling- calculate avg mean and avg std in Python

May 19th, 2022 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. '''
  2. Random Sampling from a Population
  3. From lecture, we know that Simple Random Sampling (SRS) has the following properties:
  4.  
  5.    Start with known list of N* population units, and randomly select *n units from the list
  6.    Every unit has equal probability of selection = n/N
  7.    All possible samples of size n are equaly likely
  8.    Estimates of means, proportions, and totals based on SRS are UNBIASED (meaning they are equal to the population values on average)
  9. '''
  10. mu = 0    
  11. sigma = 1
  12. Population = [random.normalvariate(mu, sigma) for _ in range(10000)]
  13.  
  14. SampleA = random.sample(Population, 500)
  15. SampleB = random.sample(Population, 500)
  16.  
  17. np.mean(SampleA)
  18. np.std(SampleA)
  19.  
  20. np.mean(SampleB)
  21. np.std(SampleB)
  22.  
  23. # average mean from 100 random samples with size = 1000
  24. means = [np.mean(random.sample(Population, 1000)) for _ in range(100)]
  25. np.mean(means)  # very close to 0
  26.  
  27. standarddevs = [np.std(random.sample(Population, 1000)) for _ in range(100)]
  28. np.mean(standarddevs) # very close to 1
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement