Friday, May 30, 2014

Stupid code fragments, part two

Probabilities are hard. As an example, there's a known puzzle: a family has two children; if one of them is a girl, what is the probability that the other one is also a girl?

The answer, un-intuitively, is not 1/2 but 1/3. There are various explanations but – as with the Monty Python puzzle years ago – I wanted to write code to check it out, so I wrote the following using LinqPad:

  void Main()  
    var rnd = new Random();  
    // Generate a random set of families with two children; true means girl, false means boy  
    var all = Enumerable.Range(1, 10000).Select(_ => new Pair(rnd.Next(2) == 0, rnd.Next(2) == 0)).ToList();  
    // Extract only the families with at least one girl  
    var oneGirl = all.Where(it => it.First || it.Second).ToList();  
    // Out of those families, how many have two girls? The result should be 1/3rd  
    var otherGirl = oneGirl.Where(it => it.First && it.Second).ToList();  
    Console.WriteLine((decimal) otherGirl.Count / oneGirl.Count);  
  // Define other methods and classes here  
  public class Pair  
    public bool First { get; private set; }  
    public bool Second { get; private set; }  
    public Pair(bool first, bool second)  
    First = first;  
    Second = second;  

Conditional probabilities (that is, probabilities where we have additional information – in this case, knowing that one child is a girl) are surprisingly tricky. A way to express the condition more clearly is: what is the probability that a family with two kids has two girls, given that they have at least one girl? Probabilities are, at base, expressions of uncertainty. If we ask "what is the probability that a family with two children has two girls?" we're in a situation of maximum uncertainty: all outcomes (BB, BG, GB and GG) are equally probable so the best we can do is 1/4 – that is the mathematical equivalent of "no clue". However, if we add some information – namely that one of the children is definitely a girl – then we removed some uncertainty from the problem: the BB case is no longer possible. This lowers the uncertainty of the GG case to 1/3.

Stupid code fragments, part one

I just discovered a surprisingly simple (and obvious in hindsight) algorithm for calculating the week index of a given date. For example, April 15th is in the 3rd week (or the 3rd Tuesday of the month).

I was going to do the usual thing and just Google for it but then I realized that the solution is extremely simple:

    private static int GetWeekPosition(DateTime date)
      // the position of the given date is how many times I can subtract 7 days (go back one week) and still be in the same month
      // in other words, it's the integer part of (day / 7)
      return date.Day / 7;

(I am returning a base-zero result, but you can of course add 1 if you need it.)

I realize this is not the answer to the Universe or anything but I thought it's interesting.

Thursday, May 15, 2014


I've long used the expression "real science, so called because it can only be found in books and movies". From a talk at Pycon 2014:
The ideals reality of science:
  • The pursuit of verifiable answers highly cited papers for your c.v.
  • The validation of our results by reproduction convincing referees who did not see your code or data
  • An altruistic, collective enterprise A race to outrun your colleagues in front of the giant bear of grant funding
H.T to Daniel Lemire.