# USQ Official Standings Algorithm

## Full Algorithm

$\mathbf{score} = \left( \mathbf{SWIM_{scaled}} \times \frac{2*opp\_w\% + opp\_opp\_w\%}{3} \times \frac{Win\% + 1}{2} \right) \\ \times \begin{cases} \frac{\sqrt{Games}}{2.25} &\text{if } Games < 5 \\ 1 &\text{if } Games \geq 5 \end{cases} \\ \times \begin{cases} 1/3 &\text{if } Opponents = 1 \\ 2/3 &\text{if } Opponents = 2 \\ 1 &\text{if } Opponents \geq 3 \end{cases} \\ \times \begin{cases} 0.5 &\text{if } Events = 1 \\ 1 &\text{if } Events \geq 2 \end{cases}$

## The Breakdown

### Calculate Performance

$\mathbf{P} = QuaffPoints_\mathrm{winner} - QuaffPoints_\mathrm{loser}$
P is essentially point differential, without the final snitch of the game. If a game goes into overtime, the regulation-time snitch gets counted as if it were quaffle points, and if a game goes into double overtime, any snitch in the first overtime is also counted as if it were quaffle points.
$\mathbf{P_{adj}} = \mathrm{min}(\mathbf{P},80) + \sqrt{\mathrm{max}(\mathbf{P}-80,0)}$
Adjusted P is a way of diminishing the returns on teams running up the score. If the differential score is over 80 points, then every 10 points after 80 is really the square root of those points. So if P is less than 80, say 50, then min(50,80) = 50 and $\sqrt{\mathrm{max}(50-80,0)} \rightarrow \sqrt{\mathrm{max}(-30,0)} \rightarrow \sqrt{0} = 0$. So 50+0 =50 points. If the differential were, say, 100, then min(100,80) = 80, and $\sqrt{\mathrm{max}(100-80,0)} \rightarrow \sqrt{\mathrm{max}(20,0)} \rightarrow \sqrt{20}$. So Adjusted P becomes 80 + sqrt(20) at that point. Any points above 80 are square-rooted to discourage running up the score.
$\mathbf{SWIM} = \mathbf{P_{adj}}+ \left[ \begin{cases} 30 & \text{if winner caught snitch}\\ 0 & \text{else } \end{cases} \times \begin{cases} e^{-0.033\times(\mathbf{P_{adj}}-20)} & \text{if } \mathbf{P_{adj}} \geq 30 \\ 1 & \text{if } \mathbf{P_{adj}} \lt 30 \end{cases} \right]$
SWIM or Snitch When It Matters is simply P adjusted, if the winner did not catch the snitch. If the winner did catch the snitch, and the game was close enough to go either-way depending on who caught it (P of less than 30) then it includes all 30 points from the snatch. The snitch mattered for that victory. However, if the winner was ahead by 30 or more points, meaning the snitch was not the deciding factor in the victory, then only a portion of those snitch points are awarded (respective to how big the point gap was). SWIM becomes negative for the team that lost.
$\mathbf{SWIM_{scaled}} = \mathbf{SWIM} - \mathrm{min}(\mathbf{SWIM})$
Scaled SWIM is calculated by adding the absolute value of the lowest value to everyone's std(SWIM). Therefore the team with the lowest value will end up with 0, and everyone's score above that becomes a positive number, but all shifted by the same amount. This is done so that SoS times this value will always be detrimental when there are low SoS values (small SoS times a negative number would be beneficial to the team).
$\mathbf{Strength\ of\ Schedule\ (SoS)} = \frac{2*opp\_w\% + opp\_opp\_w\%}{3}$
Strength of Schedule is the average win percentages of your opponents and your opponents' opponents. Your direct opponents are weighted to be more important to your score than your transitive opponents. When calculating the win percentage of your opponents, games against your team are removed from the calculation. The average of your opponents' win percentage is weighted based on how many times your team has played them.
$Win\% = \frac{Wins}{Games}$
Win% is simply your team's wins divided by the number of games your team has played.
$\mathbf{Adjusted\ Win\%} = \frac{Win\% + 1}{2}$
Adjusted Win% is calculated by adding 1 to your team's win percentage and then halving the sum. This effectively scales win percentage from 0.5 to 1 (instead of 0 to 1) so teams aren't overly penalized for playing a tough schedule. It also permits differentiation between winless teams when this factor is used as a multiplier.
$\mathbf{performance} = \mathbf{SWIM_{scaled}} \times \mathbf{SoS} \times \mathbf{Adjusted\ Win\%}$
Performance is simply measured by weighting SWIM by Strength of Schedule, to prevent inflation in consistently playing unsuccessful teams, and then multiplying by Adjusted Win%.

### Calculate Penalties/Modifiers

$\mathbf{GamePenalty} = \begin{cases} \frac{\sqrt{Games}}{2.25} &\text{if } Games < 5 \\ 1 &\text{if } Games \geq 5 \end{cases}$
The Game Penalty is a penalty for playing less than 5 games in a single season. It does not matter whether the games for this requirement are part of a tournament or not. A game always counts as 1 game.
$\mathbf{OppPenalty} = \begin{cases} 1/3 &\text{if } Opponents = 1 \\ 2/3 &\text{if } Opponents = 2 \\ 1 &\text{if } Opponents \geq 3 \end{cases}$
The Opponents Penalty penalizes teams that do not play more than unique 2 opponents in a season. The penalty is linear with the amount of teams played. Any amount of teams 3 or above simply results in no penalty. Your score is cut into a third for only playing one team, two thirds for two teams. Full credit otherwise.
$\mathbf{EventPenalty} = \begin{cases} 0.5 &\text{if } Events = 1 \\ 1 &\text{if } Events \geq 2 \end{cases}$
The Event Penalty discourages playing all games at once. Playing a series of games in a tournament only amounts to 1 event. Playing two tournaments in a season accounts for two events. Every regular season game outside of tournaments (e.g. dual meets) count as an event each. Just playing 1 game against another school in a head-to-head matchup counts as 1 event.
$\mathbf{modifiers} = \mathbf{GamePenalty} \times \mathbf{OppPenalty} \times \mathbf{EventPenalty}$
The modifiers metric is the combination of each penalty. Having only 1 event and only playing two teams (assuming you played 5 games) is a half of two thirds = one sixth = 0.1666.. If you satisfied all the requirements in the penalties this number will be 1. This number can be as low as 0.074

### Put it all together

$\mathbf{score} = \mathbf{performance} \times \mathbf{modifiers}$
Your team's Final Score is your performance times combined penalties.

Where:

Games is the number of officially ranked games played in the current season

Opponents is the number of unique opponents faced in the current season

Events is the number of Events attended. Tournaments count as 1 event. So 5 games in 1 tournament is just 1 event. Regular season games a.k.a. dual meets are also 1 event each. Two dual meets = two events. Even if they are on the same day.