Sunday, August 14, 2011

Transparent PNG (or GIF) From the Command Line

Given the popularity of my post about making transparent PNG images with the Gimp, I should share a recent discovery: If you know the exact color you want to make transparent, you can do it from the command line.

What you need is the ImageMagick package, picture manipulation software that's available for all major Linux distributions, Windows, and Mac OS X (through MacPorts). If none of those work for you, you can also compile the source code.

Specifically, you need the convert command line tool. The syntax is:
convert -transparent color original_picture.png picture_with_transparent_background.png
and you can covert from png to gif, or visa versa, with the same command.

An example: here's the good ol' pengjay, with a horrid green (#00FF00) background:

Pengjay with green background

Since we know the background color is #00FF00, we can pipe it through convert:

convert -transparent #00FF00 pengjay_green.png pengjay_transparent.png

and we get this:

Pengjay with transparent background

Note that if you don't know the color, you're better off using the GIMP.

Saturday, August 06, 2011

Can I Go Home Now?

We've all been there. The home team drops seven runs in an early inning. By the seventh inning stretch they've shown no sign of an offense, and are still six runs down. It's hot, muggy, Washington night, there are thunderstorms brewing over the horizon, and your wife just phoned that she heard on WTOP that Rt. 50 to Annapolis was closing for repair work at 10 p.m.

Question: If I go home now, am I likely to miss anything? Aside from heat stroke and road rage?

Answer: Probably not. If the visiting team is ahead by six runs after the top of the seventh, historically the chance that the home team will pull out a victory is 1.6%, or about 60 to 1 against. Unless you're of the extreme optimist persuasion I'd suggest going home.

I got to thinking about this a few years ago, when Bill James published an article in Slate on when a college basketball game is really over. Based on his observations, he was able to come up with an algorithm which predicts when a team has a safe lead, based on the lead, time left in the game, and who has the ball.

In baseball we can do the same kind of thing, except that there are only a finite number of logical stopping points (the end of a half-inning) and leads (the largest of which was less than 30 runs). Plus, we have a line score for just about every major league baseball game played since 1900, so we have a lot of data. This means that we don't need no stinkin' algorithm, we can give you the history probability that any given lead was overcome.

I didn't go all the way back to 1900. I stopped at 1948, because that was the data that was available using Retrosheet's Play-by-Play Files. With the Chadwick Software Tools we can go through all the games in the database and see how many times that, say, the visiting team was ahead by five runs after then end of the first, and how often the home won in that situation. Do that for all possible combinations of leads and innings and we get the table below. (You may have to widen your browser window to see everything.)

Visitors Lead Tie Home Lead
Inn. 10+ 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10+
T 1 .077 .000 .040 .041 .083 .157 .187 .304 .378 .486 .591                    
B 1 .000 .000 .063 .056 .055 .132 .148 .236 .313 .416 .533 .643 .740 .819 .893 .912 .933 .960 1.00 .944 1.00
T 2 .000 .027 .067 .057 .080 .169 .180 .262 .345 .452 .580 .692 .781 .846 .925 .929 .969 .974 1.00 .941 1.00
B 2 .000 .016 .043 .032 .055 .134 .127 .218 .295 .397 .532 .656 .756 .829 .895 .920 .956 .966 .957 .976 1.00
T 3 .034 .032 .019 .043 .075 .133 .166 .250 .339 .456 .582 .705 .801 .863 .927 .939 .972 .984 .980 1.00 1.00
B 3 .015 .024 .016 .031 .047 .095 .122 .200 .278 .394 .526 .655 .766 .840 .909 .930 .958 .977 .986 .989 .990
T 4 .012 .021 .028 .033 .071 .096 .145 .225 .314 .446 .589 .719 .820 .886 .933 .963 .971 .988 .990 .994 .988
B 4 .004 .011 .022 .028 .039 .062 .094 .164 .252 .369 .526 .677 .788 .866 .917 .949 .969 .986 .990 .992 .996
T 5 .004 .009 .020 .031 .045 .078 .114 .199 .290 .422 .591 .741 .843 .904 .945 .967 .982 .995 .995 .991 1.00
B 5 .002 .005 .009 .019 .026 .049 .077 .139 .224 .344 .523 .692 .808 .887 .936 .962 .981 .989 .995 .993 .999
T 6 .001 .005 .011 .019 .031 .055 .091 .161 .268 .406 .600 .771 .873 .931 .964 .975 .994 .995 .999 .998 .998
B 6 .001 .000 .005 .009 .014 .030 .049 .097 .185 .305 .520 .725 .848 .917 .956 .972 .989 .994 .998 .999 .998
T 7 .001 .001 .004 .010 .016 .038 .061 .118 .217 .357 .610 .819 .912 .958 .977 .991 .995 .998 .999 1.00 .999
B 7 .000 .002 .002 .002 .007 .015 .033 .059 .130 .245 .523 .772 .894 .949 .973 .990 .995 .998 .999 .999 .999
T 8 .000 .001 .003 .004 .007 .017 .041 .074 .153 .296 .634 .890 .956 .985 .992 .997 .999 1.00 .999 1.00 1.00
B 8 .000 .000 .000 .001 .002 .006 .013 .027 .067 .150 .519 .865 .944 .981 .990 .997 .999 1.00 .999 1.00 1.00
T 9+ .000 .000 .000 .002 .003 .007 .014 .033 .071 .157 .615 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
B 9+ .000 .000 .000 .000 .000 .000 .000 .000 .000 .000 .524 1.00 1.00 1.00 1.00            

So what is all of this?

  • The left hand column represents the situation at the end of either the top (visitor's) half of an inning, or the bottom (home team's) half, so T 7 describes the situation just before the Cubs let that day's designated karaoke singer ruin Take Me Out to the Ballgame. Since all innings after the ninth are played under the same conditions — the team ahead after the bottom of the inning wins, and if it's tied we try, try again — I combined all the data from those games into the labels T 9+ and B 9+.
  • The other columns represent the lead by either the home team (on the right) or the visiting team (left) at the end of the half-inning. Ties are right in the middle.
  • The decimal fraction in each block indicates the home team's chance of winning in that situation. So, for example, if you've just sat through an agonizing top of the third, when Yankees have scored a bunch of runs and lead by 7, we look at row T 3, column Visitors 7, and see that the Orioles have a 0.043 (4.3%) chance of winning the game — no, that's not true. Over the course of the last 60 years, 4.3% of all major league home teams down by 7 going in to the bottom of the third have come back to win. These are the Orioles, however, so they have about a 0.001% chance of coming back.
  • I put all leads of ten or more runs in the 10 + categories. The software I wrote to write the table is easy to modify to list bigger leads, if you like.. I stopped at 10 because that will more or less fit on a standard blog page.
  • The blank spaces are impossible situations. The home team can't score before it gets up to bat, so the right-hand side of the T 1 row can never be reached. And the home team doesn't need the bottom of the ninth or later inning unless it was tied or behind after the top of the inning. In that case they can never win the game by more than four runs.
  • Indeed, I debated about putting in the B 9+ column, since it's a trivial case, but I wanted to highlight the fact that the home team still has a big advantage if the score is tied in late or extra innings. I've discussed this elsewhere.
  • Finally, I haven't told you about the statistical significance of these results, i.e, the standard deviation or the sample size. Suffice it to say that there are more than enough events here for leads of nine runs or less. When we get to the 10+ category, especially in the early innings, there just aren't that many games. If you want to see the raw numbers (home wins/visitor wins/total games) drop me a line and I'll send them to you.
  • Really last finally: I made a slight modification to the Chadwick source code to make it easier to parse the runs scored per inning. I also wrote a Perl script and some Fortran code to parse the output from Chadwick and write the HTML for the table above. If you're interested in the codes, drop me a line. If there's enough interest I'll make all the code available on my website.