-
Notifications
You must be signed in to change notification settings - Fork 0
/
Glicko2.cpp
184 lines (150 loc) · 4.61 KB
/
Glicko2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <cmath>
#include <vector>
#include "Glicko2.h"
void Glicko2::CalculateNewRating(GlickoRating playerRating, GlickoRating opponentRating, float playerScore)
{
// Step 1/2: Determine player rating and convert to Glicko-2 scale
Glicko2Rating player{ _u(playerRating.m_Rating), _phi(playerRating.m_Deviation), playerRating.m_Volatility };
Glicko2Rating opponent{ _u(opponentRating.m_Rating), _phi(opponentRating.m_Deviation), opponentRating.m_Volatility };
// Step 3: Compute quantity v
float tmp_v = _v(player.u, opponent.u, opponent.phi);
// Step 4: Compute quantity delta
float tmp_delta = _delta(player.u, opponent.u, opponent.phi, playerScore);
// Step 5: Determine new volatility value, sigma'
}
void Glicko2::CalculateNewRating(GlickoRating playerRating, std::vector<GlickoRating> opponentRatings, std::vector<float> scores)
{
// Step 0: Clear all variables or create new Glicko-2 object???
// Step 1/2: Determine player rating and convert to Glicko-2 scale
player.u = _u(playerRating.m_Rating);
player.phi = _phi(playerRating.m_Deviation);
player.sigma = playerRating.m_Volatility;
Glicko2Rating opponent;
for (auto rating : opponentRatings)
{
opponent.u = _u(rating.m_Rating);
opponent.phi = _phi(rating.m_Deviation);
opponent.sigma = rating.m_Volatility;
opponents.push_back(opponent);
}
// Step 3: Compute quantity v
float tmpSum = 0.0f;
float tmp_E;
for (auto o : opponents)
{
tmp_E = _E(player.u, o.u, o.phi);
tmpSum += (powf(_g(o.phi), 2) * tmp_E * (1 - tmp_E));
}
v = 1 / tmpSum;
// Step 4: Compute quantity delta
tmpSum = 0.0f;
for (size_t i = 0; i < opponents.size(); i++)
{
tmpSum += (_g(opponents[i].phi) * (scores[i] - _E(player.u, opponents[i].u, opponents[i].phi)));
}
delta = v * tmpSum;
// Step 5: Determine new volatility value, sigma'
a = _a(player.sigma);
float A = a;
float B;
if (powf(delta, 2) > (powf(player.sigma, 2) + v)) // delta^2 > sigma^2 + v
{
B = log(powf(delta, 2) - powf(player.phi, 2) - v);
}
else // delta^2 <= phi^2 + v
{
int k = 1;
while (_f(a - k * tau) < 0)
{
k++;
}
B = a - k * tau;
}
float f_A = _f(A);
float f_B = _f(B);
float C;
float f_C;
while (abs(B - A) > e)
{
C = A + (A - B) * f_A / (f_B - f_A);
f_C = _f(C);
if ((f_C * f_B) < 0)
{
A = B;
f_A = f_B;
}
else
{
f_A = f_A / 2;
}
B = C;
f_B = f_C;
}
//sigma_prime = exp(A / 2);
player_prime.sigma = exp(A / 2);
// Step 6: Update rating deviation to new pre-rating period value, phi*
sigma_new = sqrt(powf(player.phi, 2) + powf(player_prime.sigma, 2));
// Step 7: Update rating and deviation to new values, u' and phi'
player_prime.phi = 1 / sqrt((1 / powf(sigma_new, 2)) + (1 / v));
tmpSum = 0.0f;
for (size_t i = 0; i < opponents.size(); i++)
{
tmpSum += (_g(opponents[i].phi) * (scores[i] - _E(player.u, opponents[i].u, opponents[i].phi)));
}
player_prime.u = player.u + powf(player_prime.phi, 2) * tmpSum;
}
void Glicko2::GetNewRating(GlickoRating& playerRating)
{
playerRating.m_Rating = _r(player_prime.u);
playerRating.m_Deviation = _RD(player_prime.phi);
playerRating.m_Volatility = player_prime.sigma;
}
float Glicko2::_u(float r)
{
return (r - 1500.0f) / 173.7178f;
}
float Glicko2::_phi(float RD)
{
return RD / 173.7178f;
}
float Glicko2::_r(float u)
{
return 173.7178 * u + 1500.0f;
}
float Glicko2::_RD(float phi)
{
return 173.7178 * phi;
}
float Glicko2::_g(float phi)
{
return (1 / sqrt(1 + ((3 * powf(phi, 2)) / (powf(pi, 2)))));
}
float Glicko2::_E(float u, float u_j, float phi_j)
{
return (1 / (1 + expf(-1 * _g(phi_j) * (u - u_j))));
}
float Glicko2::_v(float u, float u_j, float phi_j)
{
float tmp_g = _g(phi_j);
float tmp_E = _E(u, u_j, phi_j);
return powf((powf(tmp_g, 2)* tmp_E* (1 - tmp_E)), -1);
}
float Glicko2::_delta(float u, float u_j, float phi_j, float score)
{
float tmp_v = _v(u, u_j, phi_j);
float tmp_g = _g(phi_j);
float tmp_E = _E(u, u_j, phi_j);
return tmp_v * tmp_g * (score - tmp_E);
}
float Glicko2::_a(float sigma)
{
return log(powf(sigma, 2));
}
float Glicko2::_f(float x)
{
float A = exp(x) * (powf(delta, 2) - powf(player.phi, 2) - v - exp(x));
float B = 2 * powf((powf(player.phi, 2) + v + exp(x)), 2);
float C = x - a;
float D = powf(tau, 2);
return ((A / B) - (C / D));
}