Advertisement
elena1234

emperical rule in Python

May 19th, 2022
677
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from statsmodels.distributions.empirical_distribution import ECDF
  2. import seaborn as sns
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. import numpy as np
  6. import random
  7. import warnings
  8. warnings.filterwarnings('ignore')
  9.  
  10. random.seed(1738)
  11.  
  12. mu = 7
  13. sigma = 1.7
  14. Observations = [random.normalvariate(mu, sigma) for _ in range(100000)]
  15.  
  16. sns.distplot(Observations)
  17. plt.axvline(np.mean(Observations) + np.std(Observations), color="g")
  18. plt.axvline(np.mean(Observations) - np.std(Observations), color="g")
  19.  
  20. plt.axvline(np.mean(Observations) + (np.std(Observations) * 2), color="y")
  21. plt.axvline(np.mean(Observations) - (np.std(Observations) * 2), color="y")
  22.  
  23.  
  24. pd.Series(Observations).describe()  # mean=7.001202; std=1.701952;
  25.  
  26.  
  27. SampleA = random.sample(Observations, 100)
  28. SampleB = random.sample(Observations, 100)
  29. SampleC = random.sample(Observations, 100)
  30.  
  31. fig, ax = plt.subplots()
  32. sns.distplot(SampleA, ax=ax)
  33. sns.distplot(SampleB, ax=ax)
  34. sns.distplot(SampleC, ax=ax)
  35.  
  36.  
  37. ecdf = ECDF(Observations)
  38. plt.plot(ecdf.x, ecdf.y)
  39. plt.axhline(y=0.025, color='y', linestyle='-')
  40. plt.axvline(x=np.mean(Observations) -
  41.             (2 * np.std(Observations)), color='y', linestyle='-')
  42. plt.axhline(y=0.975, color='y', linestyle='-')
  43. plt.axvline(x=np.mean(Observations) +
  44.             (2 * np.std(Observations)), color='y', linestyle='-')
  45.  
Advertisement
RAW Paste Data Copied
Advertisement