0
votes

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))
1
Does it give you an output that you expect? - roganjosh
im not too sure, it seems right. but I want to understand the logic behind the code - darrenbarren
like what does "sample += np.random.normal(mean_list[I], std_list[I], sub_pop[I]).tolist() do exactly? I know it randomly picks data from the data I have. but wad Im unsure is, when I do this, does python know I want sub_pop 10 to be mapped with std_list 3000 and mean_list 10,000 and sub_pop 30 mapped with std_list of 5000 and mean_list of 15000 and so on - darrenbarren
What part is unclear though? You described what you want to do and then showed the code that you've implemented to do it - roganjosh
by writing mean_list[I], std_list[I], sub_pop[I] in the np.random.normal function, does it do exactly that? - darrenbarren

1 Answers

1
votes

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