-
Notifications
You must be signed in to change notification settings - Fork 9
/
bo.clj
316 lines (273 loc) · 11.7 KB
/
bo.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
(ns fastmath.optimization.bo
(:require [fastmath.gp :as gp]
[fastmath.random :as r]
[fastmath.kernel :as k]
[fastmath.core :as m]
[fastmath.optimization :as opt]))
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(defmulti utility-function :method)
(defmethod utility-function :default [m]
(utility-function (assoc m :method :ucb)))
(defmethod utility-function :ucb
[{:keys [^double kappa gp]
:or {kappa 2.576}}]
(fn [& x]
(let [[^double mean ^double stddev] (gp/predict gp x true)]
(+ mean (* kappa stddev)))))
(defmethod utility-function :ei
[{:keys [^double y-max gp ^double xi]
:or {xi 0.001}}]
(let [d (+ y-max xi)]
(fn [& x]
(let [[^double mean ^double stddev] (gp/predict gp x true)
diff (- mean d)
z (/ diff stddev)]
(+ (* diff ^double (r/cdf r/default-normal z))
(* stddev ^double (r/pdf r/default-normal z)))))))
(defmethod utility-function :poi
[{:keys [^double y-max gp ^double xi]
:or {xi 0.001}}]
(let [d (+ y-max xi)]
(fn [& x]
(let [[^double mean ^double stddev] (gp/predict gp x true)]
(r/cdf r/default-normal (/ (- mean d) stddev))))))
(defmethod utility-function :pi [m]
(utility-function (assoc m :method :poi)))
(defmethod utility-function :ei-pi
[{:keys [^double kappa] :as m
:or {kappa 1.0}}]
(let [ei (utility-function (assoc m :method :ei))
pi (utility-function (assoc m :method :poi))]
(fn [& x]
(let [^double vei (apply ei x)
^double vpi (apply pi x)]
(+ vpi (* kappa vei))))))
;; https://www.ijcai.org/Proceedings/2020/0316.pdf
(defmethod utility-function :rgp-ucb
[{:keys [^double scale ^long t] :as m
:or {scale 1.0 t 0}}]
(let [t (+ t 2)
shape (max 0.01 (/ (m/log (/ (inc (* t t)) m/SQRT2PI))
(m/log (inc scale))))
gamma (r/distribution :gamma {:shape shape scale scale})
beta (m/sqrt (r/drandom gamma))]
(utility-function (assoc m :kappa beta :method :ucb))))
;;
(defn- cartesian-prod
[l1 & rst]
(if (seq rst)
(for [l l1
r (apply cartesian-prod rst)]
(conj r l))
(map list l1)))
(defn prepare-args
([args] (prepare-args args true))
([args append?]
(let [categorical (map first (filter (comp set? second) args))
continuous (keys (apply dissoc args categorical))
result (if (seq categorical)
(let [categorical-sel (apply juxt categorical)
space (map #(vector (zipmap categorical %)
continuous) (apply cartesian-prod (categorical-sel args)))]
{::categorical categorical
::continuous continuous
::space space})
{::space continuous
::continuous continuous})]
(if append?
(merge args result)
result))))
(defn- ensure-prepared-args
[args]
(if (::space args) args (prepare-args args)))
(defn continuous-restrict
([args categorical-var categorical-val continuous-vars]
(continuous-restrict args #(= (% categorical-var) categorical-val) continuous-vars))
([args pred continuous-vars]
(-> args
(ensure-prepared-args)
(update ::space #(map (fn [[m c]]
(if (pred m)
[m continuous-vars]
[m c])) %)))))
(defn continuous-append
([args categorical-var categorical-val continuous-vars]
(continuous-append args #(= (% categorical-var) categorical-val) continuous-vars))
([args pred continuous-vars]
(-> args
(ensure-prepared-args)
(update ::space #(map (fn [[m c]]
(if (pred m)
[m (distinct (concat c continuous-vars))]
[m c])) %)))))
(defn- continuous-restrict-or-append-key
[f args categorical-var deps]
(reduce (fn [args [cat-val cont-vars]]
(f args categorical-var cat-val cont-vars)) args deps))
(defn continuous-restrict-key
[args categorical-var deps]
(continuous-restrict-or-append-key continuous-restrict args categorical-var deps))
(defn continuous-append-key
[args categorical-var deps]
(continuous-restrict-or-append-key continuous-append args categorical-var deps))
(defn categorical-drop
([args pred]
(let [prepared-args (ensure-prepared-args args)]
(update prepared-args ::space #(remove (comp pred first) %))))
([args k pred]
(let [vs (-> args
(ensure-prepared-args)
(update ::space #(->> %
(map (fn [[m c]]
(if (pred m)
[(dissoc m k) c]
[m c])))
(group-by first)
(map (fn [[m lst]]
[m (->> (map second lst)
(mapcat identity)
(distinct))])))))]
vs)))
(defn categorical-keep
([args pred]
(categorical-drop args (complement pred)))
([args k pred]
(categorical-drop args k (complement pred))))
;; (def argg {:a #{:a :b}
;; :b #{:x :z}
;; :c #{1 2}
;; :z [1 2]
;; :w [9 6]})
;; (-> (prepare-args argg)
;; (continuous-restrict #(and (= (:a %) :b) (= (:b %) :x)) [:z])
;; (categorical-drop :c #(not= (:b %) :z)))
(require '[fastmath.clustering :as clust])
(def example-data
(shuffle (concat
(r/->seq (r/distribution :weibull {:alpha 3 :beta 5}) 1000)
(r/->seq (r/distribution :gamma {:shape 10.0}) 1000))))
(defn target
[{:keys [clustering d1 d2] :as args}]
(let [clusters (clust/regroup ((get {:x-means clust/x-means
:clarans clust/clarans
:deterministic-annealing clust/deterministic-annealing} clustering) example-data 2))
distr1 (r/distribution d1 args)
distr2 (r/distribution d2 args)]
(+ (r/log-likelihood distr1 (:data (first clusters)))
(r/log-likelihood distr2 (:data (second clusters))))))
(target {:clustering :x-means
:d1 :gamma
:d2 :weibull
:alpha 3.0
:beta 5.0
:shape 10.0
:scale 4.0
:k 2.0
:lambda 2.0})
(def args (-> {:clustering #{:x-means :clarans :deterministic-annealing}
:d1 #{:gamma :weibull :erlang}
:d2 #{:gamma :weibull :erlang}
:dist #{:euclidean :manhattan :chebyshev}
:alpha [0.01 10]
:beta [0.01 10]
:shape [1.0 20.0]
:scale [1.0 10.0]
:k [1.0 10.0]
:lambda [0.1 10.0]}
(continuous-restrict-key :d1 {:erlang [:k :lambda]
:weibull [:alpha :beta]
:gamma [:shape :scale]}) ;; replaces continuous list with provided for given pair key/val from categorical
(continuous-append-key :d2 {:erlang [:k :lambda]
:weibull [:alpha :beta]
:gamma [:shape :scale]})
(categorical-drop #(= (:d1 %) (:d2 %))) ;; removes categorical when pred is true
(categorical-keep :dist #(= (:clustering %) :clarans)) ;; keeps given key only when pred is true
(categorical-drop #(and (= (:d1 %) :weibull)
(not= (:clustering %) :x-means)))))
(def args-cont {:alpha [0.01 10]
:beta [0.01 10]
:shape [1.0 20.0]
:scale [1.0 10.0]
:k [1.0 10.0]
:lambda [0.1 10.0]})
(defn- make-forward-backward
[args ks forward?]
(into {} (map (fn [k]
(let [[mn mx] (args k)]
[k (if forward?
(m/make-norm mn mx 0.0 1.0)
(m/make-norm 0.0 1.0 mn mx))])) ks)))
(defn- make-x-normalizer
[args]
(let [ks (::continuous args)
forward (make-forward-backward args ks true)
backward (make-forward-backward args ks false)]
(fn local-normalizer
([m] (local-normalizer m true))
([m forward?]
(let [fm (if forward? forward backward)]
(reduce (fn [curr k]
(update curr k (fm k))) m (keys m)))))))
(defn initialize-bo
([args] (initialize-bo args {}))
([args {:keys [noise normalize-y? normalize-x? utility-function-type utility-function-params
kernel kernel-params kernel-scale
optimizer optimizer-params
jitter]
:or {noise 1.0e-5
normalize-y? true
normalize-x? true
utility-function-type :rgp-ucb
kernel :mattern-12
optimizer :bfgs
jitter 0.1}}]
(let [args (ensure-prepared-args args)
bandit? (::categorical args)
x-normalizer (if-not normalize-x?
(fn [m & _] m)
(make-x-normalizer args))]
{:args args
:noise noise
:normalize-y? normalize-y?
:x-normalizer x-normalizer
:bandit? bandit?
:utility-function-type utility-function-type
:utility-function-params utility-function-params
:kernel kernel
:kernel-params kernel-params
:kernel-scale kernel-scale
:optimizer optimizer
:optimizer-params optimizer-params
:jitter jitter})))
(comment
(def gp (gp/gaussian-process [[1 1] [2 2] [3 3] [9 -1] [5 -2]] [-2 -1 1 -1 2] {:noise 0.001
:normalize? true
:kernel (k/kernel :mattern-52 1.48796)
:kscale 0.87396}))
(def gp (gp/gaussian-process [[1 1]] [-2] {:noise 0.001
:normalize? true
:kernel (k/kernel :mattern-52)
:kscale 0.87396}))
(gp/predict gp '(4.62100181160906 -3.5))
(opt/scan-and-maximize :bfgs (utility-function {:method :ucb
:gp gp}) {:bounds [[0 10] [-5 5]]})
;; => [(4.862100181160906 -2.349996775981009) 0.557154163177696]
;; => [(4.6001465276639495 -3.531503947537969) 4.333240536082748]
;; => [(4.981048745997669 -2.0285429857231874) 0.4899829648551593]
;; => [(4.750949198693047 -2.818882138909999) 0.19133431735709724]
;; => [(4.567607972440291 -3.6893979394097896) 4.945040161917608]
;; => [(4.600145774519866 -3.5315036892286065) 4.333240536082746]
(time (opt/scan-and-maximize :bfgs (fn [^double k ^double s]
;; (println k)
(let [gp (gp/gaussian-process [[1 1] [2 2] [3 3] [9 -1] [5 -2]] [-2 -1 1 -1 2]
{:noise 0.0001
:normalize? true
:kernel (k/kernel :mattern-52 k)
:kscale s})]
(gp/L gp))) {:bounds [[0.001 20] [0.001 20]]
:initial [3.0]}))
(gp/L gp)
(gp/predict gp [2 22] true)
(gp/predict-all gp [[1 1] [2 2]] true)
)