A Java Monte Carlo simulation for my “Minority Report” problem

This Java Monte Carlo simulation tutorial, and the corresponding Java program, was inspired by the story and subsequent movie "Minority Report", as well as my recent interest in Monte Carlo simulations.

My Monte Carlo simulation - The problem statement

Imagine that you have three people that are each "right" 80% of the time. My question to several statistician friends was "If two of the people have the same answer, what are the odds that they are correct?" They both came back and said the same thing: "80%". Since I didn't like or agree with that answer, lol, I created a Java Monte Carlo simulation of this problem.

What I'm doing in my Java Monte Carlo simulation program is assuming that there are 'N' questions in a given "test". For the purpose of simplicity I'm assuming that the "correct" answer is always 'a'. I then randomly populate the "answers" of three simulated people, but make sure that very close to 80% of each person's answers are 'a'. Once I have "N" answers for each user (in this case "N" is 10,000), I compare the answers.

Java Monte Carlo simulation of the problem

So, without any further discussion, here is my Java-based Monte Carlo simulation program for my "Minority Report" problem:

package com.devdaily.montecarlo;

import java.util.*;

/**
 * This is a little crazy, but imagine that you have three people that are each
 * "right" 80% of the time. My question to a statistician was "If two of the
 * people have the same answer, what are the odds that they are correct?
 *
 * Since I didn't get an answer I liked, I created this Monte Carlo simulation.
 * What I'm doing here is assuming that there are 'N' questions, and that the
 * "correct" answer is always 'a'.
 */
public class RandomTests
{
  final static int N = 100000;
  char person1[] = new char[N];
  char person2[] = new char[N];
  char person3[] = new char[N];

  public static void main(String[] args)
  {
    new RandomTests();
  }

  public RandomTests() {
    populateFirstPerson();
    populateSecondPerson();
    populateThirdPerson();

    p1AndP2BothHaveAForAnswer();
    p1AndP2HaveSameAnswer();
    p1p2AndP3HaveSameAnswer();
  }

  private void populateThirdPerson() {
    //person3
    Random random = new Random();
    for ( int i=0; i<N; i++ )
    {
      int randomInt = random.nextInt(100);
      if (randomInt<41 || randomInt>60) person3[i] = 'a'; else person3[i] = 'b';
    }
  }

  private void populateSecondPerson() {
    //person2
    Random random = new Random();
    for ( int i=0; i<N; i++ )
    {
      int randomInt = random.nextInt(100);
      if (randomInt>19) person2[i] = 'a'; else person2[i] = 'b';
    }
  }

  private void populateFirstPerson() {
    //person1
    Random random = new Random();
    for ( int i=0; i<N; i++ )
    {
      int randomInt = random.nextInt(100);
      if (randomInt<80) person1[i] = 'a'; else person1[i] = 'b';
    }
  }

  private void p1AndP2BothHaveAForAnswer() {
    //--------------------------------------------------------------------------
    // look at the situation where person1 and person2 both have 'a' for their
    // answer
    //--------------------------------------------------------------------------
    int numOfA = 0;
    for ( int i=0; i<N; i++ )
    {
      char c1 = person1[i];
      char c2 = person2[i];
      char c3 = person3[i];
      if (c1=='a' && c2=='a') numOfA++;
    }
    System.out.println("# of cases where person1 and person2 have 'a' for an answer: " + numOfA + " out of " + N);
  }

  //--------------------------------------------------------------------------
  // look at the situation where person1 and person2 have the same answer.
  // out of these, what percentage is correct? (i.e., what % of these are 'a'?
  //--------------------------------------------------------------------------
  private void p1AndP2HaveSameAnswer() {
    int numSame = 0;
    int numCorrect = 0;
    for ( int i=0; i<N; i++ )
    {
      char c1 = person1[i];
      char c2 = person2[i];
      if (c1==c2) {
        numSame++;
        if (c1=='a') numCorrect++;
      }
    }
    float percentCorrect = (float)numCorrect/(float)numSame * 100.0f;
    System.out.println("\nP1 = P2");
    System.out.println("numSame: " + numSame);
    System.out.println("numCorrect: " + numCorrect);
    System.out.println("% correct:  " + percentCorrect);
  }

  //--------------------------------------------------------------------------
  // look at the situation where p1, p2, and p3 have the same answer.
  // out of these, what percentage is correct? (i.e., what % of these are 'a'?
  //--------------------------------------------------------------------------
  private void p1p2AndP3HaveSameAnswer() {
    int numSame = 0;
    int numCorrect = 0;
    for ( int i=0; i<N; i++ )
    {
      char c1 = person1[i];
      char c2 = person2[i];
      char c3 = person3[i];
      if (c1==c2 && c2==c3) {
        numSame++;
        if (c1=='a') numCorrect++;
      }
    }
    float percentCorrect = (float)numCorrect/(float)numSame * 100.0f;
    System.out.println("\nP1 = P2 = P3");
    System.out.println("numSame: " + numSame);
    System.out.println("numCorrect: " + numCorrect);
    System.out.println("% correct:  " + percentCorrect);
  }
}

(In retrospect I can write that code much better. I just whipped this up one day in 2005 on my day off.)

Java Monte Carlo simulation - Results and discussion

Given this program, here are my results for one run:

# of cases where person1 and person2 have 'a' for an answer: 64215 out of 100000 

P1 = P2 numSame: 68193 numCorrect: 64215 % correct:  94.16656 
P1 = P2 = P3 numSame: 52190 numCorrect: 51532 % correct:  98.73923

Based on this run, and several similar runs, here are my results:

When looking at the data from two simulated people:

If one person is right about something 80% of the time,
and a second person is also right about the same thing 80% of the time,
and they both have the same answer,
there is ~94% chance that answer is correct.

As you can see from the current version of the program, I also ran the same test with three "people" and the "correctness" increased to ~98%.

The lesson of this for me is this: I always knew of Monte Carlo simulations before, but I never knew why they were useful. But now I know. When you can't figure out the right statistical algorithm for a given problem, run a few thousand simulations so you'll know what the answer "should" be.