Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. from math import factorial,exp,sqrt
  2.  
  3. def permutation(n,r):
  4. """returns nPr"""
  5. return factorial(n)/factorial(n-r)
  6.  
  7. def combination(n,r):
  8. """Returns nCr"""
  9. return factorial(n)/(factorial(r)*factorial(n-r))
  10.  
  11. def binomial_distribution(x,n,p):
  12. """
  13. Computes the probability of having x successes out of n trials.
  14. Each trial has a probability of success p
  15. """
  16. nCx = combination(n,x)
  17. q = 1-p
  18. return nCx*(p**x)*(q**(n-x))
  19.  
  20. def geometric_distribution(n,p):
  21. """
  22. Computes the probability of 1st success on the nth trial.
  23. Each trial has a probability of success p
  24. """
  25. q = 1 - p
  26. return p*q**(n-1)
  27.  
  28. def poisson(k,l):
  29. """
  30. Computes the probability of k successes.
  31. The average number of successes is l
  32. """
  33. return (l**k)*exp(-l)/factorial(k)
  34.  
  35. def erf(x):
  36. t = 1 / (1 + 0.5*abs(x))
  37. tau = t*exp( -x**2 - 1.26551223 + 1.00002368*t + 0.37409196*t**2 + 0.09678418*t**3
  38. - 0.18628806*t**4 + 0.27886807*t**5 -1.13520398*t**6 + 1.48851587*t**7
  39. - 0.82215223*t**8 + 0.17087277*t**9)
  40. if x < 0:
  41. return tau - 1
  42. else:
  43. return 1 - tau
  44.  
  45. def norm(x):
  46. """
  47. Computes the probability of X <= x in the normal
  48. distribution.
  49. """
  50. return 0.5*(1 + erf(x/sqrt(2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement