{"id":9341,"date":"2026-05-07T10:02:41","date_gmt":"2026-05-07T10:02:41","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=9341"},"modified":"2026-05-07T10:02:39","modified_gmt":"2026-05-07T10:02:39","slug":"dhondt-apportionment-made-easy-with-apl","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2026\/05\/dhondt-apportionment-made-easy-with-apl\/","title":{"rendered":"D&#8217;Hondt Apportionment Made Easy with APL"},"content":{"rendered":"<p>Today, the UK is holding elections for local councillors in England, and members of the devolved parliaments in Scotland and Wales. Until a political party starts pushing for mandatory APL lessons, we&#8217;re not interested in the results of these elections for this blog post. What we <em>are<\/em> interested in is algorithms, and, for the first time, the elections to the Welsh Parliament (usually referred to as the Welsh &#8216;Senedd&#8217;) used the <a href=\"https:\/\/en.wikipedia.org\/wiki\/D%27Hondt_method\" target=\"_blank\" rel=\"noopener\">D&#8217;Hondt method<\/a> for allocating seats.<\/p>\n<p>The D&#8217;Hondt method is named after Belgian mathematician and lawyer <a href=\"https:\/\/en.wikipedia.org\/wiki\/Victor_D%27Hondt\" target=\"_blank\" rel=\"noopener\">Victor D&#8217;Hondt<\/a>, although it was independently invented by United States Founding Father <a href=\"https:\/\/en.wikipedia.org\/wiki\/Thomas_Jefferson\" target=\"_blank\" rel=\"noopener\">Thomas Jefferson<\/a> almost a century earlier! The method transforms a vote count for a number of political parties into an approximately proportional allocation of seats to each of the parties. Therefore, unlike in previous Senedd elections, Welsh voters do not have individual representatives on their ballots. Instead, they each vote for one political party. The parties then send as many representatives to the Senedd as they are allocated by the D&#8217;Hondt method.<\/p>\n<p>With the D&#8217;Hondt method, seats are allocated to parties one-by-one. When each seat is allocated, it is given to the party with the largest vote count <em>relative to the number of seats they have already been given<\/em>. This way, more popular parties are given seats first, but when they have several already, a seat goes to a smaller party so that it is also represented.<\/p>\n<p>This means that, when allocating a seat, the &#8216;quotient&#8217; <code class=\"language-apl\">votes\u00f7(seats+1)<\/code> is calculated for each party, where <code class=\"language-apl\">votes<\/code> is the number of votes the party received, and <code class=\"language-apl\">seats<\/code> is the number of seats they have been allocated so far. The <code class=\"language-apl\">+1<\/code> is included to avoid division by zero. The party with the largest quotient is allocated the seat, and the process is repeated until all seats have been allocated.<\/p>\n<p>Let&#8217;s implement this in APL. Given a vector <code class=\"language-apl\">votes<\/code> and a vector <code class=\"language-apl\">seats<\/code>, with each index corresponding to a political party, the index of the party with the greatest quotient can be found as follows:<\/p>\n<pre><code class=\"language-apl\">      i\u2190\u2283\u2352votes\u00f7seats+1<\/code><\/pre>\n<p>The <em>grade down<\/em> of the quotient returns the indices to sort the quotients in descending order. Therefore, the first of these indices will be the index of the greatest quotient \u2013 exactly what the algorithm calls for.<\/p>\n<p>We then need to update <code class=\"language-apl\">seats<\/code> to give a seat to this lucky party. There&#8217;s more than one way to do this, but I&#8217;ll take a functional approach and use the <em>at<\/em> operator (<code class=\"language-apl\">@<\/code>) to add <code class=\"language-apl\">1<\/code> &#8216;at&#8217; the index of party <code class=\"language-apl\">i<\/code>:<\/p>\n<pre><code class=\"language-apl\">      1(+@i)seats<\/code><\/pre>\n<p>This can be wrapped in a dfn, in which <code class=\"language-apl\">\u237a<\/code> is the votes vector and <code class=\"language-apl\">\u2375<\/code> is the current seat allocation.<\/p>\n<pre><code class=\"language-apl\">      Step\u2190{\r\n          i\u2190\u2283\u2352\u237a\u00f7\u2375+1\r\n          1(+@i)\u2375\r\n      }<\/code><\/pre>\n<p>To allocate, say, fifty seats, the <code class=\"language-apl\">Step<\/code> function should be applied 50 times on successive seat allocations \u2013 an ideal use-case for the <em>power<\/em> operator (<code class=\"language-apl\">\u2363<\/code>). Let&#8217;s try it with a hypothetical vote share of 4:3:2 for three parties:<\/p>\n<pre><code class=\"language-apl\">      4000 3000 2000 (Step\u236350) 0 0 0\r\n22 17 11<\/code><\/pre>\n<p>That&#8217;s a ratio of around 4:3.1:2 &#8211; a good approximation of the original vote-share!<\/p>\n<p>It&#8217;s easier to visualise the behaviour of the D&#8217;Hondt method over time by considering just two parties. Let&#8217;s call them Party A and Party B. Suppose that these parties received 12,345 and 6,789 votes respectively, and share a 50 seat parliament.<\/p>\n<pre><code class=\"language-apl\">      12345 6789 (Step\u236350) 0 0\r\n32 18<\/code><\/pre>\n<p>Party A is allocated 32 seats, while party B is allocated 18. This ratio is, again, reasonably close to that of the votes:<\/p>\n<pre><code class=\"language-apl\">      12345\u00f76789  \u235d vote ratio\r\n1.818382678\r\n      32\u00f718       \u235d seat ratio\r\n1.777777778<\/code><\/pre>\n<p>Let&#8217;s see how this ratio changes with each step. By modifying the <code class=\"language-apl\">Step<\/code> function, we can record the intermediate seat allocations. <code class=\"language-apl\">\u2375<\/code> is now a matrix with a row for the seat allocation after each step. The most recent allocation is extracted with <code class=\"language-apl\">\u22a2\u233f<\/code>, which is an idiom for the last row of a matrix. The function must now be called with an initial value of <code class=\"language-apl\">[0 0\u22c4]<\/code>, which is a one-row matrix of two zeros expressed using array notation.<\/p>\n<pre><code class=\"language-apl\">      Step\u2190{\r\n          seats\u2190\u22a2\u233f\u2375\r\n          i\u2190\u2283\u2352\u237a\u00f7seats+1\r\n          \u2375\u236a1(+@i)seats\r\n      }\r\n      12345 6789 (Step\u236350) [0 0\u22c4]\r\n 0  0\r\n 1  0\r\n 1  1\r\n 2  1\r\n 3  1\r\n 3  2\r\n 4  2\r\n 5  2\r\n...<\/code><\/pre>\n<p>We can now plot the ratio (the <code class=\"language-apl\">\u00f7\/<\/code> of this result matrix, on the vertical axis) against the step of the method (on the horizontal axis). The ratio after each step is drawn as an orange dot, and the perfect ratio which the method is approaching is drawn by the lavender line.<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9350\" src=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_01.png\" alt=\"\" width=\"1200\" height=\"630\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_01.png 1200w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_01-300x158.png 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_01-768x403.png 768w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_01-1024x538.png 1024w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_01-500x263.png 500w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p>We can see the ratio converging to the ratio of the votes. The really interesting question is why. I won&#8217;t give a formal proof here, that&#8217;s one for mathematicians and political scientists to write about. I will, however, show you how I understand it.<\/p>\n<p>It&#8217;s interesting to look at a plot of the seat allocation given by the D&#8217;Hondt method alongside a &#8216;perfect&#8217; allocation where we are allowed fractional seats. The vertical axis corresponds to seats allocated to party A, and the horizontal axis corresponds to seats allocated to party B. The trace of seat allocations is drawn as an orange line in the shape of a staircase, and the line of perfect allocation is drawn as a straight lavender line.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9351\" src=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_02.png\" alt=\"\" width=\"420\" height=\"616\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_02.png 420w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_02-205x300.png 205w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><\/p>\n<p>At each step, the D&#8217;Hondt method (usually) moves towards, and subsequently crosses, the line of perfect allocation. When an allocation is above the line, Party A has &#8216;too many&#8217; seats, giving it a smaller quotient. The method then allocates a seat to Party B to correct for this, corresponding to the rightward move on the plot. The opposite also applies \u2013 when the allocation is below the line, Party B has the smaller quotient, and the method moves the allocation vertically.<\/p>\n<p>There are exceptions to this pattern \u2013 cases where the allocation moves <em>away<\/em> from the line of perfect allocation. This is because of the &#8220;fudge factor&#8221;\u00a0<code class=\"language-apl\">+1<\/code> on the seat count. This imaginary extra seat for each party can skew the allocation decision occasionally. From my limited reading, it looks like this is a known behaviour of the D&#8217;Hondt method, and is deliberately retained.<\/p>\n<p>By &#8216;hugging&#8217; the perfect allocation like this, the D&#8217;Hondt method stays within a few seats of being perfectly proportional. As the number of seats increases, this error is relatively smaller, so the accuracy of the method increases.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9353\" src=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_03.png\" alt=\"\" width=\"417\" height=\"616\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_03.png 417w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2026\/05\/blog_dhondt-apportionment-apl_03-203x300.png 203w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/p>\n<p>This is how the D&#8217;Hondt method allocates seats at a ratio close to the ratio of votes, and gets better as there are more seats available.<\/p>\n<p>Irrespective of whether you&#8217;re Welsh, I hope it was interesting to explore the D&#8217;Hondt method for apportionment with APL!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, the UK is holding elections for local councillors in England, and members of the devolved parliaments in Scotland and Wales. Until a political party starts pushing for mandatory APL lessons, we&#8217;re not interested in the results of these elections &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2026\/05\/dhondt-apportionment-made-easy-with-apl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-9341","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=9341"}],"version-history":[{"count":9,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9341\/revisions"}],"predecessor-version":[{"id":9358,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9341\/revisions\/9358"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=9341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=9341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=9341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}