Friday, January 24, 2025

Code clean-up: Bayesian Aggregation models

I recently spent some time cleaning up the Python code I use to produce the Bayesian Aggregation charts.  When I originally wrote the code some two-plus years ago, it was all in the global name-space. For short analytical notebooks, that's generally not a problem, But for larger notebooks (such as this one) it can hide name clashes, which can result in subtle errors. 

About six months ago, one of my models - the Gaussian Process model - just randomly stopped working. The error message was obscure (something about the linker not working). It was not immediately obvious what the problem was. And I did not want to spend the time diagnosing the actual problem. 

In the past week, I decided to bite the bullet, and encapsulate the Python code into functions, so that the code was mostly not in the global name-space. I also decided I would make the Python code lint compliant (using black, ruff and pylint) and type safe (using mypy). I have now completed that process (well almost, I still have one function with too many local variables). 

The unexpected benefit: the Gaussian Process model works again (even though I did not fiddle with that bit of the code). The lesson learnt (again): doing too much work in the global namespace can result in subtle and hard to detect errors. The other lesson learnt: keep the code clean, and regularly check it with linting tools such as mypy and pylint. 

If you want to see the rewritten notebook, it is here. The main supporting functions (including the Bayesian models) are here.

Anyways, the most recent endpoints for the three models I run are as follows (the values in the table are percentages). 

2pp vote ALP 2pp vote L/NP Primary vote ALP Primary vote GRN Primary vote L/NP Primary vote Other
Gaussian Random Walk - Normal likelihood - fixed priors 48.82 51.16 30.67 12.40 39.39 17.53
Gaussian Process - Normal likelihood - fixed priors 49.18 50.82 31.02 12.46 39.10 17.41
150-day local regression 48.42 51.58 30.18 12.65 39.81 16.52

My preferred model remains the Gaussian Random Walk. As I have noted before, the Gaussian Process model does not perform well when the polling information is relative rare (as it was in 2022 and into 2023. The Gaussian Process model also tends to revert back to the mean at the ends of the series. The local regression model can be overly influenced by the last few data points on the right hand side.

The latest charts follow.

Monday, January 20, 2025

Polling update mid January

January is normally a quite month for polling in Australia, and 2025 is no exception. Nonetheless there have been a couple of polls since Christmas 2024. These polls suggest little movement since Christmas in two-party preferred terms. You will note that I now indicate on the first house-effects chart below, those polling houses which I have constrained so that their house effects sum to zero. I exclude pollsters with fewer than 5 polls, and the prior methodologies from pollsters which have changed methodologies.

Tuesday, December 24, 2024

End of 2024 Polling and Betting Market Update

As a Bayesian, my prior assumption for first term governments is that they have a reasonably high probability of being re-elected come their first election as a Government. Yes there are exceptions (particularly for state governments), but Federally they have been rare. The only one term Federal government was the Scullin Labor Government that lost the 1931 election, albeit in the midst of the Great Depression. Since the 1920s, there have been more than 20 one term state governments. Recent examples include Napthine, 2014, Victoria; Campbell, 2015, Queensland; Giles, 2016, Northern Territory; and Marshall 2022 South Australia). However, even with the states, one term governments are not the norm. 

When the Albanese government was elected in 2022, I assumed Labor would most likely go on to win a second term in 2025. While my intuition remains that Labor should win a second term, my confidence is much reduced. Particularly as the betting market and the final polls of 2024 give the Coalition a slight edge. For the avid poll watcher, it means 2025 will be much more interesting than I had originally anticipated. There is good chance of a very close election and a Parliament where neither of the major parties has an absolute majority. Furthermore, there is a moderate chance of a change of government following the election. 

So let's look at the betting market. According to the collective wisdom of punters, the Coalition has a 55 per cent probability of forming government after the 2025 Federal Election.

Wednesday, December 11, 2024

Polls and betting market update

The odds at Sportsbet have settled down following the reassessment that occurred immediately after the Trump election in the United States. At the moment, the betting market sees a 45 per cent probability of a Labor Government and a 55 per cent probability of a Coalition government. Note: the gaps in the next few charts represent the days where I did not collect odds data.





In aggregate, the opinion polls suggest the Coalition would secure a larger two-party preferred vote share compared with Labor (50.6 to 49.4 per cent). However, given the large number of independents and Greens likely to be elected to the House, it is difficult to predict which party would form (probably minority) government if the current two-party preferred (2pp) polls were replicated at the next election. 






The primary vote aggregations suggest that the Coalition and Others are strengthening over the past month while Labor and the Greens are down. The Greens appear to have lost around a percentage point over the past six months or so. 







And finally, a quick look at the attitudinal polling.





Monday, November 25, 2024

Update: betting market and opinion polls 25 November

In the days following the Trump win in the United States, the betting market for the Prime Minister's party after the next Australian Federal election was a little volatile. It appears to have settled with the Coalition as slight favourite (55% probability). This reverses the position of the two major parties immediately prior to the US election. 



Apologies for the gaps in the previous charts. For various reasons I was not able to capture the odds data every day. The continuous lines represent consecutive days on which I was able to collect odds.

The two-party preferred voting intention polls now have the Coalition ahead (on average). However, given the likely number of independents and minor parties in Parliament after the next election, it is a little difficult to be confident about the major party to form government. 






The estimated house-effects that arise from Gaussian Random Walk (GRW) Bayesian Aggregation above are as follows. Note: these two charts are reporting on the same underlying data. 




Turning to the primary voting intention, we can see some clear trends. The Coalition has improved its position while Labor has declined since the last election.


The ALP is below where it was at the last election. Labor would likely lose seats to the Coalition, the Greens and/or left-leaning independents, if the election reflected these poll results. 



The Coalition is ahead of its previous election performance, and should gain both Labor and perhaps independent seats at the next election. 



The Greens should either maintain their position or gain seats at the next election.



The independents will either maintain their seat count or lose seats at the next election (based on these poll results).



The attitudinal polling shows a remarkable shift in sentiment for a first-term government.




Monday, November 11, 2024

New polls: Newspoll and Resolve

Today's Australian had a new Newspoll (49-51 in the Coalition's favour), and the Fairfax media had a new Resolve Strategic Poll (50-50). All of my polling aggregation methods suggest that the Coalition is just ahead in the two-party-preferred voting intention metric. However, the only certainty is a hung Parliament. In this range, it is just too close to call. 





Note: in the charts above, the GRW model is a Gaussian Random Walk model where the vote-shares on any day and much the same as the vote shares on the day before. The GP model is a Gaussian Process model, where polls that are closer together in time are deemed to be more highly correlated. The GP model performs less well when the density of polling data is low (as with the first year of the series above), and at both ends of the series where it tends to revert to an underlying assumption about voting intention in the model (set to an average of the most recent 10 polls). I prefer the GRW model over the GP model. 

The primary votes are also moving around a little, with the major party votes up on recent trends, at the expense of the Greens and other minor parties.







The betting market sees the next election as pretty close to the proverbial toss-up, with the Coalition the favourite to form Government.  The gaps in the following charts are days in which I did not collect odds data from SportsBet.