-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage.clj
68 lines (48 loc) · 1.33 KB
/
language.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns anglican.language
(:use [anglican core emit runtime])
(:require [gorilla-plot.core :as plot]))
;; coin model:
;; defquery -> doquery
(defquery coin-model [coin-series]
(let [prob (sample (beta 1 1))] ;; 1. prior
(loop [[f & r :as s] coin-series]
(when (seq s)
(observe (flip prob) f) ;; 2. incorporate data
(recur r)))
prob))
((doquery :smc coin-model [[true]] :number-of-particles 10000)
(take 10)
(map :result))
;; soft constrainsts:
(defquery sum-two
(let [a (- (poisson 100) 100)
b (- (poisson 100) 100)]
(observe (normal (+ a b) 0.00001) 7)
(list a b)))
(doquery :smc sum-two :number-of-particles 10000)
;; sample
(defquery example
(let [bet (sample (beta 5 3))]
(observe (flip bet) true)
(> bet 0.7)))
;; Bayesian linear regression:
(defquery linear-regression []
(let [s (sample (normal 0.0 2.0))
b (sample (normal 0.0 6.0))]
[s b]))
(def samples
(map :result
(take 1000 (take-nth 10 (drop 10 (doquery :lmh linear-regression []))))))
(count samples)
;; soft constrainsts:
(defquery sum-two []
(let [a (- (poisson 100) 100)
b (- (poisson 100) 100)]
(observe (normal (+ a b) 0.00001) 7)
(list a b)
)
)
(def samples
(map :result
(take 10 (take-nth 10 (drop 10 (doquery :lmh sum-two []))))))
(print samples)