├── .gitignore ├── project.clj ├── LICENSE ├── README.md └── src └── emcm ├── chapter2.clj └── chapter1.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject emcm "0.1.0-SNAPSHOT" 2 | :description "Clojure code of examples and problems in EMCM" 3 | :url "http://example.com/FIXME" 4 | :license {:name "MIT License" 5 | :url "https://opensource.org/licenses/MIT" 6 | :year 2017 7 | :key "mit"} 8 | :plugins [[lein-gorilla "0.4.0"]] 9 | :dependencies [[org.clojure/clojure "1.8.0"] 10 | [net.mikera/core.matrix "0.60.3"] 11 | [lein-gorilla "0.4.0"]]) 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tian Jun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Explorations-in-Monte-Carlo-Methods 2 | 3 | Here I will try to write most of the examples and problems in EMCM with clojure. 4 | 5 | ## License 6 | 7 | ``` 8 | MIT License 9 | 10 | Copyright (c) 2017 Tian Jun 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | ``` 30 | -------------------------------------------------------------------------------- /src/emcm/chapter2.clj: -------------------------------------------------------------------------------- 1 | ;; gorilla-repl.fileformat = 1 2 | 3 | ;; @@ 4 | (ns emcm.chapter1 5 | (:require [clojure.core.matrix.random :refer [sample-uniform]] 6 | [clojure.core.matrix :as m] 7 | [clojure.core.matrix.stats :refer [sum mean variance]] 8 | [gorilla-plot.core :refer [histogram list-plot bar-chart]])) 9 | ;; @@ 10 | ;; => 11 | ;;; {"type":"html","content":"nil","value":"nil"} 12 | ;; <= 13 | 14 | ;; @@ 15 | ;; P2.1 16 | (defn petersburg 17 | [] 18 | (let [init -5] 19 | (+ (Math/pow 2 (count (take-while pos? (repeatedly #(rand-int 2))))) 20 | -1 21 | init))) 22 | (for [n [100 1000 10000]] (mean (repeatedly n petersburg))) 23 | (histogram (repeatedly 1000 petersburg)) 24 | ;; @@ 25 | ;; => 26 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"652eb7b3-a44e-498a-95a5-4daaf95c30f0","values":[{"x":-5.0,"y":0},{"x":88.00000000000001,"y":986.0},{"x":181.00000000000003,"y":6.0},{"x":274.00000000000006,"y":2.0},{"x":367.00000000000006,"y":0.0},{"x":460.00000000000006,"y":0.0},{"x":553.0000000000001,"y":4.0},{"x":646.0000000000001,"y":0.0},{"x":739.0000000000001,"y":0.0},{"x":832.0000000000001,"y":0.0},{"x":925.0000000000001,"y":0.0},{"x":1018.0000000000001,"y":2.0},{"x":1111.0000000000002,"y":0}]}],"marks":[{"type":"line","from":{"data":"652eb7b3-a44e-498a-95a5-4daaf95c30f0"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"652eb7b3-a44e-498a-95a5-4daaf95c30f0","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"652eb7b3-a44e-498a-95a5-4daaf95c30f0","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"652eb7b3-a44e-498a-95a5-4daaf95c30f0\", :values ({:x -5.0, :y 0} {:x 88.00000000000001, :y 986.0} {:x 181.00000000000003, :y 6.0} {:x 274.00000000000006, :y 2.0} {:x 367.00000000000006, :y 0.0} {:x 460.00000000000006, :y 0.0} {:x 553.0000000000001, :y 4.0} {:x 646.0000000000001, :y 0.0} {:x 739.0000000000001, :y 0.0} {:x 832.0000000000001, :y 0.0} {:x 925.0000000000001, :y 0.0} {:x 1018.0000000000001, :y 2.0} {:x 1111.0000000000002, :y 0})}], :marks [{:type \"line\", :from {:data \"652eb7b3-a44e-498a-95a5-4daaf95c30f0\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"652eb7b3-a44e-498a-95a5-4daaf95c30f0\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"652eb7b3-a44e-498a-95a5-4daaf95c30f0\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 27 | ;; <= 28 | 29 | ;; @@ 30 | ;; P2.2 31 | (defn paris-salon 32 | [] 33 | (let [roll #(rand-nth (range 1 7))] 34 | (count (take-while #(not= % 6) (repeatedly roll))))) 35 | 36 | (double (mean (repeatedly 10000 paris-salon))) 37 | (histogram (repeatedly 1000 paris-salon)) 38 | ;; @@ 39 | ;; => 40 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"140aa95d-e52b-463d-9497-49ae8d41a7d0","values":[{"x":0.0,"y":0},{"x":3.2727272727272734,"y":520.0},{"x":6.545454545454547,"y":204.0},{"x":9.81818181818182,"y":117.0},{"x":13.090909090909093,"y":73.0},{"x":16.363636363636367,"y":28.0},{"x":19.63636363636364,"y":28.0},{"x":22.909090909090914,"y":14.0},{"x":26.181818181818187,"y":9.0},{"x":29.45454545454546,"y":2.0},{"x":32.727272727272734,"y":2.0},{"x":36.00000000000001,"y":3.0},{"x":39.27272727272728,"y":0}]}],"marks":[{"type":"line","from":{"data":"140aa95d-e52b-463d-9497-49ae8d41a7d0"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"140aa95d-e52b-463d-9497-49ae8d41a7d0","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"140aa95d-e52b-463d-9497-49ae8d41a7d0","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"140aa95d-e52b-463d-9497-49ae8d41a7d0\", :values ({:x 0.0, :y 0} {:x 3.2727272727272734, :y 520.0} {:x 6.545454545454547, :y 204.0} {:x 9.81818181818182, :y 117.0} {:x 13.090909090909093, :y 73.0} {:x 16.363636363636367, :y 28.0} {:x 19.63636363636364, :y 28.0} {:x 22.909090909090914, :y 14.0} {:x 26.181818181818187, :y 9.0} {:x 29.45454545454546, :y 2.0} {:x 32.727272727272734, :y 2.0} {:x 36.00000000000001, :y 3.0} {:x 39.27272727272728, :y 0})}], :marks [{:type \"line\", :from {:data \"140aa95d-e52b-463d-9497-49ae8d41a7d0\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"140aa95d-e52b-463d-9497-49ae8d41a7d0\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"140aa95d-e52b-463d-9497-49ae8d41a7d0\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 41 | ;; <= 42 | 43 | ;; @@ 44 | (double (mean (map #(if (< % 4) 0 1) (repeatedly 10000 paris-salon)))) 45 | ;; @@ 46 | ;; => 47 | ;;; {"type":"html","content":"0.4829","value":"0.4829"} 48 | ;; <= 49 | 50 | ;; @@ 51 | ;; P2.3 52 | ;; c = 5/7 53 | ;; @@ 54 | 55 | ;; @@ 56 | ;; P2.6 57 | (defn min-distance 58 | [points] 59 | (let [get-dist (fn [[x1 y1] [x2 y2]] 60 | (Math/sqrt (+ (Math/pow (- x1 x2) 2) 61 | (Math/pow (- y1 y2) 2))))] 62 | (loop [init-point (first points) 63 | rest-points (rest points) 64 | min-dist (Math/sqrt 2)] 65 | (if (seq rest-points) 66 | (let [min-dist-new (apply min (map (partial get-dist init-point) rest-points))] 67 | (recur (first rest-points) 68 | (rest rest-points) 69 | (min min-dist min-dist-new))) 70 | min-dist)))) 71 | 72 | (defn min-dist-expectation 73 | [N] 74 | (let [n 1000 75 | gen-points #(for [_ (range N)] [(rand) (rand)])] 76 | (double (mean (repeatedly n #(min-distance (gen-points))))))) 77 | 78 | (map min-dist-expectation [2 4 8 16 32 64 128]) 79 | ;; (0.5009540702075905 80 | ;; 0.20776249377493647 81 | ;; 0.09614956315172914 82 | ;; 0.04537395095149332 83 | ;; 0.022474945562603807 84 | ;; 0.01094621666895177 85 | ;; 0.005497748727015809) 86 | ;; @@ 87 | 88 | ;; @@ 89 | ;; P2.9 90 | (defn dist-of-min 91 | [n] 92 | (let [gen-min #(apply min (repeatedly n rand))] 93 | (double (mean (repeatedly 1000 gen-min))))) 94 | 95 | (map dist-of-min [10 100 1000]) 96 | ;; (0.09114434891257989 97 | ;; 0.01035934813334652 98 | ;; 0.001018004170508814) 99 | ;; @@ 100 | -------------------------------------------------------------------------------- /src/emcm/chapter1.clj: -------------------------------------------------------------------------------- 1 | ;; gorilla-repl.fileformat = 1 2 | 3 | ;; @@ 4 | (ns emcm.chapter1 5 | (:require [clojure.core.matrix.random :refer [sample-uniform]] 6 | [clojure.core.matrix :as m] 7 | [clojure.core.matrix.stats :refer [sum mean variance]] 8 | [gorilla-plot.core :refer [histogram list-plot bar-chart]])) 9 | ;; @@ 10 | ;; => 11 | ;;; {"type":"html","content":"nil","value":"nil"} 12 | ;; <= 13 | 14 | ;; @@ 15 | (defn buffon-needle-problem 16 | [N] 17 | (let [hit? (fn [] 18 | (let [x (rand) 19 | theta (rand (* Math/PI 0.5))] 20 | (if (<= x (* 0.5 (Math/sin theta))) 21 | 1 22 | 0))) 23 | n-hits (sum (repeatedly N hit?))] 24 | (double (/ N n-hits)))) 25 | 26 | (let [r 2000 27 | N 100 28 | samples (take r (repeatedly #(buffon-needle-problem N)))] 29 | (histogram samples :plot-range [[1.0 5.0] :all])) 30 | ;; @@ 31 | ;; => 32 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"4144621b-b439-4e5b-b3c5-f4fc483df43d","values":[{"x":1.0,"y":0},{"x":1.3333333333333335,"y":0.0},{"x":1.666666666666667,"y":0.0},{"x":2.0000000000000004,"y":1.0},{"x":2.333333333333334,"y":20.0},{"x":2.6666666666666674,"y":211.0},{"x":3.000000000000001,"y":516.0},{"x":3.3333333333333344,"y":671.0},{"x":3.666666666666668,"y":250.0},{"x":4.000000000000001,"y":229.0},{"x":4.333333333333334,"y":40.0},{"x":4.666666666666667,"y":45.0},{"x":5.0,"y":10.0},{"x":5.333333333333333,"y":6.0},{"x":5.666666666666666,"y":0}]}],"marks":[{"type":"line","from":{"data":"4144621b-b439-4e5b-b3c5-f4fc483df43d"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":[1.0,5.0]},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"4144621b-b439-4e5b-b3c5-f4fc483df43d","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"4144621b-b439-4e5b-b3c5-f4fc483df43d\", :values ({:x 1.0, :y 0} {:x 1.3333333333333335, :y 0.0} {:x 1.666666666666667, :y 0.0} {:x 2.0000000000000004, :y 1.0} {:x 2.333333333333334, :y 20.0} {:x 2.6666666666666674, :y 211.0} {:x 3.000000000000001, :y 516.0} {:x 3.3333333333333344, :y 671.0} {:x 3.666666666666668, :y 250.0} {:x 4.000000000000001, :y 229.0} {:x 4.333333333333334, :y 40.0} {:x 4.666666666666667, :y 45.0} {:x 5.0, :y 10.0} {:x 5.333333333333333, :y 6.0} {:x 5.666666666666666, :y 0})}], :marks [{:type \"line\", :from {:data \"4144621b-b439-4e5b-b3c5-f4fc483df43d\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain [1.0 5.0]} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"4144621b-b439-4e5b-b3c5-f4fc483df43d\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 33 | ;; <= 34 | 35 | ;; @@ 36 | (defn gamble 37 | [] 38 | (let [win? #(if (< (rand) 0.5) -1 1) 39 | start 100] 40 | (take-while #(> % 0) (reductions + start (repeatedly win?))))) 41 | 42 | ;; only take every 50th point here to reduce the size of fig 43 | (list-plot (take-nth 50 (gamble)) :joined true) 44 | ;; @@ 45 | ;; => 46 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"c7db8098-c687-47a5-8d58-ad7bf373d0cc","values":[{"x":0,"y":100},{"x":1,"y":106},{"x":2,"y":110},{"x":3,"y":118},{"x":4,"y":112},{"x":5,"y":104},{"x":6,"y":96},{"x":7,"y":98},{"x":8,"y":94},{"x":9,"y":100},{"x":10,"y":100},{"x":11,"y":100},{"x":12,"y":106},{"x":13,"y":112},{"x":14,"y":106},{"x":15,"y":104},{"x":16,"y":106},{"x":17,"y":104},{"x":18,"y":100},{"x":19,"y":98},{"x":20,"y":98},{"x":21,"y":100},{"x":22,"y":100},{"x":23,"y":90},{"x":24,"y":84},{"x":25,"y":82},{"x":26,"y":90},{"x":27,"y":94},{"x":28,"y":86},{"x":29,"y":94},{"x":30,"y":88},{"x":31,"y":88},{"x":32,"y":96},{"x":33,"y":86},{"x":34,"y":92},{"x":35,"y":88},{"x":36,"y":88},{"x":37,"y":88},{"x":38,"y":70},{"x":39,"y":74},{"x":40,"y":68},{"x":41,"y":62},{"x":42,"y":66},{"x":43,"y":76},{"x":44,"y":72},{"x":45,"y":68},{"x":46,"y":60},{"x":47,"y":64},{"x":48,"y":64},{"x":49,"y":66},{"x":50,"y":56},{"x":51,"y":58},{"x":52,"y":62},{"x":53,"y":64},{"x":54,"y":72},{"x":55,"y":72},{"x":56,"y":80},{"x":57,"y":76},{"x":58,"y":80},{"x":59,"y":94},{"x":60,"y":76},{"x":61,"y":68},{"x":62,"y":66},{"x":63,"y":74},{"x":64,"y":72},{"x":65,"y":70},{"x":66,"y":62},{"x":67,"y":70},{"x":68,"y":78},{"x":69,"y":76},{"x":70,"y":78},{"x":71,"y":82},{"x":72,"y":78},{"x":73,"y":78},{"x":74,"y":76},{"x":75,"y":76},{"x":76,"y":66},{"x":77,"y":70},{"x":78,"y":82},{"x":79,"y":78},{"x":80,"y":78},{"x":81,"y":82},{"x":82,"y":80},{"x":83,"y":82},{"x":84,"y":82},{"x":85,"y":90},{"x":86,"y":98},{"x":87,"y":106},{"x":88,"y":90},{"x":89,"y":86},{"x":90,"y":100},{"x":91,"y":88},{"x":92,"y":86},{"x":93,"y":76},{"x":94,"y":74},{"x":95,"y":64},{"x":96,"y":54},{"x":97,"y":56},{"x":98,"y":44},{"x":99,"y":50},{"x":100,"y":56},{"x":101,"y":54},{"x":102,"y":48},{"x":103,"y":42},{"x":104,"y":40},{"x":105,"y":42},{"x":106,"y":54},{"x":107,"y":58},{"x":108,"y":52},{"x":109,"y":46},{"x":110,"y":38},{"x":111,"y":60},{"x":112,"y":68},{"x":113,"y":68},{"x":114,"y":68},{"x":115,"y":68},{"x":116,"y":64},{"x":117,"y":64},{"x":118,"y":60},{"x":119,"y":66},{"x":120,"y":56},{"x":121,"y":64},{"x":122,"y":60},{"x":123,"y":56},{"x":124,"y":54},{"x":125,"y":60},{"x":126,"y":52},{"x":127,"y":48},{"x":128,"y":38},{"x":129,"y":44},{"x":130,"y":44},{"x":131,"y":40},{"x":132,"y":48},{"x":133,"y":62},{"x":134,"y":60},{"x":135,"y":62},{"x":136,"y":56},{"x":137,"y":48},{"x":138,"y":40},{"x":139,"y":34},{"x":140,"y":28},{"x":141,"y":12}]}],"marks":[{"type":"line","from":{"data":"c7db8098-c687-47a5-8d58-ad7bf373d0cc"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"stroke":{"value":"#FF29D2"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"c7db8098-c687-47a5-8d58-ad7bf373d0cc","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"c7db8098-c687-47a5-8d58-ad7bf373d0cc","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"c7db8098-c687-47a5-8d58-ad7bf373d0cc\", :values ({:x 0, :y 100} {:x 1, :y 106} {:x 2, :y 110} {:x 3, :y 118} {:x 4, :y 112} {:x 5, :y 104} {:x 6, :y 96} {:x 7, :y 98} {:x 8, :y 94} {:x 9, :y 100} {:x 10, :y 100} {:x 11, :y 100} {:x 12, :y 106} {:x 13, :y 112} {:x 14, :y 106} {:x 15, :y 104} {:x 16, :y 106} {:x 17, :y 104} {:x 18, :y 100} {:x 19, :y 98} {:x 20, :y 98} {:x 21, :y 100} {:x 22, :y 100} {:x 23, :y 90} {:x 24, :y 84} {:x 25, :y 82} {:x 26, :y 90} {:x 27, :y 94} {:x 28, :y 86} {:x 29, :y 94} {:x 30, :y 88} {:x 31, :y 88} {:x 32, :y 96} {:x 33, :y 86} {:x 34, :y 92} {:x 35, :y 88} {:x 36, :y 88} {:x 37, :y 88} {:x 38, :y 70} {:x 39, :y 74} {:x 40, :y 68} {:x 41, :y 62} {:x 42, :y 66} {:x 43, :y 76} {:x 44, :y 72} {:x 45, :y 68} {:x 46, :y 60} {:x 47, :y 64} {:x 48, :y 64} {:x 49, :y 66} {:x 50, :y 56} {:x 51, :y 58} {:x 52, :y 62} {:x 53, :y 64} {:x 54, :y 72} {:x 55, :y 72} {:x 56, :y 80} {:x 57, :y 76} {:x 58, :y 80} {:x 59, :y 94} {:x 60, :y 76} {:x 61, :y 68} {:x 62, :y 66} {:x 63, :y 74} {:x 64, :y 72} {:x 65, :y 70} {:x 66, :y 62} {:x 67, :y 70} {:x 68, :y 78} {:x 69, :y 76} {:x 70, :y 78} {:x 71, :y 82} {:x 72, :y 78} {:x 73, :y 78} {:x 74, :y 76} {:x 75, :y 76} {:x 76, :y 66} {:x 77, :y 70} {:x 78, :y 82} {:x 79, :y 78} {:x 80, :y 78} {:x 81, :y 82} {:x 82, :y 80} {:x 83, :y 82} {:x 84, :y 82} {:x 85, :y 90} {:x 86, :y 98} {:x 87, :y 106} {:x 88, :y 90} {:x 89, :y 86} {:x 90, :y 100} {:x 91, :y 88} {:x 92, :y 86} {:x 93, :y 76} {:x 94, :y 74} {:x 95, :y 64} {:x 96, :y 54} {:x 97, :y 56} {:x 98, :y 44} {:x 99, :y 50} {:x 100, :y 56} {:x 101, :y 54} {:x 102, :y 48} {:x 103, :y 42} {:x 104, :y 40} {:x 105, :y 42} {:x 106, :y 54} {:x 107, :y 58} {:x 108, :y 52} {:x 109, :y 46} {:x 110, :y 38} {:x 111, :y 60} {:x 112, :y 68} {:x 113, :y 68} {:x 114, :y 68} {:x 115, :y 68} {:x 116, :y 64} {:x 117, :y 64} {:x 118, :y 60} {:x 119, :y 66} {:x 120, :y 56} {:x 121, :y 64} {:x 122, :y 60} {:x 123, :y 56} {:x 124, :y 54} {:x 125, :y 60} {:x 126, :y 52} {:x 127, :y 48} {:x 128, :y 38} {:x 129, :y 44} {:x 130, :y 44} {:x 131, :y 40} {:x 132, :y 48} {:x 133, :y 62} {:x 134, :y 60} {:x 135, :y 62} {:x 136, :y 56} {:x 137, :y 48} {:x 138, :y 40} {:x 139, :y 34} {:x 140, :y 28} {:x 141, :y 12})}], :marks [{:type \"line\", :from {:data \"c7db8098-c687-47a5-8d58-ad7bf373d0cc\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :stroke {:value \"#FF29D2\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"c7db8098-c687-47a5-8d58-ad7bf373d0cc\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"c7db8098-c687-47a5-8d58-ad7bf373d0cc\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 47 | ;; <= 48 | 49 | ;; @@ 50 | (histogram (map count (repeatedly 100 gamble))) 51 | ;; @@ 52 | ;; => 53 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"f359f172-9e74-4451-8272-87f57432f4f7","values":[{"x":1182.0,"y":0},{"x":1699557.7500000002,"y":95.0},{"x":3397933.5000000005,"y":1.0},{"x":5096309.250000001,"y":1.0},{"x":6794685.000000001,"y":1.0},{"x":8493060.750000002,"y":0.0},{"x":1.0191436500000002E7,"y":1.0},{"x":1.1889812250000002E7,"y":0.0},{"x":1.3588188000000002E7,"y":1.0},{"x":1.5286563750000002E7,"y":0}]}],"marks":[{"type":"line","from":{"data":"f359f172-9e74-4451-8272-87f57432f4f7"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"f359f172-9e74-4451-8272-87f57432f4f7","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"f359f172-9e74-4451-8272-87f57432f4f7","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"f359f172-9e74-4451-8272-87f57432f4f7\", :values ({:x 1182.0, :y 0} {:x 1699557.7500000002, :y 95.0} {:x 3397933.5000000005, :y 1.0} {:x 5096309.250000001, :y 1.0} {:x 6794685.000000001, :y 1.0} {:x 8493060.750000002, :y 0.0} {:x 1.0191436500000002E7, :y 1.0} {:x 1.1889812250000002E7, :y 0.0} {:x 1.3588188000000002E7, :y 1.0} {:x 1.5286563750000002E7, :y 0})}], :marks [{:type \"line\", :from {:data \"f359f172-9e74-4451-8272-87f57432f4f7\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"f359f172-9e74-4451-8272-87f57432f4f7\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"f359f172-9e74-4451-8272-87f57432f4f7\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 54 | ;; <= 55 | 56 | ;; @@ 57 | (defn coupon-collecting 58 | [] 59 | (let [n-letters 9 60 | gen-rand-letter #(rand-int n-letters) 61 | update-count #(update %1 %2 (fnil inc 0)) 62 | not-fill? #(< (count %) n-letters)] 63 | (count (take-while not-fill? (reductions update-count {} (repeatedly gen-rand-letter)))))) 64 | (histogram (repeatedly 10000 coupon-collecting)) 65 | ;; @@ 66 | ;; => 67 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"3de77e5d-c357-437b-a01e-cde0b7ad6863","values":[{"x":9.0,"y":0},{"x":15.8,"y":1203.0},{"x":22.6,"y":3461.0},{"x":29.400000000000002,"y":2631.0},{"x":36.2,"y":1437.0},{"x":43.0,"y":644.0},{"x":49.8,"y":355.0},{"x":56.599999999999994,"y":163.0},{"x":63.39999999999999,"y":56.0},{"x":70.19999999999999,"y":26.0},{"x":76.99999999999999,"y":15.0},{"x":83.79999999999998,"y":5.0},{"x":90.59999999999998,"y":1.0},{"x":97.39999999999998,"y":1.0},{"x":104.19999999999997,"y":0.0},{"x":110.99999999999997,"y":1.0},{"x":117.79999999999997,"y":1.0},{"x":124.59999999999997,"y":0}]}],"marks":[{"type":"line","from":{"data":"3de77e5d-c357-437b-a01e-cde0b7ad6863"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"3de77e5d-c357-437b-a01e-cde0b7ad6863","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"3de77e5d-c357-437b-a01e-cde0b7ad6863","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"3de77e5d-c357-437b-a01e-cde0b7ad6863\", :values ({:x 9.0, :y 0} {:x 15.8, :y 1203.0} {:x 22.6, :y 3461.0} {:x 29.400000000000002, :y 2631.0} {:x 36.2, :y 1437.0} {:x 43.0, :y 644.0} {:x 49.8, :y 355.0} {:x 56.599999999999994, :y 163.0} {:x 63.39999999999999, :y 56.0} {:x 70.19999999999999, :y 26.0} {:x 76.99999999999999, :y 15.0} {:x 83.79999999999998, :y 5.0} {:x 90.59999999999998, :y 1.0} {:x 97.39999999999998, :y 1.0} {:x 104.19999999999997, :y 0.0} {:x 110.99999999999997, :y 1.0} {:x 117.79999999999997, :y 1.0} {:x 124.59999999999997, :y 0})}], :marks [{:type \"line\", :from {:data \"3de77e5d-c357-437b-a01e-cde0b7ad6863\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"3de77e5d-c357-437b-a01e-cde0b7ad6863\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"3de77e5d-c357-437b-a01e-cde0b7ad6863\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 68 | ;; <= 69 | 70 | ;; @@ 71 | ;; P1.1 72 | (let [pows (range 2 9) 73 | get-error #(Math/abs (- Math/PI (buffon-needle-problem %)))] 74 | (list-plot (for [n pows] [n (get-error (Math/pow 10 n))]))) 75 | ;; @@ 76 | ;; => 77 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"3803bea1-3f66-4f7f-af02-f80538773c9f","values":[{"x":2,"y":1.4038618918647527},{"x":3,"y":0.06353555153841217},{"x":4,"y":0.01099246620844152},{"x":5,"y":0.00974172969377074},{"x":6,"y":0.002478100578458875},{"x":7,"y":8.083922012747102E-4},{"x":8,"y":2.2885513174619732E-4}]}],"marks":[{"type":"symbol","from":{"data":"3803bea1-3f66-4f7f-af02-f80538773c9f"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"fill":{"value":"steelblue"},"fillOpacity":{"value":1}},"update":{"shape":"circle","size":{"value":70},"stroke":{"value":"transparent"}},"hover":{"size":{"value":210},"stroke":{"value":"white"}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"3803bea1-3f66-4f7f-af02-f80538773c9f","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"3803bea1-3f66-4f7f-af02-f80538773c9f","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"3803bea1-3f66-4f7f-af02-f80538773c9f\", :values ({:x 2, :y 1.4038618918647527} {:x 3, :y 0.06353555153841217} {:x 4, :y 0.01099246620844152} {:x 5, :y 0.00974172969377074} {:x 6, :y 0.002478100578458875} {:x 7, :y 8.083922012747102E-4} {:x 8, :y 2.2885513174619732E-4})}], :marks [{:type \"symbol\", :from {:data \"3803bea1-3f66-4f7f-af02-f80538773c9f\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 1}}, :update {:shape \"circle\", :size {:value 70}, :stroke {:value \"transparent\"}}, :hover {:size {:value 210}, :stroke {:value \"white\"}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"3803bea1-3f66-4f7f-af02-f80538773c9f\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"3803bea1-3f66-4f7f-af02-f80538773c9f\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 78 | ;; <= 79 | 80 | ;; @@ 81 | ;; P1.2 82 | (mean (repeatedly 10000 #(buffon-needle-problem 200))) 83 | ;; 3.175938714613824 84 | (mean (repeatedly 1000 #(buffon-needle-problem 2000))) 85 | ;; 3.1484694761628202 86 | (mean (repeatedly 100 #(buffon-needle-problem 20000))) 87 | ;; 3.140771532901823 88 | ;; @@ 89 | 90 | ;; @@ 91 | ;; P1.3 92 | ;; (a) 93 | (let [roll #(+ (rand-int 6) (rand-int 6) 2)] 94 | (histogram (repeatedly 1000 roll))) 95 | ;; @@ 96 | ;; => 97 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"421d13a7-44ff-4d6c-8621-a8894c5cba9d","values":[{"x":2.0,"y":0},{"x":2.909090909090909,"y":27.0},{"x":3.8181818181818183,"y":50.0},{"x":4.7272727272727275,"y":73.0},{"x":5.636363636363637,"y":119.0},{"x":6.545454545454546,"y":144.0},{"x":7.454545454545455,"y":169.0},{"x":8.363636363636365,"y":120.0},{"x":9.272727272727275,"y":113.0},{"x":10.181818181818185,"y":92.0},{"x":11.090909090909095,"y":53.0},{"x":12.000000000000005,"y":40.0},{"x":12.909090909090915,"y":0}]}],"marks":[{"type":"line","from":{"data":"421d13a7-44ff-4d6c-8621-a8894c5cba9d"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"421d13a7-44ff-4d6c-8621-a8894c5cba9d","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"421d13a7-44ff-4d6c-8621-a8894c5cba9d","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"421d13a7-44ff-4d6c-8621-a8894c5cba9d\", :values ({:x 2.0, :y 0} {:x 2.909090909090909, :y 27.0} {:x 3.8181818181818183, :y 50.0} {:x 4.7272727272727275, :y 73.0} {:x 5.636363636363637, :y 119.0} {:x 6.545454545454546, :y 144.0} {:x 7.454545454545455, :y 169.0} {:x 8.363636363636365, :y 120.0} {:x 9.272727272727275, :y 113.0} {:x 10.181818181818185, :y 92.0} {:x 11.090909090909095, :y 53.0} {:x 12.000000000000005, :y 40.0} {:x 12.909090909090915, :y 0})}], :marks [{:type \"line\", :from {:data \"421d13a7-44ff-4d6c-8621-a8894c5cba9d\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"421d13a7-44ff-4d6c-8621-a8894c5cba9d\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"421d13a7-44ff-4d6c-8621-a8894c5cba9d\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 98 | ;; <= 99 | 100 | ;; @@ 101 | ;; (b) 102 | (let [roll #(vector (+ 1 (rand-int 6)) 103 | (+ 1 (rand-int 6))) 104 | fq (frequencies (repeatedly 1000 roll)) 105 | ks (sort (keys fq))] 106 | (bar-chart ks (for [k ks] (get fq k)))) 107 | ;; @@ 108 | ;; => 109 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"66452fb2-69dc-4b36-b623-30df6cb16aa5","values":[{"x":[1,1],"y":30},{"x":[1,2],"y":27},{"x":[1,3],"y":31},{"x":[1,4],"y":18},{"x":[1,5],"y":29},{"x":[1,6],"y":34},{"x":[2,1],"y":33},{"x":[2,2],"y":25},{"x":[2,3],"y":24},{"x":[2,4],"y":33},{"x":[2,5],"y":26},{"x":[2,6],"y":30},{"x":[3,1],"y":21},{"x":[3,2],"y":22},{"x":[3,3],"y":26},{"x":[3,4],"y":26},{"x":[3,5],"y":23},{"x":[3,6],"y":34},{"x":[4,1],"y":30},{"x":[4,2],"y":27},{"x":[4,3],"y":25},{"x":[4,4],"y":26},{"x":[4,5],"y":23},{"x":[4,6],"y":36},{"x":[5,1],"y":32},{"x":[5,2],"y":30},{"x":[5,3],"y":34},{"x":[5,4],"y":33},{"x":[5,5],"y":33},{"x":[5,6],"y":26},{"x":[6,1],"y":22},{"x":[6,2],"y":22},{"x":[6,3],"y":21},{"x":[6,4],"y":39},{"x":[6,5],"y":31},{"x":[6,6],"y":18}]}],"marks":[{"type":"rect","from":{"data":"66452fb2-69dc-4b36-b623-30df6cb16aa5"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"width":{"scale":"x","band":true,"offset":-1},"y":{"scale":"y","field":"data.y"},"y2":{"scale":"y","value":0}},"update":{"fill":{"value":"steelblue"},"opacity":{"value":1}},"hover":{"fill":{"value":"#FF29D2"}}}}],"scales":[{"name":"x","type":"ordinal","range":"width","domain":{"data":"66452fb2-69dc-4b36-b623-30df6cb16aa5","field":"data.x"}},{"name":"y","range":"height","nice":true,"domain":{"data":"66452fb2-69dc-4b36-b623-30df6cb16aa5","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"66452fb2-69dc-4b36-b623-30df6cb16aa5\", :values ({:x [1 1], :y 30} {:x [1 2], :y 27} {:x [1 3], :y 31} {:x [1 4], :y 18} {:x [1 5], :y 29} {:x [1 6], :y 34} {:x [2 1], :y 33} {:x [2 2], :y 25} {:x [2 3], :y 24} {:x [2 4], :y 33} {:x [2 5], :y 26} {:x [2 6], :y 30} {:x [3 1], :y 21} {:x [3 2], :y 22} {:x [3 3], :y 26} {:x [3 4], :y 26} {:x [3 5], :y 23} {:x [3 6], :y 34} {:x [4 1], :y 30} {:x [4 2], :y 27} {:x [4 3], :y 25} {:x [4 4], :y 26} {:x [4 5], :y 23} {:x [4 6], :y 36} {:x [5 1], :y 32} {:x [5 2], :y 30} {:x [5 3], :y 34} {:x [5 4], :y 33} {:x [5 5], :y 33} {:x [5 6], :y 26} {:x [6 1], :y 22} {:x [6 2], :y 22} {:x [6 3], :y 21} {:x [6 4], :y 39} {:x [6 5], :y 31} {:x [6 6], :y 18})}], :marks [{:type \"rect\", :from {:data \"66452fb2-69dc-4b36-b623-30df6cb16aa5\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :width {:scale \"x\", :band true, :offset -1}, :y {:scale \"y\", :field \"data.y\"}, :y2 {:scale \"y\", :value 0}}, :update {:fill {:value \"steelblue\"}, :opacity {:value 1}}, :hover {:fill {:value \"#FF29D2\"}}}}], :scales [{:name \"x\", :type \"ordinal\", :range \"width\", :domain {:data \"66452fb2-69dc-4b36-b623-30df6cb16aa5\", :field \"data.x\"}} {:name \"y\", :range \"height\", :nice true, :domain {:data \"66452fb2-69dc-4b36-b623-30df6cb16aa5\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 110 | ;; <= 111 | 112 | ;; @@ 113 | ;; (c) 114 | (let [roll #(sum (repeatedly 6 (fn [] (+ (rand-int 6) 1))))] 115 | (histogram (repeatedly 4000 roll))) 116 | ;; @@ 117 | ;; => 118 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"c6ed12e9-7293-4e22-9e13-cce0ad476a13","values":[{"x":8.0,"y":0},{"x":10.0,"y":5.0},{"x":12.0,"y":28.0},{"x":14.0,"y":102.0},{"x":16.0,"y":244.0},{"x":18.0,"y":445.0},{"x":20.0,"y":627.0},{"x":22.0,"y":739.0},{"x":24.0,"y":686.0},{"x":26.0,"y":535.0},{"x":28.0,"y":329.0},{"x":30.0,"y":174.0},{"x":32.0,"y":62.0},{"x":34.0,"y":19.0},{"x":36.0,"y":5.0},{"x":38.0,"y":0}]}],"marks":[{"type":"line","from":{"data":"c6ed12e9-7293-4e22-9e13-cce0ad476a13"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"c6ed12e9-7293-4e22-9e13-cce0ad476a13","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"c6ed12e9-7293-4e22-9e13-cce0ad476a13","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"c6ed12e9-7293-4e22-9e13-cce0ad476a13\", :values ({:x 8.0, :y 0} {:x 10.0, :y 5.0} {:x 12.0, :y 28.0} {:x 14.0, :y 102.0} {:x 16.0, :y 244.0} {:x 18.0, :y 445.0} {:x 20.0, :y 627.0} {:x 22.0, :y 739.0} {:x 24.0, :y 686.0} {:x 26.0, :y 535.0} {:x 28.0, :y 329.0} {:x 30.0, :y 174.0} {:x 32.0, :y 62.0} {:x 34.0, :y 19.0} {:x 36.0, :y 5.0} {:x 38.0, :y 0})}], :marks [{:type \"line\", :from {:data \"c6ed12e9-7293-4e22-9e13-cce0ad476a13\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"c6ed12e9-7293-4e22-9e13-cce0ad476a13\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"c6ed12e9-7293-4e22-9e13-cce0ad476a13\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 119 | ;; <= 120 | 121 | ;; @@ 122 | ;; P1.4 123 | (let [reward #(/ 1 (+ (rand-int 6) (rand-int 6) 2)) 124 | samples (repeatedly 1000 reward)] 125 | (double (mean samples))) 126 | ;; 0.1625419552669553 127 | ;; @@ 128 | 129 | ;; @@ 130 | ;; P1.5 131 | ;; (a) 132 | (defn get-coupon-count 133 | [gen-letter update-method not-fill?] 134 | (count (take-while not-fill? (reductions update-method {} (repeatedly gen-letter))))) 135 | 136 | (defn get-milk-count-a 137 | [] 138 | (let [letters "milk"] 139 | (get-coupon-count 140 | #(rand-nth letters) 141 | #(update %1 %2 (fnil inc 0)) 142 | #(< (count %) (count letters))))) 143 | 144 | (histogram (repeatedly 1000 get-milk-count-a)) 145 | ;; @@ 146 | ;; => 147 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"0a50ea89-f136-4362-8716-0f2eb0109681","values":[{"x":4.0,"y":0},{"x":5.818181818181818,"y":242.0},{"x":7.636363636363637,"y":291.0},{"x":9.454545454545455,"y":188.0},{"x":11.272727272727273,"y":117.0},{"x":13.090909090909092,"y":76.0},{"x":14.90909090909091,"y":20.0},{"x":16.72727272727273,"y":30.0},{"x":18.54545454545455,"y":15.0},{"x":20.36363636363637,"y":10.0},{"x":22.18181818181819,"y":7.0},{"x":24.00000000000001,"y":4.0},{"x":25.81818181818183,"y":0}]}],"marks":[{"type":"line","from":{"data":"0a50ea89-f136-4362-8716-0f2eb0109681"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"0a50ea89-f136-4362-8716-0f2eb0109681","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"0a50ea89-f136-4362-8716-0f2eb0109681","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"0a50ea89-f136-4362-8716-0f2eb0109681\", :values ({:x 4.0, :y 0} {:x 5.818181818181818, :y 242.0} {:x 7.636363636363637, :y 291.0} {:x 9.454545454545455, :y 188.0} {:x 11.272727272727273, :y 117.0} {:x 13.090909090909092, :y 76.0} {:x 14.90909090909091, :y 20.0} {:x 16.72727272727273, :y 30.0} {:x 18.54545454545455, :y 15.0} {:x 20.36363636363637, :y 10.0} {:x 22.18181818181819, :y 7.0} {:x 24.00000000000001, :y 4.0} {:x 25.81818181818183, :y 0})}], :marks [{:type \"line\", :from {:data \"0a50ea89-f136-4362-8716-0f2eb0109681\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"0a50ea89-f136-4362-8716-0f2eb0109681\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"0a50ea89-f136-4362-8716-0f2eb0109681\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 148 | ;; <= 149 | 150 | ;; @@ 151 | ;; (b) 152 | (defn get-milk-count-b 153 | [] 154 | (let [letters "milk"] 155 | (get-coupon-count 156 | #(nth letters (int (/ (rand-int 31) 10))) 157 | #(update %1 %2 (fnil inc 0)) 158 | #(< (count %) (count letters))))) 159 | 160 | (histogram (repeatedly 1000 get-milk-count-b)) 161 | ;; @@ 162 | ;; => 163 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"023673ec-5246-4e14-a8c0-ec4b5803b0df","values":[{"x":4.0,"y":0},{"x":23.454545454545457,"y":534.0},{"x":42.909090909090914,"y":216.0},{"x":62.363636363636374,"y":116.0},{"x":81.81818181818183,"y":69.0},{"x":101.27272727272728,"y":35.0},{"x":120.72727272727273,"y":11.0},{"x":140.1818181818182,"y":7.0},{"x":159.63636363636365,"y":8.0},{"x":179.09090909090912,"y":1.0},{"x":198.5454545454546,"y":2.0},{"x":218.00000000000006,"y":1.0},{"x":237.45454545454552,"y":0}]}],"marks":[{"type":"line","from":{"data":"023673ec-5246-4e14-a8c0-ec4b5803b0df"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"023673ec-5246-4e14-a8c0-ec4b5803b0df","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"023673ec-5246-4e14-a8c0-ec4b5803b0df","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"023673ec-5246-4e14-a8c0-ec4b5803b0df\", :values ({:x 4.0, :y 0} {:x 23.454545454545457, :y 534.0} {:x 42.909090909090914, :y 216.0} {:x 62.363636363636374, :y 116.0} {:x 81.81818181818183, :y 69.0} {:x 101.27272727272728, :y 35.0} {:x 120.72727272727273, :y 11.0} {:x 140.1818181818182, :y 7.0} {:x 159.63636363636365, :y 8.0} {:x 179.09090909090912, :y 1.0} {:x 198.5454545454546, :y 2.0} {:x 218.00000000000006, :y 1.0} {:x 237.45454545454552, :y 0})}], :marks [{:type \"line\", :from {:data \"023673ec-5246-4e14-a8c0-ec4b5803b0df\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"023673ec-5246-4e14-a8c0-ec4b5803b0df\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"023673ec-5246-4e14-a8c0-ec4b5803b0df\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 164 | ;; <= 165 | 166 | ;; @@ 167 | ;; (c) 168 | (defn get-BC-count 169 | [] 170 | (let [coupon (frequencies "BattleCreek") 171 | letters (keys coupon)] 172 | (get-coupon-count 173 | #(nth letters (rand-int (count letters))) 174 | #(update %1 %2 (fnil inc 0)) 175 | #(some true? (for [[k v] coupon] (< (get % k 0) v)))))) 176 | (histogram (repeatedly 1000 get-BC-count)) 177 | ;; @@ 178 | ;; => 179 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"6f432371-222a-41a5-a627-7aa2c30fff10","values":[{"x":12.0,"y":0},{"x":18.181818181818183,"y":105.0},{"x":24.363636363636367,"y":249.0},{"x":30.54545454545455,"y":239.0},{"x":36.727272727272734,"y":164.0},{"x":42.90909090909092,"y":110.0},{"x":49.09090909090911,"y":80.0},{"x":55.272727272727295,"y":18.0},{"x":61.45454545454548,"y":14.0},{"x":67.63636363636367,"y":10.0},{"x":73.81818181818186,"y":7.0},{"x":80.00000000000004,"y":4.0},{"x":86.18181818181823,"y":0}]}],"marks":[{"type":"line","from":{"data":"6f432371-222a-41a5-a627-7aa2c30fff10"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"6f432371-222a-41a5-a627-7aa2c30fff10","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"6f432371-222a-41a5-a627-7aa2c30fff10","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"6f432371-222a-41a5-a627-7aa2c30fff10\", :values ({:x 12.0, :y 0} {:x 18.181818181818183, :y 105.0} {:x 24.363636363636367, :y 249.0} {:x 30.54545454545455, :y 239.0} {:x 36.727272727272734, :y 164.0} {:x 42.90909090909092, :y 110.0} {:x 49.09090909090911, :y 80.0} {:x 55.272727272727295, :y 18.0} {:x 61.45454545454548, :y 14.0} {:x 67.63636363636367, :y 10.0} {:x 73.81818181818186, :y 7.0} {:x 80.00000000000004, :y 4.0} {:x 86.18181818181823, :y 0})}], :marks [{:type \"line\", :from {:data \"6f432371-222a-41a5-a627-7aa2c30fff10\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"6f432371-222a-41a5-a627-7aa2c30fff10\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"6f432371-222a-41a5-a627-7aa2c30fff10\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 180 | ;; <= 181 | 182 | ;; @@ 183 | ;; P1.6 184 | (defn modified-coupon-collections 185 | [alphabet-size window-size] 186 | (let [not-full? #(< (count %) (Math/pow alphabet-size window-size))] 187 | (->> (reductions 188 | #(conj %1 %2) 189 | #{} 190 | (partition window-size (repeatedly #(rand-int alphabet-size)))) 191 | (take-while not-full?) 192 | count))) 193 | 194 | (histogram (repeatedly 1000 #(modified-coupon-collections 4 4))) 195 | ;; @@ 196 | ;; => 197 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"f8237e01-5462-4b17-9993-12f9314e78ad","values":[{"x":926.0,"y":0},{"x":1156.909090909091,"y":55.0},{"x":1387.818181818182,"y":253.0},{"x":1618.727272727273,"y":317.0},{"x":1849.636363636364,"y":190.0},{"x":2080.545454545455,"y":104.0},{"x":2311.454545454546,"y":48.0},{"x":2542.363636363637,"y":13.0},{"x":2773.272727272728,"y":12.0},{"x":3004.181818181819,"y":4.0},{"x":3235.09090909091,"y":2.0},{"x":3466.000000000001,"y":2.0},{"x":3696.909090909092,"y":0}]}],"marks":[{"type":"line","from":{"data":"f8237e01-5462-4b17-9993-12f9314e78ad"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"f8237e01-5462-4b17-9993-12f9314e78ad","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"f8237e01-5462-4b17-9993-12f9314e78ad","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"f8237e01-5462-4b17-9993-12f9314e78ad\", :values ({:x 926.0, :y 0} {:x 1156.909090909091, :y 55.0} {:x 1387.818181818182, :y 253.0} {:x 1618.727272727273, :y 317.0} {:x 1849.636363636364, :y 190.0} {:x 2080.545454545455, :y 104.0} {:x 2311.454545454546, :y 48.0} {:x 2542.363636363637, :y 13.0} {:x 2773.272727272728, :y 12.0} {:x 3004.181818181819, :y 4.0} {:x 3235.09090909091, :y 2.0} {:x 3466.000000000001, :y 2.0} {:x 3696.909090909092, :y 0})}], :marks [{:type \"line\", :from {:data \"f8237e01-5462-4b17-9993-12f9314e78ad\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"f8237e01-5462-4b17-9993-12f9314e78ad\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"f8237e01-5462-4b17-9993-12f9314e78ad\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 198 | ;; <= 199 | 200 | ;; @@ 201 | ;; P1.7 202 | (defn coin-toss-partition 203 | [n] 204 | (->> (repeatedly 1000 #(rand-int 2)) 205 | (partition n) 206 | frequencies)) 207 | (coin-toss-partition 1) 208 | ;; {(0) 495, (1) 505} 209 | (coin-toss-partition 2) 210 | ;; {(0 1) 120, 211 | ;; (1 1) 134, 212 | ;; (1 0) 111, 213 | ;; (0 0) 214 | ;; 135} 215 | (coin-toss-partition 3) 216 | ;; {(1 0 1) 41, 217 | ;; (0 0 1) 40, 218 | ;; (0 1 0) 42, 219 | ;; (1 0 0) 39, 220 | ;; (0 0 0) 52, 221 | ;; (0 1 1) 37, 222 | ;; (1 1 1) 46, 223 | ;; (1 1 0) 224 | ;; 36} 225 | (defn succ-ones-count 226 | ([xs c] 227 | (let [xs (drop-while zero? xs)] 228 | (if (seq xs) 229 | (succ-ones-count (drop-while #(= 1 %) xs) (inc c)) 230 | c))) 231 | ([xs] (succ-ones-count xs 0))) 232 | 233 | (succ-ones-count (repeatedly 1000 #(rand-int 2))) 234 | ;; 258 235 | ;; @@ 236 | 237 | ;; @@ 238 | ;; P1.8 239 | ;; (a) 240 | (let [N 30 241 | head? #(if (< (rand) 0.6) 1 0) 242 | xs (reductions + 0 (repeatedly N head?))] 243 | (list-plot xs :joined true)) 244 | ;; @@ 245 | ;; => 246 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7","values":[{"x":0,"y":0},{"x":1,"y":1},{"x":2,"y":2},{"x":3,"y":2},{"x":4,"y":3},{"x":5,"y":4},{"x":6,"y":5},{"x":7,"y":5},{"x":8,"y":6},{"x":9,"y":7},{"x":10,"y":8},{"x":11,"y":9},{"x":12,"y":10},{"x":13,"y":10},{"x":14,"y":11},{"x":15,"y":12},{"x":16,"y":13},{"x":17,"y":14},{"x":18,"y":15},{"x":19,"y":16},{"x":20,"y":17},{"x":21,"y":17},{"x":22,"y":18},{"x":23,"y":19},{"x":24,"y":19},{"x":25,"y":19},{"x":26,"y":20},{"x":27,"y":20},{"x":28,"y":20},{"x":29,"y":20},{"x":30,"y":21}]}],"marks":[{"type":"line","from":{"data":"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"stroke":{"value":"#FF29D2"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7\", :values ({:x 0, :y 0} {:x 1, :y 1} {:x 2, :y 2} {:x 3, :y 2} {:x 4, :y 3} {:x 5, :y 4} {:x 6, :y 5} {:x 7, :y 5} {:x 8, :y 6} {:x 9, :y 7} {:x 10, :y 8} {:x 11, :y 9} {:x 12, :y 10} {:x 13, :y 10} {:x 14, :y 11} {:x 15, :y 12} {:x 16, :y 13} {:x 17, :y 14} {:x 18, :y 15} {:x 19, :y 16} {:x 20, :y 17} {:x 21, :y 17} {:x 22, :y 18} {:x 23, :y 19} {:x 24, :y 19} {:x 25, :y 19} {:x 26, :y 20} {:x 27, :y 20} {:x 28, :y 20} {:x 29, :y 20} {:x 30, :y 21})}], :marks [{:type \"line\", :from {:data \"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :stroke {:value \"#FF29D2\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"b45b6c4e-f09e-4cb1-8f86-0ce87cc74cb7\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 247 | ;; <= 248 | 249 | ;; @@ 250 | ;; (b)(c)(d) 251 | (let [N 30 252 | n-trials 200 253 | head? #(if (< (rand) 0.6) 1 0) 254 | finals (repeatedly n-trials #(sum (repeatedly N head?)))] 255 | (prn "mean:"(double (mean finals))) 256 | (prn "variance:"(variance finals)) 257 | (histogram finals)) 258 | ;; @@ 259 | ;; -> 260 | ;;; "mean:" 17.79 261 | ;;; "variance:" 6.558693467336684 262 | ;;; 263 | ;; <- 264 | ;; => 265 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"6c7d7c9a-979f-41ed-9277-edd3dcac7833","values":[{"x":10.0,"y":0},{"x":11.555555555555555,"y":1.0},{"x":13.11111111111111,"y":8.0},{"x":14.666666666666666,"y":10.0},{"x":16.22222222222222,"y":43.0},{"x":17.77777777777778,"y":30.0},{"x":19.333333333333336,"y":56.0},{"x":20.888888888888893,"y":24.0},{"x":22.44444444444445,"y":21.0},{"x":24.000000000000007,"y":7.0},{"x":25.555555555555564,"y":0}]}],"marks":[{"type":"line","from":{"data":"6c7d7c9a-979f-41ed-9277-edd3dcac7833"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"interpolate":{"value":"step-before"},"fill":{"value":"steelblue"},"fillOpacity":{"value":0.4},"stroke":{"value":"steelblue"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"6c7d7c9a-979f-41ed-9277-edd3dcac7833","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"6c7d7c9a-979f-41ed-9277-edd3dcac7833","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"6c7d7c9a-979f-41ed-9277-edd3dcac7833\", :values ({:x 10.0, :y 0} {:x 11.555555555555555, :y 1.0} {:x 13.11111111111111, :y 8.0} {:x 14.666666666666666, :y 10.0} {:x 16.22222222222222, :y 43.0} {:x 17.77777777777778, :y 30.0} {:x 19.333333333333336, :y 56.0} {:x 20.888888888888893, :y 24.0} {:x 22.44444444444445, :y 21.0} {:x 24.000000000000007, :y 7.0} {:x 25.555555555555564, :y 0})}], :marks [{:type \"line\", :from {:data \"6c7d7c9a-979f-41ed-9277-edd3dcac7833\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :interpolate {:value \"step-before\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 0.4}, :stroke {:value \"steelblue\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"6c7d7c9a-979f-41ed-9277-edd3dcac7833\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"6c7d7c9a-979f-41ed-9277-edd3dcac7833\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 266 | ;; <= 267 | 268 | ;; @@ 269 | ;; P1.9 270 | ;; I get OutOfMemoryError when trying to solve the problem 271 | ;; @@ 272 | 273 | ;; @@ 274 | ;; P1.10 275 | (let [n 10000 276 | get-5-cards #(take 5 (shuffle (range 52))) 277 | get-card-num #(rem % 13) 278 | one-pair? #(if (= 4 (count (frequencies (map get-card-num %)))) 1 0)] 279 | (double (mean (map one-pair? (repeatedly n get-5-cards))))) 280 | ;; 0.4238 281 | ;; @@ 282 | 283 | ;; @@ 284 | ;; P1.11 285 | (let [n 10000 286 | get-5-cards #(take 5 (shuffle (range 52))) 287 | get-card-num #(rem % 13) 288 | two-pair? #(if (= [1 2 2] (sort (vals (frequencies (map get-card-num %))))) 1 0)] 289 | (double (mean (map two-pair? (repeatedly n get-5-cards))))) 290 | ;; 0.0455 291 | ;; @@ 292 | 293 | ;; @@ 294 | ;; P1.12 295 | (defn craps 296 | [] 297 | (let [get-roll-result #(+ 2 (rand-int 6) (rand-int 6)) 298 | init-roll (get-roll-result) 299 | ] 300 | (cond 301 | (contains? #{7 11} init-roll) :WIN 302 | (contains? #{2 3 12} init-roll) :LOSE 303 | :else 304 | (let [res-mapping {7 :LOSE init-roll :WIN}] 305 | (first (drop-while nil? (map res-mapping (repeatedly get-roll-result)))))))) 306 | 307 | (frequencies (repeatedly 1000 craps)) 308 | ;; {:LOSE 509, :WIN 491} 309 | ;; @@ 310 | 311 | ;; @@ 312 | ;; P1.13 313 | ;; 0.03 * 0.98 / ((1 - 0.03) * (1 - 0.98) + 0.03 * 0.98) 314 | ;; @@ 315 | 316 | ;; @@ 317 | ;; P1.14 318 | (time (dotimes [_ 1000000] (rand))) 319 | ;; "Elapsed time: 75.439993 msecs" 320 | (list-plot (repeatedly 50 #(rand)) :joined true) 321 | ;; @@ 322 | ;; -> 323 | ;;; "Elapsed time: 91.885351 msecs" 324 | ;;; 325 | ;; <- 326 | ;; => 327 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"31e29190-47d4-4ca2-a008-7bc01c6d72b6","values":[{"x":0,"y":0.27907527345410665},{"x":1,"y":0.6378569986593405},{"x":2,"y":0.7927470088464076},{"x":3,"y":0.6820203911837813},{"x":4,"y":0.9176362648557976},{"x":5,"y":0.6122997252760478},{"x":6,"y":0.8849552689466428},{"x":7,"y":0.8411650185305936},{"x":8,"y":0.6033404185001833},{"x":9,"y":0.5532813829651306},{"x":10,"y":0.19226264105020863},{"x":11,"y":0.39061300904659213},{"x":12,"y":0.1650030140332619},{"x":13,"y":0.07294551058998056},{"x":14,"y":0.0953673568524338},{"x":15,"y":0.526046782821984},{"x":16,"y":0.23467154049742556},{"x":17,"y":0.32522490693468853},{"x":18,"y":0.2555858109338126},{"x":19,"y":0.5620941849694683},{"x":20,"y":0.9397197263654794},{"x":21,"y":0.62352919752078},{"x":22,"y":0.43952295102163386},{"x":23,"y":0.8662967670955755},{"x":24,"y":0.6159612771200244},{"x":25,"y":0.07656069407240762},{"x":26,"y":0.6559154166472775},{"x":27,"y":0.733829170075213},{"x":28,"y":0.6223805024723559},{"x":29,"y":0.03306069192176253},{"x":30,"y":0.09198302491252208},{"x":31,"y":0.9512912715526296},{"x":32,"y":0.5343659570486856},{"x":33,"y":0.2366396826004754},{"x":34,"y":0.423537340313312},{"x":35,"y":0.4238707985495914},{"x":36,"y":0.2198717340250731},{"x":37,"y":0.98344676253167},{"x":38,"y":0.6912610799538624},{"x":39,"y":0.6474014412484767},{"x":40,"y":0.561721003411099},{"x":41,"y":0.24828466163285456},{"x":42,"y":0.08498561345414624},{"x":43,"y":0.9953175456321868},{"x":44,"y":0.8793044674082529},{"x":45,"y":0.6988789368897272},{"x":46,"y":0.6584579492757634},{"x":47,"y":0.7602331026517251},{"x":48,"y":0.4242165186064575},{"x":49,"y":0.7533609788096232}]}],"marks":[{"type":"line","from":{"data":"31e29190-47d4-4ca2-a008-7bc01c6d72b6"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"stroke":{"value":"#FF29D2"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"31e29190-47d4-4ca2-a008-7bc01c6d72b6","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"31e29190-47d4-4ca2-a008-7bc01c6d72b6","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"31e29190-47d4-4ca2-a008-7bc01c6d72b6\", :values ({:x 0, :y 0.27907527345410665} {:x 1, :y 0.6378569986593405} {:x 2, :y 0.7927470088464076} {:x 3, :y 0.6820203911837813} {:x 4, :y 0.9176362648557976} {:x 5, :y 0.6122997252760478} {:x 6, :y 0.8849552689466428} {:x 7, :y 0.8411650185305936} {:x 8, :y 0.6033404185001833} {:x 9, :y 0.5532813829651306} {:x 10, :y 0.19226264105020863} {:x 11, :y 0.39061300904659213} {:x 12, :y 0.1650030140332619} {:x 13, :y 0.07294551058998056} {:x 14, :y 0.0953673568524338} {:x 15, :y 0.526046782821984} {:x 16, :y 0.23467154049742556} {:x 17, :y 0.32522490693468853} {:x 18, :y 0.2555858109338126} {:x 19, :y 0.5620941849694683} {:x 20, :y 0.9397197263654794} {:x 21, :y 0.62352919752078} {:x 22, :y 0.43952295102163386} {:x 23, :y 0.8662967670955755} {:x 24, :y 0.6159612771200244} {:x 25, :y 0.07656069407240762} {:x 26, :y 0.6559154166472775} {:x 27, :y 0.733829170075213} {:x 28, :y 0.6223805024723559} {:x 29, :y 0.03306069192176253} {:x 30, :y 0.09198302491252208} {:x 31, :y 0.9512912715526296} {:x 32, :y 0.5343659570486856} {:x 33, :y 0.2366396826004754} {:x 34, :y 0.423537340313312} {:x 35, :y 0.4238707985495914} {:x 36, :y 0.2198717340250731} {:x 37, :y 0.98344676253167} {:x 38, :y 0.6912610799538624} {:x 39, :y 0.6474014412484767} {:x 40, :y 0.561721003411099} {:x 41, :y 0.24828466163285456} {:x 42, :y 0.08498561345414624} {:x 43, :y 0.9953175456321868} {:x 44, :y 0.8793044674082529} {:x 45, :y 0.6988789368897272} {:x 46, :y 0.6584579492757634} {:x 47, :y 0.7602331026517251} {:x 48, :y 0.4242165186064575} {:x 49, :y 0.7533609788096232})}], :marks [{:type \"line\", :from {:data \"31e29190-47d4-4ca2-a008-7bc01c6d72b6\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :stroke {:value \"#FF29D2\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"31e29190-47d4-4ca2-a008-7bc01c6d72b6\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"31e29190-47d4-4ca2-a008-7bc01c6d72b6\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 328 | ;; <= 329 | 330 | ;; @@ 331 | (list-plot (reductions + (repeatedly 50 #(rand))) :joined true) 332 | ;; @@ 333 | ;; => 334 | ;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"3f53930f-3409-4bf7-8688-c148edcef44f","values":[{"x":0,"y":0.20900281962312384},{"x":1,"y":0.849852407247763},{"x":2,"y":1.5267622825549163},{"x":3,"y":1.879388425188755},{"x":4,"y":2.1790377924750395},{"x":5,"y":3.07616101483936},{"x":6,"y":3.631400459533767},{"x":7,"y":4.548480643186319},{"x":8,"y":5.2241867688137456},{"x":9,"y":5.934286303978855},{"x":10,"y":5.995090916221383},{"x":11,"y":6.031479224372763},{"x":12,"y":6.164911259613933},{"x":13,"y":6.5424601235157525},{"x":14,"y":6.877748641955906},{"x":15,"y":7.678941501682832},{"x":16,"y":7.914306584816721},{"x":17,"y":8.766820339600597},{"x":18,"y":9.343352234628515},{"x":19,"y":9.670719702731395},{"x":20,"y":9.80575439427116},{"x":21,"y":10.594859408168007},{"x":22,"y":11.367727473186827},{"x":23,"y":12.313954181891633},{"x":24,"y":12.48715161747375},{"x":25,"y":13.185512306692349},{"x":26,"y":13.93979699598358},{"x":27,"y":14.678009084582722},{"x":28,"y":15.516409426160308},{"x":29,"y":15.55311694486387},{"x":30,"y":16.183384144511944},{"x":31,"y":16.756310678807456},{"x":32,"y":16.90290010118161},{"x":33,"y":16.953442572665363},{"x":34,"y":17.240159974494286},{"x":35,"y":17.82515145242138},{"x":36,"y":18.31279743694662},{"x":37,"y":19.295474693843538},{"x":38,"y":19.543605197347382},{"x":39,"y":19.60665708101211},{"x":40,"y":20.11780350139627},{"x":41,"y":20.927669409292896},{"x":42,"y":21.803921519050256},{"x":43,"y":21.86227756880919},{"x":44,"y":22.5415835532228},{"x":45,"y":22.56694570694582},{"x":46,"y":23.139672129759024},{"x":47,"y":23.769361235825723},{"x":48,"y":24.471341866649876},{"x":49,"y":24.577771177968838}]}],"marks":[{"type":"line","from":{"data":"3f53930f-3409-4bf7-8688-c148edcef44f"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"stroke":{"value":"#FF29D2"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"3f53930f-3409-4bf7-8688-c148edcef44f","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"3f53930f-3409-4bf7-8688-c148edcef44f","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"3f53930f-3409-4bf7-8688-c148edcef44f\", :values ({:x 0, :y 0.20900281962312384} {:x 1, :y 0.849852407247763} {:x 2, :y 1.5267622825549163} {:x 3, :y 1.879388425188755} {:x 4, :y 2.1790377924750395} {:x 5, :y 3.07616101483936} {:x 6, :y 3.631400459533767} {:x 7, :y 4.548480643186319} {:x 8, :y 5.2241867688137456} {:x 9, :y 5.934286303978855} {:x 10, :y 5.995090916221383} {:x 11, :y 6.031479224372763} {:x 12, :y 6.164911259613933} {:x 13, :y 6.5424601235157525} {:x 14, :y 6.877748641955906} {:x 15, :y 7.678941501682832} {:x 16, :y 7.914306584816721} {:x 17, :y 8.766820339600597} {:x 18, :y 9.343352234628515} {:x 19, :y 9.670719702731395} {:x 20, :y 9.80575439427116} {:x 21, :y 10.594859408168007} {:x 22, :y 11.367727473186827} {:x 23, :y 12.313954181891633} {:x 24, :y 12.48715161747375} {:x 25, :y 13.185512306692349} {:x 26, :y 13.93979699598358} {:x 27, :y 14.678009084582722} {:x 28, :y 15.516409426160308} {:x 29, :y 15.55311694486387} {:x 30, :y 16.183384144511944} {:x 31, :y 16.756310678807456} {:x 32, :y 16.90290010118161} {:x 33, :y 16.953442572665363} {:x 34, :y 17.240159974494286} {:x 35, :y 17.82515145242138} {:x 36, :y 18.31279743694662} {:x 37, :y 19.295474693843538} {:x 38, :y 19.543605197347382} {:x 39, :y 19.60665708101211} {:x 40, :y 20.11780350139627} {:x 41, :y 20.927669409292896} {:x 42, :y 21.803921519050256} {:x 43, :y 21.86227756880919} {:x 44, :y 22.5415835532228} {:x 45, :y 22.56694570694582} {:x 46, :y 23.139672129759024} {:x 47, :y 23.769361235825723} {:x 48, :y 24.471341866649876} {:x 49, :y 24.577771177968838})}], :marks [{:type \"line\", :from {:data \"3f53930f-3409-4bf7-8688-c148edcef44f\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :stroke {:value \"#FF29D2\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"3f53930f-3409-4bf7-8688-c148edcef44f\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"3f53930f-3409-4bf7-8688-c148edcef44f\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"} 335 | ;; <= 336 | 337 | ;; @@ 338 | ;; P1.24 339 | ;; p(stick with original choice) = 1/3 340 | ;; p(switch) = 2/3 341 | ;; @@ 342 | 343 | ;; @@ 344 | ;; P1.27 345 | ;; so what's the subtle problem? 346 | (defn rng_prob 347 | [] 348 | (loop [n 10000000 349 | low 0 350 | high 0] 351 | (if (zero? n) 352 | [low high] 353 | (if (< (rand) 0.1) 354 | (do #_(rand) 355 | (recur (dec n) (inc low) high)) 356 | (do #_(rand) 357 | #_(rand) 358 | (recur (dec n) low (inc high))))))) 359 | ;; @@ 360 | --------------------------------------------------------------------------------