Advertisement
arefindk

one_parent_family_australia

Nov 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.46 KB | None | 0 0
  1. women_possibly_fertile_until = 45
  2.    
  3. neighborhood = []
  4. for sampled_hh in households:
  5.     print 'new sample ################################'
  6.     print '###########################################'
  7.     print 'sampled_hh', sampled_hh
  8.     ## This time lets start with kids first
  9.     num_child = sampled_hh[3]
  10.     num_dep_child = sampled_hh[4]
  11.     num_non_dep_child = num_child - num_dep_child
  12.     num_dep_student = 0
  13.     if num_dep_child > 0:
  14.         for i in range(num_dep_child):
  15.             if np.random.random() < percentage_of_dependent_students:
  16.                 num_dep_student += 1
  17.                 num_dep_child -= 1
  18.     print "num_dep_child", num_dep_child, "num_dep_student", num_dep_student, "num_non_dep_child", num_non_dep_child
  19.    
  20.    
  21.     dep_child_age_range_tuple = (0,14)
  22.     dep_child_age_range = range(dep_child_age_range_tuple[0], dep_child_age_range_tuple[1] + 1)
  23.     dep_student_age_range_tuple = (15,24)
  24.     dep_student_age_range = range(dep_student_age_range_tuple[0], dep_student_age_range_tuple[1] + 1)
  25.     non_dep_child_age_range_tuple = (15,100-15)
  26.     non_dep_child_age_range = range(non_dep_child_age_range_tuple[0], non_dep_child_age_range_tuple[1] + 1)
  27.  
  28.     mom_age_range_tuple = (15,100)
  29.  
  30.     first_sampled_kids = []
  31.     ## Here I am creating a maximum age range for mother based on what kind of kid she can have
  32.     ## be careful and thoughtful, this should be if elif  statement
  33.     if num_dep_child > 0:
  34.         mom_age_range_tuple = (16 + dep_child_age_range_tuple[0], women_possibly_fertile_until + dep_child_age_range_tuple[1])
  35.     elif num_dep_student > 0:
  36.         mom_age_range_tuple = (16 + dep_student_age_range_tuple[0], women_possibly_fertile_until + dep_student_age_range_tuple[1])
  37.     elif num_non_dep_child > 0:
  38.         mom_age_range_tuple = (16 + non_dep_child_age_range_tuple[0], 100)
  39.  
  40.     ## Now I am sampling the first kids based on the type that is possible for this family
  41.     ## these should be if statements rather than if elif
  42.     if num_dep_child > 0:
  43.         first_sampled_dep_childs = []
  44.         for c in range(num_dep_child):
  45.             first_sampled_dep_child = sample_children_age_by_children_type('dep_child', num_kids=1)[0]
  46.             while True:
  47.                 if first_sampled_dep_child in molded_age_distribution['one_fam_one_parent_with_child']:
  48.                     if molded_age_distribution['one_fam_one_parent_with_child'][first_sampled_dep_child] > 0:
  49.                         print "found allowed dep kid from the mold", first_sampled_dep_child
  50.                         break
  51.                 first_sampled_dep_child = sample_children_age_by_children_type('dep_child', num_kids=1)[0]
  52.             first_sampled_dep_childs.append(first_sampled_dep_child)
  53.         print "found all the kids from the mold", first_sampled_dep_childs
  54.         first_sampled_kids.extend(first_sampled_dep_childs)
  55.     if num_dep_student > 0:
  56.         first_sampled_dep_students = []
  57.         for c in range(num_dep_student):
  58.             first_sampled_dep_student = sample_children_age_by_children_type('dep_student', num_kids=1)[0]
  59.             while True:
  60.                 if first_sampled_dep_student in molded_age_distribution['one_fam_one_parent_with_child']:
  61.                     if molded_age_distribution['one_fam_one_parent_with_child'][first_sampled_dep_student] > 0:
  62.                         break
  63.                 first_sampled_dep_student = sample_children_age_by_children_type('dep_student', num_kids=1)[0]
  64.             first_sampled_dep_students.append(first_sampled_dep_student)
  65.         first_sampled_kids.extend(first_sampled_dep_students)
  66.     if num_non_dep_child > 0:
  67.         first_sampled_non_dep_childs = sample_children_age_by_children_type('non_dep_child', num_kids=num_non_dep_child)
  68.         first_sampled_kids.extend(first_sampled_non_dep_childs)
  69.     print 'first_sampled_kids', first_sampled_kids
  70.     oldest_kid = max(first_sampled_kids)
  71.     youngest_kid = min(first_sampled_kids)
  72.     sampled_mom_age = sample_moms_age_based_on_kids_age(oldest_kid)
  73.     ## Sometimes we ge t asampled mom age that is more than 100
  74.     if sampled_mom_age > 100:
  75.         sampled_mom_age = 100
  76.     print "sampled_mom_age", sampled_mom_age
  77.     while True:
  78.         if sampled_mom_age < women_possibly_fertile_until + youngest_kid + 3:
  79.             break
  80.         else:
  81.             first_sampled_kids = []
  82.             if num_dep_child > 0:
  83.                 first_sampled_dep_childs = []
  84.                 for c in range(num_dep_child):
  85.                     first_sampled_dep_child = sample_children_age_by_children_type('dep_child', num_kids=1)[0]
  86.                     while True:
  87.                         if first_sampled_dep_child in molded_age_distribution['one_fam_one_parent_with_child']:
  88.                             if molded_age_distribution['one_fam_one_parent_with_child'][first_sampled_dep_child] > 0:
  89.                                 print "found allowed dep kid from the mold", first_sampled_dep_child
  90.                                 break
  91.                             first_sampled_dep_child = sample_children_age_by_children_type('dep_child', num_kids=1)[0]
  92.                     first_sampled_dep_childs.append(first_sampled_dep_child)
  93.                 print "found all the kids from the mold", first_sampled_dep_childs
  94.                 first_sampled_kids.extend(first_sampled_dep_childs)
  95.             if num_dep_student > 0:
  96.                 first_sampled_dep_students = []
  97.                 for c in range(num_dep_student):
  98.                     first_sampled_dep_student = sample_children_age_by_children_type('dep_student', num_kids=1)[0]
  99.                     while True:
  100.                         if first_sampled_dep_student in molded_age_distribution['one_fam_one_parent_with_child']:
  101.                             if molded_age_distribution['one_fam_one_parent_with_child'][first_sampled_dep_student] > 0:
  102.                                 break
  103.                         first_sampled_dep_student = sample_children_age_by_children_type('dep_student', num_kids=1)[0]
  104.                     first_sampled_dep_students.append(first_sampled_dep_student)
  105.                 first_sampled_kids.extend(first_sampled_dep_students)
  106.             if num_non_dep_child > 0:
  107.                 first_sampled_non_dep_childs = sample_children_age_by_children_type('non_dep_child', num_kids=num_non_dep_child)
  108.                 first_sampled_kids.extend(first_sampled_non_dep_childs)
  109.             print 'first_sampled_kids', first_sampled_kids
  110.             oldest_kid = max(first_sampled_kids)
  111.             youngest_kid = min(first_sampled_kids)
  112.             ## Sometimes we get asampled mom age that is more than 100
  113.             if sampled_mom_age > 100:
  114.                 sampled_mom_age = 100
  115.             sampled_mom_age = sample_moms_age_based_on_kids_age(oldest_kid)
  116.             print "sampled_mom_age", sampled_mom_age
  117.     print "chosen anchor mom age",sampled_mom_age
  118. #     print "mom age range tuple", mom_age_range_tuple
  119. #     anchored_mom_age_range_tuple = max(mom_age_range_tuple[0], sampled_mom_age - 2), min(mom_age_range_tuple[1], sampled_mom_age + 2)
  120. #     print 'anchored_mom_age_range_tuple', anchored_mom_age_range_tuple
  121. #     anchored_mom_age_range = range(max(mom_age_range_tuple[0],anchored_mom_age_range_tuple[0]),min(mom_age_range_tuple[1],anchored_mom_age_range_tuple[1])+1)
  122.  
  123.     sampled_anchored_mom_age = sampled_mom_age #get_mom_age_based_based_on_num_kids(num_child,allowed_age_range=anchored_mom_age_range)
  124.     print "sampled_anchored_mom_age", sampled_anchored_mom_age
  125.    
  126.    
  127.     final_sampled_kids = []
  128.     if num_dep_child > 0:
  129.         sampled_dep_childs = get_kids_based_on_mom_age(sampled_anchored_mom_age, num_dep_child, allowed_kid_age_range=dep_child_age_range)
  130.         #sampled_dep_childs_with_noise = []
  131.         for age in sampled_dep_childs:
  132.             #sampled_noise = sample_from_a_dict(noise)[0]
  133.             #age_with_noise = age + sampled_noise
  134.             #sampled_dep_childs_with_noise.append(age_with_noise)
  135.             if age in children_age_by_children_type_australia['dep_child']:
  136.                 children_age_by_children_type_australia['dep_child'][age] -= 1
  137.         final_sampled_kids.extend(sampled_dep_childs)
  138.     if num_dep_student > 0:
  139.         sampled_dep_students = get_kids_based_on_mom_age(sampled_anchored_mom_age, num_dep_student, allowed_kid_age_range=dep_student_age_range)
  140.         for age in sampled_dep_students:
  141.             children_age_by_children_type_australia['dep_student'][age] -= 1
  142.         final_sampled_kids.extend(sampled_dep_students)
  143.     if num_non_dep_child > 0:
  144.         sampled_non_dep_childs = get_kids_based_on_mom_age(sampled_anchored_mom_age, num_non_dep_child, allowed_kid_age_range=non_dep_child_age_range)
  145.         for age in sampled_non_dep_childs:
  146.             children_age_by_children_type_australia['non_dep_child'][age] -= 1
  147.         final_sampled_kids.extend(sampled_non_dep_childs)
  148.     print 'final_sampled_kids', final_sampled_kids
  149.  
  150.     ## check if the sampled_mom was the lone person ref person male or female
  151.     if np.random.random() < probability_male_lone_parent_in_primary_family:
  152.         gender = 'male'
  153.     else:
  154.         gender = 'female'
  155.     if gender == 'male':
  156.         ## we need to sample a father based on the anchored moms age
  157.         sampled_age_gap_tuple = sample_from_a_dict(age_gap_distribution)
  158.         print 'sampled_age_gap_tuple', sampled_age_gap_tuple
  159.         ## I am adding a plus one and minus one in the range in case we have a range of [0,0] from the age gap tuple
  160.         male_lone_parent_age_range_tuple = max(16, sampled_anchored_mom_age+ sampled_age_gap_tuple[0]) - 1, min(100, sampled_anchored_mom_age + sampled_age_gap_tuple[1] + 1) + 1
  161.         male_lone_parent_age_range_tuple = check_and_correct_for_extreme_age_ranges(male_lone_parent_age_range_tuple)
  162.         male_lone_parent_age_range = range(male_lone_parent_age_range_tuple[0], male_lone_parent_age_range_tuple[1] + 1)
  163.         print "male lone parent age_rage", male_lone_parent_age_range
  164.         allowed_male_lone_parent_tuples = product(['male'], male_lone_parent_age_range)
  165.         sampled_final_lone_parent_age = sample_from_a_dict(ref_person_lone_parent_age_count_by_sex_flattened, allowed_keys=allowed_male_lone_parent_tuples)[1]
  166.         print 'sampled_final_lone_parent_age', sampled_final_lone_parent_age
  167.     elif gender == 'female':
  168.         female_lone_parent_age_range_tuple = (max(15,sampled_anchored_mom_age - 1), min(100, sampled_anchored_mom_age + 1))
  169.         female_lone_parent_age_range_tuple = check_and_correct_for_extreme_age_ranges(female_lone_parent_age_range_tuple)
  170.         female_lone_parent_age_range = range(female_lone_parent_age_range_tuple[0], female_lone_parent_age_range_tuple[1] + 1)
  171.         allowed_female_lone_parent_tuples = product(['female'], female_lone_parent_age_range)
  172.         #print list(allowed_female_lone_parent_tuples)
  173.         sampled_final_lone_parent_age = sample_from_a_dict(ref_person_lone_parent_age_count_by_sex_flattened, allowed_keys=allowed_female_lone_parent_tuples)[1]
  174.         print 'sampled_final_lone_parent_age', sampled_final_lone_parent_age
  175.     household = []
  176.     household.append(sampled_final_lone_parent_age)
  177.     household.extend(final_sampled_kids)
  178.     for age in household:
  179.         if age in molded_age_distribution['one_fam_one_parent_with_child'] and molded_age_distribution['one_fam_one_parent_with_child'][age] > 0:
  180.             molded_age_distribution['one_fam_one_parent_with_child'][age] -= 1
  181.     print 'final household', household
  182.    
  183.     print
  184.     print
  185.     neighborhood.append(household)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement