/
stats.py
executable file
·255 lines (232 loc) · 6.51 KB
/
stats.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Shinichi Nakagawa'
class Stats(object):
def __init__(self):
pass
@classmethod
def ip(cls, ip_outs):
"""
Inning Pitched
:param ip_outs: inning pitched outs
:return: (float) ip
"""
return round(ip_outs / 3, 1)
@classmethod
def era(cls, er, ip):
"""
Earned run average
:param er: earned run
:param ip: inning pitched
:return: (float) era
"""
return round((9 * er) / ip, 2)
@classmethod
def whip(cls, bb, h, ip):
"""
Walks + Hits / IP
:param bb: base on ball
:param h: hits
:param ip: inning pitched
:return: (float) whip
"""
return round((bb + h) / ip, 3)
@classmethod
def h9(cls, h, ip):
"""
Hits / 9
:param h: hits
:param ip: inning pitched
:return: (float) h9
"""
return round((9 * h) / ip, 1)
@classmethod
def so9(cls, so, ip):
"""
Strike out / 9
:param so: strike out
:param ip: inning pitched
:return: (float) so9
"""
return round((9 * so) / ip, 1)
@classmethod
def bb9(cls, bb, ip):
"""
BB / 9
:param bb: base on ball
:param ip: inning pitched
:return: (float) b9
"""
return round((9 * bb) / ip, 1)
@classmethod
def hr9(cls, hr, ip):
"""
HR / 9
:param hr: home run
:param ip: inning pitched
:return: (float) hr9
"""
return round((9 * hr) / ip, 1)
@classmethod
def single(cls, h, hr, _2b, _3b):
"""
Single hits
:param h: hits(all)
:param hr: home run
:param _2b: double
:param _3b: triple
:return: (int)single hits
"""
return h - (hr + _2b + _3b)
@classmethod
def pa(cls, ab, bb, hbp, sf, sh):
"""
Plate appearance
:param ab: at bat
:param bb: base on ball
:param hbp: hit by pitch
:param sf: sacrifice fly
:param sh: sacrifice hit
:return: (int)Plate appearance
"""
return ab + bb + hbp + sf + sh
@classmethod
def tb(cls, single, hr, _2b, _3b):
"""
Total bases
:param single: single hits
:param hr: home run
:param _2b: double
:param _3b: triple
:return: (int)total bases
"""
return hr * 4 + _3b * 3 + _2b * 2 + single
@classmethod
def avg(cls, h, ab):
"""
Batting average
:param h: hits
:param ab: at bat
:return: (float)avg
"""
return round(h / ab, 3)
@classmethod
def slg(cls, tb, ab):
"""
Slugging
:param tb: total bases
:param ab: at bat
:return: (float)slugging
"""
return round(tb / ab, 3)
@classmethod
def obp(cls, h, bb, hbp, ab, sf):
"""
On base percentage
:param h: hits
:param bb: base on ball
:param hbp: hit by pitch
:param ab: at bat
:param sf: sacrifice fly
:return: (float)obp
"""
return round((h + bb + hbp) / (ab + bb + hbp + sf), 3)
@classmethod
def ops(cls, h, bb, hbp, ab, sf, tb):
"""
On the base + slugging
:param h: hits
:param bb: base on ball
:param hbp: hit by pitch
:param ab: at bat
:param sf: sacrifice fly
:param tb: total bases
:return: (float) ops
"""
# OBPとSLGを計算してから足して四捨五入
return round(((tb / ab) + (h + bb + hbp) / (ab + bb + hbp + sf)), 3)
@classmethod
def babip(cls, h, hr, ab, so, sf):
"""
Batting average on balls in play(BABIP)
:param h: hits
:param hr: home run
:param ab: at bat
:param so: strike out
:param sf: sacrifice fly
:return: (float) babip
"""
return round((h - hr) / (ab - so - hr + sf), 3)
@classmethod
def rc(cls, h, bb, hbp, cs, gidp, sf, sh, sb, so, ab, ibb, single, _2b, _3b, hr):
"""
Runs Created of 2002 ver.
[note]
http://en.wikipedia.org/wiki/Runs_created#2002_version_of_runs_created
:param h: hits
:param bb: base on ball
:param hbp: hit by pitch
:param cs: caught stealing
:param gidp: ground into duble play
:param tb: total bases
:param sf: sacrifice fly
:param sh: sacrifice hit
:param sb: stolen base
:param so: strike out
:param ab: at bat
:param ibb: intentional base on balls
:param single: single hits
:param _2b: double
:param _3b: triple
:param hr: home run
:return: (float) run created
"""
# (出塁能力A * 進塁能力B) / 出塁機会C
custom_tb = round(1.125 * single) + round(1.69 * _2b) + round(3.02 * _3b) + round(3.73 * hr)
a = h + bb + hbp - cs - gidp
b = custom_tb + round(0.29 * (bb + hbp - ibb)) + round(0.492 * (sf + sh + sb)) - round(0.04 * so)
c = ab + bb + hbp + sf + sh
a_b = round(a + 2.4 * c) * (b + 3 * c)
_9c = 9 * c
_09c = round(0.9 * c)
rc = round(a_b / _9c - _09c, 1)
return rc
@classmethod
def rc27(cls, rc, ab, h, sh, sf, cs, gidp):
"""
Runs created 27
:param rc: run created
:param ab: at bat
:param h: hits
:param sh: sacrifice hit
:param sf: sacrifice fly
:param cs: caught stealing
:param gidp: ground into duble play
:return: (float) run created 27
"""
to = ab - h + sh + sf + cs + gidp
rc27 = round(27 * rc / to, 1)
return rc27
@classmethod
def adam_dunn_batter(cls, hr, bb, so, pa):
"""
Adam dunn %(batter)
:param hr: home run
:param bb: base on ball
:param so: strike out
:param pa: plate appearance
:return: (float) adam dunn
"""
return round(((hr + bb + so) / pa) * 100, 1)
@classmethod
def adam_dunn_pitcher(cls, hr, bb, hbp, so, bfp):
"""
Adam dunn %(pitcher)
:param hr: home run
:param bb: base on ball
:param hbp: hit by pitch
:param so: strike out
:param bfp: batters faced
:return: (float) adam dunn
"""
return round(((hr + bb + hbp + so) / bfp) * 100, 1)