# A morbid Python script

Comics #493 and #893 involve actuarial tables, which are tables for calculating the probability that someone of a given age will die within a given amount of time.

One evening, when I was feeling morbid, I wrote a Python script to calculate death probabilities for any collection of people: actuary.py (.txt). It takes a list of ages and genders and produces various statistics. Here’s the report for the nine living people who have walked on the moon:

```~\$ python actuary.py 81m 82m 80m 81m 80m 81m 76m 78m 77m
There is a 5% chance of someone dying within 0.08 years (by 2012).
There is a 50% chance of someone dying within 1.1 years (by 2013).
There is a 95% chance of someone dying within 4.08 years (by 2016).

There is a 5% chance of everyone dying within 10.78 years (by 2023).
There is a 50% chance of everyone dying within 16.12 years (by 2028).
There is a 95% chance of everyone dying within 22.57 years (by 2035).

Probability of all dying in 1.0 year: <0.001%
Probability of a death within 1.0 year: 46.32%```

And here’s the table for four of the main stars of the original Star Wars (Harrison Ford, Carrie Fisher, Mark Hammill, James Earl Jones):

```~\$ python actuary.py 69m 55f 60m 81m 10
There is a 5%  chance of someone dying within 0.42 years (by 2012).
There is a 50% chance of someone dying within 4.74 years (by 2017).
There is a 95% chance of someone dying within 12.83 years (by 2025).

There is a 5%  chance of everyone dying within 18.17 years (by 2030).
There is a 50% chance of everyone dying within 31.28 years (by 2043).
There is a 95% chance of everyone dying within 42.62 years (by 2055).

Probability of all dying in 10.0 years:   0.272%
Probability of a death within 10.0 years: 85.94%```

Of course, these are based on average death rates based only on age and gender. Adding more specific information about the people in question will refine the calculation. For example, I’d guess former astronauts are more likely to be in good health—and have longer life expectancies—than the rest of us.

## 382 thoughts on “A morbid Python script”

1. It could be a mistake on my side, but doesn’t 100km² x 6cm equals 0.006 km³ and not 0.6km³ ? Which would be ‘only’ 6 mega tons? Well… still ‘a hell of a drop’…

Like

2. I hate you right now, unless…. you show the entire comic from today non of this click and drag stuff, I’ve been through A LOT of it, but I feel like there’s just way more that I’m missing, please help me fill in those spots! (or I’ll never forgive you, but I’ll still read your comic, because that non-negotiable!)
I’m emailing you too just in case!

Like

3. I can’t reach the forums at the moment. So let me use the comment function to say: I really like this one a lot.

For those who didnt bother to figure it out themselves:
1. There are at least 83 Tiles and each has a resolution of 2048×2048
I think there may be a lot more tiles.
2. If a tile is missing, the javascript code inserts black (for tiles below ground level) or white (for tiles above ground level) space.
3. There is a balloon right above the starting location (5-6 tiles up).
4. There is a system of caves 14 tiles below ground level at starting location.
5. There are some very pretty boats a few tiles to the west.

Randall, would you allow me to use an automated grabber to grab the whole picture?

Like

4. Click and drag is just fantastic. It so much more than a comic. I’m just stunned and speechless how something so “simple” can create such a powerful effect and impact. I guess that is the definition of art.
Thank you. Really.

Like

5. Love the latest comic ‘click & drag’. It reminds me of Terraria, which is a sort of 2D side scrolling Minecraft. I can’t wait to get it open on my desktop with a bigger display and a mouse instead of this tiny netbook trackpad, and do some exploring. Thanks as always for sharing your musings with us :)

Like

randall, you are a mad genius and i should make a bow if anytime see you in the streets =)

please please please make a poster of click and drag, or at least give us the tiles, i promise to print all of them and hang on my wall (trees of the world, forgiveme in advance)

there is a term for “fear of if didn’t obsessivelycheck all the spaces i might end up missing something cool” ? i think this comic needs that term =)

cheers!

Like

7. Oh my goodness! 1110 is your masterpiece, maybe. Gulp. I think I have wandered through most of it, veRy carefuLLy backing up through the paths underground, but who knows without piecing it aLL together. I did see the giant jellyfish floating in mid-air, and I made it to the submarine at what I think is the lowest depth. I love the triple Lemonade? I told my wife that I would have added a fourth lemonade ahead of the group of three. She was azamed and amazed like me. It was a little more tedious on my iPad versus the PC. Carpel tunneling ….

Like

8. Hey! I don’t write comments usually, but this one time I just had to drop by and tell you what an unbelievable, stunning, wonderfull piece of art you made there!
Keep up the great work, it’s highly apreciated :).

Greetings
Joerg

Like

9. THE WALLS WORN SMOOTH BY BILLIONS OF TUMBLING MARIO CORPSES!

And my broken mouse, a quaint friend who after a good 30 minutes of clicking and dragging expired. Alas to Walmart I go for an affordable poor quality mouse. :(

Thank you. <3

Like

10. “Carpel Diem!”

And so but no, seriously, a truly wonderful adventure to savor and repeat, although the poster – please please please say there’s going to be a poster – would be way cool as well. As good as anything Kara Walker does, and she is amazing. Seriously, this should be hung in its entirety in MOMA.

Like

11. Please, please, please, please make #1100 available to view in full and as a poster!! I’ve looked at quite a lot of it, but I KNOW I’ll have missed something. Please??

Like

12. Why are 1111 combinations all black or all white? There will be comic 1111, right?

Like

13. Oh, I’d like #1100 as a wallpaper, perhaps with even more details! I doubt I’d get my girlfriend to approve, though…

Like

14. I am amazingly peeping your world since a couple of years. I used to thought you ROCK. But dude, you SHOCK. Seriously man.

PS: English is not my native language :)

Like

15. Ok, right now I hate myself. I had been writing a comment for about 20 minutes and then I accidentally refreshed the page. Great! Ok, I’ll just start again:

Hi Randall. I’ve been reading your ‘comic’ (I really don’t know how to call it…) for about a year now. I remember the day I discovered XKCD. I was looking for ‘existentialist online/flash games’ on the Internet (that’s what I do sometimes when I feel weird and have plenty of time; there are some great games out there) when I came across a blog that showed ‘A Bunch of Rocks’. That’s the day I became addicted to the things you write, draw and think.
I think I’ve read all of the ‘comics’ in your webpage. And I’ve really liked them. Well, the thing is that this last ‘Click and Drag’ post has left me amazed. And in my amazement I decided to come here, as others have, and tell you. And thank you for sharing your work. I don’t know why I haven’t done it before, though. Anyway, one of the daily visits you get from Western Europe is mine.
Yesterday I spent about 45 minutes wandering in ‘Click and Drag’. I don’t know if I have seen a big or a small part of it, I just know I’ve seen some of the parts.
I was thinking last night about how much time you might have spent in this comic.

But I’ve realised of one thing. Right now I see you as God. Ok, this sounded strange. Let me explain. You’ve created one huge thing. Like a world. Ok. And for some reason, all of us want to understand it, and to watch all its contents. We can’t help ourselves and we desperately click and drag looking for something that we don’t find. Because, what are we looking for in Click and Drag? No one promised us anything. But we keep on searching. And then some of us realise that it was created by someone, so we go and ask for his help. Some beg for the ‘full version’, some others try to rationalize it, or even automatize it. Some insult. Some admire. But you’ve got control over them, kind of. You can decide because only you know WHAT IS exactly in Click and Drag. This makes me laugh a little. So you’ve become God. You have created a world and have now a bunch of humans of all kinds. Strange…
Perhaps Click and Drag is life. Perhaps you’ve invested so much time on it that it’d take years to watch it completely. And all we want to do is to ‘complete’ it as soon as possible and we aren’t paying attention to the beautiful things it has. That’s how some of us, humans, see life. I don’t know.

Anyway. Thank you. Yes, you make really great ‘comics’ or whatever you want to call them. But you make people think. At least you made me think. It’s not only the comic but the consequences that made me think. This is really interesting, I don’t know if you had planned it or something… maybe you did.

To finish, I’ll say that I put this God thing, but it has nothing to do with my beliefs, of which I’m not too sure. I think that Christians, Atheists, Buddhists, and people of all religions and beliefs can understand what I meant. I wasn’t making an apologia of any religion or anything, it’s just the first example that came to my mind. If any reader doesn’t like it, he/she can mentally replace it with a different example that is more suitable, from his or her point of view (what I mean is, let’s not argue on this, please).

Peace to you all. And thank you, Randall. Thank you and congratulations.

Like

16. I just read some of the other (older) comments here. The #404 is genius. I hadn’t seen it (when you’re in #403 and hit ‘Next’, it jumps to #405).

Like

17. To everyone that is interested in these pictures as backgrounds: Open up http://xkcd.com/1110/'s html code. (Most browsers use F12)

For the quick and dirty way, just open up the search box in the code menu that was pulled up, and copy this in it:

//*[@id=”comic”]/img

it should highlight something like (or exactly) this:

If so, open the tab DIRECTLY BELOW the highlighted text and there will be four pictures. Some of them might be completely black or white (that is the ground and sky chunks respectfully), but there are others that are full of the interesting bits we want.

Just open them in a new tab and/or save them. The default resolution for them is 2048×2048. My monitor is personally 1280×1024. While I did not have photoshop or gimp on it, I decided to make due with paint. It took me only 5-10 min. to make these:

http://www.mediafire.com/?nte5whrwmd0e99e,6naq2sx7xo67ii5

Just crop/cut out/modify the picture to suit your needs

Hope I helped some people :)

Like

18. I read the wikipedia article about the North Dakota tower, and examined the guy wires of your drawing. I was proud of your realism to put sag in the wires – I checked the slope of the line gradually decreasing as it goes towards the ground level. Yea!

Like

19. I too, wonder where he’ll go next. I still havn’t found the submarine or the other boats so I’ll be back when my wrist heals up.
Nice work, bud….’bout yer best so far.

Like

20. Excellent calculations.
Can you please provide a dailily (yes, dailily) updated calculation of the probability of my entire family dying within 1 year? Round up, please.

Like

21. Mr. Randall – the “Click & Drag” comic is truly wonderful.
I hope that after a while you’ll post the whole picture :)
Thank you very much.

Like

22. Hi,
I discovered your comic a couple of years ago and have been a big admirerer ever since. Just want to say I really like your work and the insight you show into all the math and computer science I am currently trying to master, this comic is both amusing and relaxing and on top of that makes me feel kind of good when I recognice nerdy stuff I’ve been learning.
Thank you.

Like

23. besides thanking you for “click and drag” (also Phoenix above for a zooming), I just would like to point out that the what-if-laser-pointed to the moon question was actually attempted a few times, wit radar first, most notably by Mr. Bay (not Michael but Zoltan) So it is not a purely speculative issue.
http://en.wikipedia.org/wiki/Zolt%C3%A1n_Lajos_Bay

Like

24. So.. Funny moment.. My wife says, “So this is what happens if you’re bitten by a python?”…

Like

25. OvernightAir Cargo to Panama. This service usually goes by weight and is used for documents, or any time sensitive materials you want in Panama overnight.

Like

26. This service usually goes by weight and is used for documents

Like

27. you were able to predict lance armstrongs death.

woah mind blown..

its because you made this script predicting a death, that a death had to follow based on this script.

Schrödinger’s idea put into play.

Like

28. You could also think the astronauts have been exposed to more radiation and super g-forces that messed up their bodies, lowering the life expectancy. And I don’t know how fit they stay after they retire. And so on. I guess that script works best with only taking into account the statistical life expectancies. Anyways, nice code and math!

And RIP Neil (Not Lance yet)

Like

29. So it’s not a Monty Python script then. OK. That reads differently in the UK.

Like

30. I did something similar a while back. The input to this script is simply your age. It tells you how many dice you should roll and how many coins you should flip in order to approximate the probability of your death this year.

note: probabilities are based on a curve fit to actuarial data.

#!/usr/bin/python
import math
import sys

def P(age):
return 1-math.exp(-0.003*math.exp((age-25.0)/10.0))

def dice_and_coins(p):
def pdc(d,c):
return 1/(math.pow(6,d)*math.pow(2,c))
# find how many dice is too many
D = int(math.ceil( -math.log(p) / math.log(6) )) + 1
# find how many coins is too many
C = int(math.ceil( -math.log(p) / math.log(2) )) + 1
# search the space from D to C to see which value is closest to P
closest_d = -1;
closest_c = -1;
closest_p = -10000;
for d in range(D):
for c in range(C):
this_p = pdc(d,c)
print “testing %d dice, %d coins” % (d,c)
if abs(this_p – p) < abs(closest_p – p):
closest_d = d
closest_c = c
closest_p = this_p
return (closest_d,closest_c,closest_p,p)
a = dice_and_coins( P( int(sys.argv[1]) ) )
print "%d dice and %d coins" % (a[0],a[1])
print "dice and coins probability %f, actual probability %f" % (a[2],a[3])

Like

31. I ran the script for my shared house. Nobody was happy when I presented them with these figures. I think it brought the mood down:

python actuarial.py 22m 27m 30m 33m 22m

There is a 5% chance of someone dying within 6.73 years (by 2019).
There is a 50% chance of someone dying within 32.4 years (by 2045).
There is a 95% chance of someone dying within 49.74 years (by 2062).

There is a 5% chance of everyone dying within 54.11 years (by 2066).
There is a 50% chance of everyone dying within 64.97 years (by 2077).
There is a 95% chance of everyone dying within 74.51 years (by 2087).

Probability of all dying in 1.0 year: <0.001%
Probability of a death within 1.0 year: 0.726%

Like

32. steven@linux-csh5:~/Documents> ./actuary.py 77m 76m 64m 79f 74m 58m 62m 58f 52f
There is a 5% chance of someone dying within 0.21 years (by 2013).
There is a 50% chance of someone dying within 2.69 years (by 2015).
There is a 95% chance of someone dying within 8.53 years (by 2021).

There is a 5% chance of everyone dying within 25.65 years (by 2038).
There is a 50% chance of everyone dying within 35.72 years (by 2048).
There is a 95% chance of everyone dying within 45.79 years (by 2058).

Probability of all dying in 1.0 year: <0.001%
Probability of a death within 1.0 year: 21.01%

I rounded ages to the nearest year since I didn't want to do the conversion to decimals. Close enough for my amusement.

Like

33. I wouldn’t say that this is a calculation of Star Wars cast or 9 people who have walked on the moon mortality probabilities since the rates you obtained from the SSA are based on the total US population in 2012, with only sex as the aggregating variable (not even race/ethnicity, education, income level, etc.) whereas we know that numerous studies have found vast differences in mortality rates by those variables. So technically what you’ve presented here are period probabilities for *any* group of 9 or 4 people in the US in 2012, with ages matching the 9 people who have walked on the moon, or the 4 stars from Star Wars.

Like

34. hyHey! I don’t write comments usually, but this one time I just had to drop by and tell you what an unbelievable, stunning, wonderfull piece of art you made there!
Keep up the great work, it’s highly apreciatedfh

Like

35. Darn. I came here looking for a script that calculates the future incidence of walking pneumonia among original Star Trek dolly grips. When I saw “morbid Python script” and Star Trek in the same post, I was sure I was in luck. Imagine my disappointment when this script calculated mortality rather than morbidity. And it apparently doesn’t account for the dolly grips at all. Back to the drawing board I guess.

Like