We’ll finally answer the long-awaited question: what’s the probability you win a ping pong game up to n points, when you

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

We’ll finally answer the long-awaited question: what’s the probability you win a ping pong game up to n points, when you

Post by answerhappygod »

We’ll finally answer the long-awaited question: what’s theprobability you win a ping pong game up to n points, when yourprobability of winning each point is p (and your friend wins thepoint with probability 1−p)? Assume you have to win by (at least)2; for example, if n = 21 and the score is 21 − 20, the game isn’tover yet. Write your code for the following parts in the providedfile:
this is my code
"""This starter code is written by Alex Tsun and modified by PemiNguyen.Permission is hereby granted to students registered for the Universityof Washington CSE 312 for use solely during Fall Quarter 2021 forpurposesof the course. No other use, copying, distribution, ormodification ispermitted without prior written consent."""
import numpy as npimport matplotlib.pyplot as plt
def part_a(n:int=21, p:float=0.3, ntrials:int=5000): """ Write code to simulate a ping pong game to npoints, where the probability of you winning a singlepoint is p. You must win by 2; for example, if the score is21 − 20, the game isn’t over yet. Simulate ntrials # ofgames.
:param n: The number of points to playto. :param p: The probability of YOU winning asingle point. :param ntrials: The number of trials tosimulate. :return: returns the probability YOU win theoverall game. """
def sim_one_game(): """ This is a nestedfunction only accessible by parent part_a, which we're in now. Youmay want to implement this function! """ playerPoint =0 compPoint = 0 while 1: game = np.random.uniform(low = 0.0, high = 1.0, size = None) if game < p: playerPoint += 1 else: compPoint += 1 if (playerPoint >= n or compPoint >= n) and abs(playerPoint -compPoint) >= 2: if playerPoint > compPoint: return True else: return False playerWin = 0 compWin = 0 for i in range(ntrials): if sim_one_game(): playerWin += 1 else: compWin += 1 return ( playerWin/ ( playerWin + compWin ))
now I need help with this :
We Ll Finally Answer The Long Awaited Question What S The Probability You Win A Ping Pong Game Up To N Points When You 1
We Ll Finally Answer The Long Awaited Question What S The Probability You Win A Ping Pong Game Up To N Points When You 1 (105.18 KiB) Viewed 66 times
Make a single plot using matplotlib with the x-axis beingp for different values of p in {0, 0.04,0.08,...,0.96, 1.0} and the y-axis being the probability of winningthe overall game (use your previous function). Plot 3 “curves” indifferent colors, one for each n in {3,11,21}.
Hint(s): 1. You'll call plt.plot(...) 3 times total, onefor each n. Make sure your calls are of the form: 'plt.plot(x_values, y_values, color=...,linestyle=..., label=...)' where color indicates the color of a line, linestyle indicates different line styles(solid, dashed, etc.), and label indicates the label of each plot inthe legend. 2. Use plt.legend(loc="upper left") to place thelegend on the upper left corner 3. Name the x-axis, the y-axis and the plottitle exactly like the sample plot 4. Use plt.savefig(...) to save the plot.
:return: Nothing. Just save the plot youmade! """ # TODO: Your code here (10-20 lines) pass
if __name__ == '__main__': # You can test out things here. Feel free towrite anything below. print(part_a()) part_b()
Implement the function in Python. you should use the space here to generate the plot asked of you below. (i) Generate a plot similar to the one shown below in Python (without the watermarks). Details on how to construct it are in the starter code. Attach your plot in your written submission for this part. Your plot should: • contain plot and axis titles, • have the same shape as the plot below, use three different colors, • use three different line styles, 2 and a legend for the three lines. Write AT MOST 2-3 sentences identifying the interesting pattern you notice when n gets larger (regarding the steepness of the curve). Try to explain why it makes sense. (Later in the course, we will see why more formally.) (iii) (iii) Each curve you make for different values of n always (approximately) passes through 3 points. Give the three points (x1, y1),(x2, y2),(x3, y3), and explain why intuitively this happens in AT MOST 2-3 sentences. Figure 1: Your plot should look (ii) نے 1.0 -n-3 0.8 0.2 Solution 0.0 Figure 1: Your plot should look something like this. Relating P(win point) to P(win game) 0.0 n=11 n=21 0.2 0.4 0.6 P(win point) Solution Solution 1.0 something like that
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply