Skip to content

Commit c23cd52

Browse files
committed
Add advantage to hero matchups
/heroes/{hero_id}/matchups only had games and wins, so anyone wanting the advantage the site's matchups page shows had to port its Wilson score (#1566). Each row now carries it, computed with the same formula: on the 126 matchups of hero 1 it equals the page's value in all of them.
1 parent 4aee752 commit c23cd52

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎svc/api/responses/HeroMatchupsResponse.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export default {
1414
description: "Number of games won",
1515
type: "integer",
1616
},
17+
advantage: {
18+
description:
19+
"Lower bound of the Wilson score interval for the win rate, as a percentage. What the hero matchups page sorts by",
20+
type: "integer",
21+
},
1722
},
1823
},
1924
};

‎svc/api/spec.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
getPlayerWordcloud,
1616
} from "../util/buildPlayer.ts";
1717
import { getStartOfBlockMinutes } from "../util/time.ts";
18-
import { queryParamToArray } from "../util/utility.ts";
18+
import { queryParamToArray, wilsonScore } from "../util/utility.ts";
1919
import {
2020
matchesCols,
2121
histogramCols,
@@ -1775,7 +1775,14 @@ You can use the API without a key, but registering for a key allows increased ra
17751775
ORDER BY games_played DESC`,
17761776
[heroId, Math.floor(Date.now() / 1000) - 365 * 86400],
17771777
);
1778-
return res.json(rows);
1778+
return res.json(
1779+
rows.map((row) => ({
1780+
...row,
1781+
advantage: Math.round(
1782+
wilsonScore(Number(row.wins), Number(row.games_played)) * 100,
1783+
),
1784+
})),
1785+
);
17791786
},
17801787
},
17811788
},

‎svc/util/utility.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ export function averageMedal(values: number[]) {
170170
return Math.floor(avgStars / 5) * 10 + (avgStars % 5) + 1;
171171
}
172172

173+
/**
174+
* Lower bound of the Wilson score interval for wins out of games, the
175+
* "advantage" the hero matchups page shows (z for a 95% one-sided bound)
176+
* */
177+
export function wilsonScore(wins: number, games: number) {
178+
if (!wins) {
179+
return 0;
180+
}
181+
const z = 1.64485;
182+
const phat = wins / games;
183+
return (
184+
(phat +
185+
(z * z) / (2 * games) -
186+
z * Math.sqrt((phat * (1 - phat) + (z * z) / (4 * games)) / games)) /
187+
(1 + (z * z) / games)
188+
);
189+
}
190+
173191
/**
174192
* Finds the standard deviation of the input array
175193
* */

‎test/test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import db, { upsertPlayer } from "../svc/store/db.ts";
2121
import clickhouse from "../svc/store/clickhouse.ts";
2222
import { suite, test, before, beforeEach, after } from "node:test";
2323
import { S3Client } from "@bradenmacdonald/s3-lite-client";
24-
import { averageMedal, styleText } from "../svc/util/utility.ts";
24+
import { averageMedal, styleText, wilsonScore } from "../svc/util/utility.ts";
2525
import { addReliableJob } from "../svc/store/queue.ts";
2626

2727
const { RETRIEVER_HOST, POSTGRES_URL } = config;
@@ -181,6 +181,12 @@ suite(styleText("blue", "AVERAGE MEDAL"), async () => {
181181
const test2 = [80, 80, 80];
182182
assert.equal(averageMedal(test2), 75);
183183
});
184+
test("wilsonScore gives the matchups page advantage", () => {
185+
// same as the wilsonScore the web matchups page uses
186+
assert.equal(Math.round(wilsonScore(38, 79) * 100), 39);
187+
assert.equal(Math.round(wilsonScore(39, 71) * 100), 45);
188+
assert.equal(wilsonScore(0, 10), 0);
189+
});
184190
});
185191

186192
suite(styleText("blue", "API MANAGEMENT"), async () => {

0 commit comments

Comments
 (0)