Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
How many times ... has this loco headed west through Tenby? - Python exceptions

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]

(written 2012-11-05, updated 2012-11-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y109 - Python - Exceptions
  [4444] Elements of an exception in Python - try, except, else, finally - (2015-02-28)
  [4161] Python varables - checking existance, and call by name or by value? - (2013-08-27)
  [4029] Exception, Lambda, Generator, Slice, Dict - examples in one Python program - (2013-03-04)
  [3930] Reporting the full stack trace when you catch a Python exception - (2012-11-22)
  [3664] Error checking in a Python program - making your program robust via exceptions - (2012-03-22)
  [3441] Pressing ^C in a Python program. Also Progress Bar. - (2011-09-15)
  [3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
  [3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
  [2998] Using an exception to initialise a static variable in a Python function / method - (2010-10-13)
  [2994] Python - some common questions answered in code examples - (2010-10-10)
  [2622] Handling unusual and error conditions - exceptions - (2010-02-03)
  [2408] Robust user input (exception handling) example in Python - (2009-09-17)
  [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
  [2281] Python - using exceptions to set a fallback - (2009-07-12)
  [2018] UnboundLocalError - Python Message - (2009-01-31)
  [1236] Trying things in Python - (2007-06-18)
  [1042] Nested exceptions in Python - (2007-01-18)
  [381] Exceptions in Python - (2005-07-17)


Back to
Sand to Arabia, Coals to Newcastle or Woodburners to Russia
Previous and next
or
Horse's mouth home
Forward to
While, for, foreach or something else to loop.
Some other Articles
BODMAS - the order a computer evaluates arithmetic expressions
PHP variables - dynamically typed. What does that mean?
How does PHP work?
While, for, foreach or something else to loop.
How many times ... has this loco headed west through Tenby? - Python exceptions
Sand to Arabia, Coals to Newcastle or Woodburners to Russia
How well do you know Perl and / or Python?
Identifying your real customers and keeping them well informed fast
Every Customer Counts
Black dogs at Halloween
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3913_How ... tions.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat