In [2]:
from pylab import *
In [3]:
#create the sample space
ss=[(i,j) for i in range(1,7) for j in range(1,7)]
print(ss)
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
In [4]:
#create the list of values of the random variable corresponding to the list of outcomes
X=[sum(outcome) for outcome in ss]
print(X)
[2, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 11, 7, 8, 9, 10, 11, 12]
In [5]:
#create a histogram giving the   frequencies of each value of the sum.  These are the values of the PMF of X
h=histogram(X,bins=arange(1.5,13.5))
print(h)
(array([1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]), array([ 1.5,  2.5,  3.5,  4.5,  5.5,  6.5,  7.5,  8.5,  9.5, 10.5, 11.5,
       12.5]))
In [5]:
#Plot the histogram, using relative frequencies as the y-values, and the possible values 2...12 as the x-values.
#This is the PMF of the random variable. 
stem(range(2,13),h[0]/36,markerfmt='ro',linefmt='k-')
show()
In [6]:
#compute the CDF
pmf=h[0]/36
cdf=cumsum(pmf)
print(cdf)
[0.02777778 0.08333333 0.16666667 0.27777778 0.41666667 0.58333333
 0.72222222 0.83333333 0.91666667 0.97222222 1.        ]
In [22]:
#plot the CDF.  I added a few decorations to the demo shown in class, so as to show
#the values at for x<2 and open circles at endpoints of intervals.
for j in range(2,13):
    plot([j,j+1],[cdf[j-2],cdf[j-2]],color='k')
plot([0,2],[0,0],color='k')
plot([13,15],[1,1],color='k')
xlim(0,15)
scatter([range(3,13)],cdf[:-1],marker='o',s=15,edgecolor='k',facecolors='w')
scatter([2],[0],marker='o',edgecolor='k',s=15,facecolors='w')
show()
In [19]:
#simulate a roll of two dice using the CDF
def simroll():
    return 2+searchsorted(cdf,random())
In [20]:
#Run the simulation a thousand times
samples=[simroll() for j in range(1000)]
#compute the relative frequencies as above, with a histogram
h=histogram(samples,bins=arange(1.5,13.5))
#plot side by side with PMF
stem(range(2,13),h[0]/1000,markerfmt='ro',linefmt='k-')
stem(arange(2.2,13.2,1),pmf,markerfmt='ko',linefmt='r-') 
show()