How do I construct a random sample from a subdivided population?

Ben S.

Suppose there are 1000 people and they are all asked if their favorite meal is breakfast, lunch, or dinner. I store that info in a list like:

mylist = [350, 450, 200]

i.e. 350 people like breakfast, 450 like lunch, 200 like dinner.

How do I randomly sample 100 people and get a similar list back? That is I want to get randomly sampled lists like

[35, 45, 20]
[33, 42, 25]
[37, 46, 17]

Thanks..

EDIT: I want to add one thing. The desired behavior is sampling without replacement. Suppose that, following the above example, I want to sample 999 people from the original 1000. It shouldn't be possible to get a list back that is, for instance, [350, 458, 201], because there aren't 201 people who like dinner.

Miriam Farber

This will do the job:

import numpy as np
res=np.random.choice(350*[0]+450*[1]+200*[2],size=100,replace=False)
np.histogram(res,range(4))[0]

And more generally:

import numpy as np
v=[350,450,200]
res=np.random.choice(np.repeat(range(len(v)),v),size=100,replace=False)
np.histogram(res,range(len(v)+1))[0]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I take a replicable random sample from a population matrix? (Matlab)

python: how do I randomly sample a number of samples from a population?

How do I filter out random sample from dataframe in which there are different sample size for each value, in python?

r: How to sample from a population of size 1?

Remove a single element from python's random.sample population

How do I change my existing R codes from the output of a random sample to a list of all possible outcomes?

How to do i print some numbers using .sample() from the random built in module in python

How do I remove brackets, quotes and commas from the output of random.sample (discord bot made with python)

How do I access a single agent from a population that was passed into a function?

how do i create a filter of planned size from a population?

How to generate random samples from a population in Python?

How do I generate random sample data in my Oracle database?

python random sample for samples larger than population

Generating a random sample list from a population that has no elements in common with another list

Python random.sample giving 'Sample larger than population or is negative' when sample same length as population

"sample larger than population" in random.sample python

How do I construct an object in Perl6 from a hash?

How do I construct a JSON from dynamic data?

How do i construct string from byte array in D

js/reactjs - How do i construct an array from two sources?

How do I construct a ConstraintViolationException?

How do I use audio sample data from Java Sound?

How do I take a sample of features from a shapefile in R?

How to create a random sample from a vector of elements?

How to select a continuous sample at random from a list?

How to sample random datapoints from a dataframe

How can I recover a number from the sum of that number and a random sample from a range?

MATLAB: sample from population randomly many times?

How do I assign a random seed to the dplyr sample_n function?