Archive for the 'Tangential' Category

Much as I love Ruby

And much as we bend Rails to our will, I am getting a bit jealous of these guys developing web apps with Scala - an elegant hybrid functional/object-oriented language with a powerful type-inferencing type system, Erlang-style Actors and other goodies. It compiles and runs fast on the JVM too and can access Java libraries in quite a native way. It’s kinda like Ruby plus OCaml plus Java minus the suck of Java.

I think it’s because the inner maths and type theory geek in me (the one who can never quite get over how awesome http://en.wikipedia.org/wiki/Curry-Howard_isomorphism is) really misses having a powerful type system - and Scala’s does seem to hit the sweet spot when it comes to a middle ground between the bafflingly powerful Hindley-Milner extensions of Haskell and OCaml, and more accessible Object-oriented type systems with subtyping.

liftweb (or ‘Scala with Sails’ - see what they did there?) seems like a pretty neat framework too. I’m just plugging it so that someone else will do (continue doing) the work of making it sufficiently ‘enterprise-ready’ for me to use in ‘the real world’. ;-)

Bus waiting times

Lazy bastard and travelcard-holder that I am, I regularly hover around the bus stop for a while waiting to see if a 55 or a 243 will arrive and take me some of the ~10min walk from tube station to Playlouder MSP (see, relevance to work!).

Having a maths degree which I struggle to put to use, I find myself wondering about the following problem: How long do you wait for a bus before you start walking? What is the optimal strategy for this?

To simplify things you might presume, along with numerous textbook probability questions, that buses follow an exponential distribution. In english: that buses are randomly scattered subject to an average number per time period. The answer is then quite clear-cut. There are only two valid strategies - a strategy in which you always wait indefinitely until a bus arrives, and a strategy where you always walk no matter what. If the average waiting time, plus the bus journey time, is greater than the walking time, then you always walk; otherwise you always wait.

This is a bit counter-intuitive, though, and doesn’t satisfy my desire for a practical strategy. In practise buses don’t follow an exponential distribution, the waiting times are correlated as buses are subject to bunching phenomena, service disruptions etc. So if you wait 3 hours and no bus arrives, you might well be able to infer extra information about subsequent waiting times based on more sophisticated distributional assumptions (perhaps a bus route suspension is a probable occurrence? perhaps you’re likely to have just missed a bunched grouping of buses?). And so with a real-life bus distribution, there is actually likely to be a valid strategy based on “Wait for n minutes; if no bus arrives then start walking”.

It turns out that there’s a lot theory on this, and the M/G/1 queue is a better model to use than the more simple M/M/1 queue based on an exponential waiting times. M/G/1 tells us that, if bus waiting times are correlated, you may actually expect to wait longer than the mean interval between buses! This is due to phenomena like bunching (’why do buses always come in threes?’), and the extra expected time can be given in quite a general way, in terms of the mean and variance of the distribution of inter-arrival times.

However the M/G/1 queuing model is a little too general to infer more detail, such as specific strategies on how long to wait before walking. To do this I’d need to assume a particular distribution for bus arrival times, and it seems at this point that most of the research turns to using empirical distributions (ie just going out and measuring loads of buses rather than trying to derive from a mathematical model), and simulations (simulate a bunch of buses on a computer and measure the arrival time distribution in different situations).

And so I lack a simple answer: given sensible real-world assumptions about bus waiting times, how do I calculate a value for my “Wait n minutes and then walk” strategy?! Queuing theorists please respond kthx.

On with some MSP work ;)