probability.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (library (probability (0 0 1))
  2. (export factorial
  3. n-choose-k
  4. probability-choose-k-out-of-n
  5. probability-min-choose-k-out-of-n
  6. probability-max-choose-k-out-of-n)
  7. (import
  8. (except (rnrs base) let-values)
  9. (only (guile)
  10. ;; lambda forms
  11. lambda* λ)
  12. (prefix (srfi srfi-1) srfi-1:)))
  13. (define factorial
  14. (λ (n)
  15. "Calculate the factorial of the given number n."
  16. (define iter
  17. (λ (n product)
  18. (cond
  19. [(< n 2) product]
  20. [else
  21. (iter (- n 1) (* n product))])))
  22. (iter n 1)))
  23. (define n-choose-k
  24. (λ (n k)
  25. "Calculate the number of possible ways to choose k out
  26. of n objects."
  27. (/ (factorial n)
  28. (* (factorial (- n k))
  29. (factorial k)))))
  30. (define probability-choose-k-out-of-n
  31. (lambda* (n k #:key (k-probability (/ 1 2)))
  32. "Calculate the probability to choose exactly K objects
  33. of one specific type T out of N objects. The probability to
  34. choose one object of type T is by default one half. It can
  35. optionally be given as keyword argument K-PROBABILITY. The
  36. calculation is done assuming, that the probability of
  37. choosing an object of type T does not change, for example by
  38. assuming, that chosen objects are put back into the pool of
  39. objects, from which objects are chosen.
  40. Another way of looking at this function is the following:
  41. 'Given a scenario, in which one chooses N items from an
  42. inexhaustable source, where the probability of choosing an
  43. item of type T is K-PROBABILITY, what is the chance of
  44. choosing K items of type T?'
  45. Example scenario: Roll a die N times, having a K-PROBABILITY
  46. of 1/2 to roll an even number, what is the probability of
  47. rolling K even numbers? -- The die is an inexhaustable
  48. source of numbers from 1 to 6, the probability of rolling an
  49. even number never changes."
  50. (* (n-choose-k n k)
  51. (expt k-probability k)
  52. (expt (- 1 k-probability) (- n k)))))
  53. (define probability-min-choose-k-out-of-n
  54. (lambda* (n k #:key (k-probability (/ 1 2)))
  55. "Calculate the probability to choose at least k objects
  56. out of n, optionally with given chance to pick a k object
  57. out of the n objects."
  58. (let next-k ([k k] [sum 0])
  59. (cond
  60. [(< k n)
  61. (next-k (+ k 1)
  62. (+ sum
  63. (probability-choose-k-out-of-n n k #:k-probability k-probability)))]
  64. [else
  65. (+ sum
  66. (expt k-probability n))]))))
  67. (define probability-max-choose-k-out-of-n
  68. (lambda* (n k #:key (k-probability (/ 1 2)))
  69. "Calculate the probability to choose at most k objects
  70. out of n, optionally with given chance to pick a k object
  71. out of the n objects."
  72. (let next-k ([k k] [sum 0])
  73. (cond
  74. [(> k 0)
  75. (next-k (- k 1)
  76. (+ sum
  77. (probability-choose-k-out-of-n n k #:k-probability k-probability)))]
  78. [else
  79. (+ sum
  80. (expt (- 1 k-probability) n))]))))