Skip to content

Commit f067f02

Browse files
Ensure consistency with national demographics
1 parent fbce0f4 commit f067f02

2 files changed

Lines changed: 32 additions & 56 deletions

File tree

  • policyengine_uk_data/datasets/local_areas

policyengine_uk_data/datasets/local_areas/constituencies/loss.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def create_constituency_target_matrix(
2929
if time_period is None:
3030
time_period = dataset.time_period
3131
ages = pd.read_csv(FOLDER / "targets" / "age.csv")
32+
national_demographics = pd.read_csv(STORAGE_FOLDER / "demographics.csv")
3233
incomes = pd.read_csv(FOLDER / "targets" / "spi_by_constituency.csv")
3334
employment_incomes = pd.read_csv(
3435
FOLDER / "targets" / "employment_income.csv"
@@ -83,7 +84,12 @@ def create_constituency_target_matrix(
8384
* national_consistency_adjustment_factor
8485
)
8586

87+
uk_total_population = national_demographics[
88+
national_demographics.name == "uk_population"
89+
][str(time_period)].values[0]
90+
8691
age = sim.calculate("age").values
92+
targets_total_pop = 0
8793
for lower_age in range(0, 80, 10):
8894
upper_age = lower_age + 10
8995

@@ -100,6 +106,16 @@ def create_constituency_target_matrix(
100106

101107
age_str = f"{lower_age}_{upper_age}"
102108
y[f"age/{age_str}"] = age_count.values
109+
targets_total_pop += age_count.values.sum()
110+
111+
# Adjust for consistency
112+
for lower_age in range(80, 120, 5):
113+
upper_age = lower_age + 5
114+
115+
in_age_band = (age >= lower_age) & (age < upper_age)
116+
117+
age_str = f"{lower_age}_{upper_age}"
118+
y[f"age/{age_str}"] *= uk_total_population / targets_total_pop
103119

104120
employment_income = sim.calculate("employment_income").values
105121
bounds = list(
@@ -158,9 +174,6 @@ def create_constituency_target_matrix(
158174
amount_target * adjustment
159175
)
160176

161-
if uprate:
162-
y = uprate_targets(y, dataset.time_period)
163-
164177
const_2024 = pd.read_csv(STORAGE_FOLDER / "constituencies_2024.csv")
165178
const_2010 = pd.read_csv(STORAGE_FOLDER / "constituencies_2010.csv")
166179

@@ -203,29 +216,3 @@ def create_country_mask(
203216
r[i] = household_countries == constituency_countries[i]
204217

205218
return r
206-
207-
208-
def uprate_targets(y: pd.DataFrame, target_year: int = 2025) -> pd.DataFrame:
209-
# Uprate age targets from 2020.
210-
211-
frs_2023 = UKSingleYearDataset(STORAGE_FOLDER / "frs_2023.h5")
212-
213-
sim = Microsimulation(dataset=frs_2023)
214-
matrix_final, _, _ = create_constituency_target_matrix(
215-
frs_2023, target_year, uprate=False
216-
)
217-
is_uprated_from_2020 = [
218-
col.startswith("age/") for col in matrix_final.columns
219-
]
220-
uprating_from_2020 = np.zeros_like(matrix_final.columns, dtype=float)
221-
population = (
222-
sim.tax_benefit_system.parameters.gov.economic_assumptions.indices.ons.population
223-
)
224-
uprating_from_2020[is_uprated_from_2020] = population(
225-
target_year
226-
) / population(2020)
227-
228-
uprating = uprating_from_2020
229-
y = y * (1 + uprating)
230-
231-
return y

policyengine_uk_data/datasets/local_areas/local_authorities/loss.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def create_local_authority_target_matrix(
7474
* national_consistency_adjustment_factor
7575
)
7676

77+
age = sim.calculate("age").values
78+
national_demographics = pd.read_csv(STORAGE_FOLDER / "demographics.csv")
79+
uk_total_population = national_demographics[
80+
national_demographics.name == "uk_population"
81+
][str(time_period)].values[0]
82+
7783
age = sim.calculate("age").values
7884
for lower_age in range(0, 80, 10):
7985
upper_age = lower_age + 10
@@ -91,6 +97,16 @@ def create_local_authority_target_matrix(
9197

9298
age_str = f"{lower_age}_{upper_age}"
9399
y[f"age/{age_str}"] = age_count.values
100+
targets_total_pop += age_count.values.sum()
101+
102+
# Adjust for consistency
103+
for lower_age in range(80, 120, 5):
104+
upper_age = lower_age + 5
105+
106+
in_age_band = (age >= lower_age) & (age < upper_age)
107+
108+
age_str = f"{lower_age}_{upper_age}"
109+
y[f"age/{age_str}"] *= uk_total_population / targets_total_pop
94110

95111
employment_income = sim.calculate("employment_income").values
96112
bounds = list(
@@ -147,9 +163,6 @@ def create_local_authority_target_matrix(
147163
amount_target * adjustment
148164
)
149165

150-
if uprate:
151-
y = uprate_targets(y, time_period)
152-
153166
country_mask = create_country_mask(
154167
household_countries=sim.calculate("country").values,
155168
codes=la_codes.code,
@@ -179,27 +192,3 @@ def create_country_mask(
179192
r[i] = household_countries == constituency_countries[i]
180193

181194
return r
182-
183-
184-
def uprate_targets(y: pd.DataFrame, target_year: int = 2025) -> pd.DataFrame:
185-
frs_2023 = UKSingleYearDataset(STORAGE_FOLDER / "frs_2023.h5")
186-
187-
sim = Microsimulation(dataset=frs_2023)
188-
matrix_final, _, _ = create_local_authority_target_matrix(
189-
frs_2023, target_year, uprate=False
190-
)
191-
is_uprated_from_2020 = [
192-
col.startswith("age/") for col in matrix_final.columns
193-
]
194-
uprating_from_2020 = np.zeros_like(matrix_final.columns, dtype=float)
195-
population = (
196-
sim.tax_benefit_system.parameters.gov.economic_assumptions.indices.ons.population
197-
)
198-
uprating_from_2020[is_uprated_from_2020] = population(
199-
target_year
200-
) / population(2020)
201-
202-
uprating = uprating_from_2020
203-
y = y * (1 + uprating)
204-
205-
return y

0 commit comments

Comments
 (0)