Restarted Gintis’s Game Theory Evolving

slaniel | Evolutionary game theory;Game Theory Evolving | Sunday, June 17th, 2007

Having loved his longtime collaborator Samuel Bowles’s microeconomics textbook, which contains more than its share of evolutionary game theory, I decided to take another crack at Herbert Gintis’s Game Theory Evolving. This is another of those (frequent) occasions when something that seemed really tricky to me at first suddenly makes a lot of sense. I have no idea why, but payoff matrices for static games have always been really tricky for me to read. Now they’re not. Go figure. The math has certainly never been beyond me; reading Maynard Smith, Bowles, and others suggests that understanding at least the rudiments of the evolutionary theory involves only knowing some basic calculus. Of course proving anything in greater depth requires more work, but nothing at the beginning levels should have confused me.

I also picked up Hofbauer and Sigmund’s Evolutionary Games and Replicator Dynamics — what I take to be a mathematically more-detailed treatment of the problems I’ve been reading about — on Cosma’s recommendation. And also Maynard Smith and Szathmary’s Major Transitions in Evolution (not really related to the subject matter of the other books I’ve mentioned here; it’s related more because Maynard Smith broke a lot of ground in this area, and because I loved his Evolution and the Theory of Games), on which I promptly spilled coffee. The library will soon have a pristine copy from Amazon, and I the sullied one. Not for nothing am I known amongst my college friends as “Sir Spills-A-Lot.”

A brief note on Call It Sleep

slaniel | Call It Sleep | Saturday, June 16th, 2007

I finished Call It Sleep a couple days ago, and didn’t have time to write anything about it then. I’ve got a moment now, so it’s worth noting just how great a book it is. Henry Roth wants to do two things well in that book: first, accurately describe the experience of being a child — not a tough, bully-type child, but a shy kid with no friends. (I can relate.) Secondly, he wants to capture the language spoken by native New Yorkers and by immigrants to the city.

It might be best to explain the book’s trick as “inside versus outside.” Most of the time, we stand in a position of semi-omniscience, much like in Crime and Punishment: while the godlike narrator in Crime and Punishment could see inside Raskolnikov’s head and no one else’s, we are allowed into David Schearl’s mind while he wanders terrified through the world. David understands perfectly well why he’s so scared, and by the end so do we — but we also understand why he can’t explain his terror to anyone else. We are trapped in the child’s head with him. It’s been a very long time — probably since I was David’s age — since I’ve remembered those feelings.

The language of New York’s Jewish ghettoes in Call It Sleep also has an inside and an outside, and Roth’s great trick is to pull us so deeply into that world that it’s a slap on the face when we’re back outside. The immigrants talk to one another in their native Yiddish, in which there’s great poetry and biblical allusion (as well as more than a few “may your remaining days be dark”-type curses). We’re steeped in that world. Only occasionally do the immigrants step outside and talk haltingly with, say, a local policeman. They are shy, awkward, and adrift. Roth is so ingenious in the delivery that we feel their shyness and awkwardness as though it were our own.

It’s rare to find a book that is so committed to its characters. Roth has no ulterior motive. He just wants to introduce us to this little community and its little people. If we happen to see larger meanings or other people in those he depicts, it’s accidental. That sort of devotion to character is extremely rare. I can only imagine how absorbed in the characters Roth must have been, if he drew his reader in that completely.

Lawrence Weschler, Mr. Wilson’s Cabinet of Wonder: Pronged Ants, Horned Humans, Mice on Toast, and Other Marvels of Jurassic Technology

slaniel | Mr. Wilson's Cabinet of Wonder | Saturday, June 16th, 2007

Splendid little read, profound in its own way, and outright devilish. Absolutely in keeping with its subject matter; anything shy of devilish would have been cheating.

The Museum of Jurassic Technology, to trim descriptions to the bare minimum in the interests of time and of not spoiling the fun, is a museum that may or may not be entirely a joke on the part of its owner. If it is a joke, it is the most ornately gilded, realistically depicted, and intellectually rewarding joke yet perpetrated on the good citizens of California.

Lawrence Weschler may or may not, himself, be in on the joke. The whole thing, if it is not a joke, is a delicious insight into what the modern world has gained and lost, and an attempt to restore some of what’s disappeared.

Well worth the two hours of reading. If I had more time, I would certainly recreate the research that Weschler did when he started to get obsessed with the MJT.

Python: Collapsing a dict with list values into a single list

slaniel | Python | Friday, June 15th, 2007

Suppose you have something like so:

my_dict = {
    'key1' : ['val11', 'val12', 'val13'],
    'key2' : ['val21', 'val22', 'val23']
} 

— i.e., a dictionary with lists as the values. Suppose you want to collapse this dict into a single list. You could do it like so:

my_list = list()
for key in my_dict.iterkeys():
    my_list.extend( my_dict[key] ) 

but there’s a nicer way using list comprehensions:

my_list = list()
[my_list.extend(x) for x in my_dict.itervalues()] 

Initially that looked like a mistake to me — as though the second line would return a new list, do nothing with it, silently drop it and leave my_list empty. But in fact that second line does just what you’d think. I find it cute.

Schneier on the “terrorist as idiot”

slaniel | Terrorism and psychopathology thereof | Thursday, June 14th, 2007

One of the things I love about the New Yorker is that its perspective is much wider than the average blog post’s, though not as wide as a book’s. But every now and again you’ll get a blog post that takes a big step back, takes a deep breath, and surveys the scene clearly and calmly. Today’s post by Bruce Schneier on the overblown reactions to idiotic terrorist conspiracies is a good example of the best that blogs can offer.

Taking maybe one extra step back here would also be valuable: we should ask why the reactions to purported terror plots are so overblown. In particular that means asking why cover-your-ass policies are encouraged, whereas substantive ones are not. Also, as Schneier points out, the media play a large role in spreading paranoia. But if they do, and if paranoia works to the advantage of those in power, then you’d expect those in power to exploit the media as much as possible — which they do. So if you actually want to minimize the effect of incompetent terror threats like the JFK one, you need to focus on the media and on making substantive policies more attractive to politicians. I have no idea how to do that, but it seems like an investment in that direction would bear the highest long-run returns.

(Post included below)

(more…)

Closer . . . 

slaniel | Red Sox | Thursday, June 14th, 2007

Ahhh, this feels closer to what it should be:

Standings as of 2007-06-14; Sox on top, Yankees 8.5 games back, etc.

Thank you, Deity Of Choice, for realigning the Forces and the Fates and so forth.

Capitalization frustration

slaniel | Programming languages;Python | Wednesday, June 13th, 2007

I wish there were a way to turn on a compiler flag in Python — in really any language — that warns me if I’ve got two variables in the same scope whose names differ only trivially — e.g., different capitalization, a dash in one where there’s an underscore in the other, etc. So much would I like this.

LaTeX fraction characters

slaniel | Tex and LaTeX | Monday, June 11th, 2007

Does anyone know how to get fraction characters in TeX/LaTeX? I.e., something that looks like the HTML ½ (½), ¼ (¼), or ¾ (¾) entities? I could do \frac{1}{2}, \frac{1}{4}, and \frac{3}{4}, or just “1/2”, “1/4”, “3/4”, but those don’t render quite like I want them.

A Woman In Charge — worth getting?

slaniel | Clinton, Hillary;Woman in Charge, A | Sunday, June 10th, 2007

Advertised on Talking Points Memo just now:

New bio of Hillary Clinton by Carl Bernstein

I wonder whether I should read it. I’m unfortunately lumping Carl Bernstein in with Bob Woodward, which makes me suspect that the Clinton bio will be fawning and political-pornographic. That’s unfair, though. Bernstein’s been out of the game for a long time, so I wonder whether he’s jumping back in because he feels like something needs to be said.

If the book manages to avoid political theatre (“Clinton won tonight’s debate by appearing warm, where she’s often portrayed as cold”) and actually explain to me what Clinton believes, then we’ll have gotten somewhere.

I also just inherently distrust books whose release is timed with campaigns. Then again, campaigns nowadays start about two years before the next presidential election, so it would be hard not to time book releases with campaigns.

Python: Default values for mapping types

slaniel | Perl;Python | Sunday, June 10th, 2007

Perl has a nice shorthand, which shows up in practice pretty often. Suppose you have a file that looks like so:

key1 3
key2 9
key1 4
...  

and you want to tally up the right column by the value on the left — e.g., in the rows shown, the total corresponding to ‘key1’ is 7 and that corresponding to ‘key2’ is 9. You could do that in Perl like so:

#!/usr/bin/perl
use strict;
use warnings;
my %map = ();
while(<>) {
    my ($key, $val) = split;
    $map{$key} += $val;
}
while( my ($key, $sum) = each %map ) {
    print "$key\t$sum\n";
}

You might try to do the same thing in Python like so:

#!/usr/bin/python
import fileinput
map = dict()
for line in fileinput.input():
    key, val = line.split()
    map[key] += val
for key, val in map.iteritems():
    print "%s\t%s" % ( key, val )  

but Python would be angry at you:

Traceback (most recent call last):
File "./row_sum_test.py", line 9, in <module>
map[key] += val KeyError: 'key1'  

It’s complaining because it hadn’t yet seen key1 when we tried to add val to it. Instead you need to go through a little hoop:

map[key] = map.get(key, 0) + int(val)  

which says “look up key in map; if you find it, great; if you don’t, use the default value 0. Then add val to whatever you get from that.” (And while we’re at it, have Python throw an error if val isn’t an integer.) That’s not a terrible hoop.

Python has a cute little short form — the setdefault method for dictionaries — that turns out to be useful when the values in your dictionary are arrays rather than scalars. For instance, suppose you wanted the output from our example above to look like so:

key1 3, 4
key2 9  

You could do that in Python like this, in relevant part:

map.setdefault( key, [] ).append(val)  

which says “if key isn’t already defined in map, use the empty list [] as the default value of map[key]; if it is defined, great. Then append val to whatever you got from that.”

Perl doesn’t let you have complicated data structures as values to hashes; it only lets you have references to complicated data structures. This is why Perl turns into line noise fairly quickly when you have mildly complicated data structures like hashes of lists. To create and unpack a hash of lists in Perl, you’d do

while(<>) {
    my ($key, $val) = split;
    push @{$map{$key}}, $val;
}
{
    local $" = ', ';
    while( my ($key, $arrayRef) = each %map ) {
        print "$key total = @$arrayRef\n";
    }
}

(The local bit in the middle there is one of Perl’s magic variables, and let me save a teensy bit of line noise elsewhere. It says “when we print out an array, separate its values with a comma and a space.” Without that, they’d be separated by spaces. We have to declare $" local and put it inside an anonymous scope; otherwise that record separator would stay set to ', ' later in the program.)

The Python approach does seem cleaner than the Perl one, and does seem to scale better to large nested data structures. This may be Perl’s story generally: while you could write readable, scalable code in Perl, it’s easy not to.

One of the ITA problems

slaniel | ITA Software | Sunday, June 10th, 2007

One of the problems that ITA is putting up on its public ads is a modified version of what’s on its website. The problem is as follows: take all the numbers between 1 and 999,999,999, write them out without dashes or spaces or the word ‘and’ (e.g., “nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine”), concatenate them (e.g., “eleventwelvethirteenfourteen”) and figure out what the 51 billionth letter of the concatenated string is.

I got a little hint today that accords with the general principle behind a lot of the ITA problems — a hint that I should have thought of on my own, but somehow didn’t — so I sat down to code the solution in Perl. What I wrote really seems pretty decent and elegant, but it’s still running quite slowly. I wonder how long they expect the program to take. Mine is spitting out updates at every hundred thousandth number; for instance, I get

At number 20,000,000: "twenty million" is 13 letters long;
total # of letters so far = 1,136,440,013 

I.e., I’m only about a fiftieth of the way there, and it’s been running for half an hour so far. Every hundred thousand numbers, we add about 6 million letters to the string, though of course the average length per number if gradually increasing. At this rate I’ll hit the 51 billionth letter around 911 million.

I wonder if there’s an even cleverer way to do this that I’m missing. Using C instead of Perl might increase the speed somewhat, but I wonder whether my algorithm is just bad.

Random WiFi availability

slaniel | WiFi | Saturday, June 9th, 2007

I’m sitting right now on a bench at the train station in Exeter, NH. The reception is really great. This never grows old.

There was a time when I was taking the bus up to Vermont with some regularity. There was a particular intersection in Hanover, New Hampshire when the bus would often stop at a red light; the time at that light was just long enough for me to download all my mail, upload anything I’d composed, and download any unread RSS posts. I still find that very cool, even though the free-WiFi thing is largely past.

Python: variables and attribute names

slaniel | Python | Thursday, June 7th, 2007

Hm. I want to do this:

for i in dir(my_object):
    print my_object.i 

Here dir() is a Python introspection method: it lists all the attributes and methods attached to an object, including any that it got through inheritance.

The trouble is that in the code above, Python interprets “.i” to mean “the method called ‘i’ that’s part of myobject” as opposed to “the variable which itself stores the name of a method which is attached to myobject.”

I’m sure there’s a well-known way around this. I’ll Google and write any results here.

P.S.: Ah so. One does

for i in dir(my_object):
    print getattr( my_object, i ) 

Jack Balkin on the “Winner’s Constitution”

Balkin has an excellent post up on his blog about how our interpretation of the Constitution is shaped. Along the way, he gives a highly believable story about what would have happened to the Constitution had the Iraq War gone better. It’s very frightening, and seems to me highly accurate. But it’s yet another reminder, if one were needed, that we don’t interpret the Constitution according to formal criteria, or according to “what the literal words say.” Whether we accept that our government can torture people depends in large part on who’s in power and on shifting political sands. I can’t imagine much being more frightening than that. Except for maybe saber-tooth tigers with forehead-mounted laser cannons.

(Balkin’s post included below. Note that I think Balkin is still the best writer on his site, though Sandy Levinson is a one-man encyclopedia of torture law.)

(more…)

Python: stringifying the None object

slaniel | Python | Wednesday, June 6th, 2007

Python’s None object, as far as I can tell, is like Perl’s undef — it’s one way of expressing a null object, with the empty list [], empty dictionary {} and empty tuple () being others.

Check what happens when I run the following:

#!/usr/bin/python
my_str = None
print "my_str:", my_str
my_str = str(my_str) + 'a thing'
print "my_str:", my_str  

I get

(09:07) slaniel@slaniel-laptop:~/python_test$ ./none_test.py
my_str: None my_str: Nonea thing  

I would have expected that the string representation of the None object was the empty string, so that the second print statement above would have given just “a thing” rather than “Nonea thing”.

Since None has no methods, it particularly has no str() method, so it doesn’t know how to represent itself as a string. In the absence of such a method — or any methods at all — I’m not sure what the default way of stringifying onesself is.

So then what I want to do initially is my_str = “. Easy enough. But I wonder if there are more tricks to handling None that I should be aware of.

P.S.: Likewise, it’d be handy if the None type had a len() method that always returned zero.

P.P.S. (7 June 2007): There’s always this approach:

class MyNone:
    def __init__(self):
        self.none = None
    def __len__(self):
        return 0
    def __str__(self):
        return ''

myobj = MyNone() print “Length of myobj = %d” % len(myobj) print “myobj = ‘%s’” % my_obj

which prints 0 and the empty string where you’d expect. I’d like to be able to override the built-in None object rather than create my own, though. Or maybe add__len__() and __str__() methods to an existing class.

Béla Fleck and Edgar Meyer, “Canon”

slaniel | Uncategorized | Tuesday, June 5th, 2007

There’s a tune on Fleck and Meyer’s Music for Two called “Canon” which calls for Fleck to play whatever Meyer plays, only delayed by 15 beats. It hurts my mind to think of the skill involved in playing that. It also happens to be a beautiful song.

Greg Egan, Permutation City

slaniel | Permutation City | Monday, June 4th, 2007

Borrowed from Dylan Thurston and recommended by Cosma Shalizi, I think it’s fair to say that Permutation City is the first book I’ve read in a long while that wrapped my brain around itself.

It’s a study of what happens when people can make perfect digital copies of themselves to guarantee their immortality. Those virtual people go on to create their own virtual worlds following the rules of simple cellular automata. Those automata evolve over billions of years into sentient beings of their own  . . .  The possibilities are endless, and the potential in the story for limitless mindbendery is truly staggering. (One natural first question, which the book impressively never really touches upon except implicitly, is whether we ourselves were made by an omnipotent software developer. Approaching theological questions through the ethically neutral ground of finite automata is really quite smart.)

Egan keeps it together beautifully. This was an intensely thought-provoking book, and I think I may need to buy my own copy and reread it; I certainly didn’t get all of it on a first pass.

The effect wouldn’t nearly be so great if Egan were a wanker. But he really does seem to understand the science (at least at the level at which I understand it, which is certainly not that of a practicioner), so I never felt hoodwinked by someone inventing consequences out of thin air. And Permutation City never suffered from the problem in so much sci-fi, namely that the author takes a pamphlet, and after much pushing and squeezing and grunting manages to fit it into a 300-page novel. Not one page of Permutation City could be deleted without either a) sacrificing a necessary consequence of the world-within-a-world-within-a-world construct, or b) leaving out necessary exposition that makes the ideas in a) understandable.

It’s a tremendous achievement, and I’m going to be mulling it over for a long while.

Misunderstanding the world and history

slaniel | Uncategorized | Sunday, June 3rd, 2007

Stirrings of a new neocon tragedy-to-come — this time in Afghanistan — has left me with a terrible, sunken feeling. It’s not just that the United States, in my name, has fouled up the world. It’s not even that we’ve done this in two countries that were already among the world’s least-fortunate and longest-suffering. It’s not just because the United States — myself included — has utterly forgotten about Afghanistan and is trying as hard as possible to forget about Iraq. The trouble isn’t even that we keep destroying places, going back to at least Vietnam, and probably back at least to the Philippines.

Really, I think what’s most upsetting about all of this is how ignorant I am, and we are in general. If we — if I — knew more, and had even an inkling about what the world was like, we wouldn’t keep fucking it up. The Onion did a good job summarizing the problem.

We just keep invading places, thinking that if we apply enough Reason and Democracy, things will look like we want them to, under our wise eye and beneficent hand. It doesn’t work like that — couldn’t work like that; it’s as unlikely as Saudi Arabia invading the United States and turning us into a Muslim theocracy.

And I’m guilty, because I don’t have the faintest clue about the history either. So I stand passively mute while my country makes life miserable for many millions of human beings.

Just a little standardization, please

slaniel | Uncategorized | Sunday, June 3rd, 2007

It’s frustrating how many ways the various Unices differ from one another, and even all the little apps between them. For instance, if you want to use grep(1) to return just the part of a given line matching a pattern, rather than the entire line, you can use grep -o on some platforms but not on others. Since you can’t rely on the -o option always existing, the only really portable way to do it is to use grep to get the entire line, then sed to cut out the parts preceding and following the match (if anyone knows a better way, do let me know). Not every version of sed allows you to do sed [action] [filename]; many require that you use sed -e.

The latest difference that got me was fg, which makes a currently running job the foreground process. Under bash, you can just do fg [job number]; under zsh, it’s fg %[job number].

I wonder if we need a new POSIX. POSIX came to be because the lack of standardization was killing UNIX; programmers couldn’t write a program to work “under UNIX,” so someone had to standardize exactly what that meant. A lot is standardized now, and presumably if you’re careful enough you can write an app that will work on most variants of UNIX. But time has marched on, and a lot has changed since POSIX came out, result being that there are new incompatibilities. There are even incompatibilities between different distributions of Linux, which is sad; one would have hoped that Linux would kill off the rest of the Unices and leave glorious standardization in its wake. But it wasn’t meant to be.

Of course this is UNIX’s glory as well as its shame. I’ve tried to explain to people that UNIX and Linux aren’t really operating systems as such — or at least not OSes the way we think of Windows or OS X. They’re not packaged goods like Pepsi or Oreos. UNIX and Linux are best thought of as folktales — an idea that I’m pretty sure I’m borrowing from “In The Beginning was the Command Line”. Like a folktale, anyone can retell them, and anyone can add his own variation or remove what he doesn’t like. Anyone on earth could make a UNIX if he wanted to. One of the reasons Linux exists, in fact, is that Linus Torvalds was taking a class from Andy Tanenbaum, got frustrated with what he saw as the cruddiness of Minix (a simplified UNIX that was good for teaching OSes), and built his own. Like any folktale, every UNIX has to conform to the standards of the genre: you couldn’t tell people you were reciting the Iliad while giving them the Rāmāyạna. The POSIX standard codifies parts of the folk tale while leaving a lot up to the discretion of the storyteller. There’s been a great flowering of Unices and Linux distributions as a result, and presumably giving people the power to operate within their own chosen version of the folktale has let people speak in idioms that make the most sense to them. But it has led to trouble for the rest of us.

My beautiful city

slaniel | Uncategorized | Sunday, June 3rd, 2007

You know, crossing over the Charles never grows old. I live in an amazingly beautiful city. I’m trying to find just the right image on Flickr — basically, what everyone sees as he crosses at Charles/MGH on the red line — and I’m having a hard time of it. But just go look at all the photos tagged ‘mgh’ and ‘charles’. How could you not love a city that beautiful?

Boston at sunset; rather orange

« Previous Page | Next Page »