Baseball and SQL

Two great tastes that go great together.

One of the greatest things ever is the Lanham Baseball Database. It’s a database of all baseball statistics since 1871, suitable for random SQL queries. Not quite pitch by pitch, but season by season. Note that retrosheet has made their boxscore database for all games since 1971 available, at least in limited fashion, so you can do a day by day database, which Baseball Musings has done. One can only imagine what sort of amazing data we’ll be able to get in 2071. :)

As an example of the kind of fun you can have, here’s a query to figure out the most recent world series win, loss and appearance for every team in the league:

SELECT
teams.franchid,
MAX(winyear) AS lastwin,
MAX(loseyear) AS lastloss,
MAX(COALESCE(winyear, loseyear)) as lastappearance
FROM teams
LEFT JOIN (
SELECT a.yearid AS winyear,
null as loseyear,
franchid
FROM seriespost a
JOIN teams b
ON (a.yearid = b.yearid AND
a.teamidwinner = b.teamid AND
a.lgidwinner = b.lgid AND a.round = 'WS')
UNION SELECT null AS winyear,
a.yearid AS loseyear,
franchid
FROM seriespost a JOIN
teams b ON (
a.yearid = b.yearid AND
a.teamidloser = b.teamid AND
a.lgidloser = b.lgid AND
a.round = 'WS')) years on (teams.franchid = years.franchid)
WHERE teams.yearid = 2004
GROUP BY teams.franchid
ORDER BY lastappearance desc;

And here’s the output:

franchid lastwin lastloss lastappearance
HOU      
MON      
TEX      
TBD      
COL      
SEA      
BOS 2004 1986 2004
STL 1982 2004 2004
NYY 2000 2003 2003
FLA 2003   2003
ANA 2002   2002
SFG 1954 2002 2002
ARI 2001   2001
NYM 1986 2000 2000
ATL 1995 1999 1999
SDP   1998 1998
CLE 1948 1997 1997
TOR 1993   1993
PHI 1980 1993 1993
MIN 1991 1965 1991
OAK 1989 1990 1990
CIN 1990 1972 1990
LAD 1988 1978 1988
KCR 1985 1980 1985
DET 1984 1940 1984
BAL 1983 1979 1983
MIL   1982 1982
PIT 1979 1927 1979
CHW 1917 1959 1959
CHC 1908 1945 1945

(30 rows)

Note that the database doesn’t yet have 2005 data. But you get the picture.

Posted in baseball, sports, tech | Leave a comment

Math is hard

Einstein says: Math is hard

(from here)

Posted in random | Leave a comment

Brent Scowcroft

Andrew Sullivan:

QUOTE FOR THE DAY:“…I … think Dick Cheney … allied to the core of neocons is that bunch who thought we made a mistake in the first Gulf War, that we should have finished the job. There was another bunch who were traumatized by 9/11, and who thought, ‘The world’s going to hell and we’ve got to show we’re not going to take this, and we’ve got to respond, and Afghanistan is O.K., but it’s not sufficient.’” – Brent Scowcroft, in the new New Yorker.

Well … the question begged is whether Cheney was actually right, if he entertained those two possibilities. After 9/11, the cost-benefit analysis changed a little, didn’t it? Who would want to be the president who gambled (in retrospect, correctly, of course) that Saddam was no WMD threat, and then discovered that some terrorist detonated a Saddam-linked chemical weapon in a major U.S. city? Do you think that president would now be popular? It’s easy to know now, not so easy to have known for sure then. Scowcroft prides himself on always asking about the potential downside. Well, there wsa a pretty major potential downside of trusting Saddam Hussein in 2002. The question was never simply whether we knew the WMDs existed or not. The question was whether, without being able to know for sure, we could trust Saddam to keep such weapons away from terrorists. There’s a realist case for the Iraq war: that the risks of inaction were too high, and that the threat posed by the entire region demanded a radical departure from the acquiescence to autocracy of the past. Scowcroft’s hindsight is a little too easy. He should enjoy it while others deal with reality; and try to change the world for the better.

Well, it’s not just hindsight. Here’s what Scowcroft said in his WSJ editorial August 15, 2002

[T]here is scant evidence to tie Saddam to terrorist organizations… less to the Sept. 11 attacks… Saddam’s goals have little in common with the terrorists… He is unlikely to risk his … country … handing such weapons to terrorists… and leave Baghdad as the return address… He seeks [WMD]… to deter us from intervening… The United States could certainly defeat… and destroy Saddam… would not be a cakewalk… be very expensive… serious consequences for the U.S. and global economy… could be bloody… a military campaign very likely would have to be followed by a large-scale, long-term military occupation…

[T]he central point is that any campaign against Iraq…. is certain to divert us… from our war on terrorism… virtual consensus in the world against an attack on Iraq… would require the U.S. to pursue a virtual go-it-alone strategy… making any military operations… difficult and expensive… Ignoring that clear sentiment would result in a serious degradation in international cooperation with us against terrorism… we simply cannot win that war without enthusiastic international cooperation…

we should be pressing the United Nations Security Council to insist on an effective no-notice inspection regime for Iraq… senior administration officials have opined that Saddam Hussein would never agree… if he did, inspections would serve to keep him off balance and under close observation… if he refused, his rejection could provide the persuasive casus belli which many claim we do not now have… evidence that Saddam had acquired nuclear-weapons capability could have a similar effect.

Here’s the reality.

One, Sullivan is repeating the same disingenuous bullshit in favor of this war when he said “Well, there wsa a pretty major potential downside of trusting Saddam Hussein in 2002.” There was not then nor ever a need to TRUST Saddam Hussein. That’s what weapons inspectors were for. Many people supported the president’s hostile stance towards Saddam when it was geared towards disarming him through the UN process. It wasn’t until February, when it became clear that the President intended to invade, not just in spite of the inspections process but because the inspections process was proving the case for war was CRAP, that many people came around to the idea that this was a really really bad idea.

Two, Scowcroft was right then and is still right now about the risks, dangers and costs of this stupid and thankless war. Sullivan still can’t admit that he was wrong to dismiss the concerns others had, before the war, in realtime, even as he talks a little more frankly about them now.

Posted in politics | 1 Comment

I'm Rich, Beyotch!


My blog is worth $5,080.86.
How much is your blog worth?

Via The Poor Man

Posted in meta | Leave a comment

This isn't me

stop staring.

I’m not like this. Not at all.

Posted in politics | Leave a comment

Sleepy

Moving sure is hard, huh? I’ll try to take some pictures of the new place and upload ‘em soon, but I’m not sure where the SD reader is at the moment.

In other news, activex sucks.

Posted in random | Leave a comment

Activism

I think this guy really, REALLY gets it. It’s kind of a long read. Like it.

Posted in politics | Leave a comment

Patriotism

Patriotism, defined: Capt. Ian Fishback, 82nd Airborne, US Army.

Posted in politics | Leave a comment

CTA transit map

Lots of people have them. I like mine. Check it out.

How I did this:

  • Installed PostgreSQL and PostGIS
  • Tried to load data from the City of Chicago Maps SHP file.
  • Discovered the City uses a weird, us-ft based projection. Installed proj4 to reproject data.
  • Reinstalled postgis to get proj4 support.
  • Realized that doing reprojection inline was crazy, and I couldn’t figure out the right incantation anyway. Installed gdal, which includes a tool called ogr2ogr.
  • Recreated my map database schema with ogr2ogr with the following incantation: ogr2ogr -f PostgreSQL -t_srs EPSG:4326 PG:dbname=maps srcdir. Explanation: -f PostgreSQL specifies output format; -t_srs EPSG:4326 says to translate coordinates from the input spec (which it can read from the .PRJ file) to the standard EPSG mapping #4326 which is mercator lat/long; srcdir contains the unzipped SHP/PRJ/DBF files.
  • Wrote a python script that outputs javascript to create routes and points for lines and stations.
  • Added a zoomlevel field to the stations, a relative ranking of importance/crowdedness, added support for that to the javascript. Once you get to zoom level 4 (two levels in from the starting level) all stations are visible. As you zoom out you see only major stations, then only endpoints, then the airports and Clark/Lake, then just Clark/Lake.
  • PROFIT!

Note that this does not query the database in realtime, once the page is loaded everything is client side. The database is only used for semi-permanent storage of custom markers like zoomlevel and as a reasonably easy way of accessing the data (I’m better with postgres data APIs than I am with csv or parsing shpfiles or whatever).

Posted in tech | 12 Comments

Help me move!

If you’d like to help carry furniture and boxes down three flights of stairs, into a truck, and up one flight of stairs, in and around Evanston and Rogers Park, between 9am and 3pm Sunday October 2nd, please let me know.

Compensation includes pizza and beer. (We’re moving in a block from Giordano’s.)

Thanks!

Update: If you can come saturday and help us carry stuff downstairs, that would be awesome too. T hanks.

Posted in random | 5 Comments