Presidents and Terms – A Monte Carlo exercise using c# , .net

Consider the 24 presidents (and their corresponding 24 terms in office) in the 19th century. A student was asked to match the president to his term. Not knowing the answers, the student resorted to guessing. He had two possible strategies – one was to keep guessing the same term for every president – he was bound to get ONE right. The other was to randomly guess a term when presented with the name of a president. How many guesses would actually be correct – on average?

An analytical solution

Is difficult. While it is possible to solve this problem analytically, it is not necessary to do so. One can arrive at the answer using another technique  – detailed below.

A Monte Carlo Approach (using C# , .NET)

Imagine that instead of ONE student, there were 10,000 students – and each of them had a chance at playing this guessing game. As each student finished his/her game, we record the number of ‘hits’ – the number of guesses they got correct. We do this for each of the 10,000 students. The underlying hope is that averaging over so many students gets us closer to an accurate ‘number of hits’ for ONE student.  The average of 10,000 students should serve as a ‘close to accurate’ answer.

This is the Monte Carlo approach. We ‘simulate’ multiple trials of the random process – so that the randomness is ‘averaged’ out. The more data points we average over, the more accurate our answer.

Here is some c# code showing the monte carlo simulation.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static int totalTerms = 24;
  11.         static int success = 0;
  12.         static int totalIterations = 10000;
  13.         static Random r = new Random();
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             for(int j = 0;  j< totalIterations; j++)
  18.             {
  19.                 innerLoop(j);
  20.             }
  21.             Console.WriteLine("Total successes in " + totalIterations + " = " + success);
  22.             float probability = success / totalIterations;
  23.             Console.WriteLine("Each student guesses correctly with probability = " + probability);
  24.  
  25.         }
  26.  
  27.         static void innerLoop(int iterNum)
  28.         {
  29.             for(int i=0;i<totalTerms; i++)
  30.             {
  31.                 int randTerm = pickRandTerm();
  32.                 if (randTerm == i)
  33.                 {
  34.                     success++;
  35.                 }
  36.             }
  37.         }
  38.  
  39.         static int pickRandTerm()
  40.         {
  41.             return r.Next(totalTerms);
  42.         }
  43.     }
  44. }

Download full solution (.zip)

Summary

Monte Carlo simulations are a computing approach used to solve tough mathematical problems – problems that typically have no analytical solution. These problems typically have randomness built into them. The simulation approach essentially ‘averages out’ the randomness – providing an answer as close to the exact probability as possible.

 

About the Author

A Microsoft .NET Architect, Anuj Varma has been fortunate to be a .NET architect on some of the largest projects across Texas. These include  Fortune 50 customers building cloud and SOA applications as well as SQL Server migrations. Anuj  continues to work (in the capacity of a .NET architect) with Texas companies – both startups as well as Fortune 500 companies – that have adopted Microsoft technologies.

Specializing in high volume web and cloud application architecture, Anuj Varma’s customer base includes Fortune 100 companies (dell.com, British Petroleum, Schlumberger).
Anuj’s training as a mathematical physicist followed by years of advanced computer programming is unique in the industry.

For Anuj’s popular technology seminars and science and scientific computing seminars, please visit ANUJ.COM

For Anuj’s Mathematical Models and Math Modeling related consulting , please visit anuj.com.

All content on this site is original and owned by AdverSite Web Holdings, Inc. – the parent company of anujvarma.com. No part of it may be reproduced without EXPLICIT consent from the owner of the content.

Anuj Varma – who has written posts on Anuj Varma, Technology Architect.


Leave a Reply

Your email address will not be published. Required fields are marked *