Skip to content

Instantly share code, notes, and snippets.

@kersny
Created December 13, 2010 22:56
Show Gist options
  • Save kersny/739754 to your computer and use it in GitHub Desktop.
Save kersny/739754 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'mysql'
require 'narray'
dbh = Mysql.real_connect("host", "username", "password", "database_name")
s = 0.85
t = 1.0-s
query = "SELECT COUNT(*) as count FROM Teams"
res = dbh.query(query)
numteams = res.fetch_hash["count"].to_i
ident = NMatrix.float(numteams, numteams)
ident.diagonal!(1.0)
p = NMatrix.float(1, numteams).fill!(1.0)
p = (1.0/numteams.to_f) * p
rawgames = NMatrix.int(numteams, numteams)
query = "SELECT * FROM Games WHERE year='2009-2010' ORDER BY date"
res = dbh.query(query)
res.each_hash do |row|
team1 = row["team1"].to_i
team2 = row["team2"].to_i
if (team2 != 0 && team1 != 0)
team1 -= 1
team2 -= 1
if (row["team1score"].to_i > row["team2score"].to_i)
rawgames[team1, team2] += 1
else
rawgames[team2, team1] += 1
end
end
end
prob = NMatrix.float(numteams, numteams)
0.upto(numteams - 1) do |i|
sum = 0.0
0.upto(numteams - 1) do |j|
sum += rawgames[j, i].to_f
end
0.upto(numteams - 1) do |j|
val = rawgames[j,i].to_f / sum
prob[i, j] = val.nan? ? 0 : val
end
end
fin = (t*((ident - (s*prob)).inverse)*p)
0.upto(numteams - 1) do |i|
val = i + 1
year = '2009-2010'
rank = fin[0,i]
dbh.query("INSERT INTO page (teamid, year, rank) VALUES (#{val}, '#{year}', #{rank})")
end
#Copyright (c) 2010-2011 Kerry Snyder
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
require 'rubygems'
require 'mysql'
require 'narray'
dbh = Mysql.real_connect("host", "username", "password", "database_name")
s = 0.85
t = 1.0-s
query = "SELECT COUNT(*) as count FROM Teams"
res = dbh.query(query)
numteams = res.fetch_hash["count"].to_i
ident = NMatrix.float(numteams, numteams)
ident.diagonal!(1.0)
p = NMatrix.float(1, numteams).fill!(1.0)
p = (1.0/numteams.to_f) * p
rawgames = NMatrix.int(numteams, numteams)
query = "SELECT * FROM Games WHERE year='2009-2010' ORDER BY date"
res = dbh.query(query)
res.each_hash do |row|
team1 = row["team1"].to_i
team2 = row["team2"].to_i
if (team2 != 0 && team1 != 0)
team1 -= 1
team2 -= 1
if (row["team1score"].to_i > row["team2score"].to_i)
rawgames[team1, team2] += 1
else
rawgames[team2, team1] += 1
end
end
end
prob = NMatrix.float(numteams, numteams)
0.upto(numteams - 1) do |i|
sum = 0.0
0.upto(numteams - 1) do |j|
sum += rawgames[j, i].to_f
end
0.upto(numteams - 1) do |j|
val = rawgames[j,i].to_f / sum
prob[i, j] = val.nan? ? 0 : val
end
end
fin = (t*((ident - (s*prob)).inverse)*p)
0.upto(numteams - 1) do |i|
val = i + 1
year = '2009-2010'
rank = fin[0,i]
dbh.query("INSERT INTO page (teamid, year, rank) VALUES (#{val}, '#{year}', #{rank})")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment