# What can be a good way to generate data(tabular) from statistical facts and probability of data?

For Example, If I have facts saying that:

1. 50% of humans are male
2. 30% of males are Indians
3. 70% of Indians are brown
4. average age of Indians are 27
5. 30% of indian females are working
6. 80% of no-indian females work

So, basically a lot of variables which are sometimes dependent on others.

Populate cells in a table with values drawn from appropriate probability distributions. An example using Python:

import random

# probability of value 'yes' for this binary table field
binaryclassprob = 0.3
# probabilities of values 'class1' and 'class2' for this multiclass table field
multiclassprobs = (0.2, 0.3)
# probabilities for dependent binary fields
depprods = [[0.1, 0.2], [0.3, 0.4]]
# parameters for a normally distributed age variable
normdistparams = (26, 7)

# initialize the variable for the table to be populated
table = []
# number of rows to be populated in this example
numrows = 10
for i in range(numrows):
# initialize the variable for the record (row) to be populated
record = [0 for i in range(5)]
# generate value for discrete binary variable using random number
# between 0 and 1 (drawn from a uniform distribution)
if random.random() < binaryclassprob:
record[0] = 'yes'
else:
record[0] = 'no'
# generate value for discrete multiclass variable; the width of an interval
# corresponding to a value is equal to the probability for that value
randnum = random.random()
if randnum < multiclassprobs[0]:
record[1] = 'class1'
elif randnum < multiclassprobs[0] + multiclassprobs[1]:
record[1] = 'class2'
else:
record[1] = 'class3'
# generate values for dependent binary variables - same computational idea
# as for the preceding multiclass field
randnum = random.random()
if randnum < depprods[0][0]:
record[2] = 'yes'
record[3] = 'yes'
elif randnum < depprods[0][0] + depprods[0][1]:
record[2] = 'yes'
record[3] = 'no'
elif randnum < depprods[0][0] + depprods[0][1] + depprods[1][0]:
record[2] = 'no'
record[3] = 'yes'
else:
record[2] = 'no'
record[3] = 'no'
# generate value for continuous normally distributed variable
record[4] = random.normalvariate(normdistparams[0], normdistparams[1])
# append the populated record to the table
table.append(record)
# view the table
print(table)

• Could you explain how the code works? – whuber Jan 23 at 0:27
• What do the code's comment lines leave unexplained? – Mark Pundurs Jan 23 at 21:35
• They explain little: from them we learn the code is intended to generate various random values, but they don't show how you interpret the question nor do they characterize these random distributions. – whuber Jan 23 at 22:01
• Yes, i think @whuber is right here, I would also like to understand more about the code!! The comments are not much explanatory here, sorry!! – Mahesh Mistry Jan 24 at 0:19