Skip to content

Instantly share code, notes, and snippets.

@supix
Last active June 19, 2018 07:11

Revisions

  1. supix revised this gist Jun 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -107,4 +107,4 @@ Running the code we have: 0,35584

    # Acknowledgement

    Thanks to [Julia](https://www.youtube.com/watch?v=BrK7X_XlGB8) for inspiring me this riddle.
    Thanks to [Julia](https://www.youtube.com/watch?v=BrK7X_XlGB8) for having inspired this riddle.
  2. supix revised this gist Jun 19, 2018. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions Bayes.md
    Original file line number Diff line number Diff line change
    @@ -44,20 +44,23 @@ 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 < 1e7; i++)
    for (int i = 0; i < totalStudents; i++)
    {
    people.Add(Student.Create());
    }

    int selectedShys = 0;
    int mathStudents = 0;

    for (int i = 0; i < 1e6; i++)
    for (int i = 0; i < studentsToPick; i++)
    {
    var s = people[rnd.Next(people.Count)];
    if (s.Shy)
  3. supix revised this gist Jun 19, 2018. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions Bayes.md
    Original file line number Diff line number Diff line change
    @@ -75,11 +75,14 @@ namespace Bayes

    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; }

    private static Random rnd = new Random();

    public Student(bool math, bool shy)
    {
    this.Math = math;
    @@ -88,8 +91,8 @@ namespace Bayes

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

    return new Student(math, shy);
    }
  4. supix revised this gist Jun 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ Doing the math we have an overall probability of 0.3571428.

    # Solution through the [Frequentist Probability](https://en.wikipedia.org/wiki/Frequentist_probability)

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

    ```csharp
    using System;
  5. supix revised this gist Jun 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Within math faculty, 75% students are shy. Within business administration, 15% s

    # Problem

    I see a student, and he is clearly shy. What's the probability that he is enrolled in math faculty.
    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

  6. supix revised this gist Jun 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ Doing the math we have an overall probability of 0.3571428.

    # Solution through the [Frequentist Probability](https://en.wikipedia.org/wiki/Frequentist_probability)

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

    ```csharp
    using System;
  7. supix revised this gist Jun 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ P(Math|Shy) = -----------------------
    P(Shy)
    ```

    From data stem that:
    From data it stems that:

    ```
    P(Shy|Math) = 0.75
  8. supix revised this gist Jun 17, 2018. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -97,4 +97,8 @@ namespace Bayes
    }
    ```

    Running the code we have: 0,35584
    Running the code we have: 0,35584

    # Acknowledgement

    Thanks to [Julia](https://www.youtube.com/watch?v=BrK7X_XlGB8) for inspiring me this riddle.
  9. supix revised this gist Jun 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Within math faculty, 75% students are shy. Within business administration, 15% s

    # Problem

    I see a student, and he is clearly shy. What's the probability that he applies to math faculty.
    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

  10. supix revised this gist Jun 17, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Bayes.md
    Original file line number Diff line number Diff line change
    @@ -21,13 +21,15 @@ P(Math|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 [Frequantist Probability](https://en.wikipedia.org/wiki/Frequentist_probability)
    # Solution through the [Frequentist Probability](https://en.wikipedia.org/wiki/Frequentist_probability)

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

  11. supix created this gist Jun 17, 2018.
    98 changes: 98 additions & 0 deletions Bayes.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    # 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 applies to math faculty.

    # Theoretical solution through Bayes Theorem

    I have to compute P(Math|Shy).

    According to [Bayes theorem](https://en.wikipedia.org/wiki/Bayes%27_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 [Frequantist Probability](https://en.wikipedia.org/wiki/Frequentist_probability)

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

    ```csharp
    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