Understand the "three door problem", double the success probability, and use the code to verify it

Seeing a video about "three door problem", the first feeling is that the conclusion of the video is wrong. I wanted to laugh it off, but after reading the comments, I was confused: what is the answer to the three questions?

As a diligent and inquisitive code farmer, it's still hard to know the final answer, so you have a deep study and find that "the clown is himself". If you want to challenge yourself, you can skip the reasoning and conclusion part, get an answer by yourself, and then see if it's correct.

A circle of friends

After spending an hour and understanding the three questions, I sent this circle of friends:

Three door question: there are three doors, one of which is behind the car and the other two are goats. When you select one door, the host opens one with goats from the other two doors. Then, will changing the door increase the probability of obtaining the car?

The first mistake: intuition, changing or not changing is 1 / 2 probability; almost stopped here and came to the conclusion that it is all deceptive.

Second error: list, (choose 1, go 2, change), (choose 2, go 1, change), (choose 3, go 1, don't change), (choose 3, go 2, don't change). It seems that the probability is still 1 / 2. However, a mistake is made here, and the preferred probability is not introduced, that is, the latter two cases can't be calculated as 1 / 4, but only 1 / 6.

The third introduction probability: 1 / 3 (select 1, go 2, change), 1 / 3 (select 2, go 1, change), 1 / 6 (1 / 3 * 1 / 2) (select 3, go 1, do not change), 1 / 6 (1 / 3 * 1 / 2) (select 3, go 2, do not change). The latter two items have only 1 / 3 probability in total.

Therefore, the answer to the three questions is: choose to change. The probability will change from 1 / 3 to 2 / 3;

Through this question, I think: sometimes, persistence may be wrong, it may be subjective judgment, and the environment may have changed; but sometimes I have to persist, I have to persist in doubting the answer and constantly look for the answer.

If from the bottom logic, it is to adhere to the dynamic view of problems, that is, scholars should treat each other with admiration.

After sending this circle of friends, I feel that it is necessary to realize this problem through procedures, and write an article to share at the same time, so I have this article.

If the above analysis is not understood, it doesn't matter. Let's analyze and practice it in combination with the code.

Three door problem

The three questions are from Let's Make a Deal, a TV game program in the United States, and the name of the questions is from Monty Hall, the host of the program.

Problem scenario:

The contestants will see three closed doors. There is a car behind one of the doors. Select the door with a car behind to win the car, and there is a goat behind the other two doors. When the contestants select a door, but do not open it, the program host will open one of the remaining two doors to expose one of the goats. The host will then ask the contestants The question is: will changing another door increase the chance of winning the car.

It is said that 90% of people choose not to change. What is your choice?

probability analysis

First look at the figure below. There are three doors: car, goat 1 and goat 2:

The probability of players choosing three doors is one-third. The following specific assumptions are made:

  • Assuming that the contestant chooses goat 1, the host can only hit goat 2, because the door with a car cannot be opened. The probability of this situation is: 1 / 3 (the probability of the contestant choosing goat 1) * 1 (the host's choice is certain) = 1 / 3; at this time, if you change, you will win the car;
  • Assuming that the player chooses goat 2, the host can only hit goat 1, because the door with a car cannot be opened. The probability of this situation is: 1 / 3 (the probability of the player choosing goat 2) * 1 (the host's choice is certain) = 1 / 3; at this time, if you change, you will win the car;
  • Assuming that the contestant chooses the car, the host has two open choices: goat 1 and goat 2. The probability of the host choosing goat 1: 1 / 3 (the probability of the contestant choosing the car) * 1 / 2 (the host chooses one of two) = 1 / 6; the probability of the host choosing goat 2: 1 / 3 (the probability of the contestant choosing the car) * 1 / 2 (the host chooses one of two) =1 / 6; therefore, when the contestant selects the door of the car, the probability of occurrence is: 1 / 3 * 1 / 2 + 1 / 3 * 1 / 2 = 1 / 3. At this time, if you don't change, you will win the car;

Obviously, the probability of three situations is one-third, and the probability of winning the car after changing is twice that of not changing. That is to say, after changing, the probability of winning the car becomes two-thirds.

Program demonstration

Theoretical analysis is made above, and a code is written below to verify:

public class ThreeDoors {

	/**
	 * Random Selector 
	 */
	private static final Random RANDOM = new Random();

	/**
	 * Total successful times
	 */
	private static int SUCCESS_COUNT = 0;

	/**
	 * Repeat 10w times
	 */
	private static final int PLAY_TIMES = 100000;

	public static void main(String[] args) {

		// Execute the game 10w times
		for (int i = 0; i < PLAY_TIMES; i++) {
			playGame();
		}

		// Calculate the probability of selecting "exchange"
		BigDecimal yield = new BigDecimal(SUCCESS_COUNT)
				.divide(new BigDecimal(PLAY_TIMES), 4, RoundingMode.HALF_UP)
				.multiply(new BigDecimal(100));
		System.out.println("implement" + PLAY_TIMES + "In this experiment, the probability of selecting [exchange] is:" + yield + "%");
	}

	public static void playGame() {

		// Initialize the three doors. The default is false. There is no car
		boolean door1 = false, door2 = false, door3 = false;

		// Whether the door selected by the contestant is a car, true: Yes
		boolean pickedDoor;
		// Whether the last remaining door is a car, true: Yes
		boolean leftDoor;

		// Step 1: randomly select a door and put it into the car
		switch (pickDoor(3)) {
			case 1:
				door1 = true;
				break;
			case 2:
				door2 = true;
				break;
			case 3:
				door3 = true;
				break;
			default:
				System.out.println("Abnormal value");
				break;
		}

		// Step 2: the contestant selects a door and still adopts the above door selection algorithm
		int playerPickedDoor = pickDoor(3);

		// Step 3: the host removes a door with goats
		// The host can only choose one of two. Removing 1 one door is equivalent to selecting another door
		if (playerPickedDoor == 1) {
			// Player selection door 1
			pickedDoor = door1;
			// If door 2 has a car, only door 3 can be removed
			if (door2) {
				leftDoor = door2;
			} else if (door3) {
				// If door 3 has a car, only door 2 can be removed
				leftDoor = door3;
			} else {
				// There is no car at either door. Choose one from two at random
				if (pickDoor(2) == 1) {
					leftDoor = door2;
				} else {
					leftDoor = door3;
				}
			}
		} else if (playerPickedDoor == 2) {
			// Player selection door 2
			pickedDoor = door2;
			// If door 1 has a car, only door 3 can be removed
			if (door1) {
				leftDoor = door1;
			} else if (door3) {
				// If door 3 has a car, only door 1 can be removed
				leftDoor = door3;
			} else {
				// There is no car at either door. Choose one from two at random
				if (pickDoor(2) == 1) {
					leftDoor = door1;
				} else {
					leftDoor = door3;
				}
			}
		} else {
			// Player selection door 3
			pickedDoor = door3;
			// If door 1 has a car, only door 2 can be removed
			if (door1) {
				leftDoor = door1;
			} else if (door2) {
				// If door 2 has a car, only door 1 can be removed
				leftDoor = door2;
			} else {
				// There is no car at either door. Choose one from two at random
				if (pickDoor(2) == 1) {
					leftDoor = door1;
				} else {
					leftDoor = door2;
				}
			}
		}

		// Step 4: when the above results are certain, the contestant chooses to replace the door
		pickedDoor = leftDoor;

		// Step 5: judge whether there is a car at the door
		if (pickedDoor) {
			SUCCESS_COUNT++;
		}
	}
	
	/**
	 * Select a door at random
	 */
	public static int pickDoor(int bound) {
		return RANDOM.nextInt(bound) + 1;
	}
}

The above implementation method does not consider algorithm optimization for the time being, but only simple case judgment and processing.

The above implementation is divided into the following steps:

  • Step 1: randomly select a door and put it into the car. Random number is used here. If the car is behind the corresponding door, the corresponding value is set to true;
  • Step 2: the player selects a door, and the algorithm still adopts Random number;
  • Step 3: on the premise that the contestant selects a door, the host removes a door without a car. The removed door is not processed here, but the value of the remaining door after removal is recorded. If there are no cars for both doors, choose one at random.
  • Step 4: the player chooses to exchange, that is, the door selected by the player becomes the remaining door.
  • Step 5: open the door and verify it. If it is successful, record it once;
  • Step 6: after 10w times of execution, calculate the percentage;

The final print log is as follows:

After 100000 experiments, the probability of selecting [exchange] is 66.7500%

If you execute it several times, you will find that almost all of them are between 66% - 67%, indicating that selecting change can indeed double the probability of success.

Summary

Finally, review the whole process: I accidentally saw a video about "three questions". First, I made an intuitive judgment (wrong), scoffed at other people's conclusions, and then found Xu and objection. So I began to seek evidence and finally got the right answer.

As said in the circle of friends: sometimes, persistence may be wrong, it may be because of subjective judgment, or it may be because the environment has changed; but sometimes we have to persist, we should persist in the doubt of the answer and the constant pursuit of the answer.

This should also be the underlying logic of our work. We can't judge by [feeling] alone. We should use facts as the basis. Especially programmers, we can also use programs to solve similar problems.

At the same time, do you find it interesting to use programs to solve some problems in life?

About the blogger: the author of the technical book "inside of SpringBoot technology", loves to study technology and write technical dry goods articles.

The official account: "new horizon of procedures", the official account of bloggers, welcome the attention.

Technical exchange: please contact blogger wechat: zhuan2quan


" New horizon of procedures ", official account of a 100% dry cargo.

Tags: Algorithm Programmer

Posted on Sun, 31 Oct 2021 18:45:10 -0400 by lesmckeown