p4.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. """ LPC stats HW2, Problem 4
  3. Author: Caleb Fangmeier
  4. Created: Oct. 8, 2017
  5. """
  6. from scipy.stats import norm, uniform, cauchy, mode
  7. from numpy import logspace, zeros, max, min, mean, median, var
  8. import matplotlib.pyplot as plt
  9. distributions = {
  10. 'uniform': lambda a, N: uniform.rvs(loc=a-0.5, scale=1, size=N),
  11. 'gaussian': lambda a, N: norm.rvs(loc=a, scale=1, size=N),
  12. 'cauchy': lambda a, N: cauchy.rvs(loc=a, size=N)
  13. }
  14. estimators = {
  15. 'midrange': lambda xs: max(xs) - min(xs),
  16. 'mean': lambda xs: mean(xs),
  17. 'median': lambda xs: median(xs),
  18. 'mode': lambda xs: mode(xs).mode[0]
  19. }
  20. def var_of_est(dist_name, est_name, N, a=1):
  21. M = 500
  22. estimates = zeros(M)
  23. for i in range(M): # run M experiments to estimate variance
  24. data = distributions[dist_name](a, N)
  25. estimates[i] = estimators[est_name](data)
  26. return var(estimates)
  27. plt.figure()
  28. for i, distribution in enumerate(distributions):
  29. plt.subplot(2, 2, i+1)
  30. for estimator in estimators:
  31. Ns = logspace(1, 3, 30, dtype=int)
  32. vars = zeros(30)
  33. for i, N in enumerate(Ns):
  34. vars[i] = var_of_est(distribution, estimator, N)
  35. plt.plot(Ns, vars, label=estimator)
  36. plt.title(distribution)
  37. plt.xlabel('N')
  38. plt.ylabel(r'$\sigma^2$')
  39. plt.xscale('log')
  40. plt.yscale('log')
  41. plt.tight_layout()
  42. plt.legend()
  43. plt.show()