-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratch.lisp
116 lines (88 loc) · 3.65 KB
/
scratch.lisp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(in-package #:bayesian-analysis)
(define-data-class 1d-data (x "x") y err
(object (source t))
(setf x (make-array 4 :initial-contents '(-1d0 0d0 1d0 2d0)
:element-type 'double-float)
y (make-array 4 :initial-contents '(2.99d0 3.1d0 3.1d0 2.99d0)
:element-type 'double-float)
err (make-array 4 :initial-contents '(0.1d0 0.1d0 0.2d0 0.1d0)
:element-type 'double-float)))
;(initialize-from-source '1d-data t)
(define-bayesian-model (quadratic 1d-data)
((a :default 0.5 :min -1 :max 1 :prior-type :uniform :sample-sigma 0.1d0)
(b :prior-type :uniform :default -0.5)
(c :prior-type :uniform :default 2 :min 2 :max 4 :sample-sigma 0.1d0))
(:d_i=f_i+gaussian_error_i_unequal_sigma)
((x) (+ (* a x) (* b x x) c)))
(define-bayesian-model (linear 1d-data)
((a :default 1 :min -1 :max 1 :prior-type :uniform :sample-sigma 0.1d0)
(b :prior-type :uniform :default 2 :min 2 :max 4 :sample-sigma 0.1d0))
(:d_i=f_i+gaussian_error_i)
((x)
(+ (* a x) b)))
(defparameter *test-model* (make-instance 'quadratic))
(labels ((cmd (fmt-str &rest args)
(mgl-gnuplot:command (apply #'format nil fmt-str args))))
(mgl-gnuplot:with-session ()
(cmd "reset")
(cmd "set terminal x11 enhanced font 'Georgia,8' dashed")
(plot-iteration-values
(optimize (make-instance 'metropolis-hastings :no-iterations 50000)
(make-instance 'quadratic)
(initialize-from-source '1d-data t))
:every 1 :end 5000)
(cmd "unset output")))
(labels ((cmd (fmt-str &rest args)
(mgl-gnuplot:command (apply #'format nil fmt-str args))))
(mgl-gnuplot:with-session ()
(cmd "reset")
(cmd "set terminal x11 enhanced font 'Georgia,12' dashed")
(plot-parameter-distribution
(get-parameter-results
(optimize (make-instance 'metropolis-hastings :no-iterations 500000)
(make-instance 'quadratic :b-bin-width 0.01 :a-bin-width 0.01 :c-bin-width 0.004)
(initialize-from-source '1d-data t))
:confidence-level 0.92
:start 200)
'c )
(cmd "unset output")))
(labels ((cmd (fmt-str &rest args)
(mgl-gnuplot:command (apply #'format nil fmt-str args))))
(mgl-gnuplot:with-session ()
(cmd "reset")
(cmd "set terminal wxt enhanced font 'Georgia,12' dashed")
(plot-result
(get-parameter-results
(optimize (make-instance 'metropolis-hastings :no-iterations 100000)
(make-instance 'quadratic :b-bin-width 0.001 :a-bin-width 0.001)
(initialize-from-source '1d-data t))
:start 200))
(cmd "unset output")))
(labels ((cmd (fmt-str &rest args)
(mgl-gnuplot:command (apply #'format nil fmt-str args))))
(mgl-gnuplot:with-session ()
(cmd "reset")
(cmd "set terminal wxt enhanced font 'Georgia,8' dashed")
(plot-data (initialize-from-source '1d-data t))
(cmd "unset output")))
(let+ ((r1 (get-parameter-results
(optimize
(make-instance 'metropolis-hastings :no-iterations 100000)
(make-instance 'linear :b-bin-width 0.005 :a-bin-width 0.005)
(initialize-from-source '1d-data t)) :start 200))
(r2 (get-parameter-results
(optimize
(make-instance 'metropolis-hastings :no-iterations 100000)
(make-instance 'quadratic :b-bin-width 0.005 :a-bin-width 0.005)
(initialize-from-source '1d-data t)) :start 200))
(m1 (model r1))
(m2 (model r2)))
; (incf (a m1) 0.01)
(labels ((cmd (fmt-str &rest args)
(mgl-gnuplot:command (apply #'format nil fmt-str args))))
(mgl-gnuplot:with-session ()
(cmd "reset")
(cmd "set terminal wxt enhanced font 'Georgia,12'")
(plot-result r2)
(cmd "unset output")))
(calculate-odds-ratio-1/2 m1 m2 (initialize-from-source '1d-data t)))