@@ -82,39 +82,60 @@ def create_constituency_target_matrix(
8282 employment_incomes .employment_income_lower_bound .sort_values ().unique ()
8383 ) + [np .inf ]
8484
85- employment_incomes_all = employment_incomes .groupby ("code" )[["employment_income_count" ,"employment_income_amount" ]].sum ().reset_index ()
86-
85+ employment_incomes_all = (
86+ employment_incomes .groupby ("code" )[
87+ ["employment_income_count" , "employment_income_amount" ]
88+ ]
89+ .sum ()
90+ .reset_index ()
91+ )
8792
8893 hmrc_all_count_target = incomes ["employment_income_count" ].values
89- ons_all_count_target = employment_incomes_all ["employment_income_count" ].values
94+ ons_all_count_target = employment_incomes_all [
95+ "employment_income_count"
96+ ].values
9097 count_scaling_factors = hmrc_all_count_target / ons_all_count_target
9198
9299 hmrc_all_amount_target = incomes ["employment_income_amount" ].values
93- ons_all_amount_target = employment_incomes_all ["employment_income_amount" ].values
100+ ons_all_amount_target = employment_incomes_all [
101+ "employment_income_amount"
102+ ].values
94103 amount_scaling_factors = hmrc_all_amount_target / ons_all_amount_target
95104
96- print (f"Average count scaling factor: { count_scaling_factors .mean ():.1%} " )
97- print (f"Average count (HMRC): { hmrc_all_count_target .mean ()/ 1e3 :,.0f} (thousands)" )
98- print (f"Average count (ONS): { ons_all_count_target .mean ()/ 1e3 :,.0f} (thousands)" )
99- print (f"Average amount scaling factor: { amount_scaling_factors .mean ():.1%} " )
100- print (f"Average amount (HMRC): { hmrc_all_amount_target .mean ()/ 1e6 :,.0f} (millions)" )
101- print (f"Average amount (ONS): { ons_all_amount_target .mean ()/ 1e6 :,.0f} (millions)" )
102-
103105 for lower_bound , upper_bound in zip (bounds [:- 1 ], bounds [1 :]):
104- continue
105- if lower_bound <= 12_570 :
106+ if (
107+ lower_bound <= 15_000
108+ ): # Skip some targets with very small sample sizes
106109 continue
107110 if upper_bound >= 200_000 :
108111 continue
109- count_target = employment_incomes [
110- (employment_incomes .employment_income_lower_bound == lower_bound )
111- & (employment_incomes .employment_income_upper_bound == upper_bound )
112- ].employment_income_count .values * count_scaling_factors
112+ count_target = (
113+ employment_incomes [
114+ (
115+ employment_incomes .employment_income_lower_bound
116+ == lower_bound
117+ )
118+ & (
119+ employment_incomes .employment_income_upper_bound
120+ == upper_bound
121+ )
122+ ].employment_income_count .values
123+ * count_scaling_factors
124+ )
113125
114- amount_target = employment_incomes [
115- (employment_incomes .employment_income_lower_bound == lower_bound )
116- & (employment_incomes .employment_income_upper_bound == upper_bound )
117- ].employment_income_amount .values * amount_scaling_factors
126+ amount_target = (
127+ employment_incomes [
128+ (
129+ employment_incomes .employment_income_lower_bound
130+ == lower_bound
131+ )
132+ & (
133+ employment_incomes .employment_income_upper_bound
134+ == upper_bound
135+ )
136+ ].employment_income_amount .values
137+ * amount_scaling_factors
138+ )
118139
119140 if count_target .mean () < 200 :
120141 print (
@@ -135,11 +156,6 @@ def create_constituency_target_matrix(
135156 & (age >= 16 )
136157 )
137158 band_str = f"{ lower_bound } _{ upper_bound } "
138- matrix [f"hmrc/employment_income/count/{ band_str } " ] = sim .map_result (
139- in_bound , "person" , "household"
140- )
141- y [f"hmrc/employment_income/count/{ band_str } " ] = count_target
142-
143159 matrix [f"hmrc/employment_income/amount/{ band_str } " ] = sim .map_result (
144160 employment_income * in_bound , "person" , "household"
145161 )
0 commit comments