Reference RPI Implementation delete lock Revision 336338616161 (Wed Jun 07 2017 at 15:18) - Diff Link to this snippet: https://friendpaste.com/4G3EnJAaNgKXvFPlyAKNHc Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107(setq glist '(("Duke" "UNC" 1) ("Duke" "Maryland" 1) ("Wake" "Duke" 0) ("UNC" "Duke" 1) ("Maryland" "Wake" 1) ("Wake" "UNC" 0)));;;; Calculate WP (Winning Percentage) for "team";;;; Note that for WP only, home games count differently than;; road games;;(defun wp (team gamelist) ;; Running count of wins and losses (let ((wins 0) (losses 0)) ;; Go through all the games (loop for g in gamelist do (if (equal team (car g)) ;; team = home (if (eq 1 (caddr g)) ;; home team won? (incf wins 0.6) ;; home win (incf losses 1.4)) ;; home loss (if (equal team (cadr g)) ;; team = road (if (eq 0 (caddr g)) ;; road team won? (incf wins 1.4) ;; road win (incf losses 0.6))))) ;; road loss ;; Result is wins/total (/ wins (+ wins losses))));;;; Helper function calculates the OWP for "opponent" of "team";;(defun owp-helper (team opponent gamelist) (let ((wins 0) (losses 0)) (loop for g in gamelist ;; Need to ignore all of opponent's games that involve team do (if (not (or (and (equal opponent (car g)) (equal team (cadr g))) (and (equal team (car g)) (equal opponent (cadr g))))) ;; Otherwise... (if (equal opponent (car g)) (if (eq 1 (caddr g)) (incf wins) ;; home win (incf losses)) ;; home loss (if (equal opponent (cadr g)) (if (eq 0 (caddr g)) (incf wins) ;; road win (incf losses)))))) ;; road loss (/ wins (+ wins losses))));;;; Calculte OWP for team;;;; Calculate the OWP for each opponent of team (counting opponents;; multiple times if the teams have played more than once) and then;; average.;;(defun owp (team gamelist) (let ((total-owp 0.0) (num-opponents 0)) (loop for g in gamelist do (if (equal team (car g)) ;; team was home (progn ;; Calculate OWP of the away team (incf total-owp (owp-helper (car g) (cadr g) gamelist)) (incf num-opponents))) do (if (equal team (cadr g)) ;; team was away (progn ;; Calculate OWP of the home team (incf total-owp (owp-helper (cadr g) (car g) gamelist)) (incf num-opponents)))) ;; Average all the OWPs (if (> num-opponents 0) (/ total-owp num-opponents) 0))) ;;;; Calculate OOWP for team;;;; OOWP is the average of all of team's opponent's OWP.;;(defun oowp (team gamelist) (let ((total-oowp 0.0) (num-opponents 0)) (loop for g in gamelist do (if (equal team (car g)) (progn (incf total-oowp (owp (cadr g) gamelist)) (incf num-opponents))) do (if (equal team (cadr g)) (progn (incf total-oowp (owp (car g) gamelist)) (incf num-opponents)))) (if (> num-opponents 0) (/ total-oowp num-opponents) 0))) ;;;; RPI = 0.25*WP + 0.50*OWP + 0.25*OOWP;;(defun rpi (team gamelist) (+ (* (wp team gamelist) 0.25) (* (owp team gamelist) 0.50) (* (oowp team gamelist) 0.25)))