|
| 1 | +# API hierarchy design |
| 2 | + |
| 3 | +## Levels of analysis |
| 4 | + |
| 5 | +``` |
| 6 | +┌─────────────────────────────────────────────────────────────┐ |
| 7 | +│ Level 2: Reports │ |
| 8 | +│ AI-generated documents, orchestrating multiple jobs │ |
| 9 | +│ /reports/* │ |
| 10 | +├─────────────────────────────────────────────────────────────┤ |
| 11 | +│ Level 1: Analyses │ |
| 12 | +│ Operations on simulations - thin wrappers around │ |
| 13 | +│ policyengine package functions │ |
| 14 | +│ │ |
| 15 | +│ Common (baked-in, trivial to call): │ |
| 16 | +│ /analysis/decile-impact/* │ |
| 17 | +│ /analysis/budget-impact/* │ |
| 18 | +│ /analysis/winners-losers/* │ |
| 19 | +│ │ |
| 20 | +│ Flexible (configurable): │ |
| 21 | +│ /analysis/compare/* │ |
| 22 | +├─────────────────────────────────────────────────────────────┤ |
| 23 | +│ Level 0: Simulations │ |
| 24 | +│ Single world-state calculations │ |
| 25 | +│ /simulate/household │ |
| 26 | +│ /simulate/economy │ |
| 27 | +└─────────────────────────────────────────────────────────────┘ |
| 28 | +``` |
| 29 | + |
| 30 | +All operations are **async** (Modal compute). The API is a thin orchestration layer - all analysis logic lives in the `policyengine` package. |
| 31 | + |
| 32 | +## Mapping to policyengine package |
| 33 | + |
| 34 | +| API endpoint | policyengine function | |
| 35 | +|--------------|----------------------| |
| 36 | +| `/simulate/household` | `calculate_household_impact()` | |
| 37 | +| `/simulate/economy` | `Simulation.run()` | |
| 38 | +| `/analysis/decile-impact/*` | `calculate_decile_impacts()` | |
| 39 | +| `/analysis/budget-impact/*` | `ProgrammeStatistics` | |
| 40 | +| `/analysis/winners-losers/*` | `ChangeAggregate` with filters | |
| 41 | +| `/analysis/compare/*` | `economic_impact_analysis()` or custom | |
| 42 | + |
| 43 | +## Level 0: Simulations |
| 44 | + |
| 45 | +### `/simulate/household` |
| 46 | + |
| 47 | +Single household calculation. Wraps `policyengine.tax_benefit_models.uk.analysis.calculate_household_impact()`. |
| 48 | + |
| 49 | +``` |
| 50 | +POST /simulate/household |
| 51 | +{ |
| 52 | + "model": "policyengine_uk", |
| 53 | + "household": { |
| 54 | + "people": [{"age": 30, "employment_income": 50000}], |
| 55 | + "benunit": {}, |
| 56 | + "household": {} |
| 57 | + }, |
| 58 | + "year": 2026, |
| 59 | + "policy_id": null |
| 60 | +} |
| 61 | +
|
| 62 | +→ Returns job_id, poll for results |
| 63 | +``` |
| 64 | + |
| 65 | +### `/simulate/economy` |
| 66 | + |
| 67 | +Population simulation. Creates and runs a `policyengine.core.Simulation`. |
| 68 | + |
| 69 | +``` |
| 70 | +POST /simulate/economy |
| 71 | +{ |
| 72 | + "model": "policyengine_uk", |
| 73 | + "dataset_id": "...", |
| 74 | + "policy_id": null, |
| 75 | + "dynamic_id": null |
| 76 | +} |
| 77 | +
|
| 78 | +→ Returns simulation_id, poll for results |
| 79 | +``` |
| 80 | + |
| 81 | +Economy simulations are **deterministic and cached** by (dataset_id, model_version, policy_id, dynamic_id). |
| 82 | + |
| 83 | +## Level 1: Analyses - Common (baked-in) |
| 84 | + |
| 85 | +These are the bread-and-butter analyses. Trivial to call, no configuration needed. |
| 86 | + |
| 87 | +### `/analysis/decile-impact/economy` |
| 88 | + |
| 89 | +Income decile breakdown. Wraps `calculate_decile_impacts()`. |
| 90 | + |
| 91 | +``` |
| 92 | +POST /analysis/decile-impact/economy |
| 93 | +{ |
| 94 | + "model": "policyengine_uk", |
| 95 | + "dataset_id": "...", |
| 96 | + "baseline_policy_id": null, |
| 97 | + "reform_policy_id": "..." |
| 98 | +} |
| 99 | +
|
| 100 | +→ Returns job_id |
| 101 | +
|
| 102 | +GET /analysis/decile-impact/economy/{job_id} |
| 103 | +→ Returns: |
| 104 | +{ |
| 105 | + "status": "completed", |
| 106 | + "deciles": [ |
| 107 | + {"decile": 1, "baseline_mean": 15000, "reform_mean": 15500, "change": 500, "pct_change": 3.3, ...}, |
| 108 | + {"decile": 2, ...}, |
| 109 | + ... |
| 110 | + {"decile": 10, ...} |
| 111 | + ] |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### `/analysis/budget-impact/economy` |
| 116 | + |
| 117 | +Tax and benefit programme totals. Wraps `ProgrammeStatistics`. |
| 118 | + |
| 119 | +``` |
| 120 | +POST /analysis/budget-impact/economy |
| 121 | +{ |
| 122 | + "model": "policyengine_uk", |
| 123 | + "dataset_id": "...", |
| 124 | + "baseline_policy_id": null, |
| 125 | + "reform_policy_id": "..." |
| 126 | +} |
| 127 | +
|
| 128 | +→ Returns job_id |
| 129 | +
|
| 130 | +GET /analysis/budget-impact/economy/{job_id} |
| 131 | +→ Returns: |
| 132 | +{ |
| 133 | + "status": "completed", |
| 134 | + "net_budget_impact": -20000000000, |
| 135 | + "programmes": [ |
| 136 | + {"name": "income_tax", "is_tax": true, "baseline_total": 200e9, "reform_total": 180e9, "change": -20e9}, |
| 137 | + {"name": "universal_credit", "is_tax": false, "baseline_total": 50e9, "reform_total": 52e9, "change": 2e9}, |
| 138 | + ... |
| 139 | + ] |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### `/analysis/winners-losers/economy` |
| 144 | + |
| 145 | +Who gains and loses. Wraps `ChangeAggregate` with change filters. |
| 146 | + |
| 147 | +``` |
| 148 | +POST /analysis/winners-losers/economy |
| 149 | +{ |
| 150 | + "model": "policyengine_uk", |
| 151 | + "dataset_id": "...", |
| 152 | + "baseline_policy_id": null, |
| 153 | + "reform_policy_id": "...", |
| 154 | + "threshold": 0 // Change threshold (default: any change) |
| 155 | +} |
| 156 | +
|
| 157 | +→ Returns job_id |
| 158 | +
|
| 159 | +GET /analysis/winners-losers/economy/{job_id} |
| 160 | +→ Returns: |
| 161 | +{ |
| 162 | + "status": "completed", |
| 163 | + "winners": {"count": 15000000, "mean_gain": 500}, |
| 164 | + "losers": {"count": 5000000, "mean_loss": -200}, |
| 165 | + "unchanged": {"count": 30000000} |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +### `/analysis/decile-impact/household` |
| 170 | + |
| 171 | +Compare household across scenarios by artificial decile assignment. |
| 172 | + |
| 173 | +``` |
| 174 | +POST /analysis/decile-impact/household |
| 175 | +{ |
| 176 | + "model": "policyengine_uk", |
| 177 | + "household": {"people": [{"employment_income": 50000}]}, |
| 178 | + "year": 2026, |
| 179 | + "baseline_policy_id": null, |
| 180 | + "reform_policy_id": "..." |
| 181 | +} |
| 182 | +
|
| 183 | +→ Returns which decile this household falls into and their change |
| 184 | +``` |
| 185 | + |
| 186 | +## Level 1: Analyses - Flexible |
| 187 | + |
| 188 | +### `/analysis/compare/economy` |
| 189 | + |
| 190 | +Full comparison with all outputs. Wraps `economic_impact_analysis()`. |
| 191 | + |
| 192 | +``` |
| 193 | +POST /analysis/compare/economy |
| 194 | +{ |
| 195 | + "model": "policyengine_uk", |
| 196 | + "dataset_id": "...", |
| 197 | + "scenarios": [ |
| 198 | + {"label": "baseline"}, |
| 199 | + {"label": "reform", "policy_id": "..."}, |
| 200 | + {"label": "reform_dynamic", "policy_id": "...", "dynamic_id": "..."} |
| 201 | + ] |
| 202 | +} |
| 203 | +
|
| 204 | +→ Returns job_id |
| 205 | +
|
| 206 | +GET /analysis/compare/economy/{job_id} |
| 207 | +→ Returns: |
| 208 | +{ |
| 209 | + "status": "completed", |
| 210 | + "scenarios": {...simulation results...}, |
| 211 | + "comparisons": { |
| 212 | + "reform": { |
| 213 | + "relative_to": "baseline", |
| 214 | + "decile_impacts": [...], |
| 215 | + "budget_impact": {...}, |
| 216 | + "winners_losers": {...} |
| 217 | + }, |
| 218 | + "reform_dynamic": {...} |
| 219 | + } |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +### `/analysis/compare/household` |
| 224 | + |
| 225 | +Compare multiple scenarios for a household. |
| 226 | + |
| 227 | +``` |
| 228 | +POST /analysis/compare/household |
| 229 | +{ |
| 230 | + "model": "policyengine_uk", |
| 231 | + "household": {...}, |
| 232 | + "year": 2026, |
| 233 | + "scenarios": [ |
| 234 | + {"label": "baseline"}, |
| 235 | + {"label": "reform", "policy_id": "..."} |
| 236 | + ] |
| 237 | +} |
| 238 | +
|
| 239 | +→ Returns all scenario results + computed differences |
| 240 | +``` |
| 241 | + |
| 242 | +### `/analysis/aggregate/economy` (power user) |
| 243 | + |
| 244 | +Custom aggregation with full filter control. Directly exposes `Aggregate` / `ChangeAggregate`. |
| 245 | + |
| 246 | +``` |
| 247 | +POST /analysis/aggregate/economy |
| 248 | +{ |
| 249 | + "model": "policyengine_uk", |
| 250 | + "dataset_id": "...", |
| 251 | + "simulation_id": "...", // or policy_id to create |
| 252 | + "variable": "household_net_income", |
| 253 | + "aggregate_type": "mean", |
| 254 | + "entity": "household", |
| 255 | + "filters": { |
| 256 | + "quantile": {"variable": "household_net_income", "n": 10, "eq": 1} |
| 257 | + } |
| 258 | +} |
| 259 | +
|
| 260 | +→ Returns single aggregate value |
| 261 | +``` |
| 262 | + |
| 263 | +## Adding new analysis types |
| 264 | + |
| 265 | +To add a new common analysis (e.g. marginal tax rates): |
| 266 | + |
| 267 | +1. **policyengine package**: Add `MarginalTaxRate` output class and `calculate_marginal_rates()` function |
| 268 | +2. **API**: Add `/analysis/marginal-rates/*` endpoint that wraps the function |
| 269 | +3. **Modal**: Add function to run it |
| 270 | + |
| 271 | +The API endpoint is ~20 lines - just parameter parsing and calling the policyengine function. |
| 272 | + |
| 273 | +## URL structure summary |
| 274 | + |
| 275 | +``` |
| 276 | +# Level 0: Simulations |
| 277 | +POST /simulate/household |
| 278 | +GET /simulate/household/{job_id} |
| 279 | +POST /simulate/economy |
| 280 | +GET /simulate/economy/{simulation_id} |
| 281 | +
|
| 282 | +# Level 1: Common analyses (baked-in, trivial) |
| 283 | +POST /analysis/decile-impact/economy |
| 284 | +GET /analysis/decile-impact/economy/{job_id} |
| 285 | +POST /analysis/budget-impact/economy |
| 286 | +GET /analysis/budget-impact/economy/{job_id} |
| 287 | +POST /analysis/winners-losers/economy |
| 288 | +GET /analysis/winners-losers/economy/{job_id} |
| 289 | +
|
| 290 | +# Level 1: Flexible analyses |
| 291 | +POST /analysis/compare/economy |
| 292 | +GET /analysis/compare/economy/{job_id} |
| 293 | +POST /analysis/compare/household |
| 294 | +GET /analysis/compare/household/{job_id} |
| 295 | +POST /analysis/aggregate/economy |
| 296 | +GET /analysis/aggregate/economy/{job_id} |
| 297 | +
|
| 298 | +# Level 2: Reports (future) |
| 299 | +POST /reports/policy-impact |
| 300 | +GET /reports/policy-impact/{report_id} |
| 301 | +``` |
| 302 | + |
| 303 | +## Use cases |
| 304 | + |
| 305 | +| Use case | Endpoint | |
| 306 | +|----------|----------| |
| 307 | +| My tax under current law | `/simulate/household` | |
| 308 | +| Reform impact on my household | `/analysis/compare/household` with 2 scenarios | |
| 309 | +| Revenue impact of reform | `/analysis/budget-impact/economy` | |
| 310 | +| Decile breakdown of reform | `/analysis/decile-impact/economy` | |
| 311 | +| Who wins and loses | `/analysis/winners-losers/economy` | |
| 312 | +| Full reform analysis | `/analysis/compare/economy` | |
| 313 | +| Compare 3 reform proposals | `/analysis/compare/economy` with 4 scenarios | |
| 314 | +| Static vs dynamic comparison | `/analysis/compare/economy` with 3 scenarios | |
| 315 | +| Custom aggregation | `/analysis/aggregate/economy` | |
| 316 | + |
| 317 | +## Migration |
| 318 | + |
| 319 | +Deprecate existing endpoints: |
| 320 | +- `/household/calculate` → `/simulate/household` |
| 321 | +- `/household/impact` → `/analysis/compare/household` |
| 322 | +- `/analysis/economic-impact` → `/analysis/compare/economy` |
| 323 | + |
| 324 | +## Implementation notes |
| 325 | + |
| 326 | +1. All Modal functions import from `policyengine` package |
| 327 | +2. API endpoints do minimal work: parse request, call Modal, store results |
| 328 | +3. New analysis types require: |
| 329 | + - Add to policyengine package (logic) |
| 330 | + - Add API endpoint (orchestration) |
| 331 | + - Add Modal function (compute) |
0 commit comments