calc-ci 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env nix-shell
  2. #!nix-shell -i python3 -p python3 python3Packages.pandas python3Packages.numpy python3Packages.scipy python3Packages.pyarrow
  3. # coding: utf-8
  4. from scipy.integrate import quad
  5. import pandas as pd
  6. import numpy as np
  7. from pathlib import Path
  8. def normalProbabilityDensity(x):
  9. constant = 1.0 / np.sqrt(2*np.pi)
  10. return (constant * np.exp((-x**2) / 2.0))
  11. home = Path.home()
  12. snt_path = Path(home, ".standard-normal.csv")
  13. if snt_path.exists():
  14. snt = pd.read_csv(snt_path, index_col=0)
  15. else:
  16. snt = pd.DataFrame(data = [],
  17. index = np.round(np.arange(0, 3.5, 0.1), 2),
  18. columns = np.round(np.arange(0.00, 0.1, 0.01), 2))
  19. for index in snt.index:
  20. for column in snt.columns:
  21. z = np.round(index + column, 2)
  22. value, _ = quad(normalProbabilityDensity, np.NINF, z)
  23. value = np.round(value - 0.5, 4)
  24. snt.loc[index,column] = value
  25. snt.to_csv(snt_path)
  26. def find_z(p):
  27. posns = []
  28. res = snt.isin([p])
  29. srs = res.any()
  30. cols = list(srs[srs == True].index)
  31. for col in cols:
  32. rows = list(res[col][res[col] == True].index)
  33. for row in rows:
  34. posns.append(float(col) + row)
  35. return np.max(posns)
  36. mean = float(input("Sample Mean: "))
  37. std = float(input("Standard Deviation: "))
  38. N = int(input("Sample Size: "))
  39. alpha = float(input("Alpha [0, 1): "))
  40. alpha_2 = alpha / 2
  41. Z_alpha_2 = find_z(alpha_2)
  42. plus_minus = np.round(Z_alpha_2 * (std / np.sqrt(N)), 4)
  43. print(f"{mean} +/- {plus_minus}")
  44. print(f"[{mean - plus_minus}, {mean + plus_minus}]")
  45. ### Local Variables:
  46. ### mode: python
  47. ### End: