Revision 336338616161 () - Diff

Link to this snippet: https://friendpaste.com/4G3EnJAaNgKXvFPlyAKNHc
Embed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(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)))