2018/10/4 hw04
http://localhost:8888/nbconvert/html/CS237/hw04.ipynb?download=false 1/7
CS 237 Fall 2018, Homework 04
Due date: Thursday October 4th at 11:59 pm (10% off if up to 24 hours late) via
Gradescope
General Instructions
Please complete this notebook by filling in solutions where indicated. Be sure to "Run All" from the Cell
menu before submitting.
You may use ordinary ASCII text to write your solutions, or (preferably) Latex. A nice introduction to Latex in
Jupyter notebooks may be found here: http://data-blog.udacity.com/posts/2016/10/latex-primer/
(http://data-blog.udacity.com/posts/2016/10/latex-primer/)
As with previous homeworks, just upload a PDF file of this notebook. Instructions for converting to PDF may
be found on the class web page right under the link for homework 1.
2018/10/4 hw04
http://localhost:8888/nbconvert/html/CS237/hw04.ipynb?download=false 2/7
In?[103]:
# Useful imports and definitions for CS 237
import numpy as np # arrays and functions which operate on array
from numpy import linspace, arange
import matplotlib.pyplot as plt # normal plotting
#import seaborn as sns # Fancy plotting
#import pandas as pd # Data input and manipulation
from random import random, randint, uniform, choice, sample, shuffle, seed
from collections import Counter
%matplotlib inline
# Calculating permutations and combinations efficiently
def P(N,K):
res = 1
for i in range(K):
res *= N
N = N - 1
return res
def C(N,K):
if(K < N/2):
K = N-K
X = [1]*(K+1)
for row in range(1,N-K+1):
X[row] *= 2
for col in range(row+1,K+1):
X[col] = X[col]+X[col-1]
return X[K]
# Round to 4 decimal places for printing numeric answers.
def round4(x):
return round(x+0.00000000001,4)
def round4_list(L):
return [ round4(x) for x in L]
# This function takes a list of outcomes and a list of probabilities and
# draws a chart of the probability distribution.
def draw_distribution(Rx, fx, title='Probability Distribution for X'):
plt.bar(Rx,fx,width=1.0,edgecolor='black')
plt.ylabel("Probability")
plt.xlabel("Outcomes")
if (Rx[-1] - Rx[0] < 30):
ticks = range(Rx[0],Rx[-1]+1)
plt.xticks(ticks, ticks)
plt.title(title)
plt.show()
2018/10/4 hw04
http://localhost:8888/nbconvert/html/CS237/hw04.ipynb?download=false 3/7
HW 04 Instructions
The first few problems ask you to "describe" a random variable, which means:
1. Give the sample space (the result of the random experiment, from which the output of the random
variable is calculated);
2. Give (you may schematize it if it is very complicated or infinite);
3. Give (you may use fractions or decimals) and show how it was calculated, unless it is very
simple;
4. Draw a probability distribution, using the function provided draw_distribution in the previous
cell.
Feel free to calculate the solutions using Python, using the code in the previous cell. Or use Wolfram
Alpha....
Problem Zero (Example)
Describe the random variable X = "the number of heads showing on 2 flipped fair coins"
Solution:
= { HH, HT, TH, TT }
= { 0, 1, 2 }
= { 1/4, 1/2, 1/4 } # in this case no work is shown because it is simple
In?[104]:
draw_distribution( [0,1,2],[ 1/4, 1/2, 1/4 ] )
fX
2018/10/4 hw04
http://localhost:8888/nbconvert/html/CS237/hw04.ipynb?download=false 4/7
Problem One
Let = "the number of spades in a random 5-card poker hand."
Describe the random variable .
Show how you calculated the probabilities, using Python preferably.
Solution: = { HH, HT, TH, TT }
Problem Two
Suppose we have a sack with red balls and black balls, and we draw balls without replacement until a
red ball is drawn. Let = "the number of balls drawn".
Describe the random variable .
Show how you calculated the probabilities.
Solution:
Problem Three
From an urn that contains five red, five white, and five blue chips, we draw two chips at random and without
replacement. For each blue chip we win $1, for each white chip we win $2, but for each red chip we lose $3.
Describe the random variable for this random experiment.
Solution:
Problem Four
Consider the random variable from Problem Zero.
Describe the random variable
You do not need to give the sample space S.
For reference:
= { 0, 1, 2 }
= { 1/4, 1/2, 1/4 }
Solution:
Z = (X ? 1)
fX
2018/10/4 hw04
http://localhost:8888/nbconvert/html/CS237/hw04.ipynb?download=false 5/7
Problem Five
Again, we refer to the random variable from Problem Zero.
Describe the random variable
You do not need to give the sample space S.
Hint: when more than one instance of a random variable is involved, it is often useful to draw a matrix of all
possibilities. Consider the two random variables and .
Solution:
Problem Six
This problem refers to the random variable from Problem One.
(a) Calculate :
(b) Calculate and .
You need to use Python to do the calculations (do the computations in this notebook and show your code);
use the code for C(N,K) at the top of this homework. Round to 4 decimal places only for printing the
results, using the functions round4(...) and round4_list(...) given above.
Solution:
Problem Seven
An urn contains five balls, two of which are marked $1, two $5, and one $15. The game is played as follows:
You pay me $10 to select two balls at random (without replacement) from the urn, at which point I pay you
the sum of the amounts marked on the two balls. </p>
(a) Is this a fair game (meaning, is the cost of each turn equal to the expected payout)? Be precise and show
all work.
(b) If your answer to (a) is "no," what should I charge for each turn to make it a fair game?
Solution:
XZ = 2X + X - 1
(2X -1) X
Var(X) σX
2018/10/4 hw04
http://localhost:8888/nbconvert/html/CS237/hw04.ipynb?download=false 6/7
Problem Eight
Wayne is interested in two games, Keno and Bolita. To play Bolita, he buys a ticket for $1 marked with a
number 1 .. 100, and one ball is drawn randomly from a collection marked with the numbers 1, ..., 100. If his
ticket number matches the number on the drawn ball, he wins $75; otherwise he gets nothing and loses his
$1 bet. To play Keno, he buys a ticket marked with the numbers 1 .. 4 and there are only 4 balls, marked 1,
..., 4; again he wins if the ticket matches the ball drawn; if he wins he gets $3; otherwise he again gets
nothing and loses his bet.
(a) What is the expected payout (expected value of net profit after buying ticket and possibly winning
something) for each of these games?
(b) What is the variance and standard deviation for each of these games?
(c) If he decides to play one (and only one) of these games for a very long time, which one should he
choose? If he decides to try one of these games for a couple of times, just for fun, which one should he
choose?
Solution:
Problem Nine
Mr. Norton owns two appliance stores. In store A the number of TV sets sold by a salesperson is, on
average, 13 per week with a standard deviation of 5. In store B the number of TV sets sold by a salesperson
is, on average, 7 with a standard deviation of 4. Mr. Norton has a position open for a person to sell TV sets.
There are two applicants. Mr. Norton asks one of them to work in store A and the other in store B, each for
one week. The salesperson in store A sold 10 sets, and the salesperson in store B sold 6 sets.
(a) Based on this information, which person should Mr. Norton hire?
(b) Suppose the person not hired asks Mr. Norton "Well, how many would I have had to sell to be exactly as
good as the person you hired?" What should Mr. Norton say? (Not quite realistic, because the answer may
not be an integer, but work with me here....)
Hint: consider standardized random varibles.
Solution:
2018/10/4 hw04
http://localhost:8888/nbconvert/html/CS237/hw04.ipynb?download=false 7/7
Problem Ten
Suppose you are playing a game with a friend in which you bet dollars on the flip of a fair coin: if the coin
lands tails you lose your dollar bet, but if it lands heads, you get dollars back (i.e., you get your
dollars back plus you win dollars).
Let = "the amount you gain or lose."
(a) What is the expected return on this game? (Give your answer in terms of .)
Now, after losing a bunch of times, suppose you decide to improve your chances with the following strategy:
you will start by betting , and if you lose, you will double your bet the next time, and you will keep playing
until you win (the coin has to land heads sometime!).
Let = "the amount you gain or lose with this strategy".
(b) What is the expected return with this strategy? (Hint: think about what happens for each of the
cases of it taking flips until you win).
(c) Hm ... do you see any problem with this strategy? How much money would you have to start with to
guarantee that you always win?
(d) Suppose when you apply this strategy, you start with and you quit the game when you run out of
money. Now what is ?
(e) (Optional) What does this tell you about "stategies" for winning at gambling??
Solution:
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。