darrenbarren
darrenbarren

Reputation: 21

Generating normally distributed samples

need help understanding these lines of code! what I have here is 3 sub population: 10, 30, and 50 each normally distributed with their respective std dev and mean as shown: for e.g.: sub population 10 would have a std dev of 3000 and mean 10,000. what I want to do is to conduct random sampling of these sub population and put the results in a list I named "sample". question is: did I code my objective correctly in the line of sample += np.random.normal(mean_List[I], std_list[I], sub_pop[I]).tolist()?

sub_pop = [10, 30, 50]
std_list = [3000, 5000, 8000]
mean_list = [10000, 15000, 30000]
sample = []
for i in range(len(sub_pop)):
sample += np.random.normal(mean_list[i],std_list[i], sub_pop[i]).tolist()
sample1 = [abs(x) for x in sample]
print(sample1)
print(np.median(sample1))

Upvotes: 0

Views: 83

Answers (1)

roganjosh
roganjosh

Reputation: 13175

From your comments, it seems that you know what the code is doing already and just want some assurance that you're calling the numpy method with the correct parameters. I think there is one issue in your current code: you're merging all of your populations into a single output sample.

To illustrate this, I've bumped the population sizes up to 5000 for plotting. If you don't clear the sample list on each iteration, you just keep adding to it:

import numpy as np

import matplotlib.pyplot as plt

sub_pop = [5000, 5000, 5000]
std_list = [3000, 5000, 8000]
mean_list = [10000, 15000, 30000]
sample = []
for i in range(len(sub_pop)):
    sample += np.random.normal(mean_list[i],std_list[i], sub_pop[i]).tolist()
    sample1 = [abs(x) for x in sample]

plt.hist(sample, bins=500)
#plt.show()
plt.savefig('all_merged.png')

Which gives: enter image description here

Instead, I think you probably want three separate populations. If you don't want separate populations, at least it serves as clarification over whether you're getting the output you expect :)

import numpy as np

import matplotlib.pyplot as plt

sub_pop = [5000, 5000, 5000]
std_list = [3000, 5000, 8000]
mean_list = [10000, 15000, 30000]
sample = []
for i in range(len(sub_pop)):
    sample += np.random.normal(mean_list[i],std_list[i], sub_pop[i]).tolist()
    sample1 = [abs(x) for x in sample]

    plt.hist(sample, bins=500)
    sample = [] # Clear the sample list
#plt.show()
plt.savefig('separated.png')

enter image description here

Upvotes: 1

Related Questions