Showing posts with label computers. Show all posts
Showing posts with label computers. Show all posts

06 August 2015

NL vs. AL leadoff hitters

Often while marveling at Ricky Henderson’s amazing stats, I wondered how much greater a leadoff hitter he would have been if he had spent his whole career in the National League. He had 11,180 plate appearances in the AL but only 2,166 in the NL. In both leagues, the leadoff hitter leads off the first inning, but is not guaranteed to bat leadoff in any following inning. However, I figured that in the National League, batting after the pitcher, it’d be substantially more common that the person batting first in the order would get to lead off. The pitcher almost always makes an out, so I figured it’d be pretty common for him to make the third out (and because of situations where the eighth batter is walked to get to the pitcher, probably more common than one in three).  The eighth batter isn’t that strong in the AL, but a lot stronger than almost any NL pitcher.

I’ve been working off and on over the past two years (more off before getting tenure, more on after getting tenure) on an extremely flexible python toolkit for examining baseball games and it finally got to the state of development where I could test my findings.  I’m not ready to release the toolkit yet (it needs to be polished enough that I’m proud of it), but here’s the code I used to work:

  gc = games.GameCollection()

  gc.yearStart = 2000

  gc.yearEnd = 2014

  gc.usesDH = True

  allGames = gc.parse()

  totalPAs = 0

  totalLeadOffs = 0

  for g in allGames:

      for halfInning in g.halfInnings:

          for p in halfInning.plateAppearances:

              if p.battingOrder == 1:

                  totalPAs += 1

                  if p.plateAppearanceInInning == 1:

                      totalLeadOffs += 1

  print(totalPAs, totalLeadOffs, totalLeadOffs*100/totalPAs)

It gets a collection of games where the DH is used or not used, looks at each game, then at each half inning, then at each plate appearance. If the batter is #1, then it checks whether it’s the first appearance in the inning, then prints out the percentage of all batter #1 plate appearances which are leadoffs.   The results were surprising to me. 

       PAs  Leadoff    %
No DH 183,033 75,364 41.2%
With DH 163,1781 63,451 38.9%

The average difference in the percentage of leadoff plate appearances between the two leagues (accounting for interleague games) is only about 2.5%. This works out to about 15 PAs a year different for Ricky in his prime. So one hypothesis down, but many more to be investigated soon.

17 September 2014

Recent Research

A few recent articles and software of interest and some older articles that might have been missed:

31 May 2014

ESPN's MLB Power(??) Rankings

The bottom rungs of the
May 26, 2014 rankings
Each week since week 15 of the 2005 baseball season, ESPN has been compiling "Power Rankings," a list from 1 to 30 of the quality/current trends/strength of each team in Major League Baseball. Each team appears with its ranking, logo, name, current record in wins and losses, and a witty factoid about the direction the team is going in or about a player or two who exemplifies that trend. (Though the meaning of these trends is sometimes obscure as I've discussed before.)

It always rankles me when my favorite teams appear lower than other teams with the worse records than my teams' own. Of course that's part of the point of these rankings: to show subjective impressions of who is actually better than someone else despite having the record not showing their "inner strength" or some mumbo-jumbo like that. Still, when week after week, it appears that your team is getting the shaft, you begin to wonder if perhaps the rankings aren't so much about power on the field as power to draw viewers to the Sunday Night featured game, which doesn't exactly showcase all thirty teams equally.

To test whether there's something more happening, I looked at all of the power rankings from Week 15 of 2005, when ESPN started them or at least put them online in their current format, to the latest incarnation, Week 10 of 2014. (No, I didn't look at them all directly, I have computers for that.). For each team each week, I assigned a "bias point" for every rank that a team was above them with a worse win-loss record than them. For instance, if a the Jays at 25-20 were ranked 2 and the teams in ranks 3 and 5 had better records then them then the Jays would get 4 bias points, one for the team with rank 3 and three for the team in rank 5. Similarly teams would get negative bias points for teams ranked above them with worse records. In the illustration above, the Padres (my favorite, generally losing team) would get -1 points for being a position below the Red Sox (my next favorite, generally winning team). The Cubs and Diamondbacks would exchange no bias points even though percentage wise the Snakes are slightly ahead.

Over about ten years of Power Rankings, the teams most often ranked above their records were

1. Yankees (2052 points)
2. Red Sox (1101)
3. Tigers (968)
4. Angels (945)
5. Blue Jays (918)

The bottom five were

30. Pirates (-1141)
29. Astros (-1113)
28. Orioles (-977)
27. Rockies (-848)
26. Marlins (-805)

Since I started this to look at the Padres (-774), I'll just note that they ranked 24th. For the most part, these numbers make sense -- even though these bias rankings already take into the power ranks assigned on the basis of current records, the people at ESPN wouldn't be earning their keep if they didn't take into account historical trends. In fact, looking at weeks 8 and beyond, the amount of bias is less, and there's some shuffling throughout the ranks, though not at the very top:

1. Yankees (285)
2. Indians (241)
3. Phillies (210)
4. A's (178)
5. Angels (122)

30. Brewers (-380)
29. D'backs (-326)
28. Astros (-220)
27. Cardinals (-175)
26. Mariners (-153)

(Padres jump to 18th at -30 points, actually beating the Red Sox who drop all the way to 22nd at -70! Clearly those second half spurts and slumps, respectively, make a difference.)

A stronger showing of bias would be whether there's a long-term difference between the record of a team and its bias measure. The five most winning teams over the ten-year period were 1. Yankees, 2. Angels, 3. Red Sox, 4. Cardinals, and 5. Phillies and the most losing were 30. Royals, 29. Pirates, 28. Astros, 27. Mariners, and 26. Orioles (Cubs fans, be glad I didn't extend this list one more spot! oops.; Padres hit #21). Subtracting the win-loss rank from the Power Rankings bias rank gives a ranking of systematic difference that cannot be explained by records alone.

Most biased for:
1. Cubs (WL rank: 24.5; PR rank: 12; = difference: 12.5)
2. Blue Jays (17 — 5 = 12)
3. Royals (!! Rob Neyer?) (30 — 22 = 8)
4. Indians (19 — 13 = 6)
5. Tigers (7 — 3 = 4)

At the bottom:
30. Cardinals (4 — 16 = -12)
29. Brewers (11 — 20 = -9)
28. Rockies (22 — 27 = -5)
27. Reds (15 — 19 = -4)
26t. D'backs, Padres, Marlins (20 — 23 or 21 — 24 or 23 — 26 = -3)

In both of these lists there are good, average, and pretty bad teams. There are some adjustments that could be made. For instance, since the records only reflect the regular season we could adjust for World Series wins and pennant wins, subtracting a system bias point for each (i.e., 2 pts for winning the Series and one for losing). To keep the numbers exactly constant we'll add one point per team (I love it when the math works out: 3 pts per season and 10 years in the dataset = 30 points, or exactly one per team). At the top nothing changes, since none of the top four teams have done anything in late October, except that the Tigers disappear from the most overrated (two pennants will do that for you) to be replaced by the Nationals. At the very bottom, nothing changes -- in fact four World Series appearances for the Cardinals just makes the bias even worse. San Francisco and Philadelphia make their way into 27 and 26.

Technically by this method, the Red Sox tie Philadelphia, but as #2 on the PR positive bias, they're hardly being discriminated against by any measure, but they do have a legitimate beef against the way they've been treated in the last two-thirds of the season.

There are a lot of ways to slice the data; some of which make the Padres look exploited, and others the give them more credit than they deserve. However, any way you look at the numbers -- adjusting or not for postseason success, including or excluding the start of the season -- there are some teams that the ESPN staff are definitely fans of: the Cubs, Royals, Indians, Nationals, Rays, and Jays. And there are some that get no love: Rockies, White Sox, Reds, Rangers, Giants, Diamondbacks, and Brewers. But most of all its the St. Louis Cardinals who time and time again get the shaft on the Power Rankings. Maybe it's time for the crew to stop batting their eyelashes at the Friendly Confines and look for inner strength down I-55.

(attachments: Excel Spreadsheet and, in case anyone wants to look at NFL, NBA, or NHL Power Rankings, the Python program that generated the data)

26 December 2009

Separating out Music Informatics topics

Hi all -- just wanted to mention that I've separated out the Music Informatics topics in a separate blog at http://music21-mit.blogspot.com/ . I suspect that this blog will mainly be about other musical and non-musical topics.

06 May 2009

Put figures etc. in MS Word without the "Figure" tag

One of the annoying things (at least for me) about Microsoft Word is that it wants all your figure numbers to have "Figure" prepended to them. It's all great if you always say "Figure 1" or "Figure 2" but it doesn't work if you like to say things such as "As we see in Figures 1 & 2" and not "As we see in Figure 1 and Figure 2." Furthermore, Word doesn't work well with things such as "Figure 4.12" or, my favorite, having Figure 1 followed by Example 2 followed by Table 3 -- I'm big into continuous numbering.

So here's what I finally came up with as a solution. Type whatever preface you want to appear in the caption of the document ("Figure ") and then run this macro and give your figure a short name with no spaces ("voiceRangesDunstaple" for instance), then two numbers will appear. Leave the first alone; It'll put a number after "Figure" ("Figure 12" for instance). Copy and cut the second number and then paste it in your text somewhere -- it's the reference number to the figure ("See figure 12" -- I like lowercase figure references). You can paste this number multiple places if you need multiple references. And a quick "Ctrl-A, F9" will update all the tables.

You'll need to give this macro a keyboard shortcut if you use it often.

---
Sub MakeFieldCrossRef()
'
' Macro recorded 3/25/2006 by Myke
'
Dim Message, Title, Default, macroName
Message = "Enter a short, unique name for the Xref (no spaces!)"
Title = "Create Crossref"
Default = ""
macroName = InputBox(Message, Title, Default)
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTONUMLGL \e", PreserveFormatting:=False
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=macroName
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:= _
wdContentText, ReferenceItem:=macroName, InsertAsHyperlink:=False, _
IncludePosition:=False
Call UpdateAllDocFields
End Sub