All files / src/utils powerRatingModel.js

100% Statements 35/35
100% Branches 11/11
100% Functions 3/3
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 34x 34x 34x 18x 18x   1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x  
/**
 * The Power Rating's per-match update (power-rating.md "The model"), for the
 * How it works screen (SB-1811). The backend recompute job is the rating of
 * record; this copy exists so the worked example on screen is computed from
 * the formula it states rather than typed in, and a test holds both to it.
 */
 
export const START = 1500;
export const HOME_EDGE = 50;
export const K = 30;
export const K_PROVISIONAL = 60;
export const PROVISIONAL_MATCHES = 8;
 
/** Share of the points a team is expected to take; a draw counts half. */
export const expectedScore = (rTeam, rOpp, home = 0) =>
  1 / (1 + 10 ** (-(rTeam - rOpp + home) / 400));
 
/** G: 1 for a draw or 1-goal margin, 1.5 for 2 goals, 1.75 for 3 or more. */
export const marginFactor = goalDiff => {
  const margin = Math.abs(goalDiff);
  if (margin >= 3) return 1.75;
  if (margin === 2) return 1.5;
  return 1;
};
 
/** K × G × (S − E) for one side of a match, unrounded. */
export const ratingChange = ({
  rating,
  opponent,
  home = 0,
  goalsFor,
  goalsAgainst,
  k = K,
}) => {
  const diff = goalsFor - goalsAgainst;
  const s = diff > 0 ? 1 : diff < 0 ? 0 : 0.5;
  const e = expectedScore(rating, opponent, home);
  return k * marginFactor(diff) * (s - e);
};