Skip to content

Commit

Permalink
implement my own percentile function so I can drop the dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Oct 29, 2020
1 parent ac9e2b1 commit 5231f8f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
1 change: 0 additions & 1 deletion elm.json
Expand Up @@ -13,7 +13,6 @@
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"elm/random": "1.0.0",
"f0i/statistics": "2.0.0",
"ohanhi/keyboard": "2.0.1",
"rtfeldman/elm-css": "16.1.0",
"tesk9/accessible-html-with-css": "2.1.1"
Expand Down
41 changes: 39 additions & 2 deletions src/League.elm
Expand Up @@ -16,7 +16,6 @@ module League exposing

import Dict exposing (Dict)
import Elo
import FStatistics
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Player exposing (Player)
Expand Down Expand Up @@ -294,7 +293,7 @@ kFactor (League league) player =
p90 =
Dict.values league.players
|> List.map .rating
|> FStatistics.percentileInt 0.9
|> percentile 0.9
|> Maybe.withDefault Elo.initialRating
in
if player.matches < playInMatches then
Expand All @@ -313,6 +312,44 @@ kFactor (League league) player =
Elo.sensitiveKFactor


{-| Not 100% correct because of the rounding but good enough for our
purposes.
-}
percentile : Float -> List Int -> Maybe Int
percentile pct items =
let
sorted =
List.sort items

offset =
pct * toFloat (List.length items)

index =
floor offset
in
if toFloat index == offset then
sorted
|> List.drop (index - 1)
|> List.head

else
let
fractionalPart =
offset - toFloat index

betweenThese =
sorted
|> List.drop (index - 1)
|> List.take 2
in
case betweenThese of
[ a, b ] ->
Just (round (toFloat a + fractionalPart * (toFloat b - toFloat a)))

_ ->
Nothing


{-| -}
higherRankedPlayer : Player -> Player -> Player
higherRankedPlayer a b =
Expand Down

0 comments on commit 5231f8f

Please sign in to comment.