-
Notifications
You must be signed in to change notification settings - Fork 2
/
mm.cpp
390 lines (345 loc) · 9.14 KB
/
mm.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/////////////////////////////////////////////////////////////////////////////
//
// mm.cpp
//
// Rémi Coulom
//
// February, 2007
//
/////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <cmath>
#include <fstream>
const double PriorVictories = 1.0;
const double PriorGames = 2.0;
const double PriorOpponentGamma = 1.0;
/////////////////////////////////////////////////////////////////////////////
// One "team": product of gammas
/////////////////////////////////////////////////////////////////////////////
class CTeam
{
private: ///////////////////////////////////////////////////////////////////
static std::vector<int> vi;
int Index;
int Size;
public: ////////////////////////////////////////////////////////////////////
CTeam(): Index(vi.size()), Size(0) {}
int GetSize() const {return Size;}
int GetIndex(int i) const {return vi[Index+i];}
void Append(int i) {vi.push_back(i); Size++;}
};
std::vector<int> CTeam::vi;
/////////////////////////////////////////////////////////////////////////////
// Read a team
/////////////////////////////////////////////////////////////////////////////
CTeam ReadTeam(std::string &s)
{
std::istringstream in(s);
CTeam team;
int Index;
while(1)
{
in >> Index;
if (in)
team.Append(Index);
else
break;
}
return team;
}
/////////////////////////////////////////////////////////////////////////////
// One "Game": One winner team out of several participants
/////////////////////////////////////////////////////////////////////////////
class CGame
{
public: ////////////////////////////////////////////////////////////////////
CTeam Winner;
std::vector<CTeam> vParticipants;
};
/////////////////////////////////////////////////////////////////////////////
// Game Collection:
/////////////////////////////////////////////////////////////////////////////
class CGameCollection
{
public: ////////////////////////////////////////////////////////////////////
std::vector<CGame> vgame;
std::vector<double> vGamma;
std::vector<int> vFeatureIndex;
std::vector<std::string> vFeatureName;
std::vector<double> vVictories;
std::vector<int> vParticipations;
std::vector<int> vPresences;
void ComputeVictories();
void MM(int Feature);
double LogLikelihood() const;
double GetTeamGamma(const CTeam &team) const
{
double Result = 1.0;
for (int i = team.GetSize(); --i >= 0;)
Result *= vGamma[team.GetIndex(i)];
return Result;
}
};
/////////////////////////////////////////////////////////////////////////////
// Compute log likelihood
/////////////////////////////////////////////////////////////////////////////
double CGameCollection::LogLikelihood() const
{
double L = 0;
for (int i = vgame.size(); --i >= 0;)
{
const CGame &game = vgame[i];
double Opponents = 0;
const std::vector<CTeam> &v = game.vParticipants;
for (int j = v.size(); --j >= 0;)
Opponents += GetTeamGamma(v[j]);
L += std::log(GetTeamGamma(game.Winner));
L -= std::log(Opponents);
}
return L;
}
/////////////////////////////////////////////////////////////////////////////
// Compute victories for each gamma (and games played)
/////////////////////////////////////////////////////////////////////////////
void CGameCollection::ComputeVictories()
{
vVictories.resize(vGamma.size());
vParticipations.resize(vGamma.size());
vPresences.resize(vGamma.size());
for (int i = vVictories.size(); --i >= 0;)
{
vVictories[i] = 0;
vParticipations[i] = 0;
vPresences[i] = 0;
}
for (int i = vgame.size(); --i >= 0;)
{
const CTeam &Winner = vgame[i].Winner;
for (int j = Winner.GetSize(); --j >= 0;)
vVictories[Winner.GetIndex(j)]++;
int tParticipations[vGamma.size()];
for (int j = vGamma.size(); --j >= 0;)
tParticipations[j] = 0;
for (int k = vgame[i].vParticipants.size(); --k >= 0;)
for (int j = vgame[i].vParticipants[k].GetSize(); --j >= 0;)
{
int Index = vgame[i].vParticipants[k].GetIndex(j);
vParticipations[Index]++;
tParticipations[Index]++;
}
for (int i = vGamma.size(); --i >= 0;)
if (tParticipations[i])
vPresences[i]++;
}
#if 0
for (int i = vGamma.size(); --i >= 0;)
std::cerr << i << ' ' << vVictories[i] << '\n';
#endif
}
/////////////////////////////////////////////////////////////////////////////
// One iteration of minorization-maximization, for one feature
/////////////////////////////////////////////////////////////////////////////
void CGameCollection::MM(int Feature)
{
//
// Interval for this feature
//
int Max = vFeatureIndex[Feature + 1];
int Min = vFeatureIndex[Feature];
//
// Compute denominator for each gamma
//
std::vector<double> vDen(vGamma.size());
for (int i = vDen.size(); --i >= 0;)
vDen[i] = 0.0;
//
// Main loop over games
//
for (int i = vgame.size(); --i >= 0;)
{
double tMul[vGamma.size()];
{
for (int i = vGamma.size(); --i >= 0;)
tMul[i] = 0.0;
}
double Den = 0.0;
std::vector<CTeam> &v = vgame[i].vParticipants;
for (int i = v.size(); --i >= 0;)
{
const CTeam &team = v[i];
double Product = 1.0;
int FeatureIndex = -1;
for (int i = 0; i < team.GetSize(); i++)
{
int Index = team.GetIndex(i);
if (Index >= Min && Index < Max)
FeatureIndex = Index;
else
Product *= vGamma[Index];
}
if (FeatureIndex >= 0)
{
tMul[FeatureIndex] += Product;
Product *= vGamma[FeatureIndex];
}
Den += Product;
}
for (int i = Max; --i >= Min;)
vDen[i] += tMul[i] / Den;
}
//
// Update Gammas
//
for (int i = Max; --i >= Min;)
{
double NewGamma = (vVictories[i] + PriorVictories) /
(vDen[i] + PriorGames / (vGamma[i] + PriorOpponentGamma));
vGamma[i] = NewGamma;
}
}
/////////////////////////////////////////////////////////////////////////////
// Read game collection
/////////////////////////////////////////////////////////////////////////////
void ReadGameCollection(CGameCollection &gcol, std::istream &in)
{
//
// Read number of gammas in the first line
//
{
std::string sLine;
std::getline(in, sLine);
std::istringstream is(sLine);
std::string s;
int Gammas = 0;
is >> s >> Gammas;
gcol.vGamma.resize(Gammas);
for (int i = Gammas; --i >= 0;)
gcol.vGamma[i] = 1.0;
}
//
// Features
//
{
gcol.vFeatureIndex.push_back(0);
int Features = 0;
in >> Features;
for (int i = 0; i < Features; i++)
{
int Gammas;
in >> Gammas;
int Min = gcol.vFeatureIndex.back();
gcol.vFeatureIndex.push_back(Min + Gammas);
std::string sName;
in >> sName;
gcol.vFeatureName.push_back(sName);
}
}
//
// Main loop over games
//
std::string sLine;
std::getline(in, sLine);
while(in)
{
//
// Parse a game
//
if (sLine == "#")
{
CGame game;
//
// Winner
//
std::getline(in, sLine);
game.Winner = ReadTeam(sLine);
//
// Participants
//
std::getline(in, sLine);
while (sLine[0] != '#' && sLine[0] != '!' && in)
{
CTeam team = ReadTeam(sLine);
game.vParticipants.push_back(team);
std::getline(in, sLine);
}
gcol.vgame.push_back(game);
}
else
{
std::getline(in, sLine);
std::cerr << '.';
}
}
std::cerr << '\n';
}
/////////////////////////////////////////////////////////////////////////////
// Write ratings
/////////////////////////////////////////////////////////////////////////////
void WriteRatings(const CGameCollection &gcol,
std::ostream &out,
int fExtraData)
{
for (unsigned i = 0; i < gcol.vGamma.size(); i++)
{
out << std::setw(3) << i << ' ' << std::setw(10) << gcol.vGamma[i] << ' ';
if (fExtraData)
{
out << std::setw(11) << gcol.vVictories[i];
out << std::setw(11) << gcol.vParticipations[i];
out << std::setw(11) << gcol.vPresences[i];
}
out << '\n';
}
}
/////////////////////////////////////////////////////////////////////////////
// main function
/////////////////////////////////////////////////////////////////////////////
int main()
{
CGameCollection gcol;
ReadGameCollection(gcol, std::cin);
gcol.ComputeVictories();
std::cerr << "Games = " << gcol.vgame.size() << '\n';
double LogLikelihood = gcol.LogLikelihood() / gcol.vgame.size();
const int Features = gcol.vFeatureName.size();
double tDelta[Features];
for (int k = 2; --k >= 0;)
{
for (int i = Features; --i >= 0;)
tDelta[i] = 10.0;
while(1)
{
//
// Select feature with max delta
//
int Feature = 0;
double MaxDelta = tDelta[0];
for (int j = Features; --j > 0;)
if (tDelta[j] > MaxDelta)
MaxDelta = tDelta[Feature = j];
if (MaxDelta < 0.0001)
break;
//
// Run one MM iteration over this feature
//
std::cerr << std::setw(20) << gcol.vFeatureName[Feature] << ' ';
std::cerr << std::setw(9) << LogLikelihood << ' ';
std::cerr << std::setw(9) << std::exp(-LogLikelihood) << ' ';
gcol.MM(Feature);
double NewLogLikelihood = gcol.LogLikelihood() / gcol.vgame.size();
double Delta = NewLogLikelihood - LogLikelihood;
tDelta[Feature] = Delta;
std::cerr << std::setw(9) << Delta << '\n';
LogLikelihood = NewLogLikelihood;
}
}
WriteRatings(gcol, std::cout, 0);
{
std::ofstream ofs("mm-with-freq.dat");
WriteRatings(gcol, ofs, 1);
}
return 0;
}