-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathplotting-the-binomial-pmf.py
More file actions
63 lines (46 loc) · 1.81 KB
/
plotting-the-binomial-pmf.py
File metadata and controls
63 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
Plotting the Binomial PMF
100xp
As mentioned in the video, plotting a nice looking PMF requires a bit of matplotlib
trickery that we will not go into here. Instead, we will plot the PMF of the Binomial
distribution as a histogram with skills you have already learned. The trick is setting
up the edges of the bins to pass to plt.hist() via the bins keyword argument. We want
the bins centered on the integers. So, the edges of the bins should be -0.5, 0.5, 1.5,
2.5, ... up to max(n_defaults) + 1.5. You can generate an array like this using np.arange()
and then subtracting 0.5 from the array.
You have already sampled out of the Binomial distribution during your exercises on loan
defaults, and the resulting samples are in the NumPy array n_defaults.
Instructions
-Using np.arange(), compute the bin edges such that the bins are centered on the integers.
Store the resulting array in the variable bins.
-Use plt.hist() to plot the histogram of n_defaults with the normed=True and bins=bins
keyword arguments.
-Leave a 2% margin and label your axes.
-Show the plot.
'''
import numpy as np
import matplotlib.pyplot as plt
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) / n
return x, y
# Seed random number generator
np.random.seed(42)
# Take 10,000 samples out of the binomial distribution: n_defaults
n_defaults = np.random.binomial(n=100, p=0.05, size=10000)
# Compute bin edges: bins
bins = np.arange(min(n_defaults), max(n_defaults) + 1.5) - 0.5
# Generate histogram
_ = plt.hist(n_defaults, normed=True, bins=bins)
# Set margins
_ = plt.margins(0.02)
# Label axes
_ = plt.xlabel('x')
_ = plt.ylabel('y')
# Show the plot
plt.show()