summaryrefslogtreecommitdiff
path: root/f1elo
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2014-11-07 16:49:24 +0100
committeremkael <emkael@tlen.pl>2014-11-07 16:49:24 +0100
commitefcf12f3e6bb437f5089215fd81b2c924961e4d2 (patch)
tree16a877357c36b54f3a3f60ad63f880f4459e0167 /f1elo
parent50eda8dcf4c764493efe3cae4cf81df916ea2e7d (diff)
* disparity variation algorithm
Diffstat (limited to 'f1elo')
-rw-r--r--f1elo/elo.py32
-rw-r--r--f1elo/interface.py11
2 files changed, 34 insertions, 9 deletions
diff --git a/f1elo/elo.py b/f1elo/elo.py
index 51c9e23..8b3c806 100644
--- a/f1elo/elo.py
+++ b/f1elo/elo.py
@@ -1,4 +1,5 @@
-import json
+import json, dateutil
+from sqlalchemy import func
from itertools import combinations
from os import path
@@ -23,6 +24,27 @@ class Elo:
return sum([self.get_ranking(d, date) for d in entry.drivers]) / len(entry.drivers)
def rank_race(self, race):
+ recent_date = race.date - dateutil.relativedelta.relativedelta(months=9)
+ recent_ratings = self.session.query(
+ func.min(Ranking.ranking).label('min'),
+ func.max(Ranking.ranking).label('max')
+ ).filter(
+ Ranking.rank_date >= recent_date
+ ).group_by(
+ Ranking._driver
+ )
+ changes_query = self.session.query(
+ func.avg(
+ func.abs(
+ recent_ratings.subquery().columns.min - recent_ratings.subquery().columns.max
+ )
+ )
+ )
+ recent_rank_changes = changes_query.scalar()
+ race_disparity = self.config['disparity']
+ if recent_rank_changes:
+ race_disparity -= recent_rank_changes
+ print(race_disparity, str(race.date))
entries = race.entries
entries_to_compare = []
rankings = {}
@@ -38,7 +60,9 @@ class Elo:
self.get_outcome(c),
self.get_importance(race,
[rankings[c[0]],
- rankings[c[1]]]))
+ rankings[c[1]]]),
+ race_disparity
+ )
new_rankings[c[0]] += score
new_rankings[c[1]] -= score
return new_rankings
@@ -59,5 +83,5 @@ class Elo:
return 0
return 0.5
- def get_score(self, difference, outcome, importance):
- return importance * (outcome - 1 / (1 + (10 ** (-difference / self.config['disparity']))))
+ def get_score(self, difference, outcome, importance, disparity):
+ return importance * (outcome - 1 / (1 + (10 ** (-difference / disparity))))
diff --git a/f1elo/interface.py b/f1elo/interface.py
index 173cfa7..3768f8a 100644
--- a/f1elo/interface.py
+++ b/f1elo/interface.py
@@ -111,11 +111,12 @@ class Interface:
one_year = dateutil.relativedelta.relativedelta(years=1)
rankings = self.session.query(
- Ranking).filter(
- Ranking.rank_date > (
- date - one_year)).filter(
- Ranking.rank_date <= date).all(
- )
+ Ranking
+ ).filter(
+ Ranking.rank_date > (date - one_year)
+ ).filter(
+ Ranking.rank_date <= date
+ ).all()
drivers = {}
for ranking in rankings: