0
votes

I want to generate a random sample size of 90 (10+30+50), create a list that holds these 90 randomly generated samples and find the sample median. this is what I'm given:

sub_pop = [300, 500, 1000]
samplesize = [10, 30, 50]
std_list = [40, 50, 60]
mean_list = [50, 60, 70]

in other words, I want to randomly generate a sub sample size of 10 from sub_pop 300, a sub sample size of 30 from sub_pop 500 and a sub sample size of 50 from sub_pop 1000 and then append the results into a single list. each sub_pop are normally distributed as follows: sub_pop 300 is normally distributed wif std dev 40 and mean 50, sub_pop 500 is normally distributed wif std dev 50 and mean 60 and so on.

this is what I've done so far:

import numpy as np
n_pop = 1800
obs_size = 90
sub_pop = [300, 500, 1000]
samplesize = [10, 30, 50]
std_list = [40, 50, 60]
mean_list = [50, 60, 70]

list = []
for i in range(300):
    list += np.random.normal(loc = 50, scale = 40, size = 10).tolist()

for i in range(500):
    list += np.random.normal(loc = 60, scale = 50, size = 30).tolist()

for i in range(1000):
    list += np.random.normal(loc = 70, scale = 60, size = 50).tolist()

list1 = [abs(x) for x in list]
print(list1)
print(np.median(list1))

is my approach right?

2
I do not get the notion of pop and sample size you are using. To me it looks like, ok, I want 10 element normally distributed around 50 with a std of 40, then I want 30 ... and so on. What is the sub_pop? - Mathieu
hi mathieu I'm sorry for not framing my question as clearly as possible. am not too sure on the technicalities as Im pretty new to python and statistics. so wad I have here is a population size of 1800 which contains 3 different sub population size of 300, 500 and 1000. wad I want to do is to sample 90 from this population while ensuring that the proportion in this sample of 90 is proportionate to the population. - darrenbarren
so "samplesize" is the proportion that I should have in each respective sub population in the sample size of 90 that I hard coded. - darrenbarren
Ok, well from what you write, I feel like we do not care about this population size. Taking directly sample_size elt with the random parameter of the population is enough. Compare to my answer, it means that the size argument must be changed to size=samplesize[i]. - Mathieu
I do not quite get it. would it be possible for u to write it out so I could visualise and understand the code better - darrenbarren

2 Answers

0
votes

Not exactly sure what you are trying to do, but instead of

for i in range(1000):
    list += np.random.normal(loc = 70, scale = 60, size = 50).tolist()

you could use

x = np.random.normal(loc = 70, scale = 60, size = (50, 1000))

Then the absolute is just

x_abs = np.abs(x)

And for the correct use of median use the keyword axis to specify which dimension to operate on.

Maybe

np.median(x_abs, axis=1)
0
votes

Let's say you want one sample, which contains the population specified.

sub_pop = [300, 500, 1000]
samplesize = [10, 30, 50]
std_list = [40, 50, 60]
mean_list = [50, 60, 70]

data=[np.random.normal(loc=mean_list[i], scale=std_list[i], size=samplesize[i]) for i in range(3)]
x = [elt for L in data for elt in L]

The code above create a flat list with all the element in it. Then you want the median, so I assume something like this:

import statistics
statistics.median(x)

Your goal / data is really unclear. Hope you can find a few ideas in the codes we provide.