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'
        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'
        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'
        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
# view the table
  • $\begingroup$ Could you explain how the code works? $\endgroup$ – whuber Jan 23 at 0:27
  • $\begingroup$ What do the code's comment lines leave unexplained? $\endgroup$ – Mark Pundurs Jan 23 at 21:35
  • $\begingroup$ 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. $\endgroup$ – whuber Jan 23 at 22:01
  • $\begingroup$ 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!! $\endgroup$ – Mahesh Mistry Jan 24 at 0:19
  • $\begingroup$ More comments added $\endgroup$ – Mark Pundurs Jan 25 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.