From 71e06435bfbf0305fba00731cbbd6d875e2a08dd Mon Sep 17 00:00:00 2001 From: avitex Date: Wed, 29 Nov 2017 13:10:02 +1100 Subject: [PATCH] Add tests and example for `win_probability` --- lib/glicko.ex | 7 +++++++ test/glicko_test.exs | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/glicko.ex b/lib/glicko.ex index c8d3c1e..b2435fc 100644 --- a/lib/glicko.ex +++ b/lib/glicko.ex @@ -21,6 +21,13 @@ defmodule Glicko do iex> Glicko.new_rating(player, [], [system_constant: 0.5]) {1.5e3, 200.27141669877065} + Calculate the probability of a player winning against an opponent. + + iex> player = Player.new_v1 + iex> opponent = Player.new_v1 + iex> Glicko.win_probability(player, opponent) + 0.5 + """ alias __MODULE__.{ diff --git a/test/glicko_test.exs b/test/glicko_test.exs index dc04122..2785f47 100644 --- a/test/glicko_test.exs +++ b/test/glicko_test.exs @@ -35,4 +35,16 @@ defmodule GlickoTest do assert_in_delta Player.rating_deviation(player), @valid_player_rating_deviation_after_no_results, 1.0e-4 end + + test "win probability with same ratings" do + assert Glicko.win_probability(Player.new_v1, Player.new_v1) == 0.5 + end + + test "win probability with better opponent" do + assert Glicko.win_probability(Player.new_v1([rating: 1500]), Player.new_v1([rating: 1600])) < 0.5 + end + + test "win probability with better player" do + assert Glicko.win_probability(Player.new_v1([rating: 1600]), Player.new_v1([rating: 1500])) > 0.5 + end end