PHP and JavaScript – Calculating Winning Probability for Teams Using History

javascriptPHP

So I'm working on this project where I have a database full of team winning history. For example let's say these are all football teams. Every match has 2 participants (2 teams) and always a winner.

Now I'm trying to make a script that actually calculates the winning probability using this info but I'm having a hard time trying to figure out a proper algoritm for this to actually predict effectively.

To make this more clear, let's make an example. We have two teams playing against each other: team 1 and team 2. Now the easiest way to calculate something would be to check if those two exact teams have ever played before and what were the results. For example if team 1 once beat team 2 and then in another game team 2 instead was the winner, then the probability for this game would be 50-50.

But I'd like to make this a little more complicated than that. For example, let's say there are three teams: team 1, team 2 and team 3. This is the match history for those teams:
team 1 beats team 2
team 2 beats team 3
And now we have a match that's team 1 vs team 3. And looking at the history, we can calculate that team 3 is very likely going to lose.

So I guess my question is, how does one code such an algorithm? I'm not looking for code snippets but instead a logical approach for these algorithms. If you have any code examples I'd be happy to look at those as well. I'm programming this in PHP myself but the snippets can be in any language. And also I'm looking for more algorithm ideas if anyone has any.

Thanks in advance!

Best Answer

Why reinvent the wheel? Try ELO rating system - it was developed for exactly this purpose, and is best known for being used to rate chess players, although not exclusively. And it stood the test of time, to be sure.

As for calculating the probability that player (or team) A defeats B, look under http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details for a formula.

Here's a piece of ready-made code that implements ELO: https://github.com/Chovanec/elo-rating

It's in PHP, since you mention it specifically, but I'm sure you could find something similar for every major language.

Note that I have not tested it and I'm in no way affiliated with the author.

Related Topic