Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions elogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type Elo struct {

// Outcome is a match result data for a single player.
type Outcome struct {
Delta int
Delta int
Rating int
}

// NewElo instantiates the Elo object with default factors.
// Default K-Factor is 32
// Default deviation is 400
// Default deviation is 400
func NewElo() *Elo {
return &Elo{K, D}
}
Expand All @@ -42,7 +42,7 @@ func (e *Elo) ExpectedScore(ratingA, ratingB int) float64 {

// ExpectedScoreWithFactors overrides default factors and gives the expected chance that the first player wins
func (e *Elo) ExpectedScoreWithFactors(ratingA, ratingB, d int) float64 {
return 1 / (1 + math.Pow(10, float64(ratingB - ratingA) / float64(d)))
return 1 / (1 + math.Pow(10, float64(ratingB-ratingA)/float64(d)))
}

// RatingDelta gives the ratings change for the first player for the given score
Expand All @@ -51,16 +51,17 @@ func (e *Elo) RatingDelta(ratingA, ratingB int, score float64) int {
}

// RatingDeltaWithFactors overrides default factors and gives the ratings change for the first player for the given score
func (e *Elo) RatingDeltaWithFactors(ratingA, ratingB int, score float64, k, d int ) int {
func (e *Elo) RatingDeltaWithFactors(ratingA, ratingB int, score float64, k, d int) int {
return int(float64(k) * (score - e.ExpectedScoreWithFactors(ratingA, ratingB, d)))
}

// Rating gives the new rating for the first player for the given score
func (e *Elo) Rating(ratingA, ratingB int, score float64) int {
return e.RatingWithFactors(ratingA, ratingB, score, e.K, e.D)
}

// RatingWithFactors overrides default factors and gives the new rating for the first player for the given score
func (e *Elo) RatingWithFactors(ratingA, ratingB int, score float64, k, d int ) int {
func (e *Elo) RatingWithFactors(ratingA, ratingB int, score float64, k, d int) int {
return ratingA + e.RatingDeltaWithFactors(ratingA, ratingB, score, k, d)
}

Expand All @@ -70,7 +71,7 @@ func (e *Elo) Outcome(ratingA, ratingB int, score float64) (Outcome, Outcome) {
}

// OutcomeWithFactors overrides default factors and gives an Outcome object for each player for the given score
func (e *Elo) OutcomeWithFactors(ratingA, ratingB int, score float64, k, d int ) (Outcome, Outcome) {
func (e *Elo) OutcomeWithFactors(ratingA, ratingB int, score float64, k, d int) (Outcome, Outcome) {
delta := e.RatingDeltaWithFactors(ratingA, ratingB, score, k, d)
return Outcome{ delta, ratingA + delta }, Outcome{ -delta, ratingB - delta }
}
return Outcome{delta, ratingA + delta}, Outcome{-delta, ratingB - delta}
}
15 changes: 7 additions & 8 deletions elogo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,18 @@ func TestElo(t *testing.T) {
t.Fatalf("Expected rating if draw %v, but got %v", 1504, rating3)
}

outcomeA1, outcomeB1 := elo.Outcome(A, B, 1)
outcomeA1, outcomeB1 := elo.Outcome(A, B, 1)
if outcomeA1.Rating != 1520 || outcomeA1.Delta != 20 {
t.Fatalf("Expected rating %v and delta %v if A wins, but got outcome %v", 1520, 20, outcomeA1)
}
}
if outcomeB1.Rating != 1580 || outcomeB1.Delta != -20 {
t.Fatalf("Expected rating %v and delta %v if B loses, but got outcome %v", 1580, -20, outcomeB1)
}
}
}

func TestEloWithFactors(t *testing.T) {
elo := NewEloWithFactors(40, 800)


expected := elo.ExpectedScore(A, B)
if expected != 0.4285368825916186 {
t.Fatalf("Expected chance %v, but got %v", 0.4285368825916186, expected)
Expand Down Expand Up @@ -87,11 +86,11 @@ func TestEloWithFactors(t *testing.T) {
t.Fatalf("Expected rating if draw %v, but got %v", 1502, rating3)
}

outcomeA1, outcomeB1 := elo.Outcome(A, B, 1)
outcomeA1, outcomeB1 := elo.Outcome(A, B, 1)
if outcomeA1.Rating != 1522 || outcomeA1.Delta != 22 {
t.Fatalf("Expected rating %v and delta %v if A wins, but got outcome %v", 1522, 22, outcomeA1)
}
}
if outcomeB1.Rating != 1578 || outcomeB1.Delta != -22 {
t.Fatalf("Expected rating %v and delta %v if B loses, but got outcome %v", 1578, -22, outcomeB1)
}
}
}
}