Prove the Monty Hall Problem (via Simulation)!

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

60 Replies - 19510 Views - Last Post: 16 June 2014 - 07:20 AM

#16 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 01:29 AM

By the way, I didn't optimize it to do dynamic doors, only 3 doors, since I think if you have a million doors and 999,998 were opened for you it's the same scenario with just 3 doors. That's what I guess, anyway.
Was This Post Helpful? 0
  • +
  • -

#17 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 02:30 AM

Proved using an exhaustive search of 9 runs:

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

namespace ExhaustiveMontyHallProblem
{
    class ExhaustiveGoatSearch
    {

        private int doorCount;

        public ExhaustiveGoatSearch(int doorCount)
        {
            this.doorCount = doorCount;
        }

        private void runSimulations()
        {
            int changeCount = 0;
            int keepCount = 0;
            int gameCount = 0;

            for (int i = 0; i < doorCount; i++)
            {
                for (int j = 0; j < doorCount; j++)
                {
                    gameCount++;
                    Game game = new Game(doorCount, i, j);
                    Console.WriteLine(game.toString());
                    if (game.shouldChangeDoors()) { changeCount++; }
                    else { keepCount++; }
                }
            }

            String bestAction = changeCount > keepCount ? "always" : "never";
            Console.WriteLine("The player should have changed doors " + changeCount + " times.");
            Console.WriteLine("The player should have kept doors " + keepCount + " times.");
            Console.WriteLine("The best strategy is to " + bestAction + " change doors.");
        }

        static void Main(string[] args)
        {
            new ExhaustiveGoatSearch(3).runSimulations();
            Console.ReadKey();
        }
    }

    class Game
    {

        private int doorCount;
        private int carPosition;
        private int initialChoice;

        public Game (int doorCount, int carPosition, int initialChoice)
        {
            this.carPosition = carPosition;
            this.initialChoice = initialChoice;
            this.doorCount = doorCount;
        }

        public bool shouldChangeDoors()
        {
            return carPosition != initialChoice;
        }

        public String toString()
        {
            String result = "";
                for(int i = 0; i < doorCount; i++) 
                {
                    if (i == carPosition)
                    {
                        result += "Car ";
                    }
                    else
                    {
                        result += "Goat ";
                    }
                }

                result += "-- Initial choice is door " + (initialChoice + 1);

                if (shouldChangeDoors())
                {
                    result += ". Player should change doors.";
                }
                else
                {
                    result += ". Player should not change doors.";
                }
                return result;
        }

    }

}


Was This Post Helpful? 1
  • +
  • -

#18 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 11:29 AM

I've not looked closely at fromTheSprawl's code, but I'm puzzled by the results he is getting. As predicted, by not swapping, chance of winning was only about 33%. No problem there. I thought that by swapping, the contestant had 66% chance of winning, but his numbers show barely 50%. As a quick sanity check, I ran Curtis Rutland's code and that was declaring around a 66% chance of winning.
Was This Post Helpful? 1
  • +
  • -

#19 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 11:50 AM

I agree with Skydiver. I think there might be something wrong. I don't do Python though, so I can't really tell you what it is. Switching should yield a ~66% chance of winning.
Was This Post Helpful? 0
  • +
  • -

#20 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 12:48 PM

No one took me up on the LINQ challenge? Oh well, curiosity won. Seems it is similar:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#21 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 12:50 PM

I honestly tried, baavgai, but I don't understand Python well enough to understand what was going on in your program.
Was This Post Helpful? 0
  • +
  • -

#22 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 12:59 PM

For the 50% on fromTheSprawl's code...
if switchOk == "yes":
	doorNumber = randint(0,2)
	# one goatDoor is chosen
	# so that's is out of the mix
	# however, we ignore doorNumber passed
	# and randomly choose one of the two remaining doors
	# one wins, one loose, hence 50%
	while doorNumber == goatDoor:
		doorNumber = randint(0,2)



View PostCurtis Rutland, on 20 September 2012 - 03:50 PM, said:

I honestly tried, baavgai, but I don't understand Python well enough to understand what was going on in your program.


Then, um, read the LINQ? ;)
Was This Post Helpful? 2
  • +
  • -

#23 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 01:01 PM

View Postbaavgai, on 20 September 2012 - 02:59 PM, said:

Then, um, read the LINQ? ;)


That's what I was working on :P

Here's a shortened version of your own code using the extension methods (that I find cleaner):

var total = Enumerable.Range(0, runs)
    .Where(i => rnd.Next(0, 3) != rnd.Next(0, 3)).Count();



Side note: Resharper bitches about rnd.Next(0,3) != rnd.Next(0,3) and wants to replace it with false. I guess it's not smart enough to realize that the function returns different values when provided the same arguments.
Was This Post Helpful? 0
  • +
  • -

#24 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 01:14 PM

A breakdown.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#25 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 06:23 PM

baavgai yeah when I was going home I remembered there's a serious flaw in my montyhall function since even if I set it to switch there's a chance the first chosen door will still be chosen so here's to recompense:

Spoiler


Results are in, not as big a difference as before but it's still there:
Spoiler


In the process I get to learn list comprehensions in Python and a lot of beginner errors. ^^
Was This Post Helpful? 1
  • +
  • -

#26 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 08:27 PM

That's still incorrect. Switching should yield 66% wins, as proven by other simulations and math.
Was This Post Helpful? 0
  • +
  • -

#27 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 09:57 PM

Hmm, okay I'll work on it! ^^
Was This Post Helpful? 0
  • +
  • -

#28 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8028
  • View blog
  • Posts: 13,740
  • Joined: 19-March 11

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 10:23 PM

View PostfromTheSprawl, on 20 September 2012 - 08:23 PM, said:

In the process I get to learn list comprehensions in Python and a lot of beginner errors. ^^


That's a worthy endeavor.

Here's a way to think about it that might help:

Three doors, A, B, C. I choose A. Host opens B, says "do you want to switch?".

Misconception: I am choosing between door A and door C. (this is in your program)
Actual case: I am choosing between "the prize is behind door A" and "The prize is behind door B or door C, and we know it's not behind door B".

It's okay if you get confused with this. I know this problem well, and I woke up this morning convinced I'd done it wrong.
Was This Post Helpful? 1
  • +
  • -

#29 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 20 September 2012 - 10:38 PM

Good grief!
Spoiler


And the new results:
Spoiler


How about now? :)

Yes Jon, that was the problem of my first code yesterday. I went the whole way home thinking about that simple but fatal mistake, and then the second one's mistake too. It's the little things that gets you huh. ^^
Was This Post Helpful? 1
  • +
  • -

#30 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 08:13 AM

Maybe im just stupid over here but if one of the door is opened then dont you have 2 choices now? and your chance is 50/50? and hows that if one of the doors is out of the game is still counted as a choice?
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »