In [3]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()


The Normal PDF¶

In this exercise, you will explore the Normal PDF and also learn a way to plot a PDF of a known distribution using hacker statistics. Specifically, you will plot a Normal PDF for various values of the variance.

• Plot a histograms of each of the samples; for each, use 100 bins, also using the keyword arguments normed=True and histtype='step'. The latter keyword argument makes the plot look much like the smooth theoretical PDF. You will need to make 3 plt.hist() calls.
In [11]:
plt.figure(figsize=[16,4])
# Draw 100000 samples from Normal distribution with stds of interest: samples_std1, samples_std3, samples_std10
samples_std1 = np.random.normal(20, 1, size=100000)
samples_std3 = np.random.normal(20, 3, size=100000)
samples_std10 = np.random.normal(20, 10, size=100000)

# Make histograms
_ = plt.hist(samples_std1, bins=100, normed=True, histtype='step')
_ = plt.hist(samples_std3, bins=100, normed=True, histtype='step')
_ = plt.hist(samples_std10, bins=100, normed=True, histtype='step')

# Make a legend, set limits and show plot
_ = plt.legend(('std = 1', 'std = 3', 'std = 10'))
plt.ylim(-0.01, 0.42)
plt.show()

In [13]:
def ecdf(data):
"""Compute ECDF for a one-dimensional array of measurements."""

# Number of data points: n
n = len(data)

# x-data for the ECDF: x
x = np.sort(data)

# y-data for the ECDF: y
y = np.arange(1, n+1) / float(n)

return x, y


The Normal CDF¶

Now that you have a feel for how the Normal PDF looks, let's consider its CDF. Using the samples you generated in the last exercise (in your namespace as samples_std1, samples_std3, and samples_std10), generate and plot the CDFs

In [23]:
# Generate CDFs
x_std1, y_std1 = ecdf(samples_std1)
x_std3, y_std3 = ecdf(samples_std3)
x_std10, y_std10 = ecdf(samples_std10)

# Plot CDFs
_ = plt.plot(x_std1, y_std1, marker='.', linestyle='none')
_ = plt.plot(x_std3, y_std3, marker='.', linestyle='none')
_ = plt.plot(x_std10, y_std10, marker='.', linestyle='none')

# Make 2% margin
plt.margins(0.02)

# Make a legend and show the plot
_ = plt.legend(('std = 1', 'std = 3', 'std = 10'), loc='lower right')
plt.show()


Are the Belmont Stakes results Normally distributed?¶

Since 1926, the Belmont Stakes is a 1.5 mile-long race of 3-year old thoroughbred horses. Secretariat ran the fastest Belmont Stakes in history in 1973. While that was the fastest year, 1970 was the slowest because of unusually wet and sloppy conditions. With these two outliers removed from the data set, compute the mean and standard deviation of the Belmont winners' times. Sample out of a Normal distribution with this mean and standard deviation using the np.random.normal() function and plot a CDF. Overlay the ECDF from the winning Belmont times. Are these close to Normally distributed?

Note: Justin scraped the data concerning the Belmont Stakes from the Belmont Wikipedia page.

https://en.wikipedia.org/wiki/Belmont_Stakes

In [52]:
import pandas as pd
df.shape

Out[52]:
(91, 8)
In [53]:
df.head()

Out[53]:
Year Winner Jockey Trainer Owner Time Track miles
0 2016 Creator Irad Ortiz, Jr Steve Asmussen WinStar Farm LLC 2:28.51 Belmont 1.5
1 2015 American Pharoah Victor Espinoza Bob Baffert Zayat Stables, LLC 2:26.65 Belmont 1.5
2 2014 Tonalist Joel Rosario Christophe Clement Robert S. Evans 2:28.52 Belmont 1.5
3 2013 Palace Malice Mike Smith Todd Pletcher Dogwood Stable 2:30.70 Belmont 1.5
4 2012 Union Rags John Velazquez Michael Matz Phyllis M. Wyeth 2:30.42 Belmont 1.5
In [57]:
df = df[df['Year'].apply(lambda x: x not in [1973, 1970])]
df.shape

Out[57]:
(89, 8)
In [58]:
df['length'] = df['Time'].apply(lambda x: int(x.split(':')[0])*60+float(x.split(':')[1]))

Out[58]:
0    148.51
1    146.65
2    148.52
3    150.70
4    150.42
Name: length, dtype: float64
In [59]:
df['length'].hist(bins=50, normed=True, histtype='step')
plt.show()

In [60]:
belmont_no_outliers = df['length']

# Compute mean and standard deviation: mu, sigma
mu = np.mean(belmont_no_outliers)
sigma = np.std(belmont_no_outliers)

# Sample out of a normal distribution with this mu and sigma: samples
samples = np.random.normal(mu, sigma, size=10000)

# Get the CDF of the samples and of the data
x_theor, y_theor = ecdf(samples)
x, y = ecdf(belmont_no_outliers)

# Plot the CDFs and show the plot
_ = plt.plot(x_theor, y_theor)
_ = plt.plot(x, y, marker='.', linestyle='none')
plt.margins(0.02)
_ = plt.xlabel('Belmont winning time (sec.)')
_ = plt.ylabel('CDF')
plt.show()


What are the chances of a horse matching or beating Secretariat's record?¶

Assume that the Belmont winners' times are Normally distributed (with the 1970 and 1973 years removed), what is the probability that the winner of a given Belmont Stakes will run it as fast or faster than Secretariat?

In [62]:
# Take a million samples out of the Normal distribution: samples
samples = np.random.normal(mu, sigma, size=1000000)

# Compute the fraction that are faster than 144 seconds: prob
prob = np.sum(samples <= 144) / float(len(samples))

# Print the result
print('Probability of besting Secretariat:', prob)

('Probability of besting Secretariat:', 0.000621)


If you have a story, you can simulate it!¶

Sometimes, the story describing our probability distribution does not have a named distribution to go along with it. In these cases, fear not! You can always simulate it. We'll do that in this and the next exercise.

In earlier exercises, we looked at the rare event of no-hitters in Major League Baseball. Hitting the cycle is another rare baseball event. When a batter hits the cycle, he gets all four kinds of hits, a single, double, triple, and home run, in a single game. Like no-hitters, this can be modeled as a Poisson process, so the time between hits of the cycle are also Exponentially distributed.

How long must we wait to see both a no-hitter and then a batter hit the cycle? The idea is that we have to wait some time for the no-hitter, and then after the no-hitter, we have to wait for hitting the cycle. Stated another way, what is the total waiting time for the arrival of two different Poisson processes? The total waiting time is the time waited for the no-hitter, plus the time waited for the hitting the cycle.

Now, you will write a function to sample out of the distribution described by this story.

• Define a function with call signature successive_poisson(tau1, tau2, size=1) that samples the waiting time for a no-hitter and a hit of the cycle.
• Draw waiting times tau1 (size number of samples) for the no-hitter out of an exponential distribution and assign to t1.
• Draw waiting times tau2 (size number of samples) for hitting the cycle out of an exponential distribution and assign to t2.
• The function returns the sum of the waiting times for the two events.
In [66]:
def successive_poisson(tau1, tau2, size=1):
# Draw samples out of first exponential distribution: t1
t1 = np.random.exponential(tau1, size=size)

# Draw samples out of second exponential distribution: t2
t2 = np.random.exponential(tau2, size=size)

return t1 + t2

In [76]:
np.random.exponential(7, 10)

Out[76]:
array([ 3.69069988,  1.40491454,  1.04198783, 24.23334735,  3.11646929,
5.96149117,  3.51969571, 19.09627262,  0.66779174,  0.07672795])

Distribution of no-hitters and cycles¶

Now, you'll use your sampling function to compute the waiting time to observe a no-hitter and hitting of the cycle. The mean waiting time for a no-hitter is 764 games, and the mean waiting time for hitting the cycle is 715 games.

In [77]:
# Draw samples of waiting times
waiting_times = successive_poisson(764, 715, size=100000)

# Make the histogram
_ = plt.hist(waiting_times, bins=100, histtype='step',
normed=True)

# Label axes
_ = plt.xlabel('total waiting time (games)')
_ = plt.ylabel('PDF')

# Show the plot
plt.show()