Jump to content

ELO Calculations


RMorrison
Go to solution Solved by mac_gyver,

Recommended Posts

I've written a small ELO class for calculating a players rating for a game league, and for the life of me I don't know why these calculations are coming up wrong.

 

The Class:

<?php
class elo {
function get_rating($current_rating, $expected, $win = true)
{
if ($win === true)
{
$r = $current_rating;
$add = 32;
$multiplier = 1 - $expected;
$add = $add*$multiplier;
$r = $current_rating + $add;
return round($r,0);
}
else
{
$r = $current_rating;
$add = 32;
$multiplier = 0 - $expected;
$add = $add*$multiplier;
$r = $current_rating + $add;
return round($r,0);
}
}

function get_expected($player1_rating, $player2_rating)
{
$e = 1;
$divider = 1;
$power_of = $player2_rating - $player1_rating;
$power_of = $power_of / 400;
$power_of = 10^($power_of);
$divider = $divider + $power_of;
$e = $e/$divider;
return round($e,2);
}
}

 

Code for testing:

$elo = new elo();
$a = 1000;
$b = 1000;
$chance['a'] = $elo->get_expected($a,$b);
$chance['b'] = $elo->get_expected($b,$a);
$chance['%a'] = $chance['a'] * 100;
$chance['%b'] = $chance['b'] * 100;
echo "Player A ({$a}) has a " . $chance['%a'] . "% Chance of winning.<br/>"; //should be 50% (0.5)
echo "Player B ({$b}) has a " . $chance['%b'] . "% chance of winning.<br/>"; //should be 50% (0.5)
$winpoints['a'] = $elo->get_rating($a, $chance['a']); //should return 1016
$winpoints['b'] = $elo->get_rating($b, $chance['b']); //should return 1016
echo "Player A's new rating will be {$winpoints['a']} if they win.<br/>";
echo "Player B's new rating will be {$winpoints['b']} if they win.<br/>";
$losspoints['a'] = $elo->get_rating($a, $chance['a'], false); //should return 984
$losspoints['b'] = $elo->get_rating($b, $chance['b'], false); //should return 984
echo "Player A's new rating will be {$losspoints['a']} if they lose.<br/>";
echo "Player B's new rating will be {$losspoints['b']} if they lose.<br/>";

 

The output i'm getting is as follows, when they should match what i've stated above (as done on paper with a calculator)

 

Player A (1000) has a 9% Chance of winning.

Player B (1000) has a 9% chance of winning.

Player A's new rating will be 1029 if they win.

Player B's new rating will be 1029 if they win.

Player A's new rating will be 997 if they lose.

Player B's new rating will be 997 if they lose.

I've tried re-writing the calculation over and over but just can't get it. I used the Wikipedia page to get my calculations. I've not added K Factor yet but I will be at some point.

Link to comment
Share on other sites

  • Solution
10^($power_of)

 

 

the problem is most likely in that bit of coding. in programming languages, and specifically php, the ^ is not a exponential operator, it is in fact a bit wise exclusive or operator. you would need to consult the php documentation to find the proper exponential operator.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.