This is a problem borrowed from the textbook *Introduction to Probability Models *by *Sheldon Ross. *The problem is explained as follows.

A mouse is trapped in a maze. Initially it has to choose one of two directions. If it goes to the right, then it will wander around in the maze for 3 minutes and will then return to its initial position. If it goes to the left, then with probability 1/3 , it will depart the maze after 2 minutes of traveling, and with probability 2/3 it will return to its initial position after 5 minutes of traveling. Assume that the mouse is at all times equally likely to go to the left or the right. Let T denote the number of minutes that it will be trapped in the maze.What is E [T]*?

* E[T] is the expected time it takes for the mouse to escape the maze.

An artist’s rendering of the problem, provided by Mr. Chandra Sekhar Kounduri for fermibot.wordpress.com

*Analytical Solution: *The problem can be solved analytically as follows.

- Looking at the first choice: The mouse has 0.5 probability of going to the left or the right.
- Choosing right tunnel will take the mouse to the original position after 3 minutes
- Choosing left will give two more choices to the mouse
- The mouse exits the maze with probability of 1/3 after 2 minutes
- The mouse gets to the starting point with a probability of 2/3 after 5 minutes

From the above, we can model the expected time as follows

- E[T] =
**½(**3**+**E[T]**)**+**½(**(⅓*2) + (⅔*(E[T] + 5)**)** - Solving for E[T], we get E[T] = 21

*Simulation Solution:*

I have written a python code for the same . The expected values (in minutes) for various trials are printed below

- The mean time spent by the mouse is 22.8 for 10 trials
- The mean time spent by the mouse is 19.95455 for 100 trials
- The mean time spent by the mouse is 21.56396 for 1000 trials
- The mean time spent by the mouse is 21.47331 for 2000 trials
- The mean time spent by the mouse is 21.43674 for 5000 trials
- The mean time spent by the mouse is 20.98964 for 100000 trials
- The mean time spent by the mouse is 20.98466 for 200000 trials
- The mean time spent by the mouse is 20.98827 for 500000 trials
- The mean time spent by the mouse is 21.00929 for 1000000 trials
- The mean time spent by the mouse is 21.01139 for 2000000 trials
- The mean time spent by the mouse is 20.99989 for 5000000 trials
- The mean time spent by the mouse is 20.99764 for 10000000 trials

Code and output screenshots

Code for download: You can get the pdf of the code here

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.