Last active
March 25, 2018 00:26
-
-
Save arnaudbos/59ab26bb6958f07fbc78ef3661572773 to your computer and use it in GitHub Desktop.
Inclusion–Exclusion Principle for probabilities in Clojure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require '[clojure.math.combinatorics :refer [combinations]]) | |
| (defn ∩-prob | |
| [a & more] | |
| (reduce * a more)) | |
| (defn ∪-prob [a b & more] | |
| "https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle#In_probability" | |
| (let [ps (concat [a b] more) | |
| n (count ps)] | |
| (transduce (comp (map #(combinations ps %)) | |
| (map (fn [cs] | |
| (transduce | |
| (map #(apply ∩-prob %)) | |
| + | |
| cs))) | |
| (map-indexed #(* %2 (Math/pow -1 (+ 2 %1))))) | |
| + | |
| (range 1 (inc n)) | |
| ))) | |
| (∪-prob 0.7 0.5 0.3 0.2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment