Advertisement
Guest User

2(a) Sampling + Regress to mean + Confidence Interval

a guest
Feb 6th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. import numpy as np
  2. import random
  3. import matplotlib.pyplot as plt
  4.  
  5. def create_pop(n):
  6.     # to randomly create population distribution set with given population size.
  7.     # replication of uniform distribution
  8.     distrib = 1000 * np.random.random_sample((n, ))
  9.     return distrib
  10.  
  11. def graph_distribution(dist, name):
  12.     plt.hist(dist)
  13.     plt.title(name)
  14.     plt.show()
  15.  
  16. def sample_procedure(dist):
  17.     sample_size = int(raw_input("Enter the sample size:"))
  18.     sample_repeats = int(raw_input("Enter the number of times to repeat the experiment:"))
  19.     sample_means = []
  20.     for _ in range(sample_repeats):        
  21.         # create sample distribution set with given sample size, and calculate the sample mean
  22.         # repeat the process for given times by while loop
  23.         sample = np.random.choice(dist, (sample_size, ))
  24.         this_mean = np.mean(sample)
  25.         sample_means.append(this_mean)
  26.    
  27.     print "Here's what the sample mean distribution looks like."
  28.    
  29.     plt.hist(sample_means)
  30.     plt.title('Sample Means')
  31.     plt.show()
  32.    
  33.     print "The mean of the sample means is:", np.mean(sample_means)
  34.     print "The Standard Error / The standard deviation of the sample means is:", np.std(sample_means)
  35.     print ""
  36.    
  37. #####################################
  38.  
  39. print "First create a (pseudo)-random distribution."
  40. population_size = int(raw_input("Enter a population size:"))
  41. print "..."
  42. population = create_pop(population_size)
  43.  
  44. print "Here's what the population distribution looks like."
  45. graph_distribution(population, 'Population Distribution')
  46.  
  47. print "The population mean is:", np.mean(population)
  48.  
  49. print "For the second step, enter the size of the samples to draw --with replacement-- from the population distribution, and how many times to repeat this procedure in order to create a distribution of sample means."
  50.  
  51. sample_flag = True
  52.  
  53. while sample_flag:
  54.     sample_procedure(population)
  55.     print "Perform sampling procedure again? Note that if no, then the population distribution will be lost."
  56.     decision = raw_input("Type y or n:")
  57.     if decision == 'n':
  58.         sample_flag = False
  59.     else:
  60.         print "Doing procedure again."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement