hw06

.pdf

School

Palomar College *

*We aren’t endorsed by this school

Course

128

Subject

Computer Science

Date

May 17, 2024

Type

pdf

Pages

17

Uploaded by ConstableSteel14439 on coursehero.com

hw06 May 16, 2024 [1]: # Initialize Otter import otter grader = otter . Notebook( "hw06.ipynb" ) 1 Homework 6: Probability, Simulation, Estimation, and Assess- ing Models Please complete this notebook by filling in the cells provided. Before you begin, execute the previous cell to load the provided tests. Helpful Resource: - Python Reference : Cheat sheet of helpful array & table methods used in Data 8! Recommended Readings : * Randomness * Sampling and Empirical Distributions * Testing Hypotheses Please complete this notebook by filling in the cells provided. Before you begin, execute the following cell to setup the notebook by importing some helpful libraries. Each time you start your server, you will need to execute this cell again. For all problems that you must write explanations and sentences for, you must provide your answer in the designated space. Moreover, throughout this homework and all future ones, please be sure to not re-assign variables throughout the notebook! For example, if you use max_temperature in your answer to one question, do not reassign it later on. Otherwise, you will fail tests that you thought you were passing previously! Note: This homework has hidden tests on it. That means even though the tests may say 100% passed, it doesn’t mean your final grade will be 100%. We will be running more tests for correctness once everyone turns in the homework. Directly sharing answers is not okay, but discussing problems with the course staff or with other students is encouraged. You should start early so that you have time to get help if you’re stuck. 1
1.1 1. Roulette [2]: # Run this cell to set up the notebook, but please don't change it. # These lines import the Numpy and Datascience modules. import numpy as np from datascience import * import d8error # These lines do some fancy plotting magic. import matplotlib % matplotlib inline import matplotlib.pyplot as plt plt . style . use( 'fivethirtyeight' ) import warnings warnings . simplefilter( 'ignore' , FutureWarning ) A Nevada roulette wheel has 38 pockets and a small ball that rests on the wheel. When the wheel is spun, the ball comes to rest in one of the 38 pockets. That pocket is declared the winner. The pockets are labeled 0, 00, 1, 2, 3, 4, … , 36. Pockets 0 and 00 are green, and the other pockets are alternately red and black. The table wheel is a representation of a Nevada roulette wheel. Note that both columns consist of strings. Below is an example of a roulette wheel! Run the cell below to load the wheel table. [3]: wheel = Table . read_table( 'roulette_wheel.csv' , dtype = str ) wheel [3]: Pocket | Color 00 | green 0 | green 1 | red 2 | black 3 | red 4 | black 5 | red 6 | black 7 | red 8 | black … (28 rows omitted) 1.1.1 Betting on Red If you bet on red , you are betting that the winning pocket will be red. This bet pays 1 to 1 . That means if you place a one-dollar bet on red, then: • If the winning pocket is red, you gain 1 dollar. That is, you get your original dollar back, plus one more dollar. • if the winning pocket is not red, you lose your dollar. In other words, you gain -1 dollars. 2
Let’s see if you can make money by betting on red at roulette. Question 1. Define a function dollar_bet_on_red that takes the name of a color and returns your gain in dollars if that color had won and you had placed a one-dollar bet on red. Remember that the gain can be negative. Make sure your function returns an integer. (4 points) Note: You can assume that the only colors that will be passed as arguments are red, black, and green. Your function doesn’t have to check that. [10]: def dollar_bet_on_red (color): if color == "red" : return 1 else : return -1 [11]: grader . check( "q1_1" ) [11]: q1_1 results: All test cases passed! Run the cell below to make sure your function is working. [12]: print (dollar_bet_on_red( 'green' )) print (dollar_bet_on_red( 'black' )) print (dollar_bet_on_red( 'red' )) -1 -1 1 Question 2. Add a column labeled Winnings: Red to the table wheel . For each pocket, the column should contain your gain in dollars if that pocket won and you had bet one dollar on red. Your code should use the function dollar_bet_on_red . (4 points) [14]: red_winnings = wheel . apply(dollar_bet_on_red, "Color" ) wheel = wheel . with_column( "Winnings: Red" ,red_winnings) wheel [14]: Pocket | Color | Winnings: Red 00 | green | -1 0 | green | -1 1 | red | 1 2 | black | -1 3 | red | 1 4 | black | -1 5 | red | 1 6 | black | -1 7 | red | 1 8 | black | -1 … (28 rows omitted) 3
[15]: grader . check( "q1_2" ) [15]: q1_2 results: All test cases passed! 1.1.2 Simulating 10 Bets on Red Roulette wheels are set up so that each time they are spun, the winning pocket is equally likely to be any of the 38 pockets regardless of the results of all other spins. Let’s see what would happen if we decided to bet one dollar on red each round. Question 3. Create a table ten_bets by sampling the table wheel to simulate 10 spins of the roulette wheel. Your table should have the same three column labels as in wheel . Once you’ve created that table, set sum_bets to your net gain in all 10 bets, assuming that you bet one dollar on red each time. (4 points) Hint: It may be helpful to print out ten_bets after you create it! [21]: ten_bets = wheel . sample( 10 ) sum_bets = sum (ten_bets . column( 'Winnings: Red' )) sum_bets [21]: 4 [22]: grader . check( "q1_3" ) [22]: q1_3 results: All test cases passed! Run the cells above a few times to see how much money you would make if you made 10 one-dollar bets on red. Making a negative amount of money doesn’t feel good, but it is a reality in gambling. Casinos are a business, and they make money when gamblers lose. Question 4. Let’s see what would happen if you made more bets. Define a function net_gain_red that takes the number of bets and returns the net gain in that number of one-dollar bets on red. (4 points) Hint: You should use your wheel table within your function. [24]: def net_gain_red (number_bets): bets_on_red = wheel . sample(number_bets) net_gain = sum (bets_on_red . column( "Winnings: Red" )) return net_gain [25]: grader . check( "q1_4" ) [25]: q1_4 results: All test cases passed! Run the cell below a few times to make sure that the results are similar to those you observed in the previous exercise. [28]: net_gain_red( 10 ) 4
[28]: 0 Question 5. Complete the cell below to simulate the net gain in 200 one-dollar bets on red, repeating the process 10,000 times. After the cell is run, all_gains_red should be an array with 10,000 entries, each of which is the net gain in 200 one-dollar bets on red. (4 points) Hint: Think about which computational tool might be helpful for simulating a process multiple times. Lab 5 might be a good resource to look at! Note: This cell might take a few seconds to run. [33]: num_bets = 200 repetitions = 10000 all_gains_red = make_array() for i in np . arange(repetitions): sum_red_gain = net_gain_red(num_bets) all_gains_red = np . append(all_gains_red,sum_red_gain) len (all_gains_red) # Do not change this line! Check that all_gains_red is length 10000. [33]: 10000 [34]: grader . check( "q1_5" ) [34]: q1_5 results: All test cases passed! Run the cell below to visualize the results of your simulation. [35]: gains = Table() . with_columns( 'Net Gain on Red' , all_gains_red) gains . hist(bins = np . arange( -80 , 41 , 4 )) 5
Question 6: Using the histogram above, decide whether the following statement is true or false: If you make 200 one-dollar bets on red, your chance of losing money is more than 50%. Assign loss_more_than_50 to either True or False depending on your answer to the question. (4 points) [37]: loss_more_than_50 = True [38]: grader . check( "q1_6" ) [38]: q1_6 results: All test cases passed! 1.1.3 Betting on a Split If betting on red doesn’t seem like a good idea, maybe a gambler might want to try a different bet. A bet on a split is a bet on two consecutive numbers such as 5 and 6. This bets pays 17 to 1. That means if you place a one-dollar bet on the split 5 and 6, then: • If the winning pocket is either 5 or 6, your gain is 17 dollars. • If any other pocket wins, you lose your dollar, so your gain is -1 dollars. Question 7. Define a function dollar_bet_on_split that takes a pocket number and returns your gain in dollars if that pocket won and you had bet one dollar on the 5-6 split. (4 points) Hint: Remember that the pockets are represented as strings. 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help