예제 #1
0
def test_multiple_ages(n_people=1e4, location='seattle_metro', state_location='Washington', country_location='usa'):
    sc.heading('Running multiple ages')

    datadir = sp.settings.datadir

    age_bracket_distr = spdd.read_age_bracket_distr(datadir, location, state_location, country_location)
    gender_fraction_by_age = sp.read_gender_fraction_by_age_bracket(datadir, location, state_location, country_location)
    age_brackets_file, age_brackets_filepath = sp.get_census_age_brackets_path(datadir, state_location, country_location)
    age_brackets = sp.get_age_brackets_from_df(age_brackets_filepath)

    ages, sexes = sp.get_age_sex_n(gender_fraction_by_age, age_bracket_distr, age_brackets, n_people)
    print(len(ages), len(sexes))

    return
 def test_get_age_sex_n_honors_ages(self):
     self.is_debugging = False
     age_probabilities = {
         0:  0.05,
         1:  0.10,
         2:  0.15,
         3:  0.20,
         4:  0.00,
         5:  0.05,
         6:  0.00,
         7:  0.09,
         8:  0.08,
         9:  0.07,
         10: 0.06,
         11: 0.05,
         12: 0.04,
         13: 0.03,
         14: 0.02,
         15: 0.01
     }
     sex_by_age_buckets = {}
     sex_by_age_buckets['male'] = {}
     sex_by_age_buckets['female'] = {}
     for x in range(0,16):
         sex_by_age_buckets['male'][x] = 0.5
         sex_by_age_buckets['female'][x] = 0.5
     age_brackets = self.get_census_age_brackets()
     age_array, sex_array = sp.get_age_sex_n(
         gender_fraction_by_age=sex_by_age_buckets,
         age_bracket_distr=age_probabilities,
         age_brackets=age_brackets,
         n_people=10000
     )
     age_count_buckets = self.bucket_population_counts(
         age_bracket_dict=age_brackets,
         ages_array=age_array
     )
     self.verify_portion_honored(
         probability_buckets=age_probabilities,
         count_buckets=age_count_buckets,
         portion=0.2
     )
     self.verify_portion_honored(
         probability_buckets=age_probabilities,
         count_buckets=age_count_buckets,
         portion=0.1
     )
     pass
 def test_get_age_sex_n_honors_sexes(self):
     self.is_debugging = False
     age_buckets = {}
     for x in range(0,10):
         age_buckets[x] = 0.0625  # 1/16 fun fact, works with floating point
     male_age_buckets = {
         0: 1.0,
         1: 0.7,
         2: 0.0,
         3: 0.4
     }
     for x in range(4, 7):
         male_age_buckets[x] = 1.0
     for x in range(7, 10):
         male_age_buckets[x] = 0.0
     female_age_buckets = {}
     for x in range(0, 10):
         female_age_buckets[x] = 1.0 - male_age_buckets[x]
     age_sex_buckets = {}
     age_sex_buckets['male'] = male_age_buckets
     age_sex_buckets['female'] = female_age_buckets
     age_brackets = {}
     for x in range(0, 10):
         age_brackets[x] = [i + (10 * x) for i in range(0,10)]
     age_array, sex_array = sp.get_age_sex_n(
         gender_fraction_by_age=age_sex_buckets,
         age_bracket_distr=age_buckets,
         age_brackets=age_brackets,
         n_people=10000
     )
     weighted_probability_buckets = {}
     total_weight = sum(male_age_buckets.values())
     for i in male_age_buckets:
         weighted_probability_buckets[i] = \
             male_age_buckets[i] / total_weight
     male_age_counts = {}
     for x in range(0,10):
         male_age_counts[x] = 0
     for x in range(0, len(sex_array)):
         if sex_array[x] == 1:
             bucket_index = (age_array[x] // 10)
             male_age_counts[bucket_index] += 1
     self.verify_portion_honored(
         probability_buckets=weighted_probability_buckets,
         count_buckets=list(male_age_counts.values()),
         portion=0.2
     )
예제 #4
0
    def test_seattle_age_sex_n(self):
        self.is_debugging = False
        sea_age_bracket_distro = self.get_seattle_age_brackets()
        sea_sex_age_brackets = self.get_seattle_gender_by_age()
        census_age_brackets = self.get_census_age_brackets()

        age_array, sex_array = sp.get_age_sex_n(
            gender_fraction_by_age=sea_sex_age_brackets,
            age_bracket_distr=sea_age_bracket_distro,
            age_brackets=census_age_brackets,
            n_people=500)
        if self.is_debugging:
            print(f"ages: {age_array}")
            print(f"age array length: {len(age_array)}")
        age_count_buckets = self.bucket_population_counts(
            age_bracket_dict=census_age_brackets, ages_array=age_array)
        self.verify_portion_honored(probability_buckets=sea_age_bracket_distro,
                                    count_buckets=age_count_buckets,
                                    portion=0.2)