mirror of
https://github.com/avitex/elixir-glicko
synced 2025-01-15 10:49:58 +00:00
Compare commits
9 Commits
b7b60afbe3
...
d934a2e30c
Author | SHA1 | Date | |
---|---|---|---|
|
d934a2e30c | ||
|
76e4a7872c | ||
|
45066f946b | ||
|
c1089996d3 | ||
|
9cb997e2c1 | ||
|
172f8dd8d0 | ||
|
636d84b4a9 | ||
|
a1f0c2f337 | ||
|
7297ff6318 |
17
.github/dependabot.yml
vendored
Normal file
17
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: mix
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: daily
|
||||||
|
time: "19:30"
|
||||||
|
open-pull-requests-limit: 10
|
||||||
|
ignore:
|
||||||
|
- dependency-name: ex_doc
|
||||||
|
versions:
|
||||||
|
- 0.23.0
|
||||||
|
- 0.24.0
|
||||||
|
- 0.24.1
|
||||||
|
- dependency-name: credo
|
||||||
|
versions:
|
||||||
|
- 1.5.4
|
38
.github/workflows/elixir.yml
vendored
Normal file
38
.github/workflows/elixir.yml
vendored
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
# This workflow uses actions that are not certified by GitHub.
|
||||||
|
# They are provided by a third-party and are governed by
|
||||||
|
# separate terms of service, privacy policy, and support
|
||||||
|
# documentation.
|
||||||
|
|
||||||
|
name: Elixir CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ "master" ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build and test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Set up Elixir
|
||||||
|
uses: erlef/setup-beam@61e01a43a562a89bfc54c7f9a378ff67b03e4a21 # v1.16.0
|
||||||
|
with:
|
||||||
|
elixir-version: '1.15.2' # [Required] Define the Elixir version
|
||||||
|
otp-version: '26.0' # [Required] Define the Erlang/OTP version
|
||||||
|
- name: Restore dependencies cache
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: deps
|
||||||
|
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
|
||||||
|
restore-keys: ${{ runner.os }}-mix-
|
||||||
|
- name: Install dependencies
|
||||||
|
run: mix deps.get
|
||||||
|
- name: Run tests
|
||||||
|
run: mix test
|
@ -14,6 +14,6 @@ Add `glicko` to your list of dependencies in `mix.exs`:
|
|||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
def deps do
|
def deps do
|
||||||
[{:glicko, "~> 0.7.0"}]
|
[{:glicko, "~> 0.8.1"}]
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
@ -13,13 +13,13 @@ defmodule Glicko do
|
|||||||
...> Result.new(Player.new_v1([rating: 1700, rating_deviation: 300]), :loss)]
|
...> Result.new(Player.new_v1([rating: 1700, rating_deviation: 300]), :loss)]
|
||||||
iex> player = Player.new_v1([rating: 1500, rating_deviation: 200])
|
iex> player = Player.new_v1([rating: 1500, rating_deviation: 200])
|
||||||
iex> Glicko.new_rating(player, results, [system_constant: 0.5])
|
iex> Glicko.new_rating(player, results, [system_constant: 0.5])
|
||||||
{1464.0506705393013, 151.51652412385727}
|
%Player.V1{rating: 1464.0506705393013, rating_deviation: 151.51652412385727}
|
||||||
|
|
||||||
Get a player's new rating when they haven't played within a rating period.
|
Get a player's new rating when they haven't played within a rating period.
|
||||||
|
|
||||||
iex> player = Player.new_v1([rating: 1500, rating_deviation: 200])
|
iex> player = Player.new_v1([rating: 1500, rating_deviation: 200])
|
||||||
iex> Glicko.new_rating(player, [], [system_constant: 0.5])
|
iex> Glicko.new_rating(player, [], [system_constant: 0.5])
|
||||||
{1.5e3, 200.27141669877065}
|
%Player.V1{rating: 1.5e3, rating_deviation: 200.27141669877065}
|
||||||
|
|
||||||
Calculate the probability of a player winning against an opponent.
|
Calculate the probability of a player winning against an opponent.
|
||||||
|
|
||||||
@ -117,24 +117,33 @@ defmodule Glicko do
|
|||||||
Player.t()
|
Player.t()
|
||||||
def new_rating(player, results, opts \\ [])
|
def new_rating(player, results, opts \\ [])
|
||||||
|
|
||||||
def new_rating(player, results, opts) when tuple_size(player) == 3 do
|
def new_rating(%Player.V2{} = player, results, opts) do
|
||||||
do_new_rating(player, results, opts)
|
do_new_rating(player, results, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_rating(player, results, opts) when tuple_size(player) == 2 do
|
def new_rating(%Player.V1{} = player, results, opts) do
|
||||||
player
|
player
|
||||||
|> Player.to_v2()
|
|> Player.to_v2()
|
||||||
|> do_new_rating(results, opts)
|
|> do_new_rating(results, opts)
|
||||||
|> Player.to_v1()
|
|> Player.to_v1()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_new_rating({player_r, player_pre_rd, player_v}, [], _) do
|
defp do_new_rating(%Player.V2{} = player, [], _) do
|
||||||
player_post_rd = calc_player_post_base_rd(:math.pow(player_pre_rd, 2), player_v)
|
player_post_rd =
|
||||||
|
calc_player_post_base_rd(:math.pow(player.rating_deviation, 2), player.volatility)
|
||||||
|
|
||||||
{player_r, player_post_rd, player_v}
|
%{player | rating_deviation: player_post_rd}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_new_rating({player_pre_r, player_pre_rd, player_pre_v}, results, opts) do
|
defp do_new_rating(
|
||||||
|
%Player.V2{
|
||||||
|
rating: player_pre_r,
|
||||||
|
rating_deviation: player_pre_rd,
|
||||||
|
volatility: player_pre_v
|
||||||
|
},
|
||||||
|
results,
|
||||||
|
opts
|
||||||
|
) do
|
||||||
sys_const = Keyword.get(opts, :system_constant, @default_system_constant)
|
sys_const = Keyword.get(opts, :system_constant, @default_system_constant)
|
||||||
conv_tol = Keyword.get(opts, :convergence_tolerance, @default_convergence_tolerance)
|
conv_tol = Keyword.get(opts, :convergence_tolerance, @default_convergence_tolerance)
|
||||||
|
|
||||||
@ -184,7 +193,11 @@ defmodule Glicko do
|
|||||||
player_post_rd = calc_new_player_rating_deviation(player_post_base_rd, variance_est)
|
player_post_rd = calc_new_player_rating_deviation(player_post_base_rd, variance_est)
|
||||||
player_post_r = calc_new_player_rating(results_effect, player_pre_r, player_post_rd)
|
player_post_r = calc_new_player_rating(results_effect, player_pre_r, player_post_rd)
|
||||||
|
|
||||||
{player_post_r, player_post_rd, player_post_v}
|
%Player.V2{
|
||||||
|
rating: player_post_r,
|
||||||
|
rating_deviation: player_post_rd,
|
||||||
|
volatility: player_post_v
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp result_calculations(results, player_pre_r) do
|
defp result_calculations(results, player_pre_r) do
|
||||||
@ -268,7 +281,7 @@ defmodule Glicko do
|
|||||||
fc = calc_f(alpha, delta, player_pre_rd_sq, variance_est, sys_const, c)
|
fc = calc_f(alpha, delta, player_pre_rd_sq, variance_est, sys_const, c)
|
||||||
|
|
||||||
{a, fa} =
|
{a, fa} =
|
||||||
if fc * fb < 0 do
|
if fc * fb <= 0 do
|
||||||
{b, fb}
|
{b, fb}
|
||||||
else
|
else
|
||||||
{a, fa / 2}
|
{a, fa / 2}
|
||||||
|
@ -7,27 +7,27 @@ defmodule Glicko.Player do
|
|||||||
Create a *v1* player with the default values for an unrated player.
|
Create a *v1* player with the default values for an unrated player.
|
||||||
|
|
||||||
iex> Player.new_v1
|
iex> Player.new_v1
|
||||||
{1.5e3, 350.0}
|
%Player.V1{rating: 1.5e3, rating_deviation: 350.0}
|
||||||
|
|
||||||
Create a *v2* player with the default values for an unrated player.
|
Create a *v2* player with the default values for an unrated player.
|
||||||
|
|
||||||
iex> Player.new_v2
|
iex> Player.new_v2
|
||||||
{0.0, 2.014761872416068, 0.06}
|
%Player.V2{rating: 0.0, rating_deviation: 2.014761872416068, volatility: 0.06}
|
||||||
|
|
||||||
Create a player with custom values.
|
Create a player with custom values.
|
||||||
|
|
||||||
iex> Player.new_v2([rating: 3.0, rating_deviation: 2.0, volatility: 0.05])
|
iex> Player.new_v2(rating: 3.0, rating_deviation: 2.0, volatility: 0.05)
|
||||||
{3.0, 2.0, 0.05}
|
%Player.V2{rating: 3.0, rating_deviation: 2.0, volatility: 0.05}
|
||||||
|
|
||||||
Convert a *v2* player to a *v1*. Note this drops the volatility.
|
Convert a *v2* player to a *v1*. Note this drops the volatility.
|
||||||
|
|
||||||
iex> Player.new_v2 |> Player.to_v1
|
iex> Player.new_v2 |> Player.to_v1
|
||||||
{1.5e3, 350.0}
|
%Player.V1{rating: 1.5e3, rating_deviation: 350.0}
|
||||||
|
|
||||||
Convert a *v1* player to a *v2*.
|
Convert a *v1* player to a *v2*.
|
||||||
|
|
||||||
iex> Player.new_v1 |> Player.to_v2(0.06)
|
iex> Player.new_v1 |> Player.to_v2(0.06)
|
||||||
{0.0, 2.014761872416068, 0.06}
|
%Player.V2{rating: 0.0, rating_deviation: 2.014761872416068, volatility: 0.06}
|
||||||
|
|
||||||
Note calling `to_v1` with a *v1* player or likewise with `to_v2` and a *v2* player
|
Note calling `to_v1` with a *v1* player or likewise with `to_v2` and a *v2* player
|
||||||
will pass-through unchanged. The volatility arg in this case is ignored.
|
will pass-through unchanged. The volatility arg in this case is ignored.
|
||||||
@ -38,39 +38,54 @@ defmodule Glicko.Player do
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
defmodule V1 do
|
||||||
|
@initial_rating 1500.0
|
||||||
|
@initial_rating_deviation 350.0
|
||||||
|
|
||||||
|
@type t :: %__MODULE__{
|
||||||
|
rating: float(),
|
||||||
|
rating_deviation: float()
|
||||||
|
}
|
||||||
|
|
||||||
|
defstruct rating: @initial_rating,
|
||||||
|
rating_deviation: @initial_rating_deviation
|
||||||
|
end
|
||||||
|
|
||||||
|
defmodule V2 do
|
||||||
|
@magic_version_scale 173.7178
|
||||||
|
@magic_version_scale_rating 1500.0
|
||||||
|
|
||||||
|
@v1_initial_rating 1500.0
|
||||||
|
@v1_initial_rating_deviation 350.0
|
||||||
|
|
||||||
|
@initial_rating (@v1_initial_rating - @magic_version_scale_rating) / @magic_version_scale
|
||||||
|
@initial_rating_deviation @v1_initial_rating_deviation / @magic_version_scale
|
||||||
|
@initial_volatility 0.06
|
||||||
|
|
||||||
|
@type t :: %__MODULE__{
|
||||||
|
rating: float(),
|
||||||
|
rating_deviation: float(),
|
||||||
|
volatility: float()
|
||||||
|
}
|
||||||
|
|
||||||
|
defstruct rating: @initial_rating,
|
||||||
|
rating_deviation: @initial_rating_deviation,
|
||||||
|
volatility: @initial_volatility
|
||||||
|
end
|
||||||
|
|
||||||
@magic_version_scale 173.7178
|
@magic_version_scale 173.7178
|
||||||
@magic_version_scale_rating 1500.0
|
@magic_version_scale_rating 1500.0
|
||||||
|
|
||||||
@type t :: v1 | v2
|
@type t :: v1 | v2
|
||||||
|
|
||||||
@type v1 :: {rating, rating_deviation}
|
@type v1 :: V1.t()
|
||||||
@type v2 :: {rating, rating_deviation, volatility}
|
@type v2 :: V2.t()
|
||||||
|
|
||||||
@type version :: :v1 | :v2
|
@type version :: :v1 | :v2
|
||||||
@type rating :: float
|
@type rating :: float
|
||||||
@type rating_deviation :: float
|
@type rating_deviation :: float
|
||||||
@type volatility :: float
|
@type volatility :: float
|
||||||
|
|
||||||
@doc """
|
|
||||||
The recommended initial rating value for a new player.
|
|
||||||
"""
|
|
||||||
@spec initial_rating(version) :: rating
|
|
||||||
def initial_rating(_version = :v1), do: 1500.0
|
|
||||||
|
|
||||||
def initial_rating(_version = :v2) do
|
|
||||||
:v1 |> initial_rating |> scale_rating_to(:v2)
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
|
||||||
The recommended initial rating deviation value for a new player.
|
|
||||||
"""
|
|
||||||
@spec initial_rating_deviation(version) :: rating_deviation
|
|
||||||
def initial_rating_deviation(_version = :v1), do: 350.0
|
|
||||||
|
|
||||||
def initial_rating_deviation(_version = :v2) do
|
|
||||||
:v1 |> initial_rating_deviation |> scale_rating_deviation_to(:v2)
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
The recommended initial volatility value for a new player.
|
The recommended initial volatility value for a new player.
|
||||||
"""
|
"""
|
||||||
@ -84,10 +99,7 @@ defmodule Glicko.Player do
|
|||||||
"""
|
"""
|
||||||
@spec new_v1(rating: rating, rating_deviation: rating_deviation) :: v1
|
@spec new_v1(rating: rating, rating_deviation: rating_deviation) :: v1
|
||||||
def new_v1(opts \\ []) when is_list(opts) do
|
def new_v1(opts \\ []) when is_list(opts) do
|
||||||
{
|
struct(V1, opts)
|
||||||
Keyword.get(opts, :rating, initial_rating(:v1)),
|
|
||||||
Keyword.get(opts, :rating_deviation, initial_rating_deviation(:v1))
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -97,11 +109,7 @@ defmodule Glicko.Player do
|
|||||||
"""
|
"""
|
||||||
@spec new_v2(rating: rating, rating_deviation: rating_deviation, volatility: volatility) :: v2
|
@spec new_v2(rating: rating, rating_deviation: rating_deviation, volatility: volatility) :: v2
|
||||||
def new_v2(opts \\ []) when is_list(opts) do
|
def new_v2(opts \\ []) when is_list(opts) do
|
||||||
{
|
struct(V2, opts)
|
||||||
Keyword.get(opts, :rating, initial_rating(:v2)),
|
|
||||||
Keyword.get(opts, :rating_deviation, initial_rating_deviation(:v2)),
|
|
||||||
Keyword.get(opts, :volatility, initial_volatility())
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -112,12 +120,12 @@ defmodule Glicko.Player do
|
|||||||
Note the volatility field used in a v2 player will be lost in the conversion.
|
Note the volatility field used in a v2 player will be lost in the conversion.
|
||||||
"""
|
"""
|
||||||
@spec to_v1(player :: t) :: v1
|
@spec to_v1(player :: t) :: v1
|
||||||
def to_v1({rating, rating_deviation}), do: {rating, rating_deviation}
|
def to_v1(%V1{} = player), do: player
|
||||||
|
|
||||||
def to_v1({rating, rating_deviation, _}) do
|
def to_v1(%V2{rating: rating, rating_deviation: rating_deviation}) do
|
||||||
{
|
%V1{
|
||||||
rating |> scale_rating_to(:v1),
|
rating: scale_rating_to(rating, :v1),
|
||||||
rating_deviation |> scale_rating_deviation_to(:v1)
|
rating_deviation: scale_rating_deviation_to(rating_deviation, :v1)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -129,53 +137,52 @@ defmodule Glicko.Player do
|
|||||||
@spec to_v2(player :: t, volatility :: volatility) :: v2
|
@spec to_v2(player :: t, volatility :: volatility) :: v2
|
||||||
def to_v2(player, volatility \\ initial_volatility())
|
def to_v2(player, volatility \\ initial_volatility())
|
||||||
|
|
||||||
def to_v2({rating, rating_deviation, volatility}, _volatility),
|
def to_v2(%V1{rating: rating, rating_deviation: rating_deviation}, volatility) do
|
||||||
do: {rating, rating_deviation, volatility}
|
%V2{
|
||||||
|
rating: scale_rating_to(rating, :v2),
|
||||||
def to_v2({rating, rating_deviation}, volatility) do
|
rating_deviation: scale_rating_deviation_to(rating_deviation, :v2),
|
||||||
{
|
volatility: volatility
|
||||||
rating |> scale_rating_to(:v2),
|
|
||||||
rating_deviation |> scale_rating_deviation_to(:v2),
|
|
||||||
volatility
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_v2(%V2{} = player, _volatility) do
|
||||||
|
player
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
A version agnostic method for getting a player's rating.
|
A version agnostic method for getting a player's rating.
|
||||||
"""
|
"""
|
||||||
@spec rating(player :: t, as_version :: version | nil) :: rating
|
@spec rating(player :: t, as_version :: version | nil) :: rating
|
||||||
def rating(player, as_version \\ nil)
|
def rating(player, as_version \\ nil)
|
||||||
def rating({rating, _}, nil), do: rating
|
def rating(%_{rating: rating}, nil), do: rating
|
||||||
def rating({rating, _, _}, nil), do: rating
|
def rating(%V1{rating: rating}, :v1), do: rating
|
||||||
def rating({rating, _}, :v1), do: rating
|
def rating(%V2{rating: rating}, :v2), do: rating
|
||||||
def rating({rating, _}, :v2), do: rating |> scale_rating_to(:v2)
|
def rating(%V1{rating: rating}, :v2), do: rating |> scale_rating_to(:v2)
|
||||||
def rating({rating, _, _}, :v1), do: rating |> scale_rating_to(:v1)
|
def rating(%V2{rating: rating}, :v1), do: rating |> scale_rating_to(:v1)
|
||||||
def rating({rating, _, _}, :v2), do: rating
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
A version agnostic method for getting a player's rating deviation.
|
A version agnostic method for getting a player's rating deviation.
|
||||||
"""
|
"""
|
||||||
@spec rating_deviation(player :: t, as_version :: version | nil) :: rating_deviation
|
@spec rating_deviation(player :: t, as_version :: version | nil) :: rating_deviation
|
||||||
def rating_deviation(player, as_version \\ nil)
|
def rating_deviation(player, as_version \\ nil)
|
||||||
def rating_deviation({_, rating_deviation}, nil), do: rating_deviation
|
def rating_deviation(%V1{rating_deviation: rating_deviation}, nil), do: rating_deviation
|
||||||
def rating_deviation({_, rating_deviation, _}, nil), do: rating_deviation
|
def rating_deviation(%V2{rating_deviation: rating_deviation}, nil), do: rating_deviation
|
||||||
def rating_deviation({_, rating_deviation}, :v1), do: rating_deviation
|
def rating_deviation(%V1{rating_deviation: rating_deviation}, :v1), do: rating_deviation
|
||||||
|
def rating_deviation(%V2{rating_deviation: rating_deviation}, :v2), do: rating_deviation
|
||||||
|
|
||||||
def rating_deviation({_, rating_deviation}, :v2),
|
def rating_deviation(%V1{rating_deviation: rating_deviation}, :v2),
|
||||||
do: rating_deviation |> scale_rating_deviation_to(:v2)
|
do: rating_deviation |> scale_rating_deviation_to(:v2)
|
||||||
|
|
||||||
def rating_deviation({_, rating_deviation, _}, :v1),
|
def rating_deviation(%V2{rating_deviation: rating_deviation}, :v1),
|
||||||
do: rating_deviation |> scale_rating_deviation_to(:v1)
|
do: rating_deviation |> scale_rating_deviation_to(:v1)
|
||||||
|
|
||||||
def rating_deviation({_, rating_deviation, _}, :v2), do: rating_deviation
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
A version agnostic method for getting a player's volatility.
|
A version agnostic method for getting a player's volatility.
|
||||||
"""
|
"""
|
||||||
@spec volatility(player :: t, default_volatility :: volatility) :: volatility
|
@spec volatility(player :: t, default_volatility :: volatility) :: volatility
|
||||||
def volatility(player, default_volatility \\ initial_volatility())
|
def volatility(player, default_volatility \\ initial_volatility())
|
||||||
def volatility({_, _}, default_volatility), do: default_volatility
|
def volatility(%V1{}, default_volatility), do: default_volatility
|
||||||
def volatility({_, _, volatility}, _), do: volatility
|
def volatility(%V2{volatility: volatility}, _), do: volatility
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
A convenience function for summarizing a player's strength as a 95%
|
A convenience function for summarizing a player's strength as a 95%
|
||||||
@ -192,7 +199,8 @@ defmodule Glicko.Player do
|
|||||||
When a player has a low RD, the interval would be narrow, so that we would
|
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.
|
be 95% confident about a player’s strength being in a small interval of values.
|
||||||
"""
|
"""
|
||||||
@spec rating_interval(player :: t, as_version :: version | nil) :: {rating_low :: float, rating_high :: float}
|
@spec rating_interval(player :: t, as_version :: version | nil) ::
|
||||||
|
{rating_low :: float, rating_high :: float}
|
||||||
def rating_interval(player, as_version \\ nil) do
|
def rating_interval(player, as_version \\ nil) do
|
||||||
{
|
{
|
||||||
rating(player, as_version) - rating_deviation(player, as_version) * 2,
|
rating(player, as_version) - rating_deviation(player, as_version) * 2,
|
||||||
|
@ -6,15 +6,19 @@ defmodule Glicko.Result do
|
|||||||
|
|
||||||
iex> opponent = Player.new_v2
|
iex> opponent = Player.new_v2
|
||||||
iex> Result.new(opponent, 1.0)
|
iex> Result.new(opponent, 1.0)
|
||||||
{0.0, 2.014761872416068, 1.0}
|
%Result{opponent_rating: 0.0, opponent_rating_deviation: 2.014761872416068, score: 1.0}
|
||||||
iex> Result.new(opponent, :draw) # With shortcut
|
iex> Result.new(opponent, :draw) # With shortcut
|
||||||
{0.0, 2.014761872416068, 0.5}
|
%Result{opponent_rating: 0.0, opponent_rating_deviation: 2.014761872416068, score: 0.5}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
alias Glicko.Player
|
alias Glicko.Player
|
||||||
|
|
||||||
@type t :: {Player.rating(), Player.rating_deviation(), score}
|
@type t :: %__MODULE__{
|
||||||
|
opponent_rating: Player.rating(),
|
||||||
|
opponent_rating_deviation: Player.rating_deviation(),
|
||||||
|
score: score
|
||||||
|
}
|
||||||
|
|
||||||
@type score :: float
|
@type score :: float
|
||||||
@type score_shortcut :: :loss | :draw | :win
|
@type score_shortcut :: :loss | :draw | :win
|
||||||
@ -22,6 +26,8 @@ defmodule Glicko.Result do
|
|||||||
@score_shortcut_map %{loss: 0.0, draw: 0.5, win: 1.0}
|
@score_shortcut_map %{loss: 0.0, draw: 0.5, win: 1.0}
|
||||||
@score_shortcuts Map.keys(@score_shortcut_map)
|
@score_shortcuts Map.keys(@score_shortcut_map)
|
||||||
|
|
||||||
|
defstruct [:opponent_rating, :opponent_rating_deviation, :score]
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a new result from an opponent rating, opponent rating deviation and score.
|
Creates a new result from an opponent rating, opponent rating deviation and score.
|
||||||
|
|
||||||
@ -31,12 +37,17 @@ defmodule Glicko.Result do
|
|||||||
"""
|
"""
|
||||||
@spec new(Player.rating(), Player.rating_deviation(), score | score_shortcut) :: t
|
@spec new(Player.rating(), Player.rating_deviation(), score | score_shortcut) :: t
|
||||||
def new(opponent_rating, opponent_rating_deviation, score) when is_number(score) do
|
def new(opponent_rating, opponent_rating_deviation, score) when is_number(score) do
|
||||||
{opponent_rating, opponent_rating_deviation, score}
|
%__MODULE__{
|
||||||
|
opponent_rating: opponent_rating,
|
||||||
|
opponent_rating_deviation: opponent_rating_deviation,
|
||||||
|
score: score
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def new(opponent_rating, opponent_rating_deviation, score_type)
|
def new(opponent_rating, opponent_rating_deviation, score_type)
|
||||||
when is_atom(score_type) and score_type in @score_shortcuts do
|
when is_atom(score_type) and score_type in @score_shortcuts do
|
||||||
{opponent_rating, opponent_rating_deviation, Map.fetch!(@score_shortcut_map, score_type)}
|
score = Map.fetch!(@score_shortcut_map, score_type)
|
||||||
|
new(opponent_rating, opponent_rating_deviation, score)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -53,17 +64,17 @@ defmodule Glicko.Result do
|
|||||||
Convenience function for accessing an opponent's rating.
|
Convenience function for accessing an opponent's rating.
|
||||||
"""
|
"""
|
||||||
@spec opponent_rating(result :: t()) :: Player.rating()
|
@spec opponent_rating(result :: t()) :: Player.rating()
|
||||||
def opponent_rating(_result = {rating, _, _}), do: rating
|
def opponent_rating(%__MODULE__{} = result), do: result.opponent_rating
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Convenience function for accessing an opponent's rating deviation.
|
Convenience function for accessing an opponent's rating deviation.
|
||||||
"""
|
"""
|
||||||
@spec opponent_rating_deviation(result :: t()) :: Player.rating_deviation()
|
@spec opponent_rating_deviation(result :: t()) :: Player.rating_deviation()
|
||||||
def opponent_rating_deviation(_result = {_, rating_deviation, _}), do: rating_deviation
|
def opponent_rating_deviation(%__MODULE__{} = result), do: result.opponent_rating_deviation
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Convenience function for accessing the score.
|
Convenience function for accessing the score.
|
||||||
"""
|
"""
|
||||||
@spec score(result :: t()) :: score
|
@spec score(result :: t()) :: score
|
||||||
def score(_result = {_, _, score}), do: score
|
def score(%__MODULE__{} = result), do: result.score
|
||||||
end
|
end
|
||||||
|
2
mix.exs
2
mix.exs
@ -8,7 +8,7 @@ defmodule Glicko.Mixfile do
|
|||||||
def project do
|
def project do
|
||||||
[
|
[
|
||||||
app: :glicko,
|
app: :glicko,
|
||||||
version: "0.7.0",
|
version: "0.8.1",
|
||||||
elixir: "~> 1.9",
|
elixir: "~> 1.9",
|
||||||
start_permanent: Mix.env() == :prod,
|
start_permanent: Mix.env() == :prod,
|
||||||
deps: deps(),
|
deps: deps(),
|
||||||
|
17
mix.lock
17
mix.lock
@ -1,12 +1,15 @@
|
|||||||
%{
|
%{
|
||||||
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
|
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
|
||||||
"credo": {:hex, :credo, "1.4.0", "92339d4cbadd1e88b5ee43d427b639b68a11071b6f73854e33638e30a0ea11f5", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1fd3b70dce216574ce3c18bdf510b57e7c4c85c2ec9cad4bff854abaf7e58658"},
|
"credo": {:hex, :credo, "1.7.6", "b8f14011a5443f2839b04def0b252300842ce7388f3af177157c86da18dfbeea", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "146f347fb9f8cbc5f7e39e3f22f70acbef51d441baa6d10169dd604bfbc55296"},
|
||||||
"earmark": {:hex, :earmark, "1.4.4", "4821b8d05cda507189d51f2caeef370cf1e18ca5d7dfb7d31e9cafe6688106a4", [:mix], [], "hexpm", "1f93aba7340574847c0f609da787f0d79efcab51b044bb6e242cae5aca9d264d"},
|
"earmark": {:hex, :earmark, "1.4.4", "4821b8d05cda507189d51f2caeef370cf1e18ca5d7dfb7d31e9cafe6688106a4", [:mix], [], "hexpm", "1f93aba7340574847c0f609da787f0d79efcab51b044bb6e242cae5aca9d264d"},
|
||||||
"ex_doc": {:hex, :ex_doc, "0.22.0", "fb0495cd70849bc4d7bc716d4e740aebfaddb77bb1074addf357912468c831d6", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f9b36237b220c8262d561489967b6a604832593f0dc1fb1608662909457e91aa"},
|
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
|
||||||
|
"ex_doc": {:hex, :ex_doc, "0.34.0", "ab95e0775db3df71d30cf8d78728dd9261c355c81382bcd4cefdc74610bef13e", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "60734fb4c1353f270c3286df4a0d51e65a2c1d9fba66af3940847cc65a8066d7"},
|
||||||
|
"file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"},
|
||||||
"inch_ex": {:hex, :inch_ex, "2.0.0", "24268a9284a1751f2ceda569cd978e1fa394c977c45c331bb52a405de544f4de", [:mix], [{:bunt, "~> 0.2", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "96d0ec5ecac8cf63142d02f16b7ab7152cf0f0f1a185a80161b758383c9399a8"},
|
"inch_ex": {:hex, :inch_ex, "2.0.0", "24268a9284a1751f2ceda569cd978e1fa394c977c45c331bb52a405de544f4de", [:mix], [{:bunt, "~> 0.2", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "96d0ec5ecac8cf63142d02f16b7ab7152cf0f0f1a185a80161b758383c9399a8"},
|
||||||
"jason": {:hex, :jason, "1.2.0", "10043418c42d2493d0ee212d3fddd25d7ffe484380afad769a0a38795938e448", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "116747dbe057794c3a3e4e143b7c8390b29f634e16c78a7f59ba75bfa6852e7f"},
|
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
|
||||||
"makeup": {:hex, :makeup, "1.0.1", "82f332e461dc6c79dbd82fbe2a9c10d48ed07146f0a478286e590c83c52010b5", [], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49736fe5b66a08d8575bf5321d716bac5da20c8e6b97714fec2bcd6febcfa1f8"},
|
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
|
||||||
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
|
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
|
||||||
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"},
|
"makeup_erlang": {:hex, :makeup_erlang, "1.0.0", "6f0eff9c9c489f26b69b61440bf1b238d95badae49adac77973cbacae87e3c2e", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "ea7a9307de9d1548d2a72d299058d1fd2339e3d398560a0e46c27dab4891e4d2"},
|
||||||
|
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
|
||||||
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
|
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,8 @@ defmodule Glicko.PlayerTest do
|
|||||||
|
|
||||||
doctest Player
|
doctest Player
|
||||||
|
|
||||||
@valid_v1_base {1.0, 2.0}
|
@valid_v1_base %Player.V1{rating: 1.0, rating_deviation: 2.0}
|
||||||
@valid_v2_base {1.0, 2.0, 3.0}
|
@valid_v2_base %Player.V2{rating: 1.0, rating_deviation: 2.0, volatility: 3.0}
|
||||||
|
|
||||||
test "create v1" do
|
test "create v1" do
|
||||||
assert @valid_v1_base == Player.new_v1(rating: 1.0, rating_deviation: 2.0)
|
assert @valid_v1_base == Player.new_v1(rating: 1.0, rating_deviation: 2.0)
|
||||||
@ -17,13 +17,18 @@ defmodule Glicko.PlayerTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "convert player v1 -> v2" do
|
test "convert player v1 -> v2" do
|
||||||
assert {Player.scale_rating_to(1.0, :v2), Player.scale_rating_deviation_to(2.0, :v2), 3.0} ==
|
assert Player.new_v2(
|
||||||
Player.to_v2(@valid_v1_base, 3.0)
|
rating: Player.scale_rating_to(1.0, :v2),
|
||||||
|
rating_deviation: Player.scale_rating_deviation_to(2.0, :v2),
|
||||||
|
volatility: 3.0
|
||||||
|
) == Player.to_v2(@valid_v1_base, 3.0)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "convert player v2 -> v1" do
|
test "convert player v2 -> v1" do
|
||||||
assert {Player.scale_rating_to(1.0, :v1), Player.scale_rating_deviation_to(2.0, :v1)} ==
|
assert Player.new_v1(
|
||||||
Player.to_v1(@valid_v2_base)
|
rating: Player.scale_rating_to(1.0, :v1),
|
||||||
|
rating_deviation: Player.scale_rating_deviation_to(2.0, :v1)
|
||||||
|
) == Player.to_v1(@valid_v2_base)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "convert player v1 -> v1" do
|
test "convert player v1 -> v1" do
|
||||||
|
Loading…
Reference in New Issue
Block a user