#!/usr/bin/env julia function get_team_games(games::DataFrame, team_names::Vector, and=true) df = "" for i = 1:nrow(games) if (and && contains(team_names, games[i,"AwayTeam"]) && contains(team_names, games[i, "HomeTeam"])) || (!and && (contains(team_names, games[i,"AwayTeam"]) || contains(team_names, games[i, "HomeTeam"]))) if df == "" df = games[i,:] else df = rbind(df, games[i,:]) end end end df end function get_teams(games) teams = Set() teams = union!(union!(teams, vector(games["AwayTeam"])), vector(games["HomeTeam"])) teams = sort!(collect(teams)) end function calculate_ratings(games, teams=get_teams(games)) games = get_team_games(games, teams) X = zeros(Float64, nrow(games), length(teams)) Y = zeros(Float64, nrow(games)) for i in 1:nrow(games) team1 = findfirst(teams, games[i, "AwayTeam"]) team2 = findfirst(teams, games[i, "HomeTeam"]) spread = games[i, "WinMargin"] if spread != 0 X[i, team1] = sign(games[i, "WinMargin"]) X[i, team2] = -sign(games[i, "WinMargin"]) else X[i, team1] = 1.0 X[i, team2] = -1.0 end Y[i] = spread end Ratings = X \ Y end function calculate_standings(games, teams=get_teams(games)) N = length(teams) gp = zeros(Int64, N) wins = zeros(Int64, N) loss = zeros(Int64, N) ties = zeros(Int64, N) pts = zeros(Int64, N) gf = zeros(Int64, N) ga = zeros(Int64, N) diff = zeros(Int64, N) winpct = zeros(Float64, N) ratings = calculate_ratings(games, teams) for i in 1:nrow(games) team1 = findfirst(teams, games[i, "AwayTeam"]) team2 = findfirst(teams, games[i, "HomeTeam"]) spread = games[i, "WinMargin"] gp[team1] += 1 gp[team2] += 1 if spread > 0 wins[team1] += 1 loss[team2] += 1 elseif spread < 0 wins[team2] += 1 loss[team1] += 1 else ties[team1] += 1 ties[team2] += 1 end gf[team1] += games[i, "AwayScore"] gf[team2] += games[i, "HomeScore"] ga[team1] += games[i, "HomeScore"] ga[team2] += games[i, "AwayScore"] end for team in 1:N pts[team] = wins[team] * 2 + ties[team] diff[team] = gf[team] - ga[team] winpct[team] = (wins[team] + ties[team]/2) / gp[team] end standings = DataFrame(Team=teams, GP=gp, W=wins, L=loss, T=ties, PTS=pts, GF=gf, GA=ga, DIFF=diff, PCT=winpct, Rating=ratings) end games = readtable("games.csv") ratings = calculate_ratings(games) standings_by_points = sortby(calculate_standings(games), "PTS") standings_by_rating = sortby(A_standings, "Rating")