-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcalibrate.py
More file actions
369 lines (324 loc) · 14.4 KB
/
Copy pathcalibrate.py
File metadata and controls
369 lines (324 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from contextlib import nullcontext
from pathlib import Path
from typing import Optional, Union
import torch
import pandas as pd
import numpy as np
import h5py
from policyengine_uk_data.storage import STORAGE_FOLDER
from policyengine_uk.data import UKSingleYearDataset
from policyengine_uk_data.utils.progress import ProcessingProgress
def load_weights(
weight_file: Union[str, Path],
dataset_key: str = "2025",
n_areas: Optional[int] = None,
n_records: Optional[int] = None,
) -> np.ndarray:
"""Load calibration weights from an h5 file and normalise their shape.
Two calibration back-ends exist in this repo's history: the L2
calibrator in `calibrate_local_areas` (this module) saves weights as a
2D ``(n_areas, n_records)`` array, while the L0-regularised variant
(when present) sometimes saves a flat 1D ``(n_records,)`` array under
the same dataset key. Consumers that are not careful about axes can
therefore silently read the wrong shape.
This helper centralises loading and always returns a 2D
``(n_areas, n_records)`` array. A 1D input is reshaped to
``(1, n_records)`` so downstream ``.sum(axis=0)`` and matrix-multiply
operations behave consistently. Optional ``n_areas`` / ``n_records``
arguments raise a clear ``ValueError`` on shape mismatch instead of
silently producing wrong answers.
Args:
weight_file: Path to the h5 file written by a calibrator. If the
path is not absolute it is resolved relative to the package
``STORAGE_FOLDER``.
dataset_key: H5 dataset key to read.
n_areas: Optional expected number of areas (first axis). When
provided, a 1D input is reshaped and its length checked; a 2D
input has its first axis checked.
n_records: Optional expected number of records (second axis).
Checked against the final axis of the loaded array.
Returns:
A 2D ``(n_areas, n_records)`` numpy array.
"""
path = Path(weight_file)
if not path.is_absolute():
path = STORAGE_FOLDER / path
with h5py.File(path, "r") as f:
if dataset_key not in f:
available = ", ".join(sorted(f.keys()))
raise KeyError(
f"Dataset key {dataset_key!r} not found in {path}; "
f"available keys: {available}"
)
arr = f[dataset_key][:]
if arr.ndim == 1:
# Flat (n_records,) layout — promote to (1, n_records) so callers
# can treat all weights as a 2D matrix.
arr = arr.reshape(1, -1)
elif arr.ndim != 2:
raise ValueError(
f"Expected weights at {dataset_key!r} in {path} to be 1D or 2D; "
f"got shape {arr.shape}"
)
if n_areas is not None and arr.shape[0] != n_areas:
raise ValueError(
f"Weights at {dataset_key!r} in {path} have {arr.shape[0]} areas, "
f"expected {n_areas}"
)
if n_records is not None and arr.shape[-1] != n_records:
raise ValueError(
f"Weights at {dataset_key!r} in {path} have {arr.shape[-1]} "
f"records, expected {n_records}"
)
return arr
def calibrate_local_areas(
dataset: UKSingleYearDataset,
matrix_fn,
national_matrix_fn,
area_count: int,
weight_file: str,
dataset_key: str = "2025",
epochs: int = 512,
excluded_training_targets=[],
log_csv=None,
verbose: bool = False,
area_name: str = "area",
get_performance=None,
nested_progress=None,
):
"""
Generic calibration function for local areas (constituencies, local authorities, etc.)
Args:
dataset: The dataset to calibrate
matrix_fn: Function that returns (matrix, targets, mask) for the local areas
national_matrix_fn: Function that returns (matrix, targets) for national totals
area_count: Number of areas (e.g., 650 for constituencies, 360 for local authorities)
weight_file: Name of the h5 file to save weights to
dataset_key: Key to use in the h5 file
epochs: Number of training epochs
excluded_training_targets: List of targets to exclude from training (for validation)
log_csv: CSV file to log performance to
verbose: Whether to print progress
area_name: Name of the area type for logging
"""
progress_tracker = ProcessingProgress() if verbose else None
def track_stage(stage_name: str):
if progress_tracker is None:
return nullcontext()
return progress_tracker.track_stage(stage_name)
with track_stage(f"{area_name}: copy dataset"):
dataset = dataset.copy()
with track_stage(f"{area_name}: build local target matrix"):
matrix, y, r = matrix_fn(dataset)
m_c, y_c = matrix.copy(), y.copy()
with track_stage(f"{area_name}: build national target matrix"):
m_national, y_national = national_matrix_fn(dataset)
m_n, y_n = m_national.copy(), y_national.copy()
with track_stage(f"{area_name}: prepare tensors and optimizer"):
# Weights - area_count x num_households
# Use country-aware initialization: divide each household's weight by the
# number of areas in its country, not the total area count. This ensures
# households start at approximately correct weight for their country's targets.
# The country_mask r[i,j]=1 iff household j is in same country as area i.
areas_per_household = r.sum(
axis=0
) # number of areas each household can contribute to
areas_per_household = np.maximum(
areas_per_household, 1
) # avoid division by zero
original_weights = np.log(
dataset.household.household_weight.values / areas_per_household
+ np.random.random(len(dataset.household.household_weight.values)) * 0.01
)
weights = torch.tensor(
np.ones((area_count, len(original_weights))) * original_weights,
dtype=torch.float32,
requires_grad=True,
)
# Set up validation targets if specified
validation_targets_local = (
matrix.columns.isin(excluded_training_targets)
if hasattr(matrix, "columns")
else None
)
validation_targets_national = (
m_national.columns.isin(excluded_training_targets)
if hasattr(m_national, "columns")
else None
)
dropout_targets = len(excluded_training_targets) > 0
# Convert to tensors
metrics = torch.tensor(
matrix.values if hasattr(matrix, "values") else matrix,
dtype=torch.float32,
)
y = torch.tensor(y.values if hasattr(y, "values") else y, dtype=torch.float32)
matrix_national = torch.tensor(
m_national.values if hasattr(m_national, "values") else m_national,
dtype=torch.float32,
)
y_national = torch.tensor(
y_national.values if hasattr(y_national, "values") else y_national,
dtype=torch.float32,
)
r = torch.tensor(r, dtype=torch.float32)
def sre(x, y):
one_way = ((1 + x) / (1 + y) - 1) ** 2
other_way = ((1 + y) / (1 + x) - 1) ** 2
return torch.min(one_way, other_way)
def loss(w, validation: bool = False):
pred_local = (w.unsqueeze(-1) * metrics.unsqueeze(0)).sum(dim=1)
if dropout_targets and validation_targets_local is not None:
if validation:
mask = validation_targets_local
else:
mask = ~validation_targets_local
pred_local = pred_local[:, mask]
mse_local = torch.mean(sre(pred_local, y[:, mask]))
else:
mse_local = torch.mean(sre(pred_local, y))
pred_national = (w.sum(axis=0) * matrix_national.T).sum(axis=1)
if dropout_targets and validation_targets_national is not None:
if validation:
mask = validation_targets_national
else:
mask = ~validation_targets_national
pred_national = pred_national[mask]
mse_national = torch.mean(sre(pred_national, y_national[mask]))
else:
mse_national = torch.mean(sre(pred_national, y_national))
return mse_local + mse_national
def pct_close(w, t=0.1, local=True, national=True):
"""Return the percentage of metrics that are within t% of the target"""
numerator = 0
denominator = 0
if local:
pred_local = (w.unsqueeze(-1) * metrics.unsqueeze(0)).sum(dim=1)
e_local = torch.sum(torch.abs((pred_local / (1 + y) - 1)) < t).item()
c_local = pred_local.shape[0] * pred_local.shape[1]
numerator += e_local
denominator += c_local
if national:
pred_national = (w.sum(axis=0) * matrix_national.T).sum(axis=1)
e_national = torch.sum(
torch.abs((pred_national / (1 + y_national) - 1)) < t
).item()
c_national = pred_national.shape[0]
numerator += e_national
denominator += c_national
return numerator / denominator
def dropout_weights(weights, p):
if p == 0:
return weights
# Replace p% of the weights with the mean value of the rest of them
mask = torch.rand_like(weights) < p
mean = weights[~mask].mean()
masked_weights = weights.clone()
masked_weights[mask] = mean
return masked_weights
optimizer = torch.optim.Adam([weights], lr=1e-1)
final_weights = (torch.exp(weights) * r).detach().numpy()
performance = pd.DataFrame()
if verbose and progress_tracker:
with progress_tracker.track_calibration(
epochs, nested_progress
) as update_calibration:
for epoch in range(epochs):
update_calibration(epoch + 1, calculating_loss=True)
optimizer.zero_grad()
weights_ = torch.exp(dropout_weights(weights, 0.05)) * r
loss_value = loss(weights_)
loss_value.backward()
optimizer.step()
local_close = pct_close(weights_, local=True, national=False)
national_close = pct_close(weights_, local=False, national=True)
if dropout_targets:
validation_loss = loss(weights_, validation=True)
update_calibration(
epoch + 1,
loss_value=validation_loss.item(),
calculating_loss=False,
)
else:
update_calibration(
epoch + 1,
loss_value=loss_value.item(),
calculating_loss=False,
)
if epoch % 10 == 0:
final_weights = (torch.exp(weights) * r).detach().numpy()
# Log performance if requested and get_performance function is available
if log_csv and get_performance:
performance_step = get_performance(
final_weights,
m_c,
y_c,
m_n,
y_n,
excluded_training_targets,
)
performance_step["epoch"] = epoch
performance_step["loss"] = performance_step.rel_abs_error**2
performance_step["target_name"] = [
f"{area}/{metric}"
for area, metric in zip(
performance_step.name, performance_step.metric
)
]
performance = pd.concat(
[performance, performance_step], ignore_index=True
)
performance.to_csv(log_csv, index=False)
# Save weights
with h5py.File(STORAGE_FOLDER / weight_file, "w") as f:
f.create_dataset(dataset_key, data=final_weights)
dataset.household.household_weight = final_weights.sum(axis=0)
else:
for epoch in range(epochs):
optimizer.zero_grad()
weights_ = torch.exp(dropout_weights(weights, 0.05)) * r
loss_value = loss(weights_)
loss_value.backward()
optimizer.step()
local_close = pct_close(weights_, local=True, national=False)
national_close = pct_close(weights_, local=False, national=True)
if verbose and (epoch % 1 == 0):
if dropout_targets:
validation_loss = loss(weights_, validation=True)
print(
f"Training loss: {loss_value.item():,.3f}, Validation loss: {validation_loss.item():,.3f}, Epoch: {epoch}, "
f"{area_name}<10%: {local_close:.1%}, National<10%: {national_close:.1%}"
)
else:
print(
f"Loss: {loss_value.item()}, Epoch: {epoch}, {area_name}<10%: {local_close:.1%}, National<10%: {national_close:.1%}"
)
if epoch % 10 == 0:
final_weights = (torch.exp(weights) * r).detach().numpy()
# Log performance if requested and get_performance function is available
if log_csv:
performance_step = get_performance(
final_weights,
m_c,
y_c,
m_n,
y_n,
excluded_training_targets,
)
performance_step["epoch"] = epoch
performance_step["loss"] = performance_step.rel_abs_error**2
performance_step["target_name"] = [
f"{area}/{metric}"
for area, metric in zip(
performance_step.name, performance_step.metric
)
]
performance = pd.concat(
[performance, performance_step], ignore_index=True
)
performance.to_csv(log_csv, index=False)
# Save weights
with h5py.File(STORAGE_FOLDER / weight_file, "w") as f:
f.create_dataset(dataset_key, data=final_weights)
dataset.household.household_weight = final_weights.sum(axis=0)
return dataset