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.
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 +23 -0
  6. data/Rakefile +54 -0
  7. data/VERSION +1 -0
  8. data/elo-ratings.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/extconf.rb +6 -0
  63. data/ext/bayeselo/list.h +367 -0
  64. data/ext/bayeselo/listi.h +125 -0
  65. data/ext/bayeselo/move.cpp +249 -0
  66. data/ext/bayeselo/move.h +139 -0
  67. data/ext/bayeselo/pgn.h +62 -0
  68. data/ext/bayeselo/pgnlex.cpp +432 -0
  69. data/ext/bayeselo/pgnlex.h +105 -0
  70. data/ext/bayeselo/pgnstr.cpp +126 -0
  71. data/ext/bayeselo/piece.h +44 -0
  72. data/ext/bayeselo/player.h +31 -0
  73. data/ext/bayeselo/position.h +89 -0
  74. data/ext/bayeselo/random.cpp +114 -0
  75. data/ext/bayeselo/random.h +63 -0
  76. data/ext/bayeselo/rb_bayeselo.cpp +76 -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/bayeselo.rb +6 -0
  86. data/lib/bayeselo/bayeselo.rb +11 -0
  87. data/lib/bayeselo/c_bayeselo.rb +7 -0
  88. data/test/bayeselo/test_bayeselo.rb +33 -0
  89. data/test/bayeselo/test_c_bayeselo.rb +84 -0
  90. data/test/helper.rb +18 -0
  91. metadata +226 -0
@@ -0,0 +1,137 @@
1
+ /////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // R�mi Coulom
4
+ //
5
+ // December, 2004
6
+ //
7
+ /////////////////////////////////////////////////////////////////////////////
8
+ #ifndef CBradleyTerry_Declared
9
+ #define CBradleyTerry_Declared
10
+
11
+ class CResultSet;
12
+ class CCondensedResults;
13
+ class CCDistribution;
14
+ class CDistributionCollection;
15
+
16
+ #include "CMatrix.h"
17
+
18
+ #include <vector>
19
+ #include <cmath>
20
+
21
+ class CBradleyTerry // bt
22
+ {
23
+ private: ///////////////////////////////////////////////////////////////////
24
+ const CCondensedResults &crs;
25
+
26
+ mutable std::vector<double> velo;
27
+ mutable double eloAdvantage;
28
+ mutable double eloDraw;
29
+
30
+ mutable std::vector<double> v1;
31
+ mutable std::vector<double> v2;
32
+ mutable double *pGamma;
33
+ mutable double *pNextGamma;
34
+ mutable double ThetaW;
35
+ mutable double ThetaD;
36
+
37
+ CMatrix mCovariance;
38
+ CMatrix mLOS;
39
+ CMatrix mWinProbability;
40
+ CMatrix mLossProbability;
41
+ CMatrix mDrawProbability;
42
+
43
+ void ConvertEloToGamma() const;
44
+ void UpdateGammas();
45
+ double UpdateThetaW();
46
+ double UpdateThetaD();
47
+ double GetDifference(int n, const double *pd1, const double *pd2);
48
+
49
+ public: ////////////////////////////////////////////////////////////////////
50
+ CBradleyTerry(const CCondensedResults &crsInit);
51
+
52
+ //
53
+ // Gets
54
+ //
55
+ const double *GetElo() const {return &velo[0];}
56
+ double GetElo(int i) const {return velo[i];}
57
+ double GetAdvantage() const {return eloAdvantage;}
58
+ double GetDrawElo() const {return eloDraw;}
59
+
60
+ //
61
+ // Sets
62
+ //
63
+ double *GetElo() {return &velo[0];}
64
+ void SetElo(int i, double x) {velo[i] = x;}
65
+ void SetAdvantage(double x) {eloAdvantage = x;}
66
+ void SetDrawElo(double x) {eloDraw = x;}
67
+
68
+ //
69
+ // Methods to compute elo ratings
70
+ //
71
+ void MinorizationMaximization(int fThetaW,
72
+ int fThetaD,
73
+ double Epsilon = 1e-5);
74
+ void ELOstat(double Epsilon = 1e-5);
75
+ void ELOstatIntervals(double *peloLower, double *peloUpper) const;
76
+
77
+ //
78
+ // Basic Probabilities
79
+ //
80
+ static double Probability(double eloDelta)
81
+ {
82
+ return 1 / (1 + std::pow(10.0, eloDelta / 400.0));
83
+ }
84
+
85
+ double ElostatProbability(double eloDelta) const
86
+ {
87
+ return Probability(-eloDelta - eloAdvantage);
88
+ }
89
+
90
+ double WinProbability(double eloDelta) const
91
+ {
92
+ return Probability(-eloDelta - eloAdvantage + eloDraw);
93
+ }
94
+
95
+ double LossProbability(double eloDelta) const
96
+ {
97
+ return Probability(eloDelta + eloAdvantage + eloDraw);
98
+ }
99
+
100
+ double DrawProbability(double eloDelta) const
101
+ {
102
+ return 1 - WinProbability(eloDelta) - LossProbability(eloDelta);
103
+ }
104
+
105
+ double ResultProbability(double eloDelta, int Result) const
106
+ {
107
+ if (Result == 0)
108
+ return LossProbability(eloDelta);
109
+ else if (Result == 1)
110
+ return DrawProbability(eloDelta);
111
+ else
112
+ return WinProbability(eloDelta);
113
+ }
114
+
115
+ //
116
+ // Likelihood of results
117
+ //
118
+ double LogLikelihood(int Player) const;
119
+ double LogLikelihood() const;
120
+ double LogLikelihood(const CResultSet &rs) const;
121
+
122
+ //
123
+ // Likelihood distributions
124
+ //
125
+ void GetPlayerDist(int Player, CCDistribution &cdist) const;
126
+ void GetAdvantageDist(CCDistribution &cdist) const;
127
+ void GetDrawEloDist(CCDistribution &cdist) const;
128
+
129
+ void GetVariance(double *pdVariance) const;
130
+ void ComputeCovariance();
131
+ void ComputeLikelihoodOfSuperiority();
132
+
133
+ const CMatrix &GetCovariance() const {return mCovariance;}
134
+ const CMatrix &GetLikelihoodOfSuperiority() const {return mLOS;}
135
+ };
136
+
137
+ #endif
@@ -0,0 +1,135 @@
1
+ /////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // R�mi Coulom
4
+ //
5
+ // December, 2004
6
+ //
7
+ /////////////////////////////////////////////////////////////////////////////
8
+ #include "CCDistribution.h"
9
+
10
+ #include <cmath>
11
+
12
+ /////////////////////////////////////////////////////////////////////////////
13
+ // Get Bound helper function
14
+ /////////////////////////////////////////////////////////////////////////////
15
+ double CCDistribution::GetBoundValue(double Confidence,
16
+ int Begin,
17
+ int End,
18
+ int Direction) const
19
+ {
20
+ const double Threshold = (1 - Confidence) / 2;
21
+ double P = 0;
22
+ double PreviousDistribution = 0;
23
+
24
+ for (int i = Begin; i != End; i += Direction)
25
+ {
26
+ double NewDistribution = vDistribution[i];
27
+ double NewP = P + (PreviousDistribution + NewDistribution) / 2;
28
+ if (NewP >= Threshold)
29
+ {
30
+ double Value = ValueFromIndex(i - Direction);
31
+ double NewValue = ValueFromIndex(i);
32
+ #if 0
33
+ double Delta = PreviousDistribution * PreviousDistribution -
34
+ 4 * (P - Threshold) * (NewDistribution - PreviousDistribution) / 2;
35
+ double Alpha = (-PreviousDistribution + std::sqrt(Delta)) /
36
+ (NewDistribution - PreviousDistribution);
37
+ return Value + (NewValue - Value) * Alpha;
38
+ #else
39
+ // The second-order approximation above causes problems when NewD=PreviousD
40
+ return Value + (NewValue - Value) * (Threshold - P) / (NewP - P);
41
+ #endif
42
+ }
43
+ P = NewP;
44
+ PreviousDistribution = NewDistribution;
45
+ }
46
+
47
+ return ValueFromIndex(End - Direction);
48
+ }
49
+
50
+ /////////////////////////////////////////////////////////////////////////////
51
+ // Get Lower Bound
52
+ /////////////////////////////////////////////////////////////////////////////
53
+ double CCDistribution::GetLowerValue(double Confidence) const
54
+ {
55
+ return GetBoundValue(Confidence, 0, vDistribution.size(), +1);
56
+ }
57
+
58
+ /////////////////////////////////////////////////////////////////////////////
59
+ // Get Upper Bound
60
+ /////////////////////////////////////////////////////////////////////////////
61
+ double CCDistribution::GetUpperValue(double Confidence) const
62
+ {
63
+ return GetBoundValue(Confidence, vDistribution.size() - 1, -1, -1);
64
+ }
65
+
66
+ /////////////////////////////////////////////////////////////////////////////
67
+ // Get Most Likely Value
68
+ /////////////////////////////////////////////////////////////////////////////
69
+ double CCDistribution::GetMostLikelyValue() const
70
+ {
71
+ double MaxP = vDistribution[0];
72
+ int iMax = 0;
73
+ for (int i = vDistribution.size(); --i > 0;)
74
+ if (vDistribution[i] > MaxP)
75
+ {
76
+ iMax = i;
77
+ MaxP = vDistribution[i];
78
+ }
79
+ return ValueFromIndex(iMax);
80
+ }
81
+
82
+ /////////////////////////////////////////////////////////////////////////////
83
+ // Mean
84
+ /////////////////////////////////////////////////////////////////////////////
85
+ double CCDistribution::GetMean() const
86
+ {
87
+ double Result = 0;
88
+ for (int i = vDistribution.size(); --i > 0;)
89
+ Result += vDistribution[i] * ValueFromIndex(i);
90
+ return Result;
91
+ }
92
+
93
+ /////////////////////////////////////////////////////////////////////////////
94
+ // Variance
95
+ /////////////////////////////////////////////////////////////////////////////
96
+ double CCDistribution::GetVariance(double Mean) const
97
+ {
98
+ double Result = 0;
99
+ for (int i = vDistribution.size(); --i > 0;)
100
+ {
101
+ double x = ValueFromIndex(i);
102
+ Result += vDistribution[i] * (x - Mean) * (x - Mean);
103
+ }
104
+ return Result;
105
+ }
106
+
107
+ /////////////////////////////////////////////////////////////////////////////
108
+ // Gaussian
109
+ /////////////////////////////////////////////////////////////////////////////
110
+ void CCDistribution::SetNormal(double Mu, double Sigma)
111
+ {
112
+ const double b = 1.0 / (2.0 * Sigma * Sigma);
113
+
114
+ for (int i = vDistribution.size(); --i > 0;)
115
+ {
116
+ double x = ValueFromIndex(i);
117
+ double delta = x - Mu;
118
+ vDistribution[i] = std::exp(-b * delta * delta);
119
+ }
120
+
121
+ Normalize();
122
+ }
123
+
124
+ /////////////////////////////////////////////////////////////////////////////
125
+ // Binomial posterior ??? log-likelihood for better stability
126
+ /////////////////////////////////////////////////////////////////////////////
127
+ void CCDistribution::SetBinomialPosterior(int N, int n)
128
+ {
129
+ for (int i = vDistribution.size(); --i > 0;)
130
+ {
131
+ double x = ValueFromIndex(i);
132
+ vDistribution[i] = std::pow(x, n) * std::pow (1 - x, N - n);
133
+ }
134
+ Normalize();
135
+ }
@@ -0,0 +1,44 @@
1
+ /////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // R�mi Coulom
4
+ //
5
+ // December, 2004
6
+ //
7
+ /////////////////////////////////////////////////////////////////////////////
8
+ #ifndef CCDistribution_Declared
9
+ #define CCDistribution_Declared
10
+
11
+ #include "CDistribution.h"
12
+ #include "CDiscretization.h"
13
+
14
+ class CCDistribution: public CDistribution, public CDiscretization // cdist
15
+ {
16
+ private: ///////////////////////////////////////////////////////////////////
17
+ double GetBoundValue(double Confidence,
18
+ int Begin,
19
+ int End,
20
+ int Direction) const;
21
+
22
+ public: ////////////////////////////////////////////////////////////////////
23
+ CCDistribution(int Size, double Min, double Max):
24
+ CDistribution(Size),
25
+ CDiscretization(Size, Min, Max)
26
+ {}
27
+
28
+ //
29
+ // Compute properties of this distribution
30
+ //
31
+ double GetLowerValue(double Confidence) const;
32
+ double GetUpperValue(double Confidence) const;
33
+ double GetMostLikelyValue() const;
34
+ double GetMean() const;
35
+ double GetVariance(double Mean) const;
36
+
37
+ //
38
+ // Standard distributions
39
+ //
40
+ void SetNormal(double Mu, double Sigma);
41
+ void SetBinomialPosterior(int N, int n);
42
+ };
43
+
44
+ #endif
@@ -0,0 +1,104 @@
1
+ ////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // Remi Coulom
4
+ //
5
+ // January, 2005
6
+ //
7
+ ////////////////////////////////////////////////////////////////////////////
8
+ #include "CCDistributionCUI.h"
9
+ #include "CCDistribution.h"
10
+
11
+ #include <sstream>
12
+
13
+ ////////////////////////////////////////////////////////////////////////////
14
+ // Constructor
15
+ ////////////////////////////////////////////////////////////////////////////
16
+ CCDistributionCUI::CCDistributionCUI(const CCDistribution &cdistInit,
17
+ CConsoleUI *pcui,
18
+ int openmode) :
19
+ CConsoleUI(pcui, openmode),
20
+ cdist(cdistInit)
21
+ {
22
+ }
23
+
24
+ ////////////////////////////////////////////////////////////////////////////
25
+ // Local prompt
26
+ ////////////////////////////////////////////////////////////////////////////
27
+ void CCDistributionCUI::PrintLocalPrompt(std::ostream &out)
28
+ {
29
+ out << "Distribution";
30
+ }
31
+
32
+ ////////////////////////////////////////////////////////////////////////////
33
+ // Command strings
34
+ ////////////////////////////////////////////////////////////////////////////
35
+ const char * const CCDistributionCUI::tszCommands[] =
36
+ {
37
+ "?",
38
+ "lower",
39
+ "upper",
40
+ "mostlikely",
41
+ "write",
42
+ 0
43
+ };
44
+
45
+ ////////////////////////////////////////////////////////////////////////////
46
+ // Process commands
47
+ ////////////////////////////////////////////////////////////////////////////
48
+ int CCDistributionCUI::ProcessCommand(const char *pszCommand,
49
+ const char *pszParameters,
50
+ std::istream &in,
51
+ std::ostream &out)
52
+ {
53
+ enum
54
+ {
55
+ IDC_Help,
56
+ IDC_Lower,
57
+ IDC_Upper,
58
+ IDC_MostLikely,
59
+ IDC_Write
60
+ };
61
+
62
+ switch (ArrayLookup(pszCommand, tszCommands))
63
+ {
64
+ case IDC_Help: ///////////////////////////////////////////////////////////
65
+ CConsoleUI::ProcessCommand(pszCommand, pszParameters, in, out);
66
+ out << "Distribution commands\n";
67
+ out << "~~~~~~~~~~~~~~~~~~~~~\n";
68
+ out << "lower [c] ....... lower bound (c=confidence, default=0.95)\n";
69
+ out << "upper [c] ....... upper bound\n";
70
+ out << "mostlikely ...... most likely value\n";
71
+ out << "write ........... write complete distribution\n";
72
+ break;
73
+
74
+ case IDC_Lower: //////////////////////////////////////////////////////////
75
+ {
76
+ double Confidence = 0.95;
77
+ std::istringstream(pszParameters) >> Confidence;
78
+ out << cdist.GetLowerValue(Confidence) << '\n';
79
+ }
80
+ break;
81
+
82
+ case IDC_Upper: //////////////////////////////////////////////////////////
83
+ {
84
+ double Confidence = 0.95;
85
+ std::istringstream(pszParameters) >> Confidence;
86
+ out << cdist.GetUpperValue(Confidence) << '\n';
87
+ }
88
+ break;
89
+
90
+ case IDC_MostLikely: /////////////////////////////////////////////////////
91
+ out << cdist.GetMostLikelyValue() << '\n';
92
+ break;
93
+
94
+ case IDC_Write: //////////////////////////////////////////////////////////
95
+ for (int i = 0; i < cdist.GetSize(); i++)
96
+ out << cdist.ValueFromIndex(i) << ' ' << cdist.GetProbability(i) << '\n';
97
+ break;
98
+
99
+ default: /////////////////////////////////////////////////////////////////
100
+ return CConsoleUI::ProcessCommand(pszCommand, pszParameters, in, out);
101
+ }
102
+
103
+ return PC_Continue;
104
+ }
@@ -0,0 +1,38 @@
1
+ ////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // CCDistributionCUI.h
4
+ //
5
+ // Remi Coulom
6
+ //
7
+ // January, 2005
8
+ //
9
+ ////////////////////////////////////////////////////////////////////////////
10
+ #ifndef CCDistributionCUI_Declared
11
+ #define CCDistributionCUI_Declared
12
+
13
+ #include "consolui.h" // CConsoleUI
14
+
15
+ class CCDistribution;
16
+
17
+ class CCDistributionCUI : public CConsoleUI // cdcui
18
+ {
19
+ private: //////////////////////////////////////////////////////////////////
20
+ static const char * const tszCommands[];
21
+
22
+ const CCDistribution &cdist;
23
+
24
+ protected: ////////////////////////////////////////////////////////////////
25
+ virtual int ProcessCommand(const char *pszCommand,
26
+ const char *pszParameters,
27
+ std::istream &in,
28
+ std::ostream &out);
29
+
30
+ virtual void PrintLocalPrompt(std::ostream &out);
31
+
32
+ public: ///////////////////////////////////////////////////////////////////
33
+ CCDistributionCUI(const CCDistribution &cdistInit,
34
+ CConsoleUI *pcui = 0,
35
+ int openmode = OpenModal);
36
+ };
37
+
38
+ #endif