Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. def pp_plot(x, dist, line=True, ax=None):
  2. '''
  3. Function for comparing empirical data to a theoretical distribution by using a P-P plot.
  4.  
  5. Params:
  6. x - empirical data
  7. dist - distribution object from scipy.stats; for example scipy.stats.norm(0, 1)
  8. line - boolean; specify if the reference line (y=x) should be drawn on the plot
  9. ax - specified ax for subplots, None is standalone
  10. '''
  11. if ax is None:
  12. ax = plt.figure().add_subplot(1, 1, 1)
  13.  
  14. n = len(x)
  15. p = np.arange(1, n + 1) / n - 0.5 / n
  16. pp = np.sort(dist.cdf(x))
  17. sns.scatterplot(x=p, y=pp, color='blue', edgecolor='blue', ax=ax)
  18. ax.set_title('PP-plot')
  19. ax.set_xlabel('Theoretical Probabilities')
  20. ax.set_ylabel('Sample Probabilities')
  21. ax.margins(x=0, y=0)
  22.  
  23. if line: plt.plot(np.linspace(0, 1), np.linspace(0, 1), 'r', lw=2)
  24.  
  25. return ax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement