From 298ee77515b70cf8fd98b906b4d7058e55b1d456 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 23:45:05 +1100 Subject: [PATCH 1/7] Create elo snippet --- snippets/elo.md | 25 +++++++++++++++++++++++++ tag_database | 1 + 2 files changed, 26 insertions(+) create mode 100644 snippets/elo.md diff --git a/snippets/elo.md b/snippets/elo.md new file mode 100644 index 000000000..9d50ba1d0 --- /dev/null +++ b/snippets/elo.md @@ -0,0 +1,25 @@ +### elo + +Computes the new ratings between two opponents using the Elo rating formula. It takes an array +of two pre-ratings and returns an array containing two post-ratings. The winner's rating is be the first element of the array. + +Use `Math.pow()` and math operators to compute the expected score (chance of winning) of each opponent +and compute the change in rating for each. Omit the second argument to use the default K-factor of +32, or supply a custom K-factor value. + +```js +const elo = ([a, b], kFactor = 32) => { + const expectedScore = (self, opponent) => 1 / (1 + Math.pow(10, (opponent - self) / 400)); + const [eA, eB] = [expectedScore(a, b), expectedScore(b, a)]; + const newRating = (rating, index) => + rating + kFactor * ((index === 0 ? 1 : 0) - (index === 0 ? eA : eB)); + return [newRating(a, 0), newRating(b, 1)]; +}; +``` + +```js +elo([1200, 1200]); // [1216, 1184] +elo([1000, 2000]); // [1031.8991261061358, 1968.1008738938642] +elo([1500, 1000]); // [1501.7036868864648, 998.2963131135352] +elo([1200, 1200], 64); // [1232, 1168] +``` diff --git a/tag_database b/tag_database index f5633a5fa..7ca86d900 100644 --- a/tag_database +++ b/tag_database @@ -29,6 +29,7 @@ distinctValuesOfArray:array dropElements:array dropRight:array elementIsVisibleInViewport:browser +elo:math escapeHTML:string escapeRegExp:string everyNth:array From f9344288aafa6c671c9c30a66c3bf8275736ce85 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 23:45:45 +1100 Subject: [PATCH 2/7] Add link --- snippets/elo.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/elo.md b/snippets/elo.md index 9d50ba1d0..85b54d35b 100644 --- a/snippets/elo.md +++ b/snippets/elo.md @@ -1,7 +1,8 @@ ### elo -Computes the new ratings between two opponents using the Elo rating formula. It takes an array -of two pre-ratings and returns an array containing two post-ratings. The winner's rating is be the first element of the array. +Computes the new ratings between two opponents using the [Elo rating system](https://en.wikipedia.org/wiki/Elo_rating_system). It takes an array +of two pre-ratings and returns an array containing two post-ratings. +The winner's rating is be the first element of the array. Use `Math.pow()` and math operators to compute the expected score (chance of winning) of each opponent and compute the change in rating for each. Omit the second argument to use the default K-factor of From aa617b989fdbefe2c76b90508e2d90d3dbf60e9a Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 23:47:43 +1100 Subject: [PATCH 3/7] fix typo --- snippets/elo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/elo.md b/snippets/elo.md index 85b54d35b..5be73476f 100644 --- a/snippets/elo.md +++ b/snippets/elo.md @@ -2,7 +2,7 @@ Computes the new ratings between two opponents using the [Elo rating system](https://en.wikipedia.org/wiki/Elo_rating_system). It takes an array of two pre-ratings and returns an array containing two post-ratings. -The winner's rating is be the first element of the array. +The winner's rating is the first element of the array. Use `Math.pow()` and math operators to compute the expected score (chance of winning) of each opponent and compute the change in rating for each. Omit the second argument to use the default K-factor of From 71422f5180756d63f67f5e3d143105fe754f6289 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 00:02:46 +1100 Subject: [PATCH 4/7] simplify newRating function --- snippets/elo.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snippets/elo.md b/snippets/elo.md index 5be73476f..1c52db62b 100644 --- a/snippets/elo.md +++ b/snippets/elo.md @@ -12,9 +12,8 @@ and compute the change in rating for each. Omit the second argument to use the d const elo = ([a, b], kFactor = 32) => { const expectedScore = (self, opponent) => 1 / (1 + Math.pow(10, (opponent - self) / 400)); const [eA, eB] = [expectedScore(a, b), expectedScore(b, a)]; - const newRating = (rating, index) => - rating + kFactor * ((index === 0 ? 1 : 0) - (index === 0 ? eA : eB)); - return [newRating(a, 0), newRating(b, 1)]; + const newRating = (rating, index) => rating + kFactor * (index - (index ? eA : eB)); + return [newRating(a, 1), newRating(b, 0)]; }; ``` From 3ef1d1dc36b767788dafb0a32ae0473db415c46f Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 17:13:05 +1100 Subject: [PATCH 5/7] Optimise further --- snippets/elo.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/elo.md b/snippets/elo.md index 1c52db62b..991b74019 100644 --- a/snippets/elo.md +++ b/snippets/elo.md @@ -5,14 +5,14 @@ of two pre-ratings and returns an array containing two post-ratings. The winner's rating is the first element of the array. Use `Math.pow()` and math operators to compute the expected score (chance of winning) of each opponent -and compute the change in rating for each. Omit the second argument to use the default K-factor of +and compute the new rating for each. Omit the second argument to use the default K-factor of 32, or supply a custom K-factor value. ```js const elo = ([a, b], kFactor = 32) => { const expectedScore = (self, opponent) => 1 / (1 + Math.pow(10, (opponent - self) / 400)); - const [eA, eB] = [expectedScore(a, b), expectedScore(b, a)]; - const newRating = (rating, index) => rating + kFactor * (index - (index ? eA : eB)); + const newRating = (rating, i) => + rating + kFactor * (i - (i ? expectedScore(a, b) : expectedScore(b, a))); return [newRating(a, 1), newRating(b, 0)]; }; ``` From cb0d65826eb20985892745fd9c4114a179e992c2 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 17:19:55 +1100 Subject: [PATCH 6/7] Avoid repeating function call --- snippets/elo.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/elo.md b/snippets/elo.md index 991b74019..1fbbe1986 100644 --- a/snippets/elo.md +++ b/snippets/elo.md @@ -11,8 +11,7 @@ and compute the new rating for each. Omit the second argument to use the default ```js const elo = ([a, b], kFactor = 32) => { const expectedScore = (self, opponent) => 1 / (1 + Math.pow(10, (opponent - self) / 400)); - const newRating = (rating, i) => - rating + kFactor * (i - (i ? expectedScore(a, b) : expectedScore(b, a))); + const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); return [newRating(a, 1), newRating(b, 0)]; }; ``` From dc1322f72b56ea0b7bc4b768504404e7376efcc3 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sun, 31 Dec 2017 19:04:35 +1100 Subject: [PATCH 7/7] switch to exponent operator --- snippets/elo.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/elo.md b/snippets/elo.md index 1fbbe1986..671d1ccea 100644 --- a/snippets/elo.md +++ b/snippets/elo.md @@ -4,13 +4,13 @@ Computes the new ratings between two opponents using the [Elo rating system](htt of two pre-ratings and returns an array containing two post-ratings. The winner's rating is the first element of the array. -Use `Math.pow()` and math operators to compute the expected score (chance of winning) of each opponent -and compute the new rating for each. Omit the second argument to use the default K-factor of -32, or supply a custom K-factor value. +Use the exponent `**` operator and math operators to compute the expected score (chance of winning) +of each opponent and compute the new rating for each. Omit the second argument to use the default +K-factor of 32, or supply a custom K-factor value. ```js const elo = ([a, b], kFactor = 32) => { - const expectedScore = (self, opponent) => 1 / (1 + Math.pow(10, (opponent - self) / 400)); + const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400)); const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); return [newRating(a, 1), newRating(b, 0)]; };