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.
I see a student, and he is clearly shy. What's the probability that he applies to math faculty.
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 stem 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 of picking a math student given that he is shy.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bayes
{
internal class Program
{
private static void Main(string[] args)
{
var rnd = new Random();
var people = new List<Student>();
for (int i = 0; i < 1e7; i++)
{
people.Add(Student.Create());
}
int selectedShys = 0;
int mathStudents = 0;
for (int i = 0; i < 1e6; 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
{
public bool Math { get; }
public bool Shy { get; }
private static Random rnd = new Random();
public Student(bool math, bool shy)
{
this.Math = math;
this.Shy = shy;
}
public static Student Create()
{
var math = rnd.NextDouble() < .1d;
var shy = math ? rnd.NextDouble() < .75d : rnd.NextDouble() < .15d;
return new Student(math, shy);
}
}
}
Running the code we have: 0,35584