Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Last active December 9, 2020 04:47
Show Gist options
  • Save dengsauve/d51346a2c402392a1dd2fe73ed9b12f0 to your computer and use it in GitHub Desktop.
Save dengsauve/d51346a2c402392a1dd2fe73ed9b12f0 to your computer and use it in GitHub Desktop.
Basic simulation of a series of the Monty Hall Problem
# ######################################################################################################################
# The Monty Hall Problem #
# ########################
#
# The Premise:
# Three identical boxes are presented to a player
# Two are duds, and one contains a prize
# The player chooses one box, having no idea of what it might contain.
# In this instance, each box has a 33.3% chance of being a winner, and a 66.6% chance of being a dud (RIP 0.1%)
# A moderator then removes one of the duds from play, leaving the players original choice, and one other box
# In this instance, the other box now has a 50% chance of being a winner, but your original choice still only has 33.3%
# Given these new odds, the player should take the other box every time instead of staying with their original choice.
#
# The Experiment:
# The following code will run a series of simulations, with an array of two `0`s, and one `1`
# The computer will randomly select a box, which will be recorded as a "held" box
# The computer will select a dud and remove it from the array
# The computer will then record the remaining box as a "final" box
#
# The Hypothesis:
# I expect the simulation to hold up to game theory, and the ratio of winning "held":"final" boxes should be 50:33.3
#
# The Result (tentative):
# It appears that running this code produces a ratio in the range of 1.5 - 2.5, with no obvious predictability
#
# The Result after some research
# After running a much higher number of series (in the millions), the sim produces an ratio of ~2:1 reliably
# ManLab's assumption of 3:2 and 'experiment' were both faulty - a lesson in running limited series experiments
# It appears that 2:1 is the expected ratio - https://en.wikipedia.org/wiki/Monty_Hall_problem
@held_boxes = []
@final_boxes = []
@number_of_series = 1000000
@number_of_series.times do
# Set up the boxes w/2 duds and a winner, then randomize the order
boxes = [0,0,1].shuffle!
# Held boxes gets a truly random choice
@held_boxes << boxes.delete_at(rand(boxes.length))
# Remove one of the duds from the equation
boxes.delete_at(boxes.index(0))
# Final boxes gets the last choice
@final_boxes << boxes.pop
# Sanity Check 1
raise "element still exists in boxes array" if boxes.size > 0
end
# Sanity Check 2
puts @held_boxes.size == @final_boxes.size
# Remove duds, leaving a count
@held_boxes.reject! {|e| e == 0}
@final_boxes.reject! {|e| e == 0}
puts "Original estimate: 50/33.3 = #{50.0/33.3}"
puts "Updated Estimate: 66.6/33.3 = #{66.6/33.3}"
puts "Actual Result: Final Boxes / Held Boxes = #{@final_boxes.size.to_f / @held_boxes.size.to_f}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment