p2.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. """ LPC stats HW2, Problem 2
  3. Author: Caleb Fangmeier
  4. Created: Sep. 27, 2017
  5. """
  6. from scipy.stats import beta, poisson
  7. from scipy.optimize import root
  8. # Declare the experimental results
  9. N = 25
  10. M = 353
  11. k = 37.6
  12. x = 1/(1+k)
  13. p = 0.683
  14. def DL(z):
  15. """
  16. This is just the DL function as defined in the problem statement. It is the
  17. cumulative distribution function for p(D|s).
  18. """
  19. num = 0
  20. den = 0
  21. for r in range(N+1):
  22. b = beta.pdf(x, r+1, M)
  23. num += b*poisson.cdf(N-r, z)
  24. den += b
  25. return num/den
  26. def DR(z):
  27. """
  28. Same as DL, but for the opposite side.
  29. """
  30. return 1 - DL(z)
  31. # Scipy's root function finds a zero crossing for the given function.
  32. # The second argument is just the starting point for the algorithm.
  33. root_DL = root(lambda z: DL(z) - (1-p)/2, 10).x[0]
  34. root_DR = root(lambda z: DR(z) - (1-p)/2, 10).x[0]
  35. print(f"The interval is: [{root_DR:.2f}, {root_DL:.2f}]")
  36. # Output
  37. # > The interval is: [11.49, 21.68]