Main Content

How many times ... has this loco headed west through Tenby? - Python exceptions

Archive - Originally posted on "The Horse's Mouth" - 2012-11-05 07:14:43 - Graham Ellis

If you have a stock of 117 locomotives on your railway, and each is on the front of a train through Tenby as it heads for Pembroke just 12 times a year (it's a Summer Saturday Special working!), will - in the course of their 30 year life - every locomotive have worked that train? Will the reason that no-one has a photo of a certain loco there be because no-one's ever taken the photo (or you haven't found it), or because the loco has never been there?

A very similar question / mathermatics applies to other events which apply randomly to and one of a limited number of members of a resource group. It almost (but not quite) applies to lottery draws - in fact it does apply to the FIRST number drawn each time and the frequency of that, but subsequent draws are skewed as the later balls are drawn from the diministed pool.

The answer - with my numbers - is that there's only about a 1 in 200 (0.5%) chance that every locomotive has headed a train west through Tenby ... it's just the lack of photographic or other evidence to show it that is worrying our enquirer. A more typical set of results:
  [5, 13, 28, 32, 16, 12, 8, 2, 0, 1]
• 5 locomotives haven't been
• 13 have been just once
• 28 twice
• 32 thrice
• and so on ...
• and one has made it no less than 9 times!

I've run a simulation (rather than a theoretic analysis) - in Python, program's [here], running it 32 times to show the spread of the results. It's rather similar to the approach that's known as the "Monte Carlo method", where a series of simulations are run, based on different random sequences, and the results are then collated and the commonality is used to indicate what will probably happen in real life, with the spread showing what alternatives are likely. It's a very interesting approach in weather forecasting, where the randomness is noise and fluctuation on measurements, and the method can show not only the spread of possibilty to the forecasters, but it will also show those days where there's some sort of pivotal split in the weather, and tiny inaccuracies in readings can lead the weather to head off in totally different directions.

As I wrote my example, I had no idea what the maximum number of times a locomotive headed the train would be. I COULD have started off with a counter array filled with zeros. However, that would have needed to be 117 elements long and (statistically) would "never" have been used. OK for my 117 samples, but what if I had a million? So I used a different approach.

Statring off with a list of zero length, I "assumed" that I could just add one to the counter I wanted. Now that will throw an IndexError if the counter element doesn't yet exist, and if that happens I keep adding a zero onto the end of my list until I have enough elements there to handle the so-far most-used unit of my 117. Here's the code:

  counter = []
    for eventcount in units:
      while True:
        try:
         counter[eventcount] += 1
         break
        except:
         counter += [0]


and - run multiple times - some of the results:
  [6, 17, 22, 30, 18, 13, 6, 4, 1]
  [7, 14, 27, 27, 20, 9, 6, 6, 1]
  [6, 19, 26, 20, 17, 20, 5, 3, 0, 0, 1]
  [5, 18, 23, 30, 20, 7, 10, 3, 0, 0, 1]
  [5, 17, 29, 21, 18, 18, 4, 4, 1]
  [5, 16, 24, 34, 18, 7, 8, 2, 2, 1]