search
top

In which Ben learns about code optimization the hard way….

I’m currently involved in a project for a paper I’m writing where I’m trying to find the auto-covariance function of a queue, which involves evaluating modified Bessel functions of the first kind; in essence these are the solutions to a particular form of differential equations which tell you how queues work, and each evaluation takes a relatively large amount of time: they are the limiting step in getting towards the answer I’m after in my code. I’m very close to generating the results I think I need to finish the paper, and was getting very frustrated by how slowly the code ran.

For my application, the Bessel function, implemented in matlab and (with a really weird bug initially) in Octave, as besseli is ideal. Essentially, I need to evaluate this over and over again; the Bessel function has two parameters, an index, which in my case is an integer, and another parameter, in my case a positive real number, which is fixed for each function evaluation.

The thing I learnt last night is the extreme time saving in doing things in a sensible order- I’ve had quite a lot of coding training, but to be honest, never really come across situations where using it is needed. So essentially my code was doing this for each z,


out=myOldFunction(z,n,N)
for m=(1:N)
out+=m*(besseli(m-n,z)-besseli(m+n,z) +...% (more functions of besseli)
end

This function has many calls to besseli, each of which is an expensive step. Sometimes we even evaluate the same function twice. By replacing this function with a vectorized call to besseli, we should reduce the overall calls to besseli.


out=myNewFunction(z,n,N)
b=besseli((0:(n+N+50)),z); % <-Vectorized version of besseli
for m=(1:N)
out+=m*(b(abs(m-n)+1)-b(abs(m+n)+1) +...%(more functions of b)

And it’s as simple as that; it helps in this case that the function is symmetric (and besseli(n,z)-besseli(-n,z). The results are that the code runs about four times faster. For those of you that programme more regularly, and as it should have been for me, this is quite obvious. I write this because

  1. With a little bit of thought, I could have saved myself many hours of computing time, and have finished this paper about a month ago, probably.
  2. It might help someone else out.
  3. As a reminder to myself to think carefully about things like this before any serious coding happens.

More bad use of statistics

I’ve been listening to the news today, and every news bulletin has a report, which cites an AA/Populus poll, that motorists are using less petrol; Sky news has a fairly common story here. Now, I’ve been unable to find the original source of the article, but here’s the interpretation.

Basically, the AA said that 16.3 billion litres were used in a period in 2008; 14.9 billion litres were used in the same period in 2009 ( a drop of 8.6%), and 13.9 billion used in 2010. The theme of the article is that the reason people are using less fuel is because it’s so expensive: another doom and gloom story, economic depression forces poor people off the road, the roads are once again the domain of the gentrified rich, and isn’t this terrible?

But official statistics from the department of energy and climate change  show that the average person drove 3332 miles in 2009, as opposed to 3485 miles in 2008; a drop of 4.4%; there is no data yet for 2010. There was a small rise in the number of passenger miles in a car.

So petrol sales have fell by 8.6%; approximately half of this fall seems to be due to falling demand for driving, but the other half seems to be due to people getting more energy efficient: presumably cars are more efficient, there are more electric cars and cars which do not use petrol, and almost every newer car seems to be doing more miles per gallon.This is backed up by the DECC data that driving had an efficiency of 37.2 (thousand tonnes of oil equivelent per billion km) in 2008 as opposed to 38.0 in 2009; a drop of 2.2%.

Even more than this, perhaps we are commuting less because more people are taking advantage of technology, and working from home? There seems to be little data available post 2005 (when the office for National Statistics stopped compiling this), but working at locations other than an office seemed to be on the rise until 2005, and it would be reasonable to extrapolate this upwards as technology has continued to improve until now.

Overall, it seems people are travelling less far to go to work; we are burning less fuel when we do go commute due to more efficient technology.  Should we not therefore rejoice in our green credentials, and perhaps acknowledge that there is a good side to this story as well as a bad?

Moreover, should we not ask that the press do a tiny bit of research to put statistics in context? Making conclusions that are not supported by the data, or at least not exploring more than one likely cause, seems irresponsible and lazy to me.

What I saw in Edinburgh 2011…

A bit of a late post, but I recently came across my collection of tickets for this year’s Edinburgh fringe, the world’s largest arts festivel. Now, due to not being well and one thing or the other, I didn’t see nearly as much as I would like to; we must fix that next year.
I’ve also forgotten what else I’ve seen; if anybody who was there can remind me, I was probably drunk at the time!

In no particular order with marks out of 5*.:

The Axis of Awesome These guys are cool; seriously, very cool, they deconstruct modern music in a very funny way. However, most of their material, even their fake ad libs, was precisely the same as I saw last year down to the geeky pauses. I did enjoy hearing the songs, but to be honest, I got nothing new from seeing them again. If you haven’t heard the four chord song, this is worth five minutes of your time. http://youtu.be/5pidokakU4I.  ***

Thirty Two Teeth, Jam Jar Productions. I took a risk on this as a last minute freebie. Thirty Two Teeth was a script based on the, admittedly novel, idea that by capturing the tooth fairy, you could do whatever you wanted, as long as you gave her enough teeth. It is, as far as I know, the only play to touch on dentistry, and fairy bondage, using the conveniently located chain on stage. The acting was very school-play intense, and I think the actors were thoroughly bored with what was a good idea for a ten minute piece of writing, but did not captivate me for more than about 15, leaving the other 45 to wonder whether I would have preferred a trip to the dentist. *

A tribute to the Blues Brothers- Live, Hartshorn and Hook. I have seen this company do some amazing things; unfortunately this wasn’t one of them. The band were phenomenal, particularly the hot trumpeter, but, oh dear, the singing was karaoke tastic. I think if you were in the mood (i.e. a little tipsy) then this would have been a great 11pm show; unfortunately I wasn’t, and it was actually annoying to have one third of the audience dancing in the aisles while the other two thirds looked slightly uncomfortable. The frequent trips of the choorus through the audience made me feel it was a bit like Blues Brothers: The Panto, which is an awesome idea, but not what was sold. **1/2

Armageddapocalypse: The Explosioning, Exploding First Productions. I enojoyed this show; there were some great puns, one liners, and performances, with some great acting from the overworked cast. The shoestring production nature made a lot of it funnier; the script could have done with a bit more of a polish, the best lines were very good, the rest unremarkable, but overall enjoyable and fresh. ****

Sunday in the Park With George, One Academy Productions. Wow. I have been to the fringe 10 times, and I think this is the best thing I have ever seen. Admittedly, I do like Sonddheim, so I had high hopes, but the singing was beautiful, the musicians perfect, staging immaculate, artistic design exquisite, and what’s more it wasn’t jsut a collection of songs; these kids can act. For no reason particularly, I cried through most of the second half, and loved every minute of it.. *****

Showstoppers, The Improvised Musical . I normally hate impro shows; I mean I love them, but I’d rather do them than watch, because once you’e done a bit, it becomes technical appreciation rather than usually genuinely funny. But once again, these guys seem to have all the impro skills, the musical skills, and are also genuinely funny and talented, the last being what a lot of impro misses. The format is superb as well: having a director able to intervene, give fourth-wall breaking penalties, and move the story along helps in those times where, inevitably, things go down the wrong road. I regret only seeing this once, but it could never be the same again. ****

Where has all the money gone and why do we care?

So here’s my question- and please be gentle as I know very little about economics- is where has all the money gone, and why do we care?

Let me explain; most people I know owe a little money, either on credit cards, or mortgages, or student loans, or whatever. But all the people who they owe this money to (the banks, the government) owe a lot of money. We keep being told how much money everyone owes. The country owes a lot of money; presumably to banks, who also owe a lot of money. The Americans owe a ridiculous amount of money. Greece owes more money than it can perhaps pay back to all the other people that owe money.

So where is all this money? Is it sitting in some vault in china somewhere? Or is there some kind of hilarious mistake where somebody’s missed a few zeros off in some computer code somewhere. And if nobody has any money (Except the chinese), why does it matter? Why do we not all get together and say, right, let’s start again, everybody has zero debt, and china can go worry about itself. SUre, it would cause some problems, but we could stop talking about how much everyone owes all the time.

Mac or PC?

What ho,
As part of my new job I have to get a new computer. The two options are either a PC or a mac. Never having had Mac hardware before, I’m tempted to go for the Mac; I intend to run Linux on it mostly. So, Internet people, is there any reason not to get an apple? The specifications look better for the Mac!

Or am I being seduced by the shinyness….

Mac

Model iMac 21.5-inch
Processor 2.5GHz Intel Quad- Core i5 Processor
Memory (RAM) 4GB (1333MHz DDR3)
Hard disk 500GB (7200RPM)
Model Viglen desktop (Intel DQ67SW Motherboard)
Processor Intel Core i5 Processor i5-2400 3.10GHz
Memory (RAM) 4GB 1333MHz PC3-10600 DDR3 Memory
Hard disk 320GB Serial ATA2 Hard Drive
Optical drive Serial ATA Multi-format DVD Writer/CD-RW Drive
Monitor 19″ Multimedia TFT monitor, height adjustable
Page 1 of 4412345»...Last »
top