# Calculating winning probability for 2 teams using their history?

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.

• "always a winner" - some new form of football? – Mawg Mar 11 '15 at 13:45
• It sounds overly simplistic to only consider win/lose, with the margin. If a beats B 9-0 three times and loses 1-2 three times, are they equally good teams? Also, you might want to award more points to away wins than to home wins ... – Mawg Mar 11 '15 at 13:46
• The project I'm working on isn't actually football related at all. Football was just an example to understand the issue a bit more clearly. Although I agree with the fact that wins/losses aren't the best means of prediction but that's the best I can work with right now. That's also the reason why I want to take game history in to consideration when calculating probability. – Martin J Mar 11 '15 at 15:59

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.

• Thanks for the response I appreciate it. The ELO rating system is actually pretty useful for this but it's not really exactly what I want. ELO system uses only it's ranking to calculate. While it does make a good prediction about a bad team and a good team it still doesn't quite do what I'd like. I have a database full of match histories and I'd like to implement this somehow to make the prediction more accurate. For example when team1 has elo 1000 and team2 has elo 500, it's very likely that team1 will win, right? Wrong. What if in the past team2 has always won against team1. – Martin J Mar 11 '15 at 0:58
• [continued].. In that case the 1000-500 prediction should be a lot different, because history shows that team1 always loses to team2 while team2 might be losing a lot to other teams (hence the low elo). – Martin J Mar 11 '15 at 1:01
• @MartinJ I see what you mean, but if you want to attribute more weight to direct encounters between team1 and team2 (disregarding, or giving less relevance to how they both ranked against team3, team4 etc.), you can simply recalculate the ratings of team1 and team2 based on the outcomes of matches they played against eachother, and these matches only. So ELO is still useful, you just feed the algorithm with a narrowed down subset of game results. How about that? – Konrad Morawski Mar 12 '15 at 12:46
• Yes, this is probably what I'm going to do. Anyway, thanks for referring the ELO system. I already actually knew about it but reading further about the algorithm it gave me a bunch of other ideas that I think I'll implement. – Martin J Mar 13 '15 at 17:30