all-up-in-your-elo 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/.document +5 -0
  2. data/Gemfile +14 -0
  3. data/Gemfile.lock +22 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.rdoc +42 -0
  6. data/Rakefile +54 -0
  7. data/VERSION +1 -0
  8. data/all-up-in-your-elo.gemspec +148 -0
  9. data/ext/bayeselo/.gitignore +4 -0
  10. data/ext/bayeselo/CBradleyTerry.cpp +670 -0
  11. data/ext/bayeselo/CBradleyTerry.h +137 -0
  12. data/ext/bayeselo/CCDistribution.cpp +135 -0
  13. data/ext/bayeselo/CCDistribution.h +44 -0
  14. data/ext/bayeselo/CCDistributionCUI.cpp +104 -0
  15. data/ext/bayeselo/CCDistributionCUI.h +38 -0
  16. data/ext/bayeselo/CCondensedResults.cpp +253 -0
  17. data/ext/bayeselo/CCondensedResults.h +64 -0
  18. data/ext/bayeselo/CDiscretization.h +40 -0
  19. data/ext/bayeselo/CDistribution.cpp +123 -0
  20. data/ext/bayeselo/CDistribution.h +58 -0
  21. data/ext/bayeselo/CDistributionCollection.cpp +34 -0
  22. data/ext/bayeselo/CDistributionCollection.h +29 -0
  23. data/ext/bayeselo/CEloRatingCUI.cpp +1046 -0
  24. data/ext/bayeselo/CEloRatingCUI.h +67 -0
  25. data/ext/bayeselo/CIndirectCompare.h +28 -0
  26. data/ext/bayeselo/CJointBayesian.cpp +111 -0
  27. data/ext/bayeselo/CJointBayesian.h +38 -0
  28. data/ext/bayeselo/CLUDecomposition.cpp +154 -0
  29. data/ext/bayeselo/CLUDecomposition.h +36 -0
  30. data/ext/bayeselo/CMatrix.cpp +48 -0
  31. data/ext/bayeselo/CMatrix.h +35 -0
  32. data/ext/bayeselo/CMatrixIO.cpp +27 -0
  33. data/ext/bayeselo/CMatrixIO.h +17 -0
  34. data/ext/bayeselo/CPredictionCUI.cpp +393 -0
  35. data/ext/bayeselo/CPredictionCUI.h +50 -0
  36. data/ext/bayeselo/CResultSet.cpp +245 -0
  37. data/ext/bayeselo/CResultSet.h +50 -0
  38. data/ext/bayeselo/CResultSetCUI.cpp +355 -0
  39. data/ext/bayeselo/CResultSetCUI.h +44 -0
  40. data/ext/bayeselo/CTimeIO.cpp +58 -0
  41. data/ext/bayeselo/CTimeIO.h +19 -0
  42. data/ext/bayeselo/CVector.cpp +110 -0
  43. data/ext/bayeselo/CVector.h +42 -0
  44. data/ext/bayeselo/EloDataFromFile.cpp +120 -0
  45. data/ext/bayeselo/EloDataFromFile.h +21 -0
  46. data/ext/bayeselo/README +8 -0
  47. data/ext/bayeselo/ReadLineToString.cpp +32 -0
  48. data/ext/bayeselo/ReadLineToString.h +18 -0
  49. data/ext/bayeselo/chtime.cpp +56 -0
  50. data/ext/bayeselo/chtime.h +61 -0
  51. data/ext/bayeselo/chtimer.h +27 -0
  52. data/ext/bayeselo/clktimer.cpp +178 -0
  53. data/ext/bayeselo/clktimer.h +29 -0
  54. data/ext/bayeselo/consolui.cpp +538 -0
  55. data/ext/bayeselo/consolui.h +154 -0
  56. data/ext/bayeselo/const.cpp +79 -0
  57. data/ext/bayeselo/const.h +42 -0
  58. data/ext/bayeselo/date.cpp +96 -0
  59. data/ext/bayeselo/date.h +44 -0
  60. data/ext/bayeselo/debug.h +58 -0
  61. data/ext/bayeselo/elomain.cpp +26 -0
  62. data/ext/bayeselo/eloratings.cpp +76 -0
  63. data/ext/bayeselo/extconf.rb +6 -0
  64. data/ext/bayeselo/list.h +367 -0
  65. data/ext/bayeselo/listi.h +125 -0
  66. data/ext/bayeselo/move.cpp +249 -0
  67. data/ext/bayeselo/move.h +139 -0
  68. data/ext/bayeselo/pgn.h +62 -0
  69. data/ext/bayeselo/pgnlex.cpp +432 -0
  70. data/ext/bayeselo/pgnlex.h +105 -0
  71. data/ext/bayeselo/pgnstr.cpp +126 -0
  72. data/ext/bayeselo/piece.h +44 -0
  73. data/ext/bayeselo/player.h +31 -0
  74. data/ext/bayeselo/position.h +89 -0
  75. data/ext/bayeselo/random.cpp +114 -0
  76. data/ext/bayeselo/random.h +63 -0
  77. data/ext/bayeselo/readstr.cpp +51 -0
  78. data/ext/bayeselo/readstr.h +19 -0
  79. data/ext/bayeselo/square.h +61 -0
  80. data/ext/bayeselo/str.cpp +81 -0
  81. data/ext/bayeselo/str.h +71 -0
  82. data/ext/bayeselo/version.cpp +30 -0
  83. data/ext/bayeselo/version.h +28 -0
  84. data/ext/bayeselo/version_number.h +1 -0
  85. data/lib/elo_ratings.rb +9 -0
  86. data/lib/elo_ratings/c_bayeselo.rb +5 -0
  87. data/lib/elo_ratings/results.rb +41 -0
  88. data/test/elo_ratings/test_c_bayeselo.rb +84 -0
  89. data/test/elo_ratings/test_results.rb +41 -0
  90. data/test/helper.rb +19 -0
  91. metadata +233 -0
@@ -0,0 +1,67 @@
1
+ ////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // CEloRatingCUI.h
4
+ //
5
+ // Remi Coulom
6
+ //
7
+ // December, 2004
8
+ //
9
+ ////////////////////////////////////////////////////////////////////////////
10
+ #ifndef CEloRatingCUI_Declared
11
+ #define CEloRatingCUI_Declared
12
+
13
+ #include "consolui.h" // CConsoleUI
14
+ #include "CCondensedResults.h"
15
+ #include "CBradleyTerry.h"
16
+
17
+ #include <vector>
18
+ #include <string>
19
+
20
+ class CResultSet;
21
+ class CDistributionCollection;
22
+ class CPredictionCUI;
23
+
24
+ class CEloRatingCUI : public CConsoleUI // ercui
25
+ {
26
+ friend class CPredictionCUI;
27
+
28
+ private: //////////////////////////////////////////////////////////////////
29
+ static const char * const tszCommands[];
30
+ const CResultSet &rs;
31
+ CCondensedResults crs;
32
+ std::vector<std::string> vecName;
33
+ unsigned MaxNameLength;
34
+ std::vector<int> vPermutation;
35
+ std::vector<double> veloLower;
36
+ std::vector<double> veloUpper;
37
+ CDistributionCollection *pdc;
38
+
39
+ double Confidence;
40
+ double eloMin;
41
+ double eloMax;
42
+ int Resolution;
43
+ double eloOffset;
44
+ double EloScale;
45
+ float Prior;
46
+
47
+ CBradleyTerry bt;
48
+ int fLOSComputed;
49
+
50
+ void ComputeVariance();
51
+
52
+ protected: ////////////////////////////////////////////////////////////////
53
+ virtual int ProcessCommand(const char *pszCommand,
54
+ const char *pszParameters,
55
+ std::istream &in,
56
+ std::ostream &out);
57
+
58
+ virtual void PrintLocalPrompt(std::ostream &out);
59
+
60
+ public: ///////////////////////////////////////////////////////////////////
61
+ CEloRatingCUI(const CResultSet &rsInit,
62
+ const std::vector<std::string> &vecNameInit,
63
+ CConsoleUI *pcui = 0,
64
+ int openmode = OpenModal);
65
+ };
66
+
67
+ #endif
@@ -0,0 +1,28 @@
1
+ /////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // CIndirectCompare.h
4
+ //
5
+ // R�mi Coulom
6
+ //
7
+ // December, 2005
8
+ //
9
+ /////////////////////////////////////////////////////////////////////////////
10
+ #ifndef CIndirectCompare_Declared
11
+ #define CIndirectCompare_Declared
12
+
13
+ template<class T>
14
+ class CIndirectCompare
15
+ {
16
+ private:
17
+ const T *pelo;
18
+
19
+ public:
20
+ CIndirectCompare(const T *p) : pelo(p) {}
21
+
22
+ bool operator()(int i, int j) const
23
+ {
24
+ return pelo[i] > pelo[j];
25
+ }
26
+ };
27
+
28
+ #endif
@@ -0,0 +1,111 @@
1
+ /////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // R�mi Coulom
4
+ //
5
+ // December, 2004
6
+ //
7
+ /////////////////////////////////////////////////////////////////////////////
8
+ #include "CJointBayesian.h"
9
+ #include "CResultSet.h"
10
+ #include "CCDistribution.h"
11
+ #include "CDistributionCollection.h"
12
+ #include "CBradleyTerry.h"
13
+
14
+ #include <iostream>
15
+
16
+ /////////////////////////////////////////////////////////////////////////////
17
+ // Constructor
18
+ /////////////////////////////////////////////////////////////////////////////
19
+ CJointBayesian::CJointBayesian(const CResultSet &rsInit,
20
+ CDistributionCollection &dcInit,
21
+ const CBradleyTerry &btInit) :
22
+ rs(rsInit),
23
+ dc(dcInit),
24
+ bt(btInit),
25
+ indexMax(dc.GetDiscretizationSize() - 1)
26
+ {
27
+ pindex = new int[rs.GetPlayers()];
28
+
29
+ //
30
+ // Pre-compute probability cache
31
+ //
32
+ pProbabilityCache = new double[3 * (indexMax * 2 + 1)];
33
+ for (int Result = 3; --Result >= 0;)
34
+ for (int i = indexMax * 2 + 1; --i >= 0;)
35
+ pProbabilityCache[i + Result * (indexMax * 2 + 1)] =
36
+ bt.ResultProbability(dc.ValueFromIndex(i) - dc.ValueFromIndex(indexMax),
37
+ Result);
38
+ }
39
+
40
+ /////////////////////////////////////////////////////////////////////////////
41
+ // Destructor
42
+ /////////////////////////////////////////////////////////////////////////////
43
+ CJointBayesian::~CJointBayesian()
44
+ {
45
+ delete[] pProbabilityCache;
46
+ delete[] pindex;
47
+ }
48
+
49
+ /////////////////////////////////////////////////////////////////////////////
50
+ // Get probability from indices
51
+ /////////////////////////////////////////////////////////////////////////////
52
+ double CJointBayesian::GetProbability() const
53
+ {
54
+ double P = 1;
55
+
56
+ for (int i = rs.GetGames(); --i >= 0;)
57
+ {
58
+ #if 0
59
+ P *= bt.ResultProbability(dc.ValueFromIndex(pindex[rs.GetWhite(i)]),
60
+ dc.ValueFromIndex(pindex[rs.GetBlack(i)]),
61
+ rs.GetResult(i));
62
+ #else
63
+ P *= pProbabilityCache[indexMax +
64
+ pindex[rs.GetWhite(i)] -
65
+ pindex[rs.GetBlack(i)] +
66
+ rs.GetResult(i) * (indexMax * 2 + 1)];
67
+ #endif
68
+ }
69
+
70
+ return P;
71
+ }
72
+
73
+ /////////////////////////////////////////////////////////////////////////////
74
+ // Recursive helper function
75
+ /////////////////////////////////////////////////////////////////////////////
76
+ void CJointBayesian::RecursiveJointBayesian(int player, int indexTotal)
77
+ {
78
+ if (player < 0)
79
+ {
80
+ double p = GetProbability();
81
+ for (int i = rs.GetPlayers(); --i >= 0;)
82
+ dc.GetDistribution(i).Add(pindex[i], p);
83
+ return;
84
+ }
85
+
86
+ int max = indexMax;
87
+ if (max > indexTotal)
88
+ max = indexTotal;
89
+
90
+ int min = 0;
91
+ if (min < indexTotal - player * indexMax)
92
+ min = indexTotal - player * indexMax;
93
+
94
+ for (pindex[player] = max + 1; --pindex[player] >= min;)
95
+ RecursiveJointBayesian(player - 1, indexTotal - pindex[player]);
96
+ }
97
+
98
+ /////////////////////////////////////////////////////////////////////////////
99
+ // Estimate rating distributions
100
+ /////////////////////////////////////////////////////////////////////////////
101
+ void CJointBayesian::RunComputation()
102
+ {
103
+ for (int i = rs.GetPlayers(); --i >= 0;)
104
+ dc.GetDistribution(i).Reset();
105
+
106
+ RecursiveJointBayesian(rs.GetPlayers() - 1,
107
+ dc.GetDiscretizationSize() * rs.GetPlayers() / 2);
108
+
109
+ for (int i = rs.GetPlayers(); --i >= 0;)
110
+ dc.GetDistribution(i).Normalize();
111
+ }
@@ -0,0 +1,38 @@
1
+ /////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // R�mi Coulom
4
+ //
5
+ // December, 2004
6
+ //
7
+ /////////////////////////////////////////////////////////////////////////////
8
+ #ifndef CJointBayesian_Declared
9
+ #define CJointBayesian_Declared
10
+
11
+ class CResultSet;
12
+ class CDistributionCollection;
13
+ class CBradleyTerry;
14
+
15
+ class CJointBayesian
16
+ {
17
+ private: //////////////////////////////////////////////////////////////////
18
+ const CResultSet &rs;
19
+ CDistributionCollection &dc;
20
+ const CBradleyTerry &bt;
21
+ int *pindex;
22
+ int indexMax;
23
+ double *pProbabilityCache;
24
+
25
+ void RecursiveJointBayesian(int player, int indexTotal);
26
+ double GetProbability() const;
27
+
28
+ public: ///////////////////////////////////////////////////////////////////
29
+ CJointBayesian(const CResultSet &rsInit,
30
+ CDistributionCollection &dcInit,
31
+ const CBradleyTerry &btInit);
32
+
33
+ void RunComputation();
34
+
35
+ ~CJointBayesian();
36
+ };
37
+
38
+ #endif
@@ -0,0 +1,154 @@
1
+ ////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // CLUDecomposition.cpp
4
+ //
5
+ // Remi Coulom
6
+ //
7
+ // September, 2000
8
+ //
9
+ ////////////////////////////////////////////////////////////////////////////
10
+ #include "CLUDecomposition.h"
11
+
12
+ #include <math.h>
13
+
14
+ ////////////////////////////////////////////////////////////////////////////
15
+ // Constructor
16
+ ////////////////////////////////////////////////////////////////////////////
17
+ CLUDecomposition::CLUDecomposition(int nInit) :
18
+ n(nInit)
19
+ {
20
+ pdImplicitScaling = new double[n];
21
+ pdy = new double[n];
22
+ }
23
+
24
+ ////////////////////////////////////////////////////////////////////////////
25
+ // Decompose
26
+ ////////////////////////////////////////////////////////////////////////////
27
+ void CLUDecomposition::Decompose(double *pdMatrix, int *pIndex) const
28
+ {
29
+ //
30
+ // Loop over rows to get the implicit scaling information
31
+ //
32
+ for (int i = n; --i >= 0;)
33
+ {
34
+ pIndex[i] = i;
35
+ double Max = 0.0;
36
+ for (int j = n; --j >= 0;)
37
+ {
38
+ double a = fabs(pdMatrix[i * n + j]);
39
+ if (a > Max)
40
+ Max = a;
41
+ }
42
+ pdImplicitScaling[i] = 1.0 / Max;
43
+ }
44
+
45
+ //
46
+ // Main loop over columns
47
+ //
48
+ for (int j = 0; j < n; j++)
49
+ {
50
+ for (int i = 0; i < j; i++)
51
+ {
52
+ double Sum = pdMatrix[i * n + j];
53
+ for (int k = 0; k < i; k++)
54
+ Sum -= pdMatrix[i * n + k] * pdMatrix[k * n + j];
55
+ pdMatrix[i * n + j] = Sum;
56
+ }
57
+
58
+ int iMax = j;
59
+ double Max = 0.0;
60
+ for (int i = j; i < n; i++)
61
+ {
62
+ double Sum = pdMatrix[i * n + j];
63
+ for (int k = 0; k < j; k++)
64
+ Sum -= pdMatrix[i * n + k] * pdMatrix[k * n + j];
65
+ pdMatrix[i * n + j] = Sum;
66
+ double a = pdImplicitScaling[i] * fabs(Sum);
67
+ if (a > Max)
68
+ {
69
+ Max = a;
70
+ iMax = i;
71
+ }
72
+ }
73
+
74
+ if (iMax != j)
75
+ {
76
+ for (int k = n; --k >= 0;)
77
+ {
78
+ double tmp = pdMatrix[iMax * n + k];
79
+ pdMatrix[iMax * n + k] = pdMatrix[j * n + k];
80
+ pdMatrix[j * n + k] = tmp;
81
+ }
82
+ pdImplicitScaling[iMax] = pdImplicitScaling[j];
83
+ {
84
+ int tmp = pIndex[j];
85
+ pIndex[j] = pIndex[iMax];
86
+ pIndex[iMax] = tmp;
87
+ }
88
+ }
89
+
90
+ {
91
+ double x = 1.0 / pdMatrix[j * n + j];
92
+ for (int i = j + 1; i < n; i++)
93
+ pdMatrix[i * n + j] *= x;
94
+ }
95
+ }
96
+ }
97
+
98
+ ////////////////////////////////////////////////////////////////////////////
99
+ // Backsubstitution
100
+ ////////////////////////////////////////////////////////////////////////////
101
+ void CLUDecomposition::Solve(const double *pdMatrix,
102
+ const int *pIndex,
103
+ const double *pb,
104
+ double *px) const
105
+ {
106
+ for (int i = 0; i < n; i++)
107
+ {
108
+ double Sum = pb[pIndex[i]];
109
+ for (int j = 0; j < i; j++)
110
+ Sum -= pdMatrix[i * n + j] * px[j];
111
+ px[i] = Sum;
112
+ }
113
+ for (int i = n; --i >= 0;)
114
+ {
115
+ double Sum = px[i];
116
+ for (int j = i + 1; j < n; j++)
117
+ Sum -= pdMatrix[i * n + j] * px[j];
118
+ px[i] = Sum / pdMatrix[i * n + i];
119
+ }
120
+ }
121
+
122
+ ////////////////////////////////////////////////////////////////////////////
123
+ // SolveTranspose
124
+ ////////////////////////////////////////////////////////////////////////////
125
+ void CLUDecomposition::SolveTranspose(const double *pdMatrix,
126
+ const int *pIndex,
127
+ const double *pb,
128
+ double *px) const
129
+ {
130
+ for (int i = 0; i < n; i++)
131
+ {
132
+ double Sum = pb[i];
133
+ for (int j = 0; j < i; j++)
134
+ Sum -= pdMatrix[j * n + i] * pdy[j];
135
+ pdy[i] = Sum / pdMatrix[i * n + i];
136
+ }
137
+ for (int i = n; --i >= 0;)
138
+ {
139
+ double Sum = pdy[i];
140
+ for (int j = i + 1; j < n; j++)
141
+ Sum -= pdMatrix[j * n + i] * pdy[j];
142
+ pdy[i] = Sum;
143
+ px[pIndex[i]] = pdy[i];
144
+ }
145
+ }
146
+
147
+ ////////////////////////////////////////////////////////////////////////////
148
+ // Destructor
149
+ ////////////////////////////////////////////////////////////////////////////
150
+ CLUDecomposition::~CLUDecomposition()
151
+ {
152
+ delete[] pdImplicitScaling;
153
+ delete[] pdy;
154
+ }
@@ -0,0 +1,36 @@
1
+ ////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // CLUDecomposition.h
4
+ //
5
+ // Remi Coulom
6
+ //
7
+ // September, 2000
8
+ //
9
+ ////////////////////////////////////////////////////////////////////////////
10
+ #ifndef Math_CLUDecomposition_Declared
11
+ #define Math_CLUDecomposition_Declared
12
+
13
+ class CLUDecomposition // lud
14
+ {
15
+ private: /////////////////////////////////////////////////////////////////
16
+ int n;
17
+ double *pdImplicitScaling;
18
+ double *pdy;
19
+
20
+ public: //////////////////////////////////////////////////////////////////
21
+ CLUDecomposition(int nInit);
22
+
23
+ void Decompose(double *pdMatrix, int *pIndex) const;
24
+ void Solve(const double *pdMatrix,
25
+ const int *pIndex,
26
+ const double *pb,
27
+ double *px) const;
28
+ void SolveTranspose(const double *pdMatrix,
29
+ const int *pIndex,
30
+ const double *pb,
31
+ double *px) const;
32
+
33
+ ~CLUDecomposition();
34
+ };
35
+
36
+ #endif
@@ -0,0 +1,48 @@
1
+ /////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // CMatrix.cpp
4
+ //
5
+ // R�mi Coulom
6
+ //
7
+ // February, 2005
8
+ //
9
+ /////////////////////////////////////////////////////////////////////////////
10
+ #include "CMatrix.h"
11
+ #include "debug.h"
12
+
13
+ /////////////////////////////////////////////////////////////////////////////
14
+ // Constructor
15
+ /////////////////////////////////////////////////////////////////////////////
16
+ CMatrix::CMatrix(int RowsInit, int ColumnsInit):
17
+ CVector(RowsInit * ColumnsInit),
18
+ Rows(RowsInit),
19
+ Columns(ColumnsInit)
20
+ {
21
+ }
22
+
23
+ /////////////////////////////////////////////////////////////////////////////
24
+ // Set size
25
+ /////////////////////////////////////////////////////////////////////////////
26
+ void CMatrix::SetSize(int RowsInit, int ColumnsInit)
27
+ {
28
+ Rows = RowsInit;
29
+ Columns = ColumnsInit;
30
+ CVector::SetSize(Rows * Columns);
31
+ }
32
+
33
+ /////////////////////////////////////////////////////////////////////////////
34
+ // Set to mA multiplied by the transpose of mB
35
+ /////////////////////////////////////////////////////////////////////////////
36
+ void CMatrix::SetProductByTranspose(const CMatrix &mA, const CMatrix &mB)
37
+ {
38
+ FATAL(mA.GetColumns() != mB.GetColumns());
39
+ SetSize(mA.GetRows(), mB.GetRows());
40
+ for (int i = mA.GetRows(); --i >= 0;)
41
+ for (int j = mB.GetRows(); --j >= 0;)
42
+ {
43
+ double x = 0.0;
44
+ for (int k = mA.GetColumns(); --k >= 0;)
45
+ x += mA.GetElement(i, k) * mB.GetElement(j, k);
46
+ SetElement(i, j, x);
47
+ }
48
+ }