 
 ;; **
 ;;; First, run the inference server from torch-infcomp:
-;;;
+;;; 
 ;;; `python -m infcomp.infer`
-;;;
+;;; 
 ;;; Then run the query, specifying number of particles:
 ;; **
 
 ;; @@
+(defn posterior-mean-std [prior-mean prior-std observation-std test-observations]
+  (let [prior-variance (* prior-std prior-std)
+        observation-variance (* observation-std observation-std)
+        num-observations (count test-observations)
+        posterior-mean (+ (/ (* observation-variance prior-mean)
+                              (+ (* num-observations prior-variance) observation-variance))
+                           (/ (* num-observations prior-variance (/ (reduce + test-observations) num-observations))
+                              (+ (* num-observations prior-variance) observation-variance)))
+        posterior-variance (/ 1 (+ (/ 1 prior-variance) (/ num-observations observation-variance)))]
+    [posterior-mean (Math/sqrt posterior-variance)]))
+
+(def test-observations [2.3])
+(def prior-mean 0)
+(def prior-std 1)
+(def observation-std 0.1)
+
+(def posterior-dist (apply normal (posterior-mean-std prior-mean prior-std observation-std test-observations)))
+(def posterior-plot (plot #(Math/exp (observe* posterior-dist %)) [-3 3]))
+;; @@
+;; =>
+;;; {"type":"html","content":"<span class='clj-var'>#&#x27;worksheets.gaussian/posterior-plot</span>","value":"#'worksheets.gaussian/posterior-plot"}
+;; <=
+
+;; @@
 (def num-particles 100)
-(def csis-states (take num-particles (infer :csis gaussian [2.3])))
-(list-plot (vec (empirical-distribution (collect-results csis-states))))
+(def csis-states (take num-particles (infer :csis gaussian test-observations)))
+(compose
+  posterior-plot
+  (histogram (repeatedly 10000 #(sample* (categorical (vec (empirical-distribution (collect-results csis-states)))))) :normalize :probability-density))
 ;; @@
+;; =>
+;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"b9d8502c-ed35-4cf4-9e86-27e00c90168e","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"b9d8502c-ed35-4cf4-9e86-27e00c90168e","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}],"data":[{"name":"b9d8502c-ed35-4cf4-9e86-27e00c90168e","values":[{"x":-3,"y":0.0},{"x":-2.9400000013411045,"y":0.0},{"x":-2.880000002682209,"y":0.0},{"x":-2.8200000040233135,"y":0.0},{"x":-2.760000005364418,"y":0.0},{"x":-2.7000000067055225,"y":0.0},{"x":-2.640000008046627,"y":0.0},{"x":-2.5800000093877316,"y":0.0},{"x":-2.520000010728836,"y":0.0},{"x":-2.4600000120699406,"y":0.0},{"x":-2.400000013411045,"y":0.0},{"x":-2.3400000147521496,"y":0.0},{"x":-2.280000016093254,"y":0.0},{"x":-2.2200000174343586,"y":0.0},{"x":-2.160000018775463,"y":0.0},{"x":-2.1000000201165676,"y":0.0},{"x":-2.040000021457672,"y":0.0},{"x":-1.9800000227987766,"y":0.0},{"x":-1.9200000241398811,"y":0.0},{"x":-1.8600000254809856,"y":0.0},{"x":-1.8000000268220901,"y":0.0},{"x":-1.7400000281631947,"y":0.0},{"x":-1.6800000295042992,"y":0.0},{"x":-1.6200000308454037,"y":0.0},{"x":-1.5600000321865082,"y":4.4E-323},{"x":-1.5000000335276127,"y":4.9113798103E-313},{"x":-1.4400000348687172,"y":3.574780202683354E-303},{"x":-1.3800000362098217,"y":1.8087798075632448E-293},{"x":-1.3200000375509262,"y":6.362277264443301E-284},{"x":-1.2600000388920307,"y":1.5557149147154005E-274},{"x":-1.2000000402331352,"y":2.644465908450867E-265},{"x":-1.1400000415742397,"y":3.1249024413932006E-256},{"x":-1.0800000429153442,"y":2.5669977469695233E-247},{"x":-1.0200000442564487,"y":1.4659023636055675E-238},{"x":-0.9600000455975533,"y":5.819358815166062E-230},{"x":-0.9000000469386578,"y":1.6059638332043679E-221},{"x":-0.8400000482797623,"y":3.080966091073537E-213},{"x":-0.7800000496208668,"y":4.108928743782036E-205},{"x":-0.7200000509619713,"y":3.809437342210934E-197},{"x":-0.6600000523030758,"y":2.4551814429930886E-189},{"x":-0.6000000536441803,"y":1.1000106365270637E-181},{"x":-0.5400000549852848,"y":3.426105323550485E-174},{"x":-0.4800000563263893,"y":7.418140732437264E-167},{"x":-0.4200000576674938,"y":1.1165545874443118E-159},{"x":-0.3600000590085983,"y":1.168302714230096E-152},{"x":-0.30000006034970284,"y":8.498090358137434E-146},{"x":-0.24000006169080734,"y":4.297123512648931E-139},{"x":-0.18000006303191185,"y":1.5105143747621218E-132},{"x":-0.12000006437301636,"y":3.691156182843066E-126},{"x":-0.060000065714120865,"y":6.270331682802175E-120},{"x":-6.705522537231445E-8,"y":7.404728517709424E-114},{"x":0.05999993160367012,"y":6.078806230830808E-108},{"x":0.11999993026256561,"y":3.4691093772710382E-102},{"x":0.1799999289214611,"y":1.3762845015356378E-96},{"x":0.2399999275803566,"y":3.795674647579944E-91},{"x":0.2999999262392521,"y":7.277132329850067E-86},{"x":0.3599999248981476,"y":9.698891457036999E-81},{"x":0.4199999235570431,"y":8.986166459405543E-76},{"x":0.47999992221593857,"y":5.7878511394684175E-71},{"x":0.5399999208748341,"y":2.5914974138440473E-66},{"x":0.5999999195337296,"y":8.066306873558855E-62},{"x":0.659999918192625,"y":1.7453769414996687E-57},{"x":0.7199999168515205,"y":2.625392628171194E-53},{"x":0.779999915510416,"y":2.74530007684044E-49},{"x":0.8399999141693115,"y":1.9956111253979068E-45},{"x":0.899999912828207,"y":1.0084457594385378E-41},{"x":0.9599999114871025,"y":3.5425803158997096E-38},{"x":1.019999910145998,"y":8.651220865755213E-35},{"x":1.0799999088048935,"y":1.4686747475789843E-31},{"x":1.139999907463789,"y":1.7332626920942418E-28},{"x":1.1999999061226845,"y":1.4219806410548192E-25},{"x":1.25999990478158,"y":8.109862896734052E-23},{"x":1.3199999034404755,"y":3.215317571586265E-20},{"x":1.379999902099371,"y":8.861857639885287E-18},{"x":1.4399999007582664,"y":1.6979159876603773E-15},{"x":1.499999899417162,"y":2.2615081632102484E-13},{"x":1.5599998980760574,"y":2.0939709637907934E-11},{"x":1.619999896734953,"y":1.3478255479072613E-9},{"x":1.6799998953938484,"y":6.030971087720636E-8},{"x":1.739999894052744,"y":1.875993556111744E-6},{"x":1.7999998927116394,"y":4.056635356393333E-5},{"x":1.859999891370535,"y":6.098052151343168E-4},{"x":1.9199998900294304,"y":0.006372455777420705},{"x":1.9799998886883259,"y":0.04629275844616156},{"x":2.0399998873472214,"y":0.2337813288199048},{"x":2.099999886006117,"y":0.8207239732100083},{"x":2.1599998846650124,"y":2.0029723270505664},{"x":2.219999883323908,"y":3.398156001567204},{"x":2.2799998819828033,"y":4.007764639778572},{"x":2.339999880641699,"y":3.2858795490140857},{"x":2.3999998793005943,"y":1.8728008725028966},{"x":2.45999987795949,"y":0.7420310868049215},{"x":2.5199998766183853,"y":0.20438222867765646},{"x":2.579999875277281,"y":0.03913404089701856},{"x":2.6399998739361763,"y":0.00520902919965667},{"x":2.699999872595072,"y":4.8200264643181317E-4},{"x":2.7599998712539673,"y":3.100506077674955E-5},{"x":2.8199998699128628,"y":1.3864566166371108E-6},{"x":2.8799998685717583,"y":4.3099329696395804E-8},{"x":2.9399998672306538,"y":9.313765097953726E-10},{"x":2.9999998658895493,"y":1.399170301845073E-11}]},{"name":"9072f028-44db-4bb3-85e0-fb5acb3fbb16","values":[{"x":1.8863403504804086,"y":0},{"x":1.9369828046586914,"y":0.013822394892943063},{"x":1.9876252588369743,"y":0.013822394892943063},{"x":2.0382677130152573,"y":0.049365696046225226},{"x":2.08891016719354,"y":0.2823717813844083},{"x":2.139552621371823,"y":0.7306123014841334},{"x":2.190195075550106,"y":1.708053083199393},{"x":2.2408375297283887,"y":3.7438943881457214},{"x":2.2914799839066715,"y":3.935433288805075},{"x":2.3421224380849544,"y":4.180287141194352},{"x":2.392764892263237,"y":3.0843686889681523},{"x":2.44340734644152,"y":1.4059350233964945},{"x":2.494049800619803,"y":0.4502151479415741},{"x":2.5446922547980857,"y":0.11255378698539352},{"x":2.5953347089763685,"y":0.029619417627735137},{"x":2.6459771631546514,"y":0.005923883525547027},{"x":2.696619617332934,"y":0}]}],"marks":[{"type":"line","from":{"data":"b9d8502c-ed35-4cf4-9e86-27e00c90168e"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"stroke":{"value":"#FF29D2"},"strokeWidth":{"value":2},"strokeOpacity":{"value":1}}}},{"type":"line","from":{"data":"9072f028-44db-4bb3-85e0-fb5acb3fbb16"},"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}}}}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"b9d8502c-ed35-4cf4-9e86-27e00c90168e\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"b9d8502c-ed35-4cf4-9e86-27e00c90168e\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}], :data ({:name \"b9d8502c-ed35-4cf4-9e86-27e00c90168e\", :values ({:x -3, :y 0.0} {:x -2.9400000013411045, :y 0.0} {:x -2.880000002682209, :y 0.0} {:x -2.8200000040233135, :y 0.0} {:x -2.760000005364418, :y 0.0} {:x -2.7000000067055225, :y 0.0} {:x -2.640000008046627, :y 0.0} {:x -2.5800000093877316, :y 0.0} {:x -2.520000010728836, :y 0.0} {:x -2.4600000120699406, :y 0.0} {:x -2.400000013411045, :y 0.0} {:x -2.3400000147521496, :y 0.0} {:x -2.280000016093254, :y 0.0} {:x -2.2200000174343586, :y 0.0} {:x -2.160000018775463, :y 0.0} {:x -2.1000000201165676, :y 0.0} {:x -2.040000021457672, :y 0.0} {:x -1.9800000227987766, :y 0.0} {:x -1.9200000241398811, :y 0.0} {:x -1.8600000254809856, :y 0.0} {:x -1.8000000268220901, :y 0.0} {:x -1.7400000281631947, :y 0.0} {:x -1.6800000295042992, :y 0.0} {:x -1.6200000308454037, :y 0.0} {:x -1.5600000321865082, :y 4.4E-323} {:x -1.5000000335276127, :y 4.9113798103E-313} {:x -1.4400000348687172, :y 3.574780202683354E-303} {:x -1.3800000362098217, :y 1.8087798075632448E-293} {:x -1.3200000375509262, :y 6.362277264443301E-284} {:x -1.2600000388920307, :y 1.5557149147154005E-274} {:x -1.2000000402331352, :y 2.644465908450867E-265} {:x -1.1400000415742397, :y 3.1249024413932006E-256} {:x -1.0800000429153442, :y 2.5669977469695233E-247} {:x -1.0200000442564487, :y 1.4659023636055675E-238} {:x -0.9600000455975533, :y 5.819358815166062E-230} {:x -0.9000000469386578, :y 1.6059638332043679E-221} {:x -0.8400000482797623, :y 3.080966091073537E-213} {:x -0.7800000496208668, :y 4.108928743782036E-205} {:x -0.7200000509619713, :y 3.809437342210934E-197} {:x -0.6600000523030758, :y 2.4551814429930886E-189} {:x -0.6000000536441803, :y 1.1000106365270637E-181} {:x -0.5400000549852848, :y 3.426105323550485E-174} {:x -0.4800000563263893, :y 7.418140732437264E-167} {:x -0.4200000576674938, :y 1.1165545874443118E-159} {:x -0.3600000590085983, :y 1.168302714230096E-152} {:x -0.30000006034970284, :y 8.498090358137434E-146} {:x -0.24000006169080734, :y 4.297123512648931E-139} {:x -0.18000006303191185, :y 1.5105143747621218E-132} {:x -0.12000006437301636, :y 3.691156182843066E-126} {:x -0.060000065714120865, :y 6.270331682802175E-120} {:x -6.705522537231445E-8, :y 7.404728517709424E-114} {:x 0.05999993160367012, :y 6.078806230830808E-108} {:x 0.11999993026256561, :y 3.4691093772710382E-102} {:x 0.1799999289214611, :y 1.3762845015356378E-96} {:x 0.2399999275803566, :y 3.795674647579944E-91} {:x 0.2999999262392521, :y 7.277132329850067E-86} {:x 0.3599999248981476, :y 9.698891457036999E-81} {:x 0.4199999235570431, :y 8.986166459405543E-76} {:x 0.47999992221593857, :y 5.7878511394684175E-71} {:x 0.5399999208748341, :y 2.5914974138440473E-66} {:x 0.5999999195337296, :y 8.066306873558855E-62} {:x 0.659999918192625, :y 1.7453769414996687E-57} {:x 0.7199999168515205, :y 2.625392628171194E-53} {:x 0.779999915510416, :y 2.74530007684044E-49} {:x 0.8399999141693115, :y 1.9956111253979068E-45} {:x 0.899999912828207, :y 1.0084457594385378E-41} {:x 0.9599999114871025, :y 3.5425803158997096E-38} {:x 1.019999910145998, :y 8.651220865755213E-35} {:x 1.0799999088048935, :y 1.4686747475789843E-31} {:x 1.139999907463789, :y 1.7332626920942418E-28} {:x 1.1999999061226845, :y 1.4219806410548192E-25} {:x 1.25999990478158, :y 8.109862896734052E-23} {:x 1.3199999034404755, :y 3.215317571586265E-20} {:x 1.379999902099371, :y 8.861857639885287E-18} {:x 1.4399999007582664, :y 1.6979159876603773E-15} {:x 1.499999899417162, :y 2.2615081632102484E-13} {:x 1.5599998980760574, :y 2.0939709637907934E-11} {:x 1.619999896734953, :y 1.3478255479072613E-9} {:x 1.6799998953938484, :y 6.030971087720636E-8} {:x 1.739999894052744, :y 1.875993556111744E-6} {:x 1.7999998927116394, :y 4.056635356393333E-5} {:x 1.859999891370535, :y 6.098052151343168E-4} {:x 1.9199998900294304, :y 0.006372455777420705} {:x 1.9799998886883259, :y 0.04629275844616156} {:x 2.0399998873472214, :y 0.2337813288199048} {:x 2.099999886006117, :y 0.8207239732100083} {:x 2.1599998846650124, :y 2.0029723270505664} {:x 2.219999883323908, :y 3.398156001567204} {:x 2.2799998819828033, :y 4.007764639778572} {:x 2.339999880641699, :y 3.2858795490140857} {:x 2.3999998793005943, :y 1.8728008725028966} {:x 2.45999987795949, :y 0.7420310868049215} {:x 2.5199998766183853, :y 0.20438222867765646} {:x 2.579999875277281, :y 0.03913404089701856} {:x 2.6399998739361763, :y 0.00520902919965667} {:x 2.699999872595072, :y 4.8200264643181317E-4} {:x 2.7599998712539673, :y 3.100506077674955E-5} {:x 2.8199998699128628, :y 1.3864566166371108E-6} {:x 2.8799998685717583, :y 4.3099329696395804E-8} {:x 2.9399998672306538, :y 9.313765097953726E-10} {:x 2.9999998658895493, :y 1.399170301845073E-11})} {:name \"9072f028-44db-4bb3-85e0-fb5acb3fbb16\", :values ({:x 1.8863403504804086, :y 0} {:x 1.9369828046586914, :y 0.013822394892943063} {:x 1.9876252588369743, :y 0.013822394892943063} {:x 2.0382677130152573, :y 0.049365696046225226} {:x 2.08891016719354, :y 0.2823717813844083} {:x 2.139552621371823, :y 0.7306123014841334} {:x 2.190195075550106, :y 1.708053083199393} {:x 2.2408375297283887, :y 3.7438943881457214} {:x 2.2914799839066715, :y 3.935433288805075} {:x 2.3421224380849544, :y 4.180287141194352} {:x 2.392764892263237, :y 3.0843686889681523} {:x 2.44340734644152, :y 1.4059350233964945} {:x 2.494049800619803, :y 0.4502151479415741} {:x 2.5446922547980857, :y 0.11255378698539352} {:x 2.5953347089763685, :y 0.029619417627735137} {:x 2.6459771631546514, :y 0.005923883525547027} {:x 2.696619617332934, :y 0})}), :marks ({:type \"line\", :from {:data \"b9d8502c-ed35-4cf4-9e86-27e00c90168e\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :stroke {:value \"#FF29D2\"}, :strokeWidth {:value 2}, :strokeOpacity {:value 1}}}} {:type \"line\", :from {:data \"9072f028-44db-4bb3-85e0-fb5acb3fbb16\"}, :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}}}})}}"}
+;; <=
 
 ;; **
 ;;; Using the same number of particles, we don't have such good results with importance sampling:

;; @@
(def torch-connection (start-torch-connection gaussian [1] combine-observes-fn))
;; @@

;; **
;;; Then run the following to train the neural network:
;;;
;;; `python -m infcomp.compile`
;; **

;; @@
(stop-torch-connection torch-connection)
;; @@

;; **
;;; ## Inference


;; @@
(defquery gaussian [obs]
  (let [mean (sample (normal 0 1))
        std 0.1]
    (observe (normal mean std) obs)
    mean))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#&#x27;worksheets.gaussian/gaussian</span>","value":"#'worksheets.gaussian/gaussian"}
;; <=

;; **
;;; ## Inference Compilation
