Skip to content

Repository files navigation

Benefit Cliff Calculator

CliffWatch compares modeled household resources across earnings levels and states. Users can open a precomputed chart for a sampled household or enter their own household for an on-demand PolicyEngine calculation.

What This Repo Does

  • compares a selected household across all 50 states and DC
  • traces a single household through an earnings curve to surface cliffs
  • shows a first-pass modeled bundle of:
    • SNAP
    • TANF or state equivalent
    • WIC
    • free school meals
    • refundable tax credits
    • healthcare value

The v1 metric is:

net resources = market income + modeled support - taxes before refundable credits

Chart views

The cliff chart draws one accounting identity — net income = earnings + supports − taxes − household costs — from four angles, controlled by two toggles:

Total By program
Net income net income line with cliff markers and dead-zone shading stacked program areas summing to the net income line
Marginal tax rate step line of the rate between sampled earnings points stacked per-program contributions (−Δprogram / Δearnings) summing to the total rate

Because differencing is linear, the marginal tax rate decomposes exactly: each program's phase-out (or phase-in, shown below zero) stacks to the combined rate. The rate axis caps at 100% — cliffs push far past it, and the tooltip reports exact rates. The registry that drives all four views lives in frontend/src/utils/chartModel.js.

Architecture

Layer Technology
Frontend Next.js static export, React, Recharts
Runtime calculations Python, PolicyEngine US
Local API built-in http.server wrapper around shared compute helpers
Deploy target static frontend + serverless-style api/ handlers

CliffWatch calculates custom households on demand and caches repeated requests in memory by input signature.

Try a sample household opens a chart from the shared wizard’s 2024 US microdata catalog. The catalog supplies the state. CliffWatch uses an editable example ZIP from that state or keeps the user’s ZIP if it matches. An example ZIP is the most populous single-county residential ZIP in the state that the wizard recognizes, using the bundled 2020 Census lookup.

For a sample with an example ZIP, CliffWatch downloads the selected household’s earnings curve, including the more closely spaced observations around each cliff. Its share link opens the same chart. The browser checks the calculation inputs and model revision. It can reopen a finished chart from browser storage. For an older link without a sample ID, the browser looks for a match locally using the state’s chart file. Custom inputs, missing intervals and model revisions without matching results use the live calculator.

After changing the model, ZIP data, sample catalog or refinement intervals, install the frontend dependencies and run uv run python -m scripts.build_sample_charts from the repository root. Commit the regenerated state bundles with the change. The frontend extracts individual chart files from those bundles during development and production builds. CI checks that every catalog slot and every requested cliff interval has a saved result.

More detail is in COMPUTE_STRATEGY.md.

Repo Layout

  • frontend/: React app
  • cliff_watch/: installable Python package with the calculator, config, and a local dev API server
  • api/: serverless-style request handlers that share the same compute layer
  • examples/: sample payloads for the CLI
  • tests/: pytest suite
  • docs/: static build output from Vite

Getting Started

1. Python setup

Install the locked environment Vercel deploys (pulls PolicyEngine-US automatically):

uv sync --extra dev

pyproject.toml pins the engine (policyengine-us, policyengine-core, spm-calculator) exactly, with the reason for each pin, and uv.lock locks the rest. GET /api/metadata reports the deployed versions under engine. Bump a pin on purpose: engine releases change displayed numbers, so compare /api/series output before and after.

To calculate with a local policyengine-us checkout, set the environment variable below. CliffWatch then calculates sample charts with that checkout as well.

export POLICYENGINE_US_REPO=../policyengine-us

2. Frontend setup

cd frontend
bun install

3. Run locally

Start the local API:

cliff-watch-dev-server

In a second terminal:

cd frontend
bun run dev

The app will run at http://localhost:3000/, with /api/* proxied to the local Python server.

Tests

uv run pytest                  # calculator, payload invariants, JS-vs-Python builder differential
cd frontend && bun run test    # Vitest, including fast-check payload invariants

Both suites check the situation builders (cliff_watch/calculator.py and frontend/src/policyengineApi.js) against the PolicyEngine US variable graph. Some PolicyEngine US variables are sums of components (adds). An input on such a total replaces the sum, so every rule that reads a component sees zero: sending pension_income once left pension income out of AGI. The builders therefore send each income field to a component (HEAD_INCOME_INPUTS), and the invariant tests fail if either builder sets a total that isn't listed, with a reason, in REVIEWED_TOTAL_INPUTS. The frontend tests read a snapshot of the graph; regenerate it after changing either builder or the pinned policyengine-us:

uv run python scripts/generate_variable_graph.py

Opt-in live checks post built situations to api.policyengine.org. They send no credential, so they stop working once the Public API accepts only PolicyEngine's own servers:

POLICYENGINE_LIVE_API=1 uv run pytest tests/test_live_policyengine_api.py
cd frontend && bun run test:live

Modal Fallback API

If the public PolicyEngine API has not yet picked up a required policyengine-us fix, deploy the same CliffWatch compute layer to Modal and point the frontend at it.

First verify the Modal CLI is using the PolicyEngine workspace:

modal token new --profile policyengine  # only needed the first time
modal profile activate policyengine
modal profile list

Then deploy the backend:

modal deploy modal_app.py --env main

The production URL should look like:

https://policyengine--cliff-watch-fastapi-app.modal.run

Set the frontend environment variable to that origin and redeploy the frontend:

vercel env add NEXT_PUBLIC_CLIFF_WATCH_API_ORIGIN production
vercel --prod --force --yes --scope policy-engine

With that variable set, browser requests go to the Modal backend's /api/metadata, /api/calculate, /api/series, and /api/households routes.

Cold-start install

Vercel's Python runtime ships a function's dependencies in its zip while the function totals 225 MiB or less. Past that, it installs the public packages into /tmp when an instance starts, before any of this app's code runs, and those packages leave the zip. pyproject.toml removes packages a household calculation never imports, so this function totals about 189 MiB and no install runs. The builder then fills the zip with bytecode up to 220 MiB. That fill shrinks as the function grows, and a preview with none of it took 38 to 42 seconds on first requests, a few seconds longer than previews with the fill and inside the routes' 60-second limit.

If a version bump takes the function past 225 MiB, the install comes back, and two platform limits shape it:

  • Lambda gives an instance's init 10 seconds. An init that runs longer stops and starts again on the same instance, and /tmp keeps what it wrote.
  • /tmp holds about 513 MiB, and each file takes whole 4 KiB blocks, so many small files take far more room than their sizes add up to.

An install that stops partway leaves its temporary files in /tmp. If a complete install no longer fits beside them, every request on that instance fails with "No space left on device" until the platform replaces it. tests/test_deploy_footprint.py fails when the function passes 225 MiB, and when a complete install stops fitting on top of a stopped one. If the first fails after a version bump, trim another package, or accept the install and update this section. If the second fails, trim another package or give one a URL source. main requires no checks, so a failing run warns the author of a bump and doesn't block its merge.

County from ZIP

The form asks for a ZIP and no county. The API resolves the county, so the form, share links, direct API calls and the Modal fallback all follow one rule:

  1. An explicit county, such as a share link's county=MIDDLESEX_COUNTY_MA, always wins.
  2. Otherwise the household gets the ZIP's first county in its state. A ZIP that spans counties lists the county with the most 2020 Census residents first, and a tie goes to the lower county code.
  3. A ZIP code tabulation area (ZCTA) with no residents ranks its counties by land area.
  4. A ZIP with no ZCTA of its own, such as a post-office-box ZIP, uses GeoNames' county, or else the first county in its state of the ZCTA the crosswalk gives it.
  5. A ZIP with no county in the household's state resolves to nothing. policyengine-us then uses the state's first county alphabetically, and the results page says so.

A series response carries a location block saying which county the calculation used and why (source is explicit, zip or none). When a ZIP spans counties, the results page shows each county's share of the ZIP's residents and lets the user pick another. The choice goes in the share link, and editing the ZIP clears it. The API also sends the ZIP to policyengine-us, because Los Angeles County's rating area reads it.

The small-area rent variables read the ZIP too, but no household takes up housing assistance. CliffWatch has no input for whether a household holds a voucher (#30), and its rent field also takes mortgage payments, so wherever the ZIP carries a payment standard the engine would otherwise pay a voucher's subsidy to renters and homeowners alike.

policyengine-us finds a county's ACA rating area by joining the county's code to its aca_rating_areas.csv, and it gives rating area 1 to any county the join misses. That file spells 31 counties differently from their codes: BAYLOR_TX for BAYLOR_COUNTY_TX, DONA_ANA_NM for DOÑA_ANA_COUNTY_NM, and the former Valdez-Cordova and Wade Hampton rows for the census areas that replaced them. RATING_AREA_OVERRIDES in cliff_watch/zip_county.py sends those counties' rating areas with the county. Oglala Lakota County, South Dakota, has no row under any name and keeps rating area 1. tests/test_zip_county_engine.py fails when an engine update changes those rows or fixes the names, so the list goes when the gap does.

policyengine-us has no benchmark premium for three counties: Calhoun and Edgefield in South Carolina, and Gulf in Florida. A household there reads no premium tax credit, so location.has_benchmark_premium turns false and the results page says so. tests/test_zip_county_engine.py compares that list with the engine and fails when an engine update adds those rating areas, so the note goes when the gap does.

scripts/build_zip_county.py builds cliff_watch/data/zip_county.json.gz. It reads the Census 2020 ZCTA-county relationship file, 2020 DHC table P1 at summary level 881, GeoNames US postal codes (CC BY 4.0; the table keeps each ZIP's county and drops the rest) and the UDS Mapper ZIP-to-ZCTA crosswalk as policyengine-us-data stored it. The script's docstring lists the sources, and the table's meta block records each downloaded file's hash.

Rebuild the table when policyengine-us renames a county; tests/test_zip_county.py fails if a code stops being an engine county.

CENSUS_API_KEY=... uv run python scripts/build_zip_county.py --cache-dir /tmp/zip-county

Quick CLI Smoke Test

You can also run the calculator without the frontend:

python3 -m cliff_watch.calculator examples/sample_household.json --mode household
python3 -m cliff_watch.calculator examples/sample_household.json --mode states
python3 -m cliff_watch.calculator examples/sample_household.json --mode series

Current Scope Notes

  • Household inputs are person-based in v1, with each family member entered individually by type and age.
  • Earnings are assigned to the first adult and held constant over the year.
  • A second adult is treated as a spouse, and later adults are modeled as additional household members in the same household unit.
  • The form has no county field. The API resolves the county from the ZIP, as County from ZIP describes.
  • If a household gets SNAP in any month, policyengine-us treats it as categorically eligible for Head Start, Early Head Start and free school meals for the whole year. CliffWatch charts January's SNAP for all twelve months, and USDA updates SNAP's income limits and benefits each October. So just past SNAP's January limits, a household can get SNAP from October and a full year of these programs, with no SNAP on the chart. CliffWatch names those wages and programs in a note under the cliff chart. In PolicyEngine/policyengine-us#10766, we propose counting these programs from a household's first month of SNAP.
  • The modeled bundle is intentionally narrower than every possible administrative program.

License

This project is licensed under the MIT License. See LICENSE.

Releases

Packages

Contributors

Languages