From 6bafa2be0b0985aa5cbe6711cd5f50c8ee3fe64a Mon Sep 17 00:00:00 2001 From: avitex Date: Wed, 15 Nov 2017 23:32:47 +1100 Subject: [PATCH] Add rating_interval --- lib/glicko/player.ex | 21 +++++++++++++++++++++ test/player_test.exs | 10 ++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/glicko/player.ex b/lib/glicko/player.ex index 4ef030b..e30da2e 100644 --- a/lib/glicko/player.ex +++ b/lib/glicko/player.ex @@ -107,6 +107,27 @@ defmodule Glicko.Player do volatility: volatility, ]) + @doc """ + A convenience function for summarizing a player's strength as a 95% + confidence interval. + + The lowest value in the interval is the player's rating minus twice the RD, + and the highest value is the player's rating plus twice the RD. + The volatility measure does not appear in the calculation of this interval. + + An example would be if a player's rating is 1850 and the RD is 50, + the interval would range from 1750 to 1950. We would then say that we're 95% + confident that the player's actual strength is between 1750 and 1950. + + When a player has a low RD, the interval would be narrow, so that we would + be 95% confident about a player’s strength being in a small interval of values. + """ + @spec rating_interval(player :: t) :: {rating_low :: float, rating_high :: float} + def rating_interval(player), do: { + player.rating - player.rating_deviation * 2, + player.rating + player.rating_deviation * 2, + } + @doc """ Scales a players rating. """ diff --git a/test/player_test.exs b/test/player_test.exs index 03c3b21..1718380 100644 --- a/test/player_test.exs +++ b/test/player_test.exs @@ -57,4 +57,14 @@ defmodule Glicko.PlayerTest do test "scale rating deviation v2 -> v1" do assert_in_delta Player.scale_rating_deviation_to(1.0, :v1), 173.7178, 0.1 end + + test "rating interval" do + assert {rating_low, rating_high} = + [rating: 1850, rating_deviation: 50] + |> Player.new_v2 + |> Player.rating_interval + + assert_in_delta rating_low, 1750, 0.1 + assert_in_delta rating_high, 1950, 0.1 + end end