########################
# #
# RRRR PPPP IIIIII #
# RRRRR PPPPP IIIIII #
# RR RR PP PP II #
# RR RR PP PP II #
# RRRRR PPPPP II #
# RRRR PPPP II #
# RR RRR PP II #
# RR RR PP II #
# RR RR PP IIIIII #
# RR RR PP IIIIII #
# #
########################
#########################################
# RATINGS PERCENTAGE INDEX COMPUTATIONS #
#########################################
#CONSTRUCT FULL NUMBER OF GAMES BY OPPONENT MATRIX
#CONSTRUCT FULL W-L-T AGGREGATION MATRTIX
#I'VE DONE THIS BEFORE, GET INTO RPI DETAILS LATER
####################
#FUN LETTERS!
#########################
# PAIRWISE COMPUTATIONS #
#########################
#I'VE ALSO DONE THIS BEFORE
#NOT WITH CURRENT COMMON OPPONENTS RULE THOUGH
#COMPUTE A TEAM BY TEAM WIN % MATRIX
#EFFECTIVE WINS (W+.5T) DIVIDED INTO GAMES
#IF NUMBER OF GAMES EQUALS ZERO THEN LET VALUE BE ZERO
#number of games by opponent
n.teams=59
#this is going to be slower than desired!
n.games=dim(game.results)[1]
games.mtx.nat=matrix(0,n.teams,n.teams)
wlt.mtx.nat=matrix(0,n.teams,n.teams)
for(i in 1:n.games){
games.mtx.nat[game.results[i,1],game.results[i,3]]=1+games.mtx.nat[game.results[i,1],game.results[i,3]]
games.mtx.nat[game.results[i,3],game.results[i,1]]=1+games.mtx.nat[game.results[i,3],game.results[i,1]]
wlt.mtx.nat[game.results[i,1],game.results[i,3]]=(game.results[i,2]>game.results[i,4])+(game.results[i,2]==game.results[i,4])/2+wlt.mtx.nat[game.results[i,1],game.results[i,3]]
wlt.mtx.nat[game.results[i,3],game.results[i,1]]=(game.results[i,4]>game.results[i,2])+(game.results[i,2]==game.results[i,4])/2+wlt.mtx.nat[game.results[i,3],game.results[i,1]]
}
#there must be a faster way... tables?
n.games.team=rowSums(games.mtx.nat)
win.pctg=rowSums(wlt.mtx.nat)/n.games.team
#something here doesn't quite work
adj.win.pctg=((matrix(rowSums(wlt.mtx.nat),n.teams,n.teams))-wlt.mtx.nat)/((matrix(n.games.team,n.teams,n.teams))-games.mtx.nat)
#I think?!?!
opp.pctg=diag(games.mtx.nat%*%(adj.win.pctg))/rowSums(games.mtx.nat)
oppopp.pctg=(games.mtx.nat%*%matrix(opp.pctg,n.teams,1))/rowSums(games.mtx.nat)
rpi.prelim=win.pctg*.25+opp.pctg*.21+oppopp.pctg*.54
############
# EVALUATE GAME POINTS
##########
####
#GAME.RESULTS -- FOR NOW ADD OTHER ITEMS LATER
#
#1-AWAY TEAM
#2-AWAY TEAM SCORE
#3-HOME TEAM
#4-HOME TEAM SCORE
#5-NEUTRAL INDICATOR (DOES NOT MATTER HERE)
#
##############
game.pt.eval=matrix(0,dim(game.results)[1],2)
game.pt.eval[,1]=((game.results[,2]>game.results[,4])+((game.results[,2]==game.results[,4]))*.5)*.25+unlist(lapply(1:n.games,function(w){adj.win.pctg[game.results[w,3],game.results[w,1]]}))*.21+opp.pctg[game.results[,3]]*.54
game.pt.eval[,2]=((game.results[,4]>game.results[,2])+((game.results[,4]==game.results[,2]))*.5)*.25+unlist(lapply(1:n.games,function(w){adj.win.pctg[game.results[w,1],game.results[w,3]]}))*.21+opp.pctg[game.results[,1]]*.54
rpi.prelim.2=rep(0,59)
for(i in 1:59){
left.col.id=which((game.results[,1]==i))
right.col.id=which((game.results[,3]==i))
rpi.prelim.2[i]=(sum(game.pt.eval[left.col.id,1])+sum(game.pt.eval[right.col.id,2]))/(length(left.col.id)+length(right.col.id))
}
eff.rpi.wins=rpi.prelim.2*n.games.team
n.games.team.mod=n.games.team
#basics... if from the first team, it wins and its game value is lower
#than preliminary rpi then identify for chucking,
#repeat with second team
#once doing so, then subtract the relevant games
keep.left.id=which((((game.pt.eval[,1]<rpi.prelim[game.results[,1]])&(game.results[,2]>game.results[,4]))))
keep.right.id=which(((game.pt.eval[,2]<rpi.prelim[game.results[,3]])&(game.results[,4]>game.results[,2])))
if (length(keep.left.id)>0){
for(i in 1:length(keep.left.id)){
eff.rpi.wins[game.results[keep.left.id[i],1]]=eff.rpi.wins[game.results[keep.left.id[i],1]]-game.pt.eval[keep.left.id[i],1]
n.games.team.mod[game.results[keep.left.id[i],1]]=n.games.team.mod[game.results[keep.left.id[i],1]]-1
}
}
if (length(keep.right.id)>0){
for(i in 1:length(keep.right.id)){
eff.rpi.wins[game.results[keep.right.id[i],3]]=eff.rpi.wins[game.results[keep.right.id[i],3]]-game.pt.eval[keep.right.id[i],2]
n.games.team.mod[game.results[keep.right.id[i],3]]=n.games.team.mod[game.results[keep.right.id[i],3]]-1
}
}
rpi.final=eff.rpi.wins/n.games.team.mod
#here the idea would be to subtract away instead of add
#if dim(take.out)==0 then ... move on
#########
#if the abomination below works then great!
#########
#lapply should leave a list of arrays/vectors
#unlist coerses them all down down to a single dim array
#matrix, should, re-constitute this in the appropriate order
#hooboy!
rpi.pts=matrix(unlist(lapply(1:n.teams,function(w){rpi.final>rpi.final[w]})),n.teams,n.teams)
#IDENTIFY FOR EACH PAIR (I != J) (LOOP)
#THE TEAMS IN WHICH THERE ARE GAMES IN COMMON
#VECTOR OF COMMON OPPONENT IDS
cop.mtx=matrix(0,n.teams,n.teams)
for(i in 1:(n.teams-1)){
for(j in (i+1):(n.teams)){
cop.id=which(games.mtx.nat[i,]>0 & games.mtx.nat[j,]>0)
cop.mtx[i,j]=sum(wlt.mtx.nat[i,cop.id]/games.mtx.nat[i,cop.id])
cop.mtx[j,i]=sum(wlt.mtx.nat[j,cop.id]/games.mtx.nat[j,cop.id])
}}
cop.pts=pairoff.matrix(cop.mtx)
#alternatively, side compute cop.mtx and determine who won
#this pair-point... store in cop.pt
#bet there's a sneaky way to do this
#TEAMS UNDER CONSIDERATION CALCULATION... LATER
#IDENTIFY THOSE UNDER CONSIDERATION
tuc.id=which(rpi.final>=.5)
#compute for each, the percentage less one element
#I think there's an efficient way to do that
#intersect will do it, but its inconvenient
#something nicer? Set minus?
#that might be more than I really need
#(sum(wlt.mtx.nat[i,tuc.id])-wlt.mtx.nat[i,j])/(sum(games.mtx.nat[i,tuc.id])-games.mtx.nat[i,j])
#ONE BETTER!
tuc.mtx=matrix(0,n.teams,n.teams)
tuc.games=rowSums(games.mtx.nat[,tuc.id])
#matrix(unlist(lapply(1:n.teams,function(w){tuc.games[w]-games.mtx.nat[w,tuc.id]})),n.teams,n.teams)
for(i in tuc.id){
tuc.mtx[i,tuc.id]=(sum(wlt.mtx.nat[i,tuc.id])-wlt.mtx.nat[i,tuc.id])/(sum(games.mtx.nat[i,tuc.id])-games.mtx.nat[i,tuc.id])
tuc.mtx[i,tuc.id]=tuc.mtx[i,tuc.id]*((tuc.games[i]-games.mtx.nat[i,tuc.id])>=10)*((tuc.games[tuc.id]-games.mtx.nat[i,tuc.id])>=10)
}
#modify later in case of NaN results 0/0
#I hope to god that worked!
#if either is zero then zero the whole
tuc.pts=pairoff.matrix(tuc.mtx)
#get it all in one shot!
#need a quick matrix greater than symm element function
#q[i,j]=1 if p[i,j]>p[j,i]
#1.0001 is the lazy man's RPI tie-breaker
pwr.pts=matrix(0,n.teams,n.teams)
#pwr.pts[tuc.id,tuc.id]=rpi.pts[tuc.id,tuc.id]*(1.0001)+cop.pts[tuc.id,tuc.id]+tuc.pts[tuc.id,tuc.id]+wlt.mtx.nat[tuc.id,tuc.id]
pwr.pts=rpi.pts*(1.0001)+cop.pts+tuc.pts+wlt.mtx.nat
pwr.mtx=pairoff.matrix(pwr.pts)
###run through special matrix function
# pwr.mtx=matrix.function(pwr.pts)
pairs.won=rowSums(pwr.mtx[,tuc.id])
pairs.won[-tuc.id]=0
pairwise.sort=matrix(0,n.teams,3)
pairwise.sort[,1]=1:n.teams
pairwise.sort[,2]=pairs.won
pairwise.sort[,3]=rpi.final
#sort on RPI first, then on pairs won
#automatically breaking the necessary ties
tuc.labels=team.labels
tuc.labels=tuc.labels[order(-pairwise.sort[,3])]
pairwise.sort=pairwise.sort[order(-pairwise.sort[,3]),]
tuc.labels=tuc.labels[order(-pairwise.sort[,2])]
pairwise.sort=pairwise.sort[order(-pairwise.sort[,2]),]
tourn.champ.id=c(hea.champ,ecac.champ,ccha.champ,wcha.champ,aha.champ)
tourn.field=pairwise.sort[is.element(pairwise.sort[,1],c(tourn.champ.id,pairwise.sort[-which(is.element(pairwise.sort[,1],tourn.champ.id)),1][1:11])),]