From 8fd979c23bcb4d8be51325e1a1cc6da8e74db5fa 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 d434b99f5cfef653b3b09b150e67d3fd025ff034 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 c7faf04ce0b1fb768cc5ade7f4b36d0d3192b04a 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 e093e132c3182f0fea70b9c1d70f8c57dc04c5d3 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 ee4820d61e5ed4f1351dd7fd3800d82ddac400ae 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 13e4c9280034c29974adeaf2746db8c66e6aaf09 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 5099ed40c305168ed12eae11eb3555c530459825 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)]; };