Skip to content

Commit

Permalink
prettier reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
philihp committed Apr 19, 2023
1 parent d1429ce commit 82efe56
Show file tree
Hide file tree
Showing 25 changed files with 54 additions and 149 deletions.
10 changes: 2 additions & 8 deletions src/__tests__/predict-draw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,11 @@ describe('predictDraw', () => {

it('predicts draw for two teams', () => {
expect.assertions(1)
expect(predictDraw([team1, team2])).toBeCloseTo(
0.1260703143635969,
precision
)
expect(predictDraw([team1, team2])).toBeCloseTo(0.1260703143635969, precision)
})

it('predicts draw for three asymmetric teams', () => {
expect.assertions(1)
expect(predictDraw([team1, team2, [a1], [a2], [b1]])).toBeCloseTo(
0.04322122887507519,
precision
)
expect(predictDraw([team1, team2, [a1], [a2], [b1]])).toBeCloseTo(0.04322122887507519, precision)
})
})
9 changes: 3 additions & 6 deletions src/__tests__/rate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,9 @@ describe('rate', () => {

it('four-way-tie with newbies', () => {
expect.assertions(1)
const [[a], [b], [c], [d]] = rate(
[[rating()], [rating()], [rating()], [rating()]],
{
rank: [1, 1, 1, 1],
}
)
const [[a], [b], [c], [d]] = rate([[rating()], [rating()], [rating()], [rating()]], {
rank: [1, 1, 1, 1],
})
expect([a, b, c, d]).toStrictEqual([
{ mu: 25, sigma: 8.263160757613477 },
{ mu: 25, sigma: 8.263160757613477 },
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/util/rankings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ describe('util#rankings', () => {
})
it('ranks sparse scores', () => {
expect.assertions(1)
expect(rankings([a, b, c, d, e], [14, 32, 47, 47, 48])).toStrictEqual([
0, 1, 2, 2, 4,
])
expect(rankings([a, b, c, d, e], [14, 32, 47, 47, 48])).toStrictEqual([0, 1, 2, 2, 4])
})
})
3 changes: 1 addition & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Options } from './types'
export const z = (options: Options) => options?.z ?? 3
export const mu = (options: Options) => options?.mu ?? 25
export const tau = (options: Options) => options?.tau ?? mu(options) / 300
export const sigma = (options: Options) =>
options?.sigma ?? mu(options) / z(options)
export const sigma = (options: Options) => options?.sigma ?? mu(options) / z(options)

export const epsilon = (options: Options) => options?.epsilon ?? 0.0001
export const beta = (options: Options) => options?.beta ?? sigma(options) / 2
Expand Down
11 changes: 4 additions & 7 deletions src/models/__tests__/bradley-terry-full-series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ describe('bradleyTerryFull#series', () => {
const p20 = rating()
const p30 = rating()
const p40 = rating()
const [[p01], [p11], [p21], [p31], [p41]] = rate(
[[p00], [p10], [p20], [p30], [p40]],
{
model,
score: [9, 7, 7, 5, 5],
}
)
const [[p01], [p11], [p21], [p31], [p41]] = rate([[p00], [p10], [p20], [p30], [p40]], {
model,
score: [9, 7, 7, 5, 5],
})
const p02 = p01
const p32 = p31
const [[p42], [p22], [p12]] = rate([[p41], [p21], [p11]], {
Expand Down
4 changes: 1 addition & 3 deletions src/models/__tests__/bradley-terry-full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ describe('bradleyTerryFull', () => {

it('can use a custom gamma with k=5', () => {
expect.assertions(1)
expect(
rate([team1, team1, team1, team1, team1], { gamma: (_, k) => 1 / k })
).toStrictEqual([
expect(rate([team1, team1, team1, team1, team1], { gamma: (_, k) => 1 / k })).toStrictEqual([
[{ mu: 35.5409255338946, sigma: 7.993052538854532 }],
[{ mu: 30.2704627669473, sigma: 7.993052538854532 }],
[{ mu: 25, sigma: 7.993052538854532 }],
Expand Down
11 changes: 4 additions & 7 deletions src/models/__tests__/bradley-terry-part-series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ describe('bradleyTerryPart#series', () => {
const p20 = rating()
const p30 = rating()
const p40 = rating()
const [[p01], [p11], [p21], [p31], [p41]] = rate(
[[p00], [p10], [p20], [p30], [p40]],
{
model,
score: [9, 7, 7, 5, 5],
}
)
const [[p01], [p11], [p21], [p31], [p41]] = rate([[p00], [p10], [p20], [p30], [p40]], {
model,
score: [9, 7, 7, 5, 5],
})
const p02 = p01
const p32 = p31
const [[p42], [p22], [p12]] = rate([[p41], [p21], [p11]], {
Expand Down
8 changes: 1 addition & 7 deletions src/models/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { rate, rating } from '../..'
import {
bradleyTerryFull,
bradleyTerryPart,
plackettLuce,
thurstoneMostellerFull,
thurstoneMostellerPart,
} from '..'
import { bradleyTerryFull, bradleyTerryPart, plackettLuce, thurstoneMostellerFull, thurstoneMostellerPart } from '..'

// numbers in this test suite come from rank-1.02 on a 3-way
// these differ in that it uses an epsilon of 0.1
Expand Down
11 changes: 4 additions & 7 deletions src/models/__tests__/plackett-luce-series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ describe('plackettLuce#series', () => {
const p20 = rating()
const p30 = rating()
const p40 = rating()
const [[p01], [p11], [p21], [p31], [p41]] = rate(
[[p00], [p10], [p20], [p30], [p40]],
{
model,
score: [9, 7, 7, 5, 5],
}
)
const [[p01], [p11], [p21], [p31], [p41]] = rate([[p00], [p10], [p20], [p30], [p40]], {
model,
score: [9, 7, 7, 5, 5],
})
const p02 = p01
const p32 = p31
const [[p42], [p22], [p12]] = rate([[p41], [p21], [p11]], {
Expand Down
13 changes: 5 additions & 8 deletions src/models/__tests__/thurstone-mosteller-full-series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ describe('thurstoneMostellerFull#series', () => {
const p20 = rating()
const p30 = rating()
const p40 = rating()
const [[p01], [p11], [p21], [p31], [p41]] = rate(
[[p00], [p10], [p20], [p30], [p40]],
{
model,
epsilon: 0.1,
score: [9, 7, 7, 5, 5],
}
)
const [[p01], [p11], [p21], [p31], [p41]] = rate([[p00], [p10], [p20], [p30], [p40]], {
model,
epsilon: 0.1,
score: [9, 7, 7, 5, 5],
})
const p02 = p01
const p32 = p31
const [[p42], [p22], [p12]] = rate([[p41], [p21], [p11]], {
Expand Down
15 changes: 6 additions & 9 deletions src/models/__tests__/thurstone-mosteller-part-series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ describe('thurstoneMostellerPart#series', () => {
const p20 = rating()
const p30 = rating()
const p40 = rating()
const [[p01], [p11], [p21], [p31], [p41]] = rate(
[[p00], [p10], [p20], [p30], [p40]],
{
model,
epsilon: 0.1,
gamma: () => 1,
score: [9, 7, 7, 5, 5],
}
)
const [[p01], [p11], [p21], [p31], [p41]] = rate([[p00], [p10], [p20], [p30], [p40]], {
model,
epsilon: 0.1,
gamma: () => 1,
score: [9, 7, 7, 5, 5],
})

expect(p01.mu).toBeCloseTo(27.108980741)
expect(p01.sigma).toBeCloseTo(8.063357519)
Expand Down
4 changes: 1 addition & 3 deletions src/models/__tests__/thurstone-mosteller-part.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ describe('thurstoneMostellerPart', () => {

it('can use a custom gamma with k=5', () => {
expect.assertions(1)
expect(
rate([team1, team1, team1, team1, team1], { gamma: (_, k) => 1 / k })
).toStrictEqual([
expect(rate([team1, team1, team1, team1, team1], { gamma: (_, k) => 1 / k })).toStrictEqual([
[{ mu: 27.102616738180256, sigma: 8.280111667130026 }],
[{ mu: 25, sigma: 8.226545690375827 }],
[{ mu: 25, sigma: 8.226545690375827 }],
Expand Down
4 changes: 1 addition & 3 deletions src/models/bradley-terry-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ const model: Model = (game: Rating[][], options: Options = {}) => {
const sigmaSq = sigma * sigma
return {
mu: mu + (sigmaSq / iSigmaSq) * iOmega,
sigma:
sigma *
Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
sigma: sigma * Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
}
})
})
Expand Down
4 changes: 1 addition & 3 deletions src/models/bradley-terry-part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ const model: Model = (game: Rating[][], options = {}) => {
const sigmaSq = sigma * sigma
return {
mu: mu + (sigmaSq / iSigmaSq) * iOmega,
sigma:
sigma *
Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
sigma: sigma * Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
}
})
})
Expand Down
8 changes: 1 addition & 7 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ import bradleyTerryPart from './bradley-terry-part'
import thurstoneMostellerFull from './thurstone-mosteller-full'
import thurstoneMostellerPart from './thurstone-mosteller-part'

export {
plackettLuce,
bradleyTerryFull,
bradleyTerryPart,
thurstoneMostellerFull,
thurstoneMostellerPart,
}
export { plackettLuce, bradleyTerryFull, bradleyTerryPart, thurstoneMostellerFull, thurstoneMostellerPart }
9 changes: 2 additions & 7 deletions src/models/plackett-luce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ const model: Model = (game: Rating[][], options: Options = {}) => {
.reduce(
([omega, delta], [_], q) => {
const quotient = iMuOverCe / sumQ[q]
return [
omega + (i === q ? 1 - quotient : -quotient) / a[q],
delta + (quotient * (1 - quotient)) / a[q],
]
return [omega + (i === q ? 1 - quotient : -quotient) / a[q], delta + (quotient * (1 - quotient)) / a[q]]
},
[0, 0]
)
Expand All @@ -32,9 +29,7 @@ const model: Model = (game: Rating[][], options: Options = {}) => {

return iTeam.map(({ mu, sigma }) => ({
mu: mu + (sigma ** 2 / iSigmaSq) * iOmega,
sigma:
sigma *
Math.sqrt(Math.max(1 - (sigma ** 2 / iSigmaSq) * iDelta, EPSILON)),
sigma: sigma * Math.sqrt(Math.max(1 - (sigma ** 2 / iSigmaSq) * iDelta, EPSILON)),
}))
})
}
Expand Down
10 changes: 3 additions & 7 deletions src/models/thurstone-mosteller-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ const model: Model = (game: Rating[][], options: Options = {}) => {
if (qRank === iRank) {
return [
omega + sigSqToCiq * vt(deltaMu, EPSILON / ciq),
delta +
((iGamma * sigSqToCiq) / ciq) * wt(deltaMu, EPSILON / ciq),
delta + ((iGamma * sigSqToCiq) / ciq) * wt(deltaMu, EPSILON / ciq),
]
}

const sign = qRank > iRank ? 1 : -1
return [
omega + sign * sigSqToCiq * v(sign * deltaMu, EPSILON / ciq),
delta +
((iGamma * sigSqToCiq) / ciq) * w(sign * deltaMu, EPSILON / ciq),
delta + ((iGamma * sigSqToCiq) / ciq) * w(sign * deltaMu, EPSILON / ciq),
]
},
[0, 0]
Expand All @@ -41,9 +39,7 @@ const model: Model = (game: Rating[][], options: Options = {}) => {
const sigmaSq = sigma * sigma
return {
mu: mu + (sigmaSq / iSigmaSq) * iOmega,
sigma:
sigma *
Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
sigma: sigma * Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
}
})
})
Expand Down
7 changes: 2 additions & 5 deletions src/models/thurstone-mosteller-part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const model: Model = (game: Rating[][], options: Options = {}) => {
const sign = qRank > iRank ? 1 : -1
return [
omega + sign * sigSqToCiq * v(sign * deltaMu, EPSILON / ciq),
delta +
((iGamma * sigSqToCiq) / ciq) * w(sign * deltaMu, EPSILON / ciq),
delta + ((iGamma * sigSqToCiq) / ciq) * w(sign * deltaMu, EPSILON / ciq),
]
},
[0, 0]
Expand All @@ -40,9 +39,7 @@ const model: Model = (game: Rating[][], options: Options = {}) => {
const sigmaSq = sigma * sigma
return {
mu: mu + (sigmaSq / iSigmaSq) * iOmega,
sigma:
sigma *
Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
sigma: sigma * Math.sqrt(Math.max(1 - (sigmaSq / iSigmaSq) * iDelta, EPSILON)),
}
})
})
Expand Down
12 changes: 3 additions & 9 deletions src/predict-draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const predictWin = (teams: Team[], options: Options = {}) => {

const denom = (n * (n - 1)) / (n > 2 ? 1 : 2)
const teamRatings = teamRating(teams)
const drawMargin =
Math.sqrt(flatten(teams).length) * BETA * phiMajorInverse((1 + 1 / n) / 2)
const drawMargin = Math.sqrt(flatten(teams).length) * BETA * phiMajorInverse((1 + 1 / n) / 2)

return (
Math.abs(
Expand All @@ -24,13 +23,8 @@ const predictWin = (teams: Team[], options: Options = {}) => {
teamRatings
.filter((_, q) => i !== q)
.map(([muB, sigmaSqB]) => {
const sigmaBar = Math.sqrt(
n * BETASQ + sigmaSqA ** 2 + sigmaSqB ** 2
)
return (
phiMajor((drawMargin - muA + muB) / sigmaBar) -
phiMajor((muA - muB - drawMargin) / sigmaBar)
)
const sigmaBar = Math.sqrt(n * BETASQ + sigmaSqA ** 2 + sigmaSqB ** 2)
return phiMajor((drawMargin - muA + muB) / sigmaBar) - phiMajor((muA - muB - drawMargin) / sigmaBar)
})
)
.flat()
Expand Down
6 changes: 1 addition & 5 deletions src/predict-win.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ const predictWin = (teams: Team[], options: Options = {}) => {
([muA, sigmaSqA], i) =>
teamRatings
.filter((_, q) => i !== q)
.map(([muB, sigmaSqB]) =>
phiMajor(
(muA - muB) / Math.sqrt(n * BETASQ + sigmaSqA ** 2 + sigmaSqB ** 2)
)
)
.map(([muB, sigmaSqB]) => phiMajor((muA - muB) / Math.sqrt(n * BETASQ + sigmaSqA ** 2 + sigmaSqB ** 2)))
.reduce(sum, 0) / denom
)
}
Expand Down
5 changes: 1 addition & 4 deletions src/rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ const rate = (teams: Team[], options: Options = {}): Team[] => {
}

// if rank provided, use it, otherwise transition scores and use that
const rank =
options.rank ??
options.score?.map((points) => -points) ??
range(1, teams.length + 1)
const rank = options.rank ?? options.score?.map((points) => -points) ?? range(1, teams.length + 1)

const [orderedTeams, tenet] = unwind(rank, processedTeams)
const newRatings = model(orderedTeams, {
Expand Down
5 changes: 1 addition & 4 deletions src/rating.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { mu as defaultMu, sigma as defaultSigma } from './constants'
import { Options, Rating } from './types'

const rating = (
init?: { mu?: number; sigma?: number },
options: Options = {}
): Rating => ({
const rating = (init?: { mu?: number; sigma?: number }, options: Options = {}): Rating => ({
mu: init?.mu ?? defaultMu(options),
sigma: init?.sigma ?? defaultSigma({ ...options, mu: init?.mu }),
})
Expand Down
3 changes: 1 addition & 2 deletions src/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ export const wt = (x: number, t: number) => {
const b = phiMajor(t - xx) - phiMajor(-t - xx)
return b < Number.EPSILON
? 1.0
: ((t - xx) * phiMinor(t - xx) + (t + xx) * phiMinor(-t - xx)) / b +
vt(x, t) * vt(x, t)
: ((t - xx) * phiMinor(t - xx) + (t + xx) * phiMinor(-t - xx)) / b + vt(x, t) * vt(x, t)
}
9 changes: 1 addition & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ export type Team = Rating[]

export type Rank = number

export type Gamma = (
c: number,
k: number,
mu: number,
sigmaSq: number,
team: Rating[],
qRank: number
) => number
export type Gamma = (c: number, k: number, mu: number, sigmaSq: number, team: Rating[], qRank: number) => number

// eslint-disable-next-line no-use-before-define
export type Model = (teams: Team[], options?: Options) => Team[]
Expand Down
Loading

0 comments on commit 82efe56

Please sign in to comment.