   [-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
    3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3])
+
+(def test-observations
+  [1.45 1 0.9 2.5 7.8 8.2 7.9 7.8])
 ;; @@
 ;; =>
 ;;; {"type":"html","content":"<span class='clj-var'>#&#x27;worksheets.dirichlet-process-mixture-stick-breaking/test-observations</span>","value":"#'worksheets.dirichlet-process-mixture-stick-breaking/test-observations"}

;; @@
(def num-particles 100)
(def is-states (take num-particles (infer :importance dirichlet-process-mixture-stick-breaking [test-observations])))
(plot/histogram (repeatedly 1000 #(sample* (categorical (vec (empirical-distribution (collect-results is-states)))))) :normalize :probability-density :bins 100 :plot-range [[-8 15] :all])
;; @@
;; =>
;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"d9ccd0ff-bad7-40ac-af6d-5228e202b7af","values":[{"x":-8.0,"y":0},{"x":-7.77,"y":0.0},{"x":-7.539999999999999,"y":0.0},{"x":-7.309999999999999,"y":0.0},{"x":-7.079999999999998,"y":0.0},{"x":-6.849999999999998,"y":0.0},{"x":-6.619999999999997,"y":0.0},{"x":-6.389999999999997,"y":0.0},{"x":-6.159999999999997,"y":0.0},{"x":-5.929999999999996,"y":0.0},{"x":-5.699999999999996,"y":0.0},{"x":-5.469999999999995,"y":0.0},{"x":-5.239999999999995,"y":0.0},{"x":-5.0099999999999945,"y":0.0},{"x":-4.779999999999994,"y":0.0},{"x":-4.549999999999994,"y":0.0},{"x":-4.319999999999993,"y":0.0},{"x":-4.089999999999993,"y":0.0},{"x":-3.8599999999999928,"y":0.0},{"x":-3.629999999999993,"y":0.0},{"x":-3.399999999999993,"y":0.0},{"x":-3.169999999999993,"y":0.0},{"x":-2.939999999999993,"y":0.0},{"x":-2.709999999999993,"y":0.0},{"x":-2.479999999999993,"y":0.0},{"x":-2.249999999999993,"y":0.0},{"x":-2.019999999999993,"y":0.0},{"x":-1.789999999999993,"y":0.0},{"x":-1.559999999999993,"y":0.0},{"x":-1.329999999999993,"y":0.0},{"x":-1.099999999999993,"y":0.0},{"x":-0.869999999999993,"y":0.0},{"x":-0.639999999999993,"y":0.0},{"x":-0.40999999999999304,"y":0.0},{"x":-0.17999999999999303,"y":0.0},{"x":0.05000000000000698,"y":0.0},{"x":0.280000000000007,"y":0.0},{"x":0.510000000000007,"y":0.0},{"x":0.740000000000007,"y":0.0},{"x":0.970000000000007,"y":0.0},{"x":1.200000000000007,"y":0.0},{"x":1.430000000000007,"y":0.0},{"x":1.660000000000007,"y":0.0},{"x":1.890000000000007,"y":4.3478260869565215},{"x":2.120000000000007,"y":0.0},{"x":2.350000000000007,"y":0.0},{"x":2.580000000000007,"y":0.0},{"x":2.810000000000007,"y":0.0},{"x":3.040000000000007,"y":0.0},{"x":3.270000000000007,"y":0.0},{"x":3.500000000000007,"y":0.0},{"x":3.730000000000007,"y":0.0},{"x":3.960000000000007,"y":0.0},{"x":4.1900000000000075,"y":0.0},{"x":4.420000000000008,"y":0.0},{"x":4.650000000000008,"y":0.0},{"x":4.880000000000009,"y":0.0},{"x":5.110000000000009,"y":0.0},{"x":5.34000000000001,"y":0.0},{"x":5.57000000000001,"y":0.0},{"x":5.8000000000000105,"y":0.0},{"x":6.030000000000011,"y":0.0},{"x":6.260000000000011,"y":0.0},{"x":6.490000000000012,"y":0.0},{"x":6.720000000000012,"y":0.0},{"x":6.950000000000013,"y":0.0},{"x":7.180000000000013,"y":0.0},{"x":7.4100000000000135,"y":0.0},{"x":7.640000000000014,"y":0.0},{"x":7.870000000000014,"y":0.0},{"x":8.100000000000014,"y":0.0},{"x":8.330000000000014,"y":0.0},{"x":8.560000000000015,"y":0.0},{"x":8.790000000000015,"y":0.0},{"x":9.020000000000016,"y":0.0},{"x":9.250000000000016,"y":0.0},{"x":9.480000000000016,"y":0.0},{"x":9.710000000000017,"y":0.0},{"x":9.940000000000017,"y":0.0},{"x":10.170000000000018,"y":0.0},{"x":10.400000000000018,"y":0.0},{"x":10.630000000000019,"y":0.0},{"x":10.860000000000019,"y":0.0},{"x":11.09000000000002,"y":0.0},{"x":11.32000000000002,"y":0.0},{"x":11.55000000000002,"y":0.0},{"x":11.78000000000002,"y":0.0},{"x":12.010000000000021,"y":0.0},{"x":12.240000000000022,"y":0.0},{"x":12.470000000000022,"y":0.0},{"x":12.700000000000022,"y":0.0},{"x":12.930000000000023,"y":0.0},{"x":13.160000000000023,"y":0.0},{"x":13.390000000000024,"y":0.0},{"x":13.620000000000024,"y":0.0},{"x":13.850000000000025,"y":0.0},{"x":14.080000000000025,"y":0.0},{"x":14.310000000000025,"y":0.0},{"x":14.540000000000026,"y":0.0},{"x":14.770000000000026,"y":0.0},{"x":15.000000000000027,"y":0.0},{"x":15.230000000000027,"y":0}]}],"marks":[{"type":"line","from":{"data":"d9ccd0ff-bad7-40ac-af6d-5228e202b7af"},"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":[-8,15]},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"d9ccd0ff-bad7-40ac-af6d-5228e202b7af","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 \"d9ccd0ff-bad7-40ac-af6d-5228e202b7af\", :values ({:x -8.0, :y 0} {:x -7.77, :y 0.0} {:x -7.539999999999999, :y 0.0} {:x -7.309999999999999, :y 0.0} {:x -7.079999999999998, :y 0.0} {:x -6.849999999999998, :y 0.0} {:x -6.619999999999997, :y 0.0} {:x -6.389999999999997, :y 0.0} {:x -6.159999999999997, :y 0.0} {:x -5.929999999999996, :y 0.0} {:x -5.699999999999996, :y 0.0} {:x -5.469999999999995, :y 0.0} {:x -5.239999999999995, :y 0.0} {:x -5.0099999999999945, :y 0.0} {:x -4.779999999999994, :y 0.0} {:x -4.549999999999994, :y 0.0} {:x -4.319999999999993, :y 0.0} {:x -4.089999999999993, :y 0.0} {:x -3.8599999999999928, :y 0.0} {:x -3.629999999999993, :y 0.0} {:x -3.399999999999993, :y 0.0} {:x -3.169999999999993, :y 0.0} {:x -2.939999999999993, :y 0.0} {:x -2.709999999999993, :y 0.0} {:x -2.479999999999993, :y 0.0} {:x -2.249999999999993, :y 0.0} {:x -2.019999999999993, :y 0.0} {:x -1.789999999999993, :y 0.0} {:x -1.559999999999993, :y 0.0} {:x -1.329999999999993, :y 0.0} {:x -1.099999999999993, :y 0.0} {:x -0.869999999999993, :y 0.0} {:x -0.639999999999993, :y 0.0} {:x -0.40999999999999304, :y 0.0} {:x -0.17999999999999303, :y 0.0} {:x 0.05000000000000698, :y 0.0} {:x 0.280000000000007, :y 0.0} {:x 0.510000000000007, :y 0.0} {:x 0.740000000000007, :y 0.0} {:x 0.970000000000007, :y 0.0} {:x 1.200000000000007, :y 0.0} {:x 1.430000000000007, :y 0.0} {:x 1.660000000000007, :y 0.0} {:x 1.890000000000007, :y 4.3478260869565215} {:x 2.120000000000007, :y 0.0} {:x 2.350000000000007, :y 0.0} {:x 2.580000000000007, :y 0.0} {:x 2.810000000000007, :y 0.0} {:x 3.040000000000007, :y 0.0} {:x 3.270000000000007, :y 0.0} {:x 3.500000000000007, :y 0.0} {:x 3.730000000000007, :y 0.0} {:x 3.960000000000007, :y 0.0} {:x 4.1900000000000075, :y 0.0} {:x 4.420000000000008, :y 0.0} {:x 4.650000000000008, :y 0.0} {:x 4.880000000000009, :y 0.0} {:x 5.110000000000009, :y 0.0} {:x 5.34000000000001, :y 0.0} {:x 5.57000000000001, :y 0.0} {:x 5.8000000000000105, :y 0.0} {:x 6.030000000000011, :y 0.0} {:x 6.260000000000011, :y 0.0} {:x 6.490000000000012, :y 0.0} {:x 6.720000000000012, :y 0.0} {:x 6.950000000000013, :y 0.0} {:x 7.180000000000013, :y 0.0} {:x 7.4100000000000135, :y 0.0} {:x 7.640000000000014, :y 0.0} {:x 7.870000000000014, :y 0.0} {:x 8.100000000000014, :y 0.0} {:x 8.330000000000014, :y 0.0} {:x 8.560000000000015, :y 0.0} {:x 8.790000000000015, :y 0.0} {:x 9.020000000000016, :y 0.0} {:x 9.250000000000016, :y 0.0} {:x 9.480000000000016, :y 0.0} {:x 9.710000000000017, :y 0.0} {:x 9.940000000000017, :y 0.0} {:x 10.170000000000018, :y 0.0} {:x 10.400000000000018, :y 0.0} {:x 10.630000000000019, :y 0.0} {:x 10.860000000000019, :y 0.0} {:x 11.09000000000002, :y 0.0} {:x 11.32000000000002, :y 0.0} {:x 11.55000000000002, :y 0.0} {:x 11.78000000000002, :y 0.0} {:x 12.010000000000021, :y 0.0} {:x 12.240000000000022, :y 0.0} {:x 12.470000000000022, :y 0.0} {:x 12.700000000000022, :y 0.0} {:x 12.930000000000023, :y 0.0} {:x 13.160000000000023, :y 0.0} {:x 13.390000000000024, :y 0.0} {:x 13.620000000000024, :y 0.0} {:x 13.850000000000025, :y 0.0} {:x 14.080000000000025, :y 0.0} {:x 14.310000000000025, :y 0.0} {:x 14.540000000000026, :y 0.0} {:x 14.770000000000026, :y 0.0} {:x 15.000000000000027, :y 0.0} {:x 15.230000000000027, :y 0})}], :marks [{:type \"line\", :from {:data \"d9ccd0ff-bad7-40ac-af6d-5228e202b7af\"}, :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 [-8 15]} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"d9ccd0ff-bad7-40ac-af6d-5228e202b7af\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"}
;; <=

;; **

(defquery dirichlet-process-mixture-stick-breaking
  [input-data]
  (let
    [concentration 1.72
     base (fn []
            (let [v (/ 1.0 (sample (gamma 1 10)))]
              (list (sample (normal 0 (sqrt (* v 10)))) 
                    (sqrt v))))
     obs-param (DPmem concentration base)]

