Skip to content

Commit b05be6d

Browse files
authored
Merge pull request #1571 from PolicyEngine/codex/fix-1162
Document entity-table datasets
2 parents 8724fa2 + 360b888 commit b05be6d

1 file changed

Lines changed: 70 additions & 1 deletion

File tree

docs/book/usage/simulations.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,75 @@ print(f"Total income tax from all people: £{total_tax:.2f}")
209209
The column naming follows the pattern `variable_name__year` for time-varying variables. Note the double underscore `__` between the variable name and year.
210210
```
211211

212+
### From entity tables with `UKSingleYearDataset` and `UKMultiYearDataset`
213+
214+
If your data is already split into person-, benefit-unit-, and household-level
215+
tables, use the UK dataset classes directly:
216+
217+
```python
218+
import pandas as pd
219+
220+
from policyengine_uk import Simulation
221+
from policyengine_uk.data import UKMultiYearDataset, UKSingleYearDataset
222+
223+
person_2025 = pd.DataFrame({
224+
"person_id": [1, 2],
225+
"person_benunit_id": [1, 1],
226+
"person_household_id": [1, 1],
227+
"age": [35, 7],
228+
"employment_income": [32_000, 0],
229+
})
230+
231+
benunit_2025 = pd.DataFrame({
232+
"benunit_id": [1],
233+
})
234+
235+
household_2025 = pd.DataFrame({
236+
"household_id": [1],
237+
"region": ["LONDON"],
238+
"council_tax": [1_800],
239+
})
240+
241+
dataset_2025 = UKSingleYearDataset(
242+
person=person_2025,
243+
benunit=benunit_2025,
244+
household=household_2025,
245+
fiscal_year=2025,
246+
)
247+
248+
sim = Simulation(dataset=dataset_2025)
249+
print(sim.calculate("income_tax", 2025))
250+
```
251+
252+
Each table should contain the ID columns needed to link entities together:
253+
254+
- `person`: `person_id`, `person_benunit_id`, `person_household_id`, plus any person-level variables
255+
- `benunit`: `benunit_id`, plus any benefit-unit variables
256+
- `household`: `household_id`, plus any household-level variables
257+
258+
Use `UKSingleYearDataset` when you have one cross-section and want PolicyEngine
259+
UK to extend it forward using its standard uprating assumptions.
260+
261+
Use `UKMultiYearDataset` when you already have explicit tables for multiple
262+
years and want those year-by-year values loaded as-is:
263+
264+
```python
265+
dataset = UKMultiYearDataset(
266+
datasets=[
267+
dataset_2025,
268+
UKSingleYearDataset(
269+
person=person_2026,
270+
benunit=benunit_2026,
271+
household=household_2026,
272+
fiscal_year=2026,
273+
),
274+
]
275+
)
276+
277+
sim = Simulation(dataset=dataset)
278+
print(sim.calculate("household_net_income", 2026))
279+
```
280+
212281
### From survey datasets
213282

214283
For population-level analysis, use survey data:
@@ -453,4 +522,4 @@ This makes it easy to trace through exactly how a reform affects different compo
453522
- Cache simulation results when running the same calculation multiple times
454523
```
455524

456-
The simulation system is designed to be flexible and powerful. Start with simple examples and gradually build up to more complex analyses as you become familiar with the structure and capabilities.
525+
The simulation system is designed to be flexible and powerful. Start with simple examples and gradually build up to more complex analyses as you become familiar with the structure and capabilities.

0 commit comments

Comments
 (0)