From polls to seats: how Àgora projects the Congress

The 350 seats of the Spanish Congress are not decided in one national count but in 52 separate ones, one per province plus Ceuta and Melilla, each with its own number of seats and its own cut-off. A poll gives one national percentage per party. Àgora, our site for following the general election of 29 November 2026, has a page that goes from the second to the first: it takes an average of the polls and returns seats per province, a range of seats per party and the probability that a group of parties reaches 176.

This post explains how that page works, reading the code. It describes the method, not the result, which changes with every poll. The live page is the model (in Spanish, Catalan, Basque and Galician) and its methodology page has the calibration tables. Figures are dated 6 November 2026 and come from the repository or its scripts, named next to each.

Step 1: an average, not a single poll

The input is pollAverage in src/features/polls/average.ts, the function that also draws the polls page. On 6 November it used 45 polls from 18 pollsters, with fieldwork ending between 7 September and 6 November (node scripts/model/run.mjs 2026-11-06). The rules are fixed in the code:

RuleValue
Window60 days, widened by 1.5 times (up to 240) while there are fewer than 5 polls
AgeThe weight halves every 14 days after the end of fieldwork
SampleWeight grows with the square root of n / 1000, with n kept between 200 and 3,000 (500 if missing)
One pollsterIts combined weight is capped at 20%
ExcludedPolls from parties, and third-party recalculations of CIS data; the official CIS counts
A party appearsOnly with at least 2 polls

Simplified from src/features/polls/average.ts:

const w = rows.map((p) =>
  Math.pow(0.5, Math.abs(D - p.t) / halfLifeDays) * sampleFactor(p.n));
capPollsters(rows.map((p) => p.pollster), w, maxPollsterShare);
const mean = rows.reduce((a, p, i) => a + w[i]! * get(p)!, 0) / sw;

data/polls/polls.json holds 593 polls from 38 pollsters (fieldwork ending 25 July 2023 to 1 November 2026), converted from the English Wikipedia table (revision of 2 November 2026, CC BY-SA 4.0). Rows from three sources, including the CIS, are cross-checked against their own publications in data/polls/primary/.

Step 2: from a national swing to 52 provincial results

The base is the last election, 23 July 2023 (the 23J), by province. Each party spreads its national poll share over the provinces in proportion to what it got there in the 23J. In project.ts that is one ratio, target share over base share:

const r = T[p]! / B;
for (let i = 0; i < I; i++) row[i] = (bp.votes[i]! / valid[i]!) * r;

A party going from 10% to 15% nationally multiplies its 23J share by 1.5 in every province where it had votes. This proportional swing cannot produce a negative share. A uniform swing (the same points everywhere, floored at zero) is also in the code and selectable on the page.

Provincial shares must still add up to 100% and national totals must match the polls, so the code alternates two corrections 25 times (IPF_ITERS): scale each party to its national target, then scale each province to 100%. No provincial share may pass 85% (MAX_CELL). Valid votes per province are those of the 23J: turnout is assumed unchanged.

Some parties have no 23J footprint to inherit, and SPEC_2026 in base.ts handles them explicitly. Podemos, SALF, Adelante Andalucía and Front Valencià get a borrowed distribution (Podemos, for example, takes half the 2019 Unidas Podemos map and half Sumar’s 2023 map; the last two exist only in their regions). PACMA, NC and Teruel Existe have no national poll, so they keep their 23J votes. The CUP and Aliança Catalana are not among the candidacies proclaimed for 29 November (BOE of 3 November), so their 23J votes go to an “others” bucket that receives no seats.

Step 3: D’Hondt with a 3% cut-off, province by province

The seats per province are those of Real Decreto 806/2026: 350 in total, Madrid going from 37 to 38 and Cádiz from 9 to 8 (data/circumscriptions.json). Eleven of the 52 districts have three seats or fewer; Madrid has 38.

In each district only candidacies with at least 3% of the valid votes, blank votes included, take part in the count (LOREG, article 163). The seats then go one at a time to the highest quotient votes / (seats won + 1). Simplified from src/features/model/project.ts:

for (let s = 0; s < seats; s++) {
  let best = -1, bq = -1;
  for (let k = 0; k < n; k++) {
    const v = shares[k]!;
    if (v <= 0 || v < minShare) continue; // under the 3% cut-off
    const q = v / (out[k]! + 1);
    if (best < 0 || q > bq) { best = k; bq = q; }
  }
  out[best]! += 1;
}

A small invented example: five seats and shares of 40%, 30%, 20%, 7% and 2% for candidacies A to E. E is out before the count. The five largest quotients are 40 and 20 (A), 30 and 15 (B) and 20 (C), so A and B win two seats, C one and D none, despite its 7%. Ties go to the candidacy with more votes, then to the smaller id, so every run is reproducible. A test in model.test.ts checks this fast version against the readable one in src/lib/dhondt.ts.

Step 4: 10,000 simulations

The steps above give one answer. To ask how wrong it could be, the model repeats them with random errors: DEFAULT_SIMS = 10000 in simulate.ts. Each run perturbs three things, in this order: a national error per party, a regional error per community and party, and a provincial noise per province and party, larger in small provinces. The parameters are in src/features/model/calibration.ts:

export const ERROR_MODEL: ErrorModel = {
  sysK: 0.4, avgN: 4000, driftK: 0.45,
  axisFrac: 0.35, blocFrac: 0.2,
  regionSd: 0.09, provSd: 0.065, provSizeExp: 0.25,
};

The national error of a party has a standard deviation, in points, of sqrt(sysK² × share + driftK² × share × days/30 + sampling²): the systematic error all pollsters share, the drift until election day, and the sampling error of the average. At 23 days out, it works out at 3.2 points for a party at 30%, 1.3 for one at 5% and 0.6 for one at 1% (derived from the parameters, not measured).

The errors are correlated between parties, and that is what keeps the ranges honest. Each party’s error mixes a left-right axis, a component shared by its group in parties.json (izquierda, derecha, nacionalista, otros) and its own noise. With axisFrac at 0.35 and blocFrac at 0.2, two parties in the same left or right group have an error correlation of 0.55, and one on each side, -0.35 (derived). Afterwards the shares are rescaled to the original total. With independent errors the seat totals would look far too stable.

Regional and provincial noise are multiplicative. The provincial one is 0.065 for a province of 500,000 valid votes, scaled by (valid / 500000)^-0.25: 0.040 for Madrid and 0.134 for Melilla. A seeded generator (mulberry32 in rng.ts, DEFAULT_SEED = 20261129) makes the same input give the same output in Node and in the browser.

Ranges and the probability of 176

In every run the model records each party’s total seats. A party’s range is read from the 10,000 results: the median, the central 50% (p25 to p75) and the central 90% (p5 to p95).

The probability of a majority is a count. DEFAULT_MAJORITIES lists eight groups of parties; for each one the model adds up its members’ seats in every run and counts the runs with 176 or more. The eight groups are our list, not a forecast of any pact: the model knows nothing about negotiations.

A probability from simulations has its own error. Near 50%, the standard error sqrt(p(1-p)/N) is about 0.5 points with 10,000 runs and 1.1 with 2,000 (arithmetic, not a measurement). The fixed seed hides it, but it is there.

Where it runs: build, Web Worker, main thread

Àgora is a static site, so the default projection is computed at build: getScenario() in scenario.ts builds the average, runs project() once and simulate() with 10,000 runs.

The “what if” mode, with a slider per party, runs in the browser. On each change paintQuick() redraws the map and the province table at once with the simple projection, which is cheap. After 280 ms without movement the page sends the work to a Web Worker (worker.ts) with 2,000 runs (whatIfSims in ModelFigures.astro). If the Worker cannot start, compute() in mount.ts simulates on the main thread with fewer runs:

sim = simulate(base, values, {
  sims: Math.min(1000, cfg.whatIfSims), method, horizonDays });

Going back to the default source reuses the build’s 10,000 runs. In Node on this laptop, 10,000 runs took 1.7 seconds, 2,000 took 0.35 and 1,000 took 0.24. We have not timed a browser.

Calibration and backtest

The error parameters come from two scripts. scripts/model/calibrate.mjs computes the poll average at several distances before the November 2019 and July 2023 elections, using the 155 and 161 pre-election polls in scripts/model/data/prelection-polls.json, and compares it with the result for every party above 1% of the vote:

Days beforeCasesMean error (points)Root mean square (points)
7190.951.45
21191.201.99
42181.642.69

(scripts/model/data/calibration.json also has 14, 28 and 35 days.) A week out, the average had Vox 3.2 points below its 2019 result and the PSOE 3.6 points below its 2023 one. The drift parameter, 0.45, sits between two estimates in units of the square root of a party’s share: the volatility of the average between 2023 and 2026 (about 0.14 over 30 days) and the average’s error before the two elections (0.83 at 30 days). It is a judgement. The same script finds that 84% of historical party errors fall inside the nominal 90% interval, slightly narrow; for large parties the model is wider than the history (2.6 points for a 30% party at 7 days, against an observed 1.8 above 10%).

scripts/model/backtest.mjs takes 2019 as the base and compares with the real 2023 seats. Part A uses the real 2023 national shares, which removes poll error and tests only the method:

MethodSum of errors per party (seats)Seats in the wrong province (of 350)Exact provinces (of 52)
Proportional swing (ours)101835
Uniform swing123128
No swing (repeat 2019)1006111

Part B measures the error of provincial shares in log terms (0.094 between communities, 0.075 within them), which sets regionSd and provSd. Part C checks the intervals. With real shares and only regional and provincial noise, 10 of 12 parties had their real seat total inside the 90% range, and so did 173 of 181 party-province cases; this test is not independent, because the same comparison set those two parameters.

End to end, with the poll average of 16 July 2023 and the full model, 12 of 13 parties fell inside their 90% range; the exception was a party held at its previous result. But the deterministic projection had the PSOE at 106 seats against a real 121, and PP at 143 against 137. The range (90 to 126 for the PSOE) held the result, yet the central estimate was 15 seats off.

What it doesn’t do