I need the complete Python code for the question. Thank you.
Problem 5: (Discrete Event Simulation) Suppose a YouBike rental station near an MRT station has merely a capacity of 4 docks (one dock holds one bike), and with appropriate normalizations, customers arrive at the YouBike station in a discrete-time sequence, say, t={1,2,3,}. There are two types of arriving customers: Type I: Customer who wants to rent a bike; Type II: Customer who wants to return a bike. Suppose the "type" of every customer arriving at the YouBike station is "random", say, P( customer is Type I)=p1 and P( customer is Type II )=p2,p1+p2=1. If an arriving customer is Type I and at least one bike is available on the dock, then a bike is successfully rented; otherwise, the customer will leave the station right away (thus being lost). If an arriving customer is Type II and at least one dock is empty, then a bike is successfully returned; otherwise, the customer will leave the station right away. Let Xt be the number of bikes available on docks at time t, and assume Xt can be modeled as a (discrete-time) Markov chain with one-step transition probabilities pij=P(Xt=jXt1=i),wherei,j=0,1,,4. Let p1=0.6,p2=0.4, and X0=2 ( 2 bikes on docks at time 0). (c) (5pts) Estimating all non-zero pij based on the simulation length determined in (b), write down the estimated one-step transition probability matrix P=p^00p^10000p^010p^21000p^120p^32000p^230p^43000p^34p^44=? Using the estimated p^ij in P to solve the stationary probabilities i= P(Xt=i),i=0,1,,4. (d) (5pts) Suppose the hourly rental fee for a bike is 10 NTD (thus 240 NTD for the whole day). Also, the daily holding cost for each bike dock is 15 NTD. Calculate the expected (or average) daily revenue (in NTD) for this particular YouBike station by using the results obtained in c). [Notes: (i) Revenue = (rental income) - (holding cost); (ii) Do not use simulation to estimate the expected revenue. Instead, you can calculate the "expected rental income" by examining the "expected \# of bikes on the docks" (at any point in time t ) based on the stationary distribution of i obtained in c).] (e) (5pts) It the primary time unit refers to " 3 minutes" (i.e., t=1 means 3 mins, t=2 means 6mins, and so on) and each Type I customer in average spends 15 NTD on renting a bike. Calculate the expected daily loss (in NTD) of this YouBike station caused by losing customers who fail to rent a bike? arrive at the YouBike station in a discrete-time sequence, say, t={1,2,3,}. There are two types of arriving customers: Type I: Customer who wants to rent a bike; Type II: Customer who wants to return a bike. Suppose the "type" of every customer arriving at the YouBike station is "random", say, P(customerisType1)=p1andP(customerisTypeII)=p2,p1+p2=1. If an arriving customer is Type I and at least one bike is available on the dock, then a bike is successfully rented; otherwise, the customer will leave the station right away (thus being lost). If an arriving customer is Type II and at least one dock is empty, then a bike is successfully returned; otherwise, the customer will leave the station right away. Let Xt be the number of bikes available on docks at time t, and assume Xt can be modeled as a (discrete-time) Markov chain with one-step transition probabilities pij=P(Xt=jXt1=i),wherei,j=0,1,,4 Let p1=0.6,p2=0.4, and X0=2 ( 2 bikes on docks at time 0). Answer the following questions in order: (a) (5pts) Write down an algorithm with detailed steps to simulate this YouBike system. The algorithm should at least record all the event times, the evolution of Xt and the number of events transiting from state i to j. (b) (5pts) Suppose we want to estimate all non-zero transition probabilities pij(i,j= 0,1,,4 ) based on the simulation. Then, what is the required simulation length (or when to stop the simulation) so that with a 95% confidence we can say that all estimates p^ij have the margin of error less than 2% ? Clearly address all necessary steps to obtain the answer. [Hint: With a 95% confidence level, the margin of error for estimating each pij has an upper bound 1.96nij0.25. Use this upper bound to determine the required