def perform_bernoulli_trials(n, p):
"""Perform n Bernoulli trials with success probability p
and return number of successes."""
# Initialize number of successes: n_success
n_success = 0
# Perform trials
for i in range(n):
# Choose random number between zero and one: random_number
random_number=np.random.random()
# If less than p, it's a success so add one to n_success
if random_number<p:
n_success+=1
return n_success
贷款违约率的模拟
# Seed random number generator
np.random.seed(42)
# Initialize the number of defaults: n_defaults
n_defaults=np.empty(1000)
# Compute the number of defaults
for i in range(1000):
n_defaults[i] =perform_bernoulli_trials(100,0.05)
# Plot the histogram with default number of bins; label your axes
_ = plt.hist(n_defaults,normed=True)
_ = plt.xlabel('number of defaults out of 100 loans')
_ = plt.ylabel('probability')
# Show the plot
plt.show()
ecdf曲线:
# Compute ECDF: x, y
x,y=ecdf(n_defaults)
# Plot the ECDF with labeled axes
plt.plot(x,y,marker='.',linestyle='none')
plt.xlabel('x')
plt.ylabel('y')
# Show the plot
plt.show()
# Compute the number of 100-loan simulations with 10 or more defaults: n_lose_money
n_lose_money=np.sum(n_defaults>=10)
# Compute and print probability of losing money
print('Probability of losing money =', n_lose_money / len(n_defaults))
另一种简便的方法:
# Take 10,000 samples out of the binomial distribution: n_defaults
n_defaults=np.random.binomial(n=100,p=0.05,size=10000)
# Compute CDF: x, y
x,y=ecdf(n_defaults)
# Plot the CDF with axis labels
plt.plot(x,y,marker = '.',linestyle = 'none')
plt.xlabel('he number of defaults out of 100 loans,')
plt.ylabel('CDF')
# Show the plot
plt.show()
画直方图,自定义bins
# Compute bin edges: bins
bins = np.arange(0, max(n_defaults) + 1.5) - 0.5
# Generate histogram
plt.hist(n_defaults,bins=bins,normed=True)
# Set margins
plt.margins(0.02)
# Label axes
plt.xlabel('违约')
plt.ylabel('数目')
# Show the plot
plt.show()