Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def get_exp_dist(mbt = 7, size = 10_000):
  5. """
  6. returns array of booking times
  7. """
  8.  
  9. np.random.seed(1)
  10. bookings = np.random.exponential(mbt+1, size)
  11.  
  12. return bookings
  13.  
  14. # ---------------------------
  15.  
  16. mbt=7
  17. bookings = get_exp_dist(mbt=mbt)
  18. prob, x = np.histogram(bookings, bins=100)
  19. prob = np.divide(prob, prob.sum())
  20. x = x[1:]
  21. idx = np.sum(x<=mbt)
  22.  
  23. plt.figure(figsize=(8,5))
  24. plt.title(f'Exponential | mbt = {int(bookings.mean())}', size =14)
  25.  
  26. # Plot the stuff
  27. plt.bar(x, prob, width= x[1]-x[0],color='#2377ae',alpha=0.80, label = 'PDF')
  28. plt.fill_between(x,prob,0,where=x<14, label=f'+/- {mbt} days')
  29.  
  30. # Add annotations + labels
  31. plt.vlines(x[idx],0,prob[idx], colors='black', linestyles='--', linewidth=2.0, alpha=0.65, label = 'mean')
  32. plt.xlabel('Booking Time')
  33. plt.ylabel('Probability')
  34. plt.legend()
  35.  
  36. plt.show()
  37.  
  38. # ---------------------------
  39.  
  40. cum_prob = np.cumsum(prob[::-1])[::-1]
  41.  
  42. plt.figure(figsize=(8,5))
  43. plt.title(f'Exponential – Cumulative | mbt = {mbt}', size =14)
  44. plt.plot(x, cum_prob, color='#2377ae', label = 'CDF', linewidth=3)
  45.  
  46. idx = np.sum(cum_prob>=0.5)
  47. plt.vlines(x[idx], 0, cum_prob[idx], colors='black', linestyles='--', linewidth=2.5, alpha=0.65, label = 'median')
  48. plt.hlines(cum_prob[idx], x[0], x[idx],colors='black', linestyles='--', linewidth=2.5, alpha=0.65)
  49.  
  50. plt.fill_between(x, cum_prob, 0, alpha=0.60)
  51. plt.xlabel('Booking Time')
  52. plt.ylabel('Cumulative Probability')
  53. plt.legend()
  54. plt.savefig(f'output/exponential cumulative distribution mbt={mbt}.png', dpi=200)
  55. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement