Skip to content

Instantly share code, notes, and snippets.

@supix
Last active June 19, 2018 07:11
Show Gist options
  • Save supix/30006bde62a572e4967ae63ff841e692 to your computer and use it in GitHub Desktop.
Save supix/30006bde62a572e4967ae63ff841e692 to your computer and use it in GitHub Desktop.
Applying Bayes Theorem and verifying if through frequentist probability approach

Context

We are in a university campus. There are two faculties: math and business administration. 10% of student are enrolled in math (then, 90% of students are enrolled in business administration). Within math faculty, 75% students are shy. Within business administration, 15% students are shy.

Problem

I see a student, and he is clearly shy. What's the probability that he is enrolled in math faculty?

Theoretical solution through Bayes Theorem

I have to compute P(Math|Shy).

According to Bayes theorem, we have:

               P(Shy|Math) P(Math)
P(Math|Shy) = -----------------------
                     P(Shy)

From data it stems that:

P(Shy|Math) = 0.75
P(Math) = 0.1
P(Shy) = P(Shy|Math) P(Math) + P(Shy|BA) P(BA) = 0.75 * 0.1 + 0.15 * 0.90

Doing the math we have an overall probability of 0.3571428.

Solution through the Frequentist Probability

This piece of code, written in C#, prints the probability that picking a shy student he happens to be a math student.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bayes
{
    internal class Program
    {
        private const double totalStudents = 1e6;
        private const double studentsToPick = 1e5;

        private static void Main(string[] args)
        {
            var rnd = new Random();
            var people = new List<Student>();

            for (int i = 0; i < totalStudents; i++)
            {
                people.Add(Student.Create());
            }

            int selectedShys = 0;
            int mathStudents = 0;

            for (int i = 0; i < studentsToPick; i++)
            {
                var s = people[rnd.Next(people.Count)];
                if (s.Shy)
                {
                    selectedShys++;
                    if (s.Math)
                        mathStudents++;
                }
            }

            Console.WriteLine(mathStudents / (double)selectedShys);
            Console.ReadLine();
        }
    }
    
    public class Student
    {
        private const double probToBeMathStudent = .1d;
        private const double probToBeShyGivenMath = .75d;
        private const double probToBeShyGivenBA = .15d;
        private static Random rnd = new Random();

        public bool Math { get; }
        public bool Shy { get; }

        public Student(bool math, bool shy)
        {
            this.Math = math;
            this.Shy = shy;
        }

        public static Student Create()
        {
            var math = rnd.NextDouble() < probToBeMathStudent;
            var shy = math ? rnd.NextDouble() < probToBeShyGivenMath : rnd.NextDouble() < probToBeShyGivenBA;

            return new Student(math, shy);
        }
    }
}

Running the code we have: 0,35584

Acknowledgement

Thanks to Julia for having inspired this riddle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment