elo-ratings 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/elo-ratings.gemspec +148 -0
- data/ext/bayeselo/.gitignore +4 -0
- data/ext/bayeselo/CBradleyTerry.cpp +670 -0
- data/ext/bayeselo/CBradleyTerry.h +137 -0
- data/ext/bayeselo/CCDistribution.cpp +135 -0
- data/ext/bayeselo/CCDistribution.h +44 -0
- data/ext/bayeselo/CCDistributionCUI.cpp +104 -0
- data/ext/bayeselo/CCDistributionCUI.h +38 -0
- data/ext/bayeselo/CCondensedResults.cpp +253 -0
- data/ext/bayeselo/CCondensedResults.h +64 -0
- data/ext/bayeselo/CDiscretization.h +40 -0
- data/ext/bayeselo/CDistribution.cpp +123 -0
- data/ext/bayeselo/CDistribution.h +58 -0
- data/ext/bayeselo/CDistributionCollection.cpp +34 -0
- data/ext/bayeselo/CDistributionCollection.h +29 -0
- data/ext/bayeselo/CEloRatingCUI.cpp +1046 -0
- data/ext/bayeselo/CEloRatingCUI.h +67 -0
- data/ext/bayeselo/CIndirectCompare.h +28 -0
- data/ext/bayeselo/CJointBayesian.cpp +111 -0
- data/ext/bayeselo/CJointBayesian.h +38 -0
- data/ext/bayeselo/CLUDecomposition.cpp +154 -0
- data/ext/bayeselo/CLUDecomposition.h +36 -0
- data/ext/bayeselo/CMatrix.cpp +48 -0
- data/ext/bayeselo/CMatrix.h +35 -0
- data/ext/bayeselo/CMatrixIO.cpp +27 -0
- data/ext/bayeselo/CMatrixIO.h +17 -0
- data/ext/bayeselo/CPredictionCUI.cpp +393 -0
- data/ext/bayeselo/CPredictionCUI.h +50 -0
- data/ext/bayeselo/CResultSet.cpp +245 -0
- data/ext/bayeselo/CResultSet.h +50 -0
- data/ext/bayeselo/CResultSetCUI.cpp +355 -0
- data/ext/bayeselo/CResultSetCUI.h +44 -0
- data/ext/bayeselo/CTimeIO.cpp +58 -0
- data/ext/bayeselo/CTimeIO.h +19 -0
- data/ext/bayeselo/CVector.cpp +110 -0
- data/ext/bayeselo/CVector.h +42 -0
- data/ext/bayeselo/EloDataFromFile.cpp +120 -0
- data/ext/bayeselo/EloDataFromFile.h +21 -0
- data/ext/bayeselo/README +8 -0
- data/ext/bayeselo/ReadLineToString.cpp +32 -0
- data/ext/bayeselo/ReadLineToString.h +18 -0
- data/ext/bayeselo/chtime.cpp +56 -0
- data/ext/bayeselo/chtime.h +61 -0
- data/ext/bayeselo/chtimer.h +27 -0
- data/ext/bayeselo/clktimer.cpp +178 -0
- data/ext/bayeselo/clktimer.h +29 -0
- data/ext/bayeselo/consolui.cpp +538 -0
- data/ext/bayeselo/consolui.h +154 -0
- data/ext/bayeselo/const.cpp +79 -0
- data/ext/bayeselo/const.h +42 -0
- data/ext/bayeselo/date.cpp +96 -0
- data/ext/bayeselo/date.h +44 -0
- data/ext/bayeselo/debug.h +58 -0
- data/ext/bayeselo/elomain.cpp +26 -0
- data/ext/bayeselo/extconf.rb +6 -0
- data/ext/bayeselo/list.h +367 -0
- data/ext/bayeselo/listi.h +125 -0
- data/ext/bayeselo/move.cpp +249 -0
- data/ext/bayeselo/move.h +139 -0
- data/ext/bayeselo/pgn.h +62 -0
- data/ext/bayeselo/pgnlex.cpp +432 -0
- data/ext/bayeselo/pgnlex.h +105 -0
- data/ext/bayeselo/pgnstr.cpp +126 -0
- data/ext/bayeselo/piece.h +44 -0
- data/ext/bayeselo/player.h +31 -0
- data/ext/bayeselo/position.h +89 -0
- data/ext/bayeselo/random.cpp +114 -0
- data/ext/bayeselo/random.h +63 -0
- data/ext/bayeselo/rb_bayeselo.cpp +76 -0
- data/ext/bayeselo/readstr.cpp +51 -0
- data/ext/bayeselo/readstr.h +19 -0
- data/ext/bayeselo/square.h +61 -0
- data/ext/bayeselo/str.cpp +81 -0
- data/ext/bayeselo/str.h +71 -0
- data/ext/bayeselo/version.cpp +30 -0
- data/ext/bayeselo/version.h +28 -0
- data/ext/bayeselo/version_number.h +1 -0
- data/lib/bayeselo.rb +6 -0
- data/lib/bayeselo/bayeselo.rb +11 -0
- data/lib/bayeselo/c_bayeselo.rb +7 -0
- data/test/bayeselo/test_bayeselo.rb +33 -0
- data/test/bayeselo/test_c_bayeselo.rb +84 -0
- data/test/helper.rb +18 -0
- metadata +226 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// R�mi Coulom
|
|
4
|
+
//
|
|
5
|
+
// December, 2004
|
|
6
|
+
//
|
|
7
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
#include "CCondensedResults.h"
|
|
9
|
+
#include "CResultSet.h"
|
|
10
|
+
#include "debug.h"
|
|
11
|
+
|
|
12
|
+
#include <set>
|
|
13
|
+
#include <iostream>
|
|
14
|
+
|
|
15
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
16
|
+
// Constructor
|
|
17
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
18
|
+
CCondensedResults::CCondensedResults(const CResultSet &rs) :
|
|
19
|
+
Players(rs.GetPlayers())
|
|
20
|
+
{
|
|
21
|
+
pOpponents = new int[Players];
|
|
22
|
+
ppcr = new CCondensedResult*[Players];
|
|
23
|
+
|
|
24
|
+
//
|
|
25
|
+
// Count opponents of each player
|
|
26
|
+
//
|
|
27
|
+
{
|
|
28
|
+
std::set<int> *pset = new std::set<int>[Players];
|
|
29
|
+
|
|
30
|
+
for (int i = rs.GetGames(); --i >= 0;)
|
|
31
|
+
{
|
|
32
|
+
int White = rs.GetWhite(i);
|
|
33
|
+
int Black = rs.GetBlack(i);
|
|
34
|
+
pset[White].insert(Black);
|
|
35
|
+
pset[Black].insert(White);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
for (int i = Players; --i >= 0;)
|
|
39
|
+
{
|
|
40
|
+
pOpponents[i] = pset[i].size();
|
|
41
|
+
ppcr[i] = new CCondensedResult[pOpponents[i]];
|
|
42
|
+
for (int j = pOpponents[i]; --j >= 0;)
|
|
43
|
+
ppcr[i][j].Reset();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
delete[] pset;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//
|
|
50
|
+
// Fill-in all CCondensedResult's
|
|
51
|
+
//
|
|
52
|
+
for (int i = rs.GetGames(); --i >= 0;)
|
|
53
|
+
{
|
|
54
|
+
int White = rs.GetWhite(i);
|
|
55
|
+
int Black = rs.GetBlack(i);
|
|
56
|
+
|
|
57
|
+
CCondensedResult &crWhite = FindOpponent(White, Black);
|
|
58
|
+
CCondensedResult &crBlack = FindOpponent(Black, White);
|
|
59
|
+
crWhite.TrueGames++;
|
|
60
|
+
crBlack.TrueGames++;
|
|
61
|
+
|
|
62
|
+
switch(rs.GetResult(i))
|
|
63
|
+
{
|
|
64
|
+
case 0: //////////////////////////////////////////////////////////////////
|
|
65
|
+
crWhite.l_ij++;
|
|
66
|
+
crBlack.l_ji++;
|
|
67
|
+
break;
|
|
68
|
+
|
|
69
|
+
case 1: //////////////////////////////////////////////////////////////////
|
|
70
|
+
crWhite.d_ij++;
|
|
71
|
+
crBlack.d_ji++;
|
|
72
|
+
break;
|
|
73
|
+
|
|
74
|
+
case 2: //////////////////////////////////////////////////////////////////
|
|
75
|
+
crWhite.w_ij++;
|
|
76
|
+
crBlack.w_ji++;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
83
|
+
// Add prior
|
|
84
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
85
|
+
void CCondensedResults::AddPrior(float PriorDraw)
|
|
86
|
+
{
|
|
87
|
+
for (int i = Players; --i >= 0;)
|
|
88
|
+
{
|
|
89
|
+
float Prior = PriorDraw * 0.25 / CountTrueGames(i);
|
|
90
|
+
for (int j = pOpponents[i]; --j >= 0;)
|
|
91
|
+
{
|
|
92
|
+
CCondensedResult &crPlayer = ppcr[i][j];
|
|
93
|
+
CCondensedResult &crOpponent = FindOpponent(crPlayer.Opponent, i);
|
|
94
|
+
float ThisPrior = Prior * crPlayer.TrueGames;
|
|
95
|
+
crPlayer.d_ij += ThisPrior;
|
|
96
|
+
crPlayer.d_ji += ThisPrior;
|
|
97
|
+
crOpponent.d_ij += ThisPrior;
|
|
98
|
+
crOpponent.d_ji += ThisPrior;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
104
|
+
// Reset
|
|
105
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
106
|
+
void CCondensedResult::Reset()
|
|
107
|
+
{
|
|
108
|
+
Opponent = -1;
|
|
109
|
+
TrueGames = 0;
|
|
110
|
+
w_ij = 0;
|
|
111
|
+
d_ij = 0;
|
|
112
|
+
l_ij = 0;
|
|
113
|
+
w_ji = 0;
|
|
114
|
+
d_ji = 0;
|
|
115
|
+
l_ji = 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
119
|
+
// Find Opponent
|
|
120
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
121
|
+
CCondensedResult &CCondensedResults::FindOpponent(int Player, int Opponent)
|
|
122
|
+
{
|
|
123
|
+
for (int i = pOpponents[Player]; --i >= 0;)
|
|
124
|
+
{
|
|
125
|
+
CCondensedResult &cr = ppcr[Player][i];
|
|
126
|
+
if (cr.Opponent == Opponent)
|
|
127
|
+
return cr;
|
|
128
|
+
if (cr.Opponent < 0)
|
|
129
|
+
{
|
|
130
|
+
cr.Opponent = Opponent;
|
|
131
|
+
return cr;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
FATAL(1);
|
|
136
|
+
return ppcr[Player][0];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
140
|
+
// Count the number of true games played by one player
|
|
141
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
142
|
+
int CCondensedResults::CountTrueGames(int Player) const
|
|
143
|
+
{
|
|
144
|
+
int Result = 0;
|
|
145
|
+
for (int i = pOpponents[Player]; --i >= 0;)
|
|
146
|
+
{
|
|
147
|
+
CCondensedResult &cr = ppcr[Player][i];
|
|
148
|
+
Result += cr.TrueGames;
|
|
149
|
+
}
|
|
150
|
+
return Result;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
154
|
+
// Evaluate the average opponent of one player
|
|
155
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
156
|
+
double CCondensedResults::AverageOpponent(int Player, const double *pelo) const
|
|
157
|
+
{
|
|
158
|
+
double Total = 0;
|
|
159
|
+
int GameCount = 0;
|
|
160
|
+
for (int i = pOpponents[Player]; --i >= 0;)
|
|
161
|
+
{
|
|
162
|
+
CCondensedResult &cr = ppcr[Player][i];
|
|
163
|
+
Total += cr.TrueGames * pelo[cr.Opponent];
|
|
164
|
+
GameCount += cr.TrueGames;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (GameCount)
|
|
168
|
+
return Total / GameCount;
|
|
169
|
+
else
|
|
170
|
+
return 0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
174
|
+
// Count the number of games played by one player
|
|
175
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
176
|
+
float CCondensedResults::CountGames(int Player) const
|
|
177
|
+
{
|
|
178
|
+
float Result = 0;
|
|
179
|
+
for (int i = pOpponents[Player]; --i >= 0;)
|
|
180
|
+
{
|
|
181
|
+
CCondensedResult &cr = ppcr[Player][i];
|
|
182
|
+
Result += cr.w_ij + cr.d_ij + cr.l_ij + cr.w_ji + cr.d_ji + cr.l_ji;
|
|
183
|
+
}
|
|
184
|
+
return Result;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
188
|
+
// Count the number of draws played by one player
|
|
189
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
190
|
+
float CCondensedResults::CountDraws(int Player) const
|
|
191
|
+
{
|
|
192
|
+
float Result = 0;
|
|
193
|
+
for (int i = pOpponents[Player]; --i >= 0;)
|
|
194
|
+
{
|
|
195
|
+
CCondensedResult &cr = ppcr[Player][i];
|
|
196
|
+
Result += cr.d_ij + cr.d_ji;
|
|
197
|
+
}
|
|
198
|
+
return Result;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
202
|
+
// Get the score of one player
|
|
203
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
204
|
+
float CCondensedResults::Score(int Player) const
|
|
205
|
+
{
|
|
206
|
+
float Result = 0;
|
|
207
|
+
for (int i = pOpponents[Player]; --i >= 0;)
|
|
208
|
+
{
|
|
209
|
+
CCondensedResult &cr = ppcr[Player][i];
|
|
210
|
+
Result += 2 * cr.w_ij + cr.d_ij + cr.d_ji + 2 * cr.l_ji;
|
|
211
|
+
}
|
|
212
|
+
return Result;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
216
|
+
// Destructor
|
|
217
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
218
|
+
CCondensedResults::~CCondensedResults()
|
|
219
|
+
{
|
|
220
|
+
for (int i = Players; --i >= 0;)
|
|
221
|
+
delete[] ppcr[i];
|
|
222
|
+
delete[] ppcr;
|
|
223
|
+
delete[] pOpponents;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
227
|
+
// Dump
|
|
228
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
229
|
+
void CCondensedResults::Dump(std::ostream &out) const
|
|
230
|
+
{
|
|
231
|
+
out << "Players = " << Players << '\n';
|
|
232
|
+
for (int i = 0; i < Players; i++)
|
|
233
|
+
{
|
|
234
|
+
out << "Player " << i << '\n';
|
|
235
|
+
out << "pOpponents[i] = " << pOpponents[i] << '\n';
|
|
236
|
+
out << "CountTrueGames(i) = " << CountTrueGames(i) << '\n';
|
|
237
|
+
for (int j = 0; j < pOpponents[i]; j++)
|
|
238
|
+
{
|
|
239
|
+
const CCondensedResult &cr = ppcr[i][j];
|
|
240
|
+
out << "Opponent " << j << " = " << cr.Opponent << '\n';
|
|
241
|
+
out << "TrueGames = " << cr.TrueGames << '\n';
|
|
242
|
+
out << "w_ij = " << cr.w_ij << ' ';
|
|
243
|
+
out << "d_ij = " << cr.d_ij << ' ';
|
|
244
|
+
out << "l_ij = " << cr.l_ij << ' ';
|
|
245
|
+
out << "w_ji = " << cr.w_ji << ' ';
|
|
246
|
+
out << "d_ji = " << cr.d_ji << ' ';
|
|
247
|
+
out << "l_ji = " << cr.l_ji;
|
|
248
|
+
out << '\n';
|
|
249
|
+
}
|
|
250
|
+
out << '\n';
|
|
251
|
+
}
|
|
252
|
+
out.flush();
|
|
253
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// R�mi Coulom
|
|
4
|
+
//
|
|
5
|
+
// December, 2004
|
|
6
|
+
//
|
|
7
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
#ifndef CCondensedResults_Declared
|
|
9
|
+
#define CCondensedResults_Declared
|
|
10
|
+
|
|
11
|
+
#include <iosfwd>
|
|
12
|
+
|
|
13
|
+
class CResultSet;
|
|
14
|
+
|
|
15
|
+
class CCondensedResult // cr
|
|
16
|
+
{
|
|
17
|
+
public:
|
|
18
|
+
int Opponent;
|
|
19
|
+
int TrueGames;
|
|
20
|
+
float w_ij;
|
|
21
|
+
float d_ij;
|
|
22
|
+
float l_ij;
|
|
23
|
+
float w_ji;
|
|
24
|
+
float d_ji;
|
|
25
|
+
float l_ji;
|
|
26
|
+
|
|
27
|
+
void Reset();
|
|
28
|
+
float Games() const {return w_ij + d_ij + l_ij + w_ji + d_ji + l_ji;}
|
|
29
|
+
float Score() const {return w_ij + l_ji + (d_ij + d_ji) * 0.5;}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
class CCondensedResults // crs
|
|
33
|
+
{
|
|
34
|
+
private: ///////////////////////////////////////////////////////////////////
|
|
35
|
+
int Players;
|
|
36
|
+
int *pOpponents;
|
|
37
|
+
CCondensedResult **ppcr;
|
|
38
|
+
|
|
39
|
+
public: ////////////////////////////////////////////////////////////////////
|
|
40
|
+
CCondensedResults(const CResultSet &rs);
|
|
41
|
+
|
|
42
|
+
void AddPrior(float PriorDraw);
|
|
43
|
+
|
|
44
|
+
int GetPlayers() const {return Players;}
|
|
45
|
+
int GetOpponents(int Player) const {return pOpponents[Player];}
|
|
46
|
+
|
|
47
|
+
const CCondensedResult &GetCondensedResult(int Player, int i) const
|
|
48
|
+
{return ppcr[Player][i];}
|
|
49
|
+
CCondensedResult &GetCondensedResult(int Player, int i)
|
|
50
|
+
{return ppcr[Player][i];}
|
|
51
|
+
CCondensedResult &FindOpponent(int Player, int Opponent);
|
|
52
|
+
|
|
53
|
+
float CountGames(int Player) const;
|
|
54
|
+
int CountTrueGames(int Player) const;
|
|
55
|
+
float CountDraws(int Player) const;
|
|
56
|
+
float Score(int Player) const; // win = 2, draw = 1
|
|
57
|
+
double AverageOpponent(int Player, const double *pelo) const;
|
|
58
|
+
|
|
59
|
+
void Dump(std::ostream &out) const;
|
|
60
|
+
|
|
61
|
+
~CCondensedResults();
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
#endif
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// R�mi Coulom
|
|
4
|
+
//
|
|
5
|
+
// January, 2005
|
|
6
|
+
//
|
|
7
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
#ifndef CDiscretization_Declared
|
|
9
|
+
#define CDiscretization_Declared
|
|
10
|
+
|
|
11
|
+
class CDiscretization
|
|
12
|
+
{
|
|
13
|
+
protected: /////////////////////////////////////////////////////////////////
|
|
14
|
+
const int Size;
|
|
15
|
+
const double Min;
|
|
16
|
+
const double Max;
|
|
17
|
+
|
|
18
|
+
public: ////////////////////////////////////////////////////////////////////
|
|
19
|
+
CDiscretization(int SizeInit, double MinInit, double MaxInit):
|
|
20
|
+
Size(SizeInit),
|
|
21
|
+
Min(MinInit),
|
|
22
|
+
Max(MaxInit)
|
|
23
|
+
{}
|
|
24
|
+
|
|
25
|
+
int GetDiscretizationSize() const {return Size;}
|
|
26
|
+
|
|
27
|
+
int IndexFromValue(double x) const
|
|
28
|
+
{
|
|
29
|
+
return int(0.5 + (Size - 1) * (x - Min) / (Max - Min));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
double ValueFromIndex(int i) const
|
|
33
|
+
{
|
|
34
|
+
return Min + ((i + 0.5) * (Max - Min)) / Size;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
virtual ~CDiscretization() {}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
#endif
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// R�mi Coulom
|
|
4
|
+
//
|
|
5
|
+
// December, 2004
|
|
6
|
+
//
|
|
7
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
#include "CDistribution.h"
|
|
9
|
+
|
|
10
|
+
#include <cmath>
|
|
11
|
+
|
|
12
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
13
|
+
// reset to zero
|
|
14
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
15
|
+
void CDistribution::Reset()
|
|
16
|
+
{
|
|
17
|
+
for (int i = vDistribution.size(); --i >= 0;)
|
|
18
|
+
vDistribution[i] = 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
22
|
+
// set to uniform distribution
|
|
23
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
24
|
+
void CDistribution::SetUniform()
|
|
25
|
+
{
|
|
26
|
+
for (int i = vDistribution.size(); --i >= 0;)
|
|
27
|
+
vDistribution[i] = 1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
31
|
+
// normalize
|
|
32
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
33
|
+
void CDistribution::Normalize()
|
|
34
|
+
{
|
|
35
|
+
double Total = 0;
|
|
36
|
+
for (int i = vDistribution.size(); --i >= 0;)
|
|
37
|
+
Total += vDistribution[i];
|
|
38
|
+
if (Total > 0)
|
|
39
|
+
for (int i = vDistribution.size(); --i >= 0;)
|
|
40
|
+
vDistribution[i] /= Total;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
44
|
+
// Normalize, assuming the distribution contains log-likelihoods
|
|
45
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
46
|
+
void CDistribution::LogNormalize()
|
|
47
|
+
{
|
|
48
|
+
double Max = vDistribution[0];
|
|
49
|
+
for (int i = vDistribution.size(); --i > 0;)
|
|
50
|
+
if (vDistribution[i] > Max)
|
|
51
|
+
Max = vDistribution[i];
|
|
52
|
+
|
|
53
|
+
for (int i = vDistribution.size(); --i >= 0;)
|
|
54
|
+
vDistribution[i] = std::exp(vDistribution[i] - Max);
|
|
55
|
+
|
|
56
|
+
Normalize();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
60
|
+
// Discrete GetBound
|
|
61
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
62
|
+
int CDistribution::GetBoundIndex(double Confidence,
|
|
63
|
+
int Begin,
|
|
64
|
+
int End,
|
|
65
|
+
int Direction) const
|
|
66
|
+
{
|
|
67
|
+
const double Threshold = (1 - Confidence) / 2;
|
|
68
|
+
double P = 0;
|
|
69
|
+
|
|
70
|
+
for (int i = Begin; i != End; i += Direction)
|
|
71
|
+
{
|
|
72
|
+
P += vDistribution[i];
|
|
73
|
+
if (P > Threshold)
|
|
74
|
+
return i;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return End;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
81
|
+
// Discrete lower bound
|
|
82
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
83
|
+
int CDistribution::GetLowerIndex(double Confidence) const
|
|
84
|
+
{
|
|
85
|
+
return GetBoundIndex(Confidence, 0, vDistribution.size(), +1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
89
|
+
// Discrete upper bound
|
|
90
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
91
|
+
int CDistribution::GetUpperIndex(double Confidence) const
|
|
92
|
+
{
|
|
93
|
+
return GetBoundIndex(Confidence, vDistribution.size() - 1, -1, -1);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
97
|
+
// Binomial distribution
|
|
98
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
99
|
+
CDistribution BinomialDistribution(double p)
|
|
100
|
+
{
|
|
101
|
+
CDistribution distResult(2);
|
|
102
|
+
distResult.vDistribution[0] = 1 - p;
|
|
103
|
+
distResult.vDistribution[1] = p;
|
|
104
|
+
return distResult;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
108
|
+
// Distribution of the sum
|
|
109
|
+
/////////////////////////////////////////////////////////////////////////////
|
|
110
|
+
CDistribution SumDistribution(const CDistribution &dist1,
|
|
111
|
+
const CDistribution &dist2)
|
|
112
|
+
{
|
|
113
|
+
CDistribution distResult(dist1.GetSize() + dist2.GetSize() - 1);
|
|
114
|
+
|
|
115
|
+
distResult.Reset();
|
|
116
|
+
|
|
117
|
+
for (int i1 = dist1.GetSize(); --i1 >= 0;)
|
|
118
|
+
for (int i2 = dist2.GetSize(); --i2 >= 0;)
|
|
119
|
+
distResult.vDistribution[i1 + i2] += dist1.vDistribution[i1] *
|
|
120
|
+
dist2.vDistribution[i2];
|
|
121
|
+
|
|
122
|
+
return distResult;
|
|
123
|
+
}
|