Sunday, 17 June 2012

My first computer program

I've just read Bill Gates's Wikipedia entry and was interested by the fact that he wrote his first computer program at the age of 13 or 14, to play tic-tac-toe (noughts and crosses to us Brits).  This reminded me that I have been meaning for years to write up my first computer programming experience, in 1975 at the age of 15 or 16, when I was a sixth-former at RGS Guildford (then a state grammar school).  The computing facility available to us was an ICL 1900 series machine, which seemed to take up most of the fourth floor of a modern building at the local technical college.  To begin with, our method of entering code was by filling in forms, which staff at the Tech would transfer onto punched cards.  We would submit our lines of FORTRAN IV code, and a few days later receive a printout, usually with compilation (syntax) errors, and eventually, after some corrections, with the output from our program.  Later, we would punch our own cards using entirely mechanical punches, for which we needed to learn the two or three-hole codes for each of the ASCII characters.

As a reasonably bright youngster in the early days of school computing, my idea for a first program was quite ambitious.  I had started to learn the guitar, so I thought it would be cool to do some computer-generated music.  I made a list of guitar chords and drew up a two-dimensional table of conditional probabilities for one chord followed by another.  So for example, a D7 might have a 60% chance of being followed by a G, a 20% chance by a C and a 20% chance by an A7.  For a G, it might have been 30% C, 20% E minor, 20% D7, 20% A minor and 10% B minor.  The program would run in a loop, starting with a particular chord in the set, and generating subsequent chords randomly according to the probability table.  I was fascinated by the idea of changing this table and seeing whether the music that came out would have a different "feel".  Reasonably advanced stuff in those days (or so I like to think)!

This project immediately set me apart as an awkward customer, because I needed a random number generator.  I can't remember how I did that in the end, but I don't think it was a built-in function.  What I do remember, with pride and embarrassment in equal measure, is the results of my efforts.  Pride because, of the ten or so of us, my printout was the only one that came back first time without any compilation errors.  But embarrassment because there didn't seem to be any output at all.  My teacher and I thought this must be a one-off problem, so we re-submitted  the code with no changes (we didn't think of inserting "hello world" print statements or anything like that).  Back it came the following week, same problem.  Nothing!  When the same thing happened the third time I noticed that the printouts did have something on them: a black rectangular blob in the top left corner.  Here is the explanation:  I had chosen to output my chords as single-digit numbers (dealing with characters and strings seemed too hard for a first program).  A chord would be printed each time a print statement was encountered in my loop.  Normally, that would produce a new chord on each line.  But I wanted to save paper and print the chords across the page.  I had been told that the "+" control character would make the next piece of output appear on the same line.  But what I had not appreciated was that there was an implicit carriage return as well.  So the 50 chords I asked for were indeed printed - all on top of each other!  The following week I had my first readable output and I remember playing my first piece of computer-composed music - which has been lost to posterity.

My second project was much more ambitious, and never properly completed.  It just had to be computer chess.  That was a tall order in those days.  I just about managed to write a program to play legal chess, but did not make much progress on getting it to play well!

Tuesday, 13 March 2012

The National Grid, Eastings and Northings

Yesterday I reported some nasty fly-tipping on one of the country lanes on the way to work.  I reported its location using the standard Ordnance Survey system of two letters and six digits, so SU 731189.  I copied it to the Hampshire police, who had to phone me to check the location as they "don't use that system".  "We use Eastings and Northings" said the copper.  But the OS system is Eastings and Northings: the 731 is Eastings in units of 100 metres and the 189 is Northings.  But the police mean something different.  They use two six-digit numbers which do the job of the letters and the numbers of the OS system. Their two numbers are measured in metres from the "false origin" of the national grid.  The true origin is at 2° west and 49° north.  The 2° is important - it's the "central meridian" going down the middle of the country, which makes it a good basis for the map projection they use.  But the trouble with the true origin is that you would have to have negative Eastings for everything west of the line.  So they have a "false origin" 400 km to the west, somewhere beyond the Isles of Scilly, so all the Eastings are positive.

Now, to keep the numbers manageable, they use letters to define 100 km grid squares.  The first letter chooses one of 25 500 km squares in a pattern like this:

A B C D E
F G H J K
L M N O P
Q R.S T U
V W X Y Z

with the false origin at the bottom left corner of the S (where I've put a dot).  They're not all needed - in fact H, N, O, S and T cover the whole of Great Britain.  Then the second letter selects a 100 km square using the same pattern, but within the 500 km square already chosen.

So now we can go from our OS grid reference to our eastings-and-northings numbers-only reference as used by the police.  In my example, the big square S places us at the false origin, and the U then takes us 400 km east and 100 km north.  That 4 and 1 give us the first digits of our eastings and northings.  So our map reference SU 731189 becomes 473100 118900.  The zeros are because the OS reference is only accurate to 100 metres, while the 12-digit one is accurate to one metre.  Actually, it should be a 13-digit reference - the northings really need an exrta digit to allow us to get up to square H which covers Orkney and Shetland.

Now finally here's a handy table matching two-letter grid squares to the first eastings digit and the first (two) northings digit(s).  I haven't found this table on the OS website, but it might be there somewhere.  Most of the other stuff is covered by a nice little presentation that starts here.


Second          First letter
letter    H     N     O     S     T
  A      0,14  0,9   5,9   0,4   5,4
  B      1,14  1,9   6,9   1,4   6,4
  C      2,14  2,9   7,9   2,4   7,4
  D      3,14  3,9   8,9   3,4   8,4
  E      4,14  4,9   9,9   4,4   9,4
  F      0,13  0,8   5,8   0,3   5,3
  G      1,13  1,8   6,8   1,3   6,3
  H      2,13  2,8   7,8   2,3   7,3
  J      3,13  3,8   8,8   3,3   8,3
  K      4,13  4,8   9,8   4,3   9,3
  L      0,12  0,7   5,7   0,2   5,2
  M      1,12  1,7   6,7   1,2   6,2
  N      2,12  2,7   7,7   2,2   7,2
  O      3,12  3,7   8,7   3,2   8,2
  P      4,12  4,7   9,7   4,2   9,2
  Q      0,11  0,6   5,6   0,1   5,1
  R      1,11  1,6   6,6   1,1   6,1
  S      2,11  2,6   7,6   2,1   7,1
  T      3,11  3,6   8,6   3,1   8,1
  U      4,11  4,6   9,6   4,1   9,1
  V      0,10  0,5   5,5   0,0   5,0
  W      1,10  1,5   6,5   1,0   6,0
  X      2,10  2,5   7,5   2,0   7,0
  Y      3,10  3,5   8,5   3,0   8,0
  Z      4,10  4,5   9,5   4,0   9,0

Saturday, 14 January 2012

Friday 13th

One or two people have asked me to explain why the 13th of the month is more likely to fall on a Friday than on any other day.  There's no special reason why it happens to be a Friday, but it arises from two pieces of luck, one of which explains why there is any variation at all between the days of the week, and the other leading to Friday being top of the list.

The first piece of luck is to do with the length of our calendar cycle.  We use the Gregorian calendar, which has a 400-year cycle of year lengths.  Why 400?  Because the rule is that we have a leap year every four years, except for the century years which don't, except every fourth century when we do.  So 2100, 2200 and 2300 will not be leap years, but 2000 was - a double exception which meant that nobody noticed how special it was (apart from the fear of the millennium bug of course, but that's another story).  Now if you work out the number of days in that 400-year cycle, it comes to 400 x 365 + 97 (because of 100 leap years minus the 3 special non-leap years), which is 146,097.  The coincidence is that that huge number of days happens to be a whole number of weeks (20,871 to be precise).  If it weren't a whole number of weeks, then the length of the calendar cycle taking days of the week into account would be 2,800 years (because it would take seven 400-year cycles for everything to come back to start on the same day of the week).  And the 13th (or any other day of the month) would over that 2,800 years occur an equal number of times on every day of the week.

So with the fact that the 400-year cycle is a whole number of weeks, we are left with counting up how many times the 13th of the month falls on each day of the week.  We can know straight away that there will not be an even spread, because there are 4,800 months in the cycle, and 4,800 is not divisible by 7.  When you count it up, this is what you get for each day:

Sunday 687
Monday 685
Tuesday 685
Wednesday 687
Thursday 684
Friday 688
Saturday 684

So Friday wins!  Not by much... but what surprises me a bit is that the variation is more than just one day.  It all comes down to the effect of the three very specific hiccups in the cycle introduced by the century days.

As for the 13th of January, this will be on a Friday, Sunday or Wednesday 58 times each, a Monday or Tuesday 57 times each and a Thursday or Saturday 56 times each.

So now you know.  Not that it will have any effect on anyone of course.  Pure geekery!

Tuesday, 10 January 2012

Glass half full? Glass half empty?

I am generally an optimistic type - some might say a "glass half full" person.  But I'm pessimistic about the survival of those metaphors, particularly if pedantic geeks like me get their teeth into them.  Because of course, "glass half full" could equally allude to pessimism.  It depends on whether the glass is in the process of being filled or emptied at the time.  And it depends, too, on what adverbs you might use with the phrase.  All this leads to a list of possibilities:

Glass being filled
Glass only half full: pessimistic - need to wait
Glass already half full: optimistic - not long now
Glass only half empty: optimistic - it was emptier before
Glass still half empty: pessimistic - it's taking ages

Glass being emptied
Glass only half empty: optimistic - still plenty to enjoy
Glass already half empty: pessimistic - oh dear, well, I suppose I was very thirsty 
Glass only half full: pessimistic - I'm sure there was more left than that!
Glass still half full: optimistic- I'm savouring this.

So all this just goes to show...

Wednesday, 8 June 2011

Losing my place in heaven?

A local church's "wayside pulpit" notice board has the following message:

"What if... you doubt God exists? Don't let doubt lose your place in heaven"

As a Christian I find this message profoundly offensive on several levels.  First, it is a message of fear.  To me it is the ultimate backfiring of Protestant religion.  One aspect of the Protestant Reformation was that it sought to remove a climate of fear based on having to behave in a certain way.  By recognizing that we are sinners, asking God's forgiveness in the sincere belief that Christ died to take the punishment upon himself, we can be redeemed.  In shorthand, "all you have to do is believe, and everything will follow from that".  There is no condemnation for those who believe.  And perhaps part of the Protestant appeal to reason is that if belief gets you to heaven, then lack of it will lose you your place in heaven.  But as a message to passers by?  Surely "doubt and you'll go to hell" is no more enlightened than "sin and you'll go to hell".

The second thing wrong with the message is that confusion about what doubt is.  It is being taken as the opposite of belief.  But doubt is a necessary part of belief!  Without doubt, you would have certainty, which is not the same as belief.  Certainty seems to me to be a tremendously damaging force in religion.  Suicide bombers are driven by certainty about their place in heaven.  But the rest of us, however faithful we may be, do not know.  Even this church's own website says "The truthful answer is that we cannot know for certain [that there is a God]"

The third problem for me is the level of certainty that this poster has not only in the existence of God but also in the existence of heaven, with "places" that could be denied to us like seats in a concert hall (would my place be given to someone else, I wonder?)  We really don't know what heaven is.  My understanding comes from Jesus' Sermon on the Mount, where he gives several pictures of the kingdom of heaven, and to me those pictures are so earth-based that there is no need for pie in the sky.  So ironically I end up having nothing to fear from the fearsome message of this church banner.  But I still find it offensive.  I say "keep those doubts flowing".

Tuesday, 1 March 2011

A Fair Day's Pay

The concept of a fair day's pay based on annual salary should not be difficult, should it?  Of course not!  But what should it be?  Is it 1/365 of annual salary, because there are 365 days in a year?  Or should it be 1/260, which counts only "working days" (Mondays to Fridays) in the calculation?  Or 1/252, because there are also 8 bank holidays?  Or, for a company that offers 25 days' annual leave, should it be 1/227?

Most of the time it doesn't matter.  We work a certain number of days per year and our pay is usually spread over the year in 52 or 12 equal portions.  But it does start to matter when the company starts buying or selling holiday as part of a flexible benefits package, and it also matters for part-time staff.  How much is a day's work worth?

I'm going to ignore any consideration of how much it might cost or benefit the company to offer flexible benefits.  If there needs to be an administration charge, that's fine, but that should be made clear.  What I am concerned about is the raw price that should be attached to a day's work, which should be the basic price that should be charged for buying or selling days of holiday (buying a day's holiday is the same as selling a day's work, and vice versa).  I'm also going to ignore things like pension contributions and tax - I'm just considering the basic salary.

My argument goes against UK employment practice.  But I am right and UK employment practice is wrong!  UK employment practice says that a day's pay is the annual salary divided by 260, which is the number of weekdays in a typical year (actually it's more likely to be 261 and can sometimes be 262, but we can put that to one side for now).  My argument is that a day's pay should be the annual salary divided by the number of days the employee actually works for that salary.  How many days is that?  From 260 weekdays, we subtract 8 bank holidays (royal weddings excepted!) which brings us down to 252 days.  And if the employee has 25 days' paid holiday, the total number of actual working days comes down to 227.

The UK norm is usually justified by saying that because bank holidays and annual leave are both "paid" then those days should count in the calculations.  That is fine when an employee works for the full 227 days.  But suppose an employee would like to buy some more holiday from the company (or take unpaid leave - it's the same thing).  With the UK norm, she would pay 1/260 salary per day, which seems OK until you look at what happens when she buys more and more days.  Suppose the company were very flexible and allowed her to buy enough holiday to take the whole year off.  To do that, she would need to buy 227 days, which would cost 227/260 of her salary.  She would be left with 33/260, or 12.7%, of her salary for doing no work at all!  Why does this happen?  It's because as her number of actual working days decreases, she continues to be entitled to 25 days' (paid) holiday and 8 (paid) bank holidays.  What should happen is that as she works fewer days, her paid holiday entitlement should also go down.  This is what we normally do with part-time staff.  In a company where full-timers get 25 days' holiday, someone who works 3 days a week will normally be given 3/5 x 25 = 15 days, and there will normally be some kind of arrangement to deal with bank holidays as well.  A full-timer buying holiday should be treated in exactly the same way as a part-timer.  If she is not, then the system is unfair to part-timers.

With my approach, a day's holiday should be valued at 1/227 salary.  Our mythical year-long holidaymaker would then pay 227/227, or all of her salary for the privilege, which is as it should be.  And for more realistic amounts of holiday, the employee will be treated exactly the same as a part-timer working the same number of days.

It might be argued that using 1/227 contradicts the principle of paid holiday, since this holiday is not part of the 227 days.  But in fact it's the other way round.  1/227 salary for each working day includes the money paid for the correct proportion of annual holiday, so as the employee buys holiday, part of what she pays goes to buy back the annual holiday she should be losing as her working year gets shorter.

If the employee sells holiday, the same rate of 1/227 should also be used, because by working for more days the employee is effectively earning more paid holiday.  The difference between 1/227 and 1/260 is 12.7% (the same as the proportion of holidays to weekdays) which is quite a serious amount.  Giving an employee 1/260 for extra days of work is equivalent to paying time minus 12.7% for overtime - not a good deal!

Friday, 25 February 2011

Costing a day’s work at 1/260 annual salary is unfair, especially to part-time staff

Some companies allow employees to buy or sell annual holiday at a rate of 1/260 of annual salary per day. This rate is the standard payroll rate in the UK for such calculations. But it is unfair, especially to part-time employees.

Here is an example to explain why. To make the example clear, it is necessary to suppose that the maximum amount of holiday that can be purchased is quite high. But this doesn’t alter the principle, and the calculations could easily be redone with whatever limits a particular company has put in place.

Suppose Adam works full-time on a salary of £50,000. He has 25 days’ holiday a year and there are 8 bank holidays. There are 260 working days in a year, so Adam actually works 260-25-8 = 227 days in the year.

Eve has an identical salary to begin with but then negotiates a modest part-time arrangement of 9-day fortnights, so 90% of full time. The company agrees to pay 90% of her full-time salary, which is £45,000. As is usually done with part-timers, her annual holiday entitlement is also reduced to 90% of the previous level, so she has 22.5 days’ holiday. Suppose for now that Eve’s reduction in working time is small enough for bank holidays to be ignored in the negotiations: she will still get all 8 bank holidays as before. Eve’s part time arrangement therefore means that she will be working (260 x 0.9) – 22.5 – 8 = 203.5 days. This is slightly less (by 0.8 days) than 90% of Adam’s 227 days because we have decided to ignore the bank holidays.

Now suppose Adam and Eve have a relationship and Adam decides that he would like to buy extra holiday to be with Eve on all her days off. As I said, we are allowing him to buy quite a lot of holiday. He needs to buy 227 – 203.5 = 23.5 days. The typical arrangement would allow him to buy this holiday at 1/260 of his salary per day. So he has to pay £50,000 x 23.5 / 260 = £4,519 to get this holiday. So working exactly the same number of days as Eve, he will end up with a salary of £45,481 compared with Eve’s £45,000.  Unfair!  Actually, Eve's deal is fair, it's Adam's that is unfair by comparison.

Why does this happen? It is because the rate of 1/260 is too cheap! We consider our annual holiday to be paid holiday. In other words, our salary is spread over the year including the time we take off as holiday. Paid holiday is not a free gift from the company to all employees – we effectively earn it by working. A half-timer does not get 25 days a year but 12.5 days, or 25 half-days. So when we reduce an employee’s hours in a part-time arrangement, we reduce their working hours, their holiday and their salary pro rata.

By contrast, allowing Adam to buy his holiday at 1/260 of salary per day is effectively allowing him to keep his full 25 days’ original holiday entitlement even when he starts to reduce his working time.

How do we make it fair? We charge for bought holiday according to the number of days Adam actually works for his full-time salary. He works 227 days, so we should perhaps charge him 1/227 of his salary. Then, when he buys his Eve-leave of 23.5 days, he pays £50,000 x 23.5 / 227 = £5,176. This is a bit more than Eve has “paid” because of the bank holidays. So a compromise might be to suppose that bank holidays are a free gift; this works reasonably well even for “stronger” part-timers because some of the bank holidays will probably fall on the part-timer’s days off. So with this compromise we include the 8 bank holidays but not the 25 days’ annual holiday in Adam’s holiday purchase rate. So he should buy holiday at 1/235 of his salary. At that rate, his Eve-leave of 23.5 days will cost him £50,000 x 23.5 / 235 = £5,000, and he ends up exactly the same as Eve.

The nature of bank holidays does complicate matters slightly, but the headline result is that the number of working days used to calculate the daily rate should be reduced by the employee’s annual holiday entitlement. For companies that offer 25 days’ annual holiday, a much fairer rate would therefore be 1/235 rather than 1/260. This is an increase of 10.6%. Not only is it fairer to part-timers by comparison, it means the company will receive more for purchased holiday.

The other side to all this is that 1/260 is an unfairly low rate when employees wish to sell holiday. If Cain earns £50,000, he is receiving £50,000 / 235 = £212.77 on average for every day he goes into work (we are assuming now that bank holidays are full working days). But if he wants to sell holiday, every extra day he comes into work he will only earn £50,000 / 260 = £192.31. Quite a big difference! One way of thinking of this is that if Cain works for more days, by rights he should be given back a little bit of paid holiday for every extra day he works. The exact figure should be 25 / 260 = 0.096. This means that if he works an extra 10 days he should be given approximately one day’s holiday, so in fact he need only work 9 days. So if we really must keep the 1/260 rule, we would need to start giving employees extra annual holiday pro rata to the number of extra days worked.

So, in summary, allowing an employee to buy holiday at 1/260 is unfair on both the company and on part-timers, and giving an employee 1/260 for holiday he sells is unfair on the employee. A fairer figure would be 1/235 or, in general 1 / (260 – usual annual holiday entitlement)

If there is an absolute need to stick to 1/260, then employees purchasing holiday should lose some of their original annual holiday pro rata, and employees selling holiday should receive additional annual holiday pro rata. But that is very messy and difficult to understand; the equivalent measure of using 1/235 is surely the better option.