# Help implementing the Elo Rating System in Julia?

**URL:** <https://discourse.julialang.org/t/help-implementing-the-elo-rating-system-in-julia/32721>\
**Category:** General Usage\
**Tags:** array\
**Created:** [December 26, 2019, 5:46pm UTC](https://discourse.julialang.org/t/help-implementing-the-elo-rating-system-in-julia/32721 "2019-12-26T17:46:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PseudoCodeNerd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pseudocodenerd/32/11948_2.png) [@PseudoCodeNerd](https://discourse.julialang.org/u/PseudoCodeNerd)\
**Post date:** [December 26, 2019, 5:46pm UTC](https://discourse.julialang.org/t/help-implementing-the-elo-rating-system-in-julia/32721/1 "2019-12-26T17:46:21Z")

</div>

Hello There !  
I recently thought out to implement the Elo Rating System in Julia.  
[Elo rating system - Wikipedia](https://en.wikipedia.org/wiki/Elo_rating_system) More information about this.

The function should take an array of pre-ratings and returns an array containing post-ratings and it should be ordered from best performer to worst performer (winner → loser).

My TA told me to use the default K factor as 32. I saw up an implementation on Ranking’s.jl but couldn’t understand. With help from my brother I was able to make it in javaScript.

I’m relatively new to Julia and would like to convert this into Julia code.

```julia
const elo = ([...ratings], kFactor = 32, selfRating) => {
  const [a, b] = ratings;
  const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
  const newRating = (rating, i) =>
    (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
  if (ratings.length === 2) return [newRating(a, 1), newRating(b, 0)];

  for (let i = 0, len = ratings.length; i < len; i++) {
    let j = i;
    while (j < len - 1) {
      j++;
      [ratings[i], ratings[j]] = elo([ratings[i], ratings[j]], kFactor);
    }
  }
  return ratings;
};

```

Output

```julia
// Standard 1v1s
elo([1200, 1200]); // [1216, 1184]
elo([1200, 1200], 64); // [1232, 1168]
// 4 player FFA, all same rank
elo([1200, 1200, 1200, 1200]).map(Math.round); // [1246, 1215, 1185, 1154]
/*
For teams, each rating can adjusted based on own team's average rating vs.
average rating of opposing team, with the score being added to their
own individual rating by supplying it as the third argument.
*/

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 26, 2019, 6:17pm UTC](https://discourse.julialang.org/t/help-implementing-the-elo-rating-system-in-julia/32721/2 "2019-12-26T18:17:02Z")

</div>

What have you tried so far? Where did you get stuck?

---

<div class="post-metadata">

**Author:** ![PseudoCodeNerd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pseudocodenerd/32/11948_2.png) [@PseudoCodeNerd](https://discourse.julialang.org/u/PseudoCodeNerd)\
**Post date:** [December 26, 2019, 6:18pm UTC](https://discourse.julialang.org/t/help-implementing-the-elo-rating-system-in-julia/32721/3 "2019-12-26T18:18:54Z")

</div>

I don’t seem to understand how

```julia
  const newRating = (rating, i) =>
    (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));

```

can be coverted to julia  
All other stuff has been done.  
Thanks

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [December 26, 2019, 8:49pm UTC](https://discourse.julialang.org/t/help-implementing-the-elo-rating-system-in-julia/32721/4 "2019-12-26T20:49:43Z")

</div>

how do you consider ties? for what i remembered, a tie is assigned 0.5 to each player, and the result of `elo([1200, 1200]); // [1216, 1184]` is weird in my opinion.  
you can replace that with a function,taking into account that in julia `1 != true`. As a workaround, you can do `isone(1)`

```julia
const newRating = (rating, i) =>
    (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));

```

```julia
function newRating(rating, i)
  return (selfRating || rating) + kFactor * (i - expectedScore(isone(i) ? a : b, isone(i) ? b : a))
end

```

now there is still one part that needs correcting and can’t be passed to julia as-is, this statement:

```julia
(selfRating || rating)

```

as i have no real experience in javascript, i had to play around to see what this part does. technically, if the first argument exists, returns the first argument, else, return the second. i don’t know how do you represented the undefined or null value here. but taking a guess, you maybe used `nothing`. if this is the case, you can use [something(a,b)](https://docs.julialang.org/en/v1/base/base/#Base.something) . replacing this you have a valid (i think) julia function

```julia
function newRating(rating, i)
  return something(selfRating,rating) + kFactor * (i - expectedScore(isone(i) ? a : b, isone(i) ? b : a))
end

```
