Question

Only do Q2 please!!! please Type the code in python!!!thanks

1. (10 points) (Without Python) Unfortunately, Eddard is lost in the dark dungeon of the Red Keep. He knows there are three doors, and one of the doors will lead him to freedom. If Eddard takes Door A, he will wander around the dungeon for 3 days and return to where he started If he takes Door B, he will wander around the dungeon for 4 days and return to where h<e started. If he takes Door C, he will find the exit after 2 days. If Eddard returns to where he started, he immediately picks a door to pass through, and since its pitch black in the dungeon, Eddard picks each door uniformly at random. How long, on average, will Eddard wander in the dungeon before finding his way out? 2. (30 points) (With Python) In this Python exercise, you will estimate the number of days it will take for Eddard to escape the dungeon. The setup for the problem is the same as above. For this exercise, you may want to recall the following: » The Law of Large Numbers says that if we have a sequence of i.i.d. random variables Xi for i = 1, 2, . . . , n, then Tl That is, the average of the results found from a large number of trials tends to the expected value. Be sure to annotate your code with short explanations of what you are doing (worth 10 points) (a) Simulate Eddards journey to freedom 10,000 times. Find the average time it takes for Eddard to escape the dungeon. (b) Print out your result using the print function in Python. Please be sure to say want you are printing/outputting. For example, print(After 10,000 simulations, the average time it takes Eddard to escape is %s days. %averageTimeInDungeon) Take a screenshot showing your code and your results side by side.

Only do Q2 please!!! please Type the code in python!!!thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Doing 1 just to establish the theoretical value

1. Let T be the time in Dungeon.

Let D={A,B,C} be the door taken with probability P(D=A) = P(D=B)=P(D=C)=1/3

The expectation of T can be written as

egin{align*} E(T)&=E[E(Tmid D)] &=P(D=A)E(Tmid D=A)+P(D=B)E(Tmid D=B)+P(D=C)E(Tmid D=C) &=rac{1}{3}left( E(Tmid D=A)+E(Tmid D=B)+E(Tmid D=C) ight) end{align*}

If D=A, then Eddard wanders around for 3 days and returns back to the start and hence would take on an average E(T) more before he gets out

E(T|D=A) = 3+E(T)

If D=B, then Eddard wanders around for 4 days and returns back to the start and hence would take on an average E(T) more before he gets out

E(T|D=B) = 4+E(T)

If D=C, then Eddard would find the exit in 2 days

E(T|D=C) = 2

Hence

egin{align*} E(T)&=E[E(Tmid D)] &=P(D=A)E(Tmid D=A)+P(D=B)E(Tmid D=B)+P(D=C)E(Tmid D=C) &=rac{1}{3}left( E(Tmid D=A)+E(Tmid D=B)+E(Tmid D=C) ight) &=rac{1}{3}(3+E(T)+4+E(T)+2) end{align*}

by solving the equation we get

egin{align*} &3E(T)=9+2E(T) implies& E(T)=9 end{align*}

Eddard would take on an average 9 days to get out.

2) Now the Python code with comments is below

import numpy as np #define a function to randomly choose a door def choosedoor): #get a uniform random number in the interval

Get the following output

After 10,000 simulations, the average time it takes Eddard to escape is 9.0264 days.

The result is close enough to the theoretical value calculated in 1)

The code in text format (the tabs may be lost)

import numpy as np

#define a function to randomly choose a door
def choosedoor():
#get a uniform random number in the interval (0,1)
u=np.random.random()
# choose the door A if 0<=u<1/3, B if 1/3<=u<2/3, C if 2/3<=u<1
if u<1/3:
return('A')
elif u<2/3:
return('B')
else:
return('C')

# function to get the total time spent in the dungeon
def dungeon(door,days):
# if door C get out in 2 days
if door=='C':
days=days+2
#if door A take 3 days and go back to start by recursively call back the function
elif door=='A':
days=days+3
door=choosedoor()
days=dungeon(door,days)
#if door B take 4 days and go back to start by recursively call back the function
else:
days=days+4
door=choosedoor()
days=dungeon(door,days)
return(days)


# set the seed for the random number generator
np.random.seed(123)
# set the number of simulations
N=10000
#initialize a variable to hold the time spent
TimeInDungeon=[]
# simulate the run for N times
for i in range(N):
# choose a door for the first time
door=choosedoor()
#call the function dungeon and store the time spent
TimeInDungeon.append(dungeon(door,0))

#find the average time spent
avergaeTimeInDungeon=np.mean(TimeInDungeon)
print("After 10,000 simulations, the average time it takes Eddard to escape is %s days."%avergaeTimeInDungeon)

Add a comment
Know the answer?
Add Answer to:
Only do Q2 please!!! please Type the code in python!!!thanks Only do Q2 please!!! please Type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Python 3. Help please. Here is my current code. What can I do to fix it?...

    Python 3. Help please. Here is my current code. What can I do to fix it? I list=[1,2,2,3,4,5] d = {} for item in list: if item in d: return True d[item] = True return False Exercise 11.4. If you did Exercise 10.7, you already have a function named has_duplicates that takes a list as a parameter and returns True if there is any object that appears more than once in the list. Use a dictionary to write a faster,...

  • Please answer this question using python programming only and provide a screen short for this code....

    Please answer this question using python programming only and provide a screen short for this code. Homework 3 - Multiples not less than a number Question 1 (out of 3) This homework is a SOLO assignment. While generally I encourage you to help one another to learn the material, you may only submit code that you have composed and that you understand. Use this template to write a Python 3 program that calculates the nearest multiple of a number that...

  • Please help me write these in R script / Code 1, Suppose you're on a game...

    Please help me write these in R script / Code 1, Suppose you're on a game show, and you're given the choice of three doors. Behind one door is a car; behind the others, goats. You pick a door, say #1, and the host, who knows what's behind the doors, opens another door, say #3, which has a goat. He then says to you, "Do you want to pick door #2?" What is the probability of winning the car if...

  • Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a...

    Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processes the contents the file as follows: For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line. For file lines that begin with the letter 'u', print the upper case format for each word following the u. Sample...

  • Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...

    Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: return mlist else: mid=len(mlist)//2 return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...

  • how to make my code of python work probely my q is how to write a...

    how to make my code of python work probely my q is how to write a code to find the average actual water level, average error by using python and my program should be flexible where user can select the column no. If you can see in the main program, it asked which column to know the maximum or average, the column number is passed to the function. The function should look which column to do. #THIS PROGRAMMING IS ABLE...

  • please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords,...

    please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords, Y_coords) below, which should take two arrays of equal length containing the x-andy- coordinates for some number of particles. We'll use a very simple random walk algorithm • For each particle, choose a random angle between 0 and 2 • The particle moves by 1 unit of distance in the direction given by d.o. It is displaced by (Ax, Ay) (cos, sino). We'll do...

  • please help me write the python code for this problem Problem 2: Restaurant Tipping IAE IOL...

    please help me write the python code for this problem Problem 2: Restaurant Tipping IAE IOL (Fall 2015) HR . Problem 2 After many years, you have developed a system for determining exactly how much to tip when eating out at a restaurant: de tip mount bill, good service ADD YOUR COOE MERE return -1. CHANGE OR REMOVE THIS LINE • If the final bill is $30.00 or less, you will leave a tip of exactly $5.00 (regardless of the...

  • this is a python question please help me with this code thank you and appreciate it...

    this is a python question please help me with this code thank you and appreciate it 1. Write a Boolean function between that takes two My Time objects, t1 and t2, as arguments, and returns True if the invoking object falls between the two times. Assume t1 <= t2, and make the test closed at the lower bound and open at the upper bound, i.e. return True if t1 <= obj <t2. 2. Turn the above function into a method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT