# What Brazil's runoff counts can and cannot tell you

Brazil's 2026 presidential election went to a runoff between Lula and Flávio Bolsonaro. The ballot is secret, so the
only evidence of how voters moved between the rounds is the official totals per polling section. The study asks what
those totals prove on their own, then estimates the rest with a model and tests it twice on planted elections where
the answer is known: a self-test that also chose the model's settings, and an independent test written by a separate
author who saw neither the model's code nor its output files, never used to choose or tune anything and scored once on
the final model. The page is `index.html`; its Method section states the rules, which were declared before the runs they
govern. `pt.html` is the same page in Portuguese, with the same numbers. `DESIGN-baseline-change.md` declares what was
decided after the results: the checks made with no model run and the two readings that depart from the result files.
How figures are rounded, also decided after the results, is stated in the page's Method section.

## What is here

- `data/`: the official results from Brazil's Superior Electoral Court (TSE): per-municipality totals and TSE's
  national totals (`data/tse/`). The polling-section table is built from TSE's ballot-box reports, one per polling
  section, each file checked against TSE's published SHA-512; each round is gated exactly against the municipality
  table and the national totals, and sections are matched between the rounds, pooled by polling place, then by
  municipality, where they cannot be matched. `results/units_section.json` records the tiers and the hashes of the
  table's inputs and output.
- `tools/transfer_model.py`: the model (constrained weighted least squares per group, then each unit raked to its own
  totals). `tests/test_groupings.py` checks that a small state is fitted with its region, never on its own.
- `synthetic/`: the self-test's planted elections (generator, its README, planted truth, manifests;
  `synthetic/section/` holds the polling-section draws). The page's Method section describes the seven forms. `synthetic/independent/`: the independent author's
  generator, scenarios and planted truth, kept as written. `synthetic/independent/validate.py` writes the author's
  validation log, `synthetic/independent/manifest.txt`; the log is not in this download, and no step below reads it.
- `tools/make_section_suite.py`: draws a suite on the section units with its own generator and writes the manifest.
- `tools/score_suite.py`: scores the self-test and applies the self-test's rules (`results/suite_section.json`).
  `tools/score_independent.py`: scores the frozen model once on the independent set and applies the independent
  test's rules (`results/independent_section.json`); it refuses to run unless the model and the self-test results are
  committed and unchanged. `tools/real_band.py`: each flow's low and high value across the model's 45 settings on the
  real counts (`results/real_band_section.json`), checked against the self-test results before it writes.
- `results/suite_municipality.json`: the self-test scored on municipalities, which the page cites in a few places.
- `site/render.py`: builds the page from the result files and checks every claim the text makes against them.
- `site/render_pt.py`: builds `pt.html` from the computing part of `site/render.py`, so the two pages share one set of
  numbers. `tests/test_pt_page.py` compares them number by number and pins every published file by hash
  (`tests/frozen-files.json`).
- `tools/planted_baseline.py`: checks added after the results, read from the stored files with no model run: the flow
  counts by each rule, what a guess of zero would score, the range each flow was planted over, and the planted direction
  (`results/planted_baseline.json`, `tests/test_planted_baseline.py`). They change no verdict stored in the result files.
- `tools/no_model_baseline.py`: more checks added after the results, also with no model run: what one fixed share from
  the national totals would score, how each flow that passed compares with it, and the model's error on the gap between
  Lula and Flávio by form (`results/no_model_baseline.json`, `tests/test_no_model_baseline.py`). They change no verdict
  stored in the result files.

## Reproduce

Python 3.12 with numpy and requests. The polling-section table (about 50 MB) is not included; the steps below rebuild
it, and `results/units_section.json` gives its sha256. The first five lines download TSE's files and build the table;
they need the full project, because the fetch and table-building tools are not in this download. The next six read
that table. The last seven run on the files in this download alone.

```
python3 -I tools/fetch_results.py
python3 -I tools/fetch_ballot_boxes.py 1 && python3 -I tools/fetch_ballot_boxes.py 2
python3 -I -B tools/build_section_table.py 1 data/tse/br-c0001-e006257-u.json data/president_by_municipality.csv
python3 -I -B tools/build_section_table.py 2 data/tse/br-c0001-e006258-u.json data/president_by_municipality.csv
python3 -I -B tools/join_rounds.py <r1_sections.csv> <r2_sections.csv> data/president_by_municipality.csv
python3 -I -B tools/make_round1_input.py <president_by_section.csv> <round1_by_section.csv>
python3 -I -B tools/make_section_suite.py synthetic/generate.py <round1_by_section.csv> <out> synthetic/section 8 S1,S2,S3,S4,S5,S6,S7
python3 -I -B tools/make_section_suite.py synthetic/independent/generate.py <round1_by_section.csv> <out-ind> synthetic/independent/section 8 I1,I2,I3,I4,I5,I6
python3 -I -B tools/score_suite.py section <president_by_section.csv> <out> synthetic/section/truth results/suite_section.json --jobs 8
python3 -I -B tools/score_independent.py <president_by_section.csv> <out-ind> synthetic/independent/section/truth results/independent_section.json --jobs 8
python3 -I -B tools/real_band.py <president_by_section.csv> results/real_band_section.json --jobs 5
python3 -I -B tools/planted_baseline.py
python3 -I -B tests/test_planted_baseline.py
python3 -I -B tools/no_model_baseline.py
python3 -I -B tests/test_no_model_baseline.py
python3 -I -B site/render.py section
python3 -I -B site/render_pt.py
python3 -I -B tests/test_pt_page.py
```

In the full project, the two table builders print the path of the table they write, which `<r1_sections.csv>`,
`<r2_sections.csv>` and `<president_by_section.csv>` stand for; the other names in angle brackets are files or folders
you choose. Each step prints a last line
starting `VERDICT:`. The self-test fits 45 model settings on each of 22 datasets of about 490,000 units.

## Data and licence

Election results: Tribunal Superior Eleitoral (TSE), open data published under Creative Commons Attribution. The
planted elections are synthetic. The code may be reused. The fonts and design system under `vendor/` carry their own
licences.

Some files are packed in brazil-runoff-vote-transfer-data-1.zip; unzip it here to restore the full export, which the manifest lists file by file.
