Source file linear_regression.ml
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
(** Testing various backends on bayesian linear regression *)
open Dagger
(** {2 Setup and helper definitions } *)
(** {3 Definition of the non-linearity to infer } *)
let nonlinearity x = x *. x
let intercept = 0.03
let coeff = 12.2
(** {3 Sampling of a synthetic data set } *)
let rng_state = RNG.make [| 0x1337; 0x533D |]
let synthetic_data_set =
List.init 100 (fun x ->
let x = float_of_int x /. 10. in
let y =
intercept
+. (coeff *. nonlinearity x)
+. Stats_dist.Gen.gaussian ~mean:0.0 ~std:1.0 rng_state
in
(x, y))
(** {3 Plotting helpers } *)
let from_parameters ~intercept ~coeff =
List.init 100 (fun x ->
let x = float_of_int x /. 10. in
let y = intercept +. (coeff *. nonlinearity x) in
Plot.r2 x y)
let plot title samples =
if Helpers.produce_artifacts then
let truth = from_parameters ~intercept ~coeff in
let few_samples =
Array.to_list samples |> List.sort_uniq compare
|> List.map (fun (intercept, coeff) ->
Plot.Line.line_2d
~style:Plot.Style.(default |> set_circle ~radius:1.)
~points:Plot.Data.(of_list (from_parameters ~intercept ~coeff))
())
in
Plot.(
run
~target:(qt ())
exec
(plot2
~xaxis:"x"
~yaxis:"freq"
~title
([ Line.line_2d
~style:Style.default
~points:(Data.of_list truth)
~legend:"truth"
();
Scatter.points_2d
~points:
(Data.of_seq
@@ Seq.map (fun (x, y) -> r2 x y)
@@ List.to_seq synthetic_data_set)
~style:Style.default
~legend:"data"
() ]
@ few_samples)))
(** {2 Regression using Lmh_inference } *)
module Traced = struct
open Lmh_inference
let regression data (nonlinearity : float -> float) =
let open Infix in
let* intercept = sample (Stats_dist.gaussian ~mean:0.0 ~std:1.0) in
let* coeff = sample (Stats_dist.gaussian ~mean:0.0 ~std:15.) in
let+ () =
List.fold_left
(fun acc (x, y) ->
let predicted = intercept +. (coeff *. nonlinearity x) in
let s = Stats.Pdfs.gaussian ~mean:predicted ~std:1.0 y in
let* () = score s in
acc)
(return ())
data
in
(intercept, coeff)
let model = regression synthetic_data_set nonlinearity
let eval nsamples : (float * float) array =
Lmh_inference.stream_samples model rng_state
|> Helpers.drop 1000 |> Helpers.take nsamples |> Array.of_seq
let test =
QCheck.Test.make ~name:"linear regression, traced" ~count:1 QCheck.unit
@@ fun () ->
let t0 = Unix.gettimeofday () in
let res = eval 2000 in
let t1 = Unix.gettimeofday () in
Format.printf "data generated in %f seconds@." (t1 -. t0) ;
let () = Format.printf "samples: %d@." (Array.length res) in
let () = plot "regression, traced" res in
Array.for_all
(fun (a, b) ->
abs_float (a -. intercept) <. 3.0 && abs_float (b -. coeff) <. 0.5)
res
end
(** {2 Regression using Lmh_incremental_inference } *)
module Traced_incremental = struct
open Lmh_incremental_inference
let regression data (nonlinearity : float -> float) =
let open Infix in
let* intercept = sample (Stats_dist.gaussian ~mean:0.0 ~std:1.0) in
let* coeff = sample (Stats_dist.gaussian ~mean:0.0 ~std:15.) in
let+ () =
List.fold_left
(fun acc (x, y) ->
let predicted = intercept +. (coeff *. nonlinearity x) in
let s = Stats.Pdfs.gaussian ~mean:predicted ~std:1.0 y in
let* () = score s in
acc)
(return ())
data
in
(intercept, coeff)
let model = regression synthetic_data_set nonlinearity
let eval nsamples : (float * float) array =
Lmh_incremental_inference.stream_samples model rng_state
|> Helpers.drop 1000 |> Helpers.take nsamples |> Array.of_seq
let test =
QCheck.Test.make
~name:"linear regression, traced, incremental"
~count:1
QCheck.unit
@@ fun () ->
let t0 = Unix.gettimeofday () in
let res = eval 2000 in
let t1 = Unix.gettimeofday () in
Format.printf "data generated in %f seconds@." (t1 -. t0) ;
let () = Format.printf "samples: %d@." (Array.length res) in
let () = plot "regression, traced (incremental)" res in
Array.for_all
(fun (a, b) ->
abs_float (a -. intercept) <. 3.0 && abs_float (b -. coeff) <. 0.5)
res
end
(** {2 Regression using Smc_inference } *)
module Smc = struct
open Smc_inference.Unit_smc
let regression data (nonlinearity : float -> float) =
let open Infix in
let* intercept = sample (Stats_dist.gaussian ~mean:0.0 ~std:1.0) in
let* coeff = sample (Stats_dist.gaussian ~mean:0.0 ~std:15.) in
let+ () =
List.fold_left
(fun acc (x, y) ->
let predicted = intercept +. (coeff *. nonlinearity x) in
let s = Stats.Pdfs.gaussian ~mean:predicted ~std:1.0 y in
let* () = score s in
let* () = yield () in
acc)
(return ())
data
in
(intercept, coeff)
let model = regression synthetic_data_set nonlinearity
module R2 :
Basic_structures.Basic_intf.Module_std
with type R.t = float
and type t = float * float = struct
module R = Basic_structures.Basic_impl.Reals.Float
type t = float * float
let zero = (0., 0.)
let add (x, y) (x', y') = (x +. x', y +. y')
let neg (x, y) = (~-.x, ~-.y)
let smul s (x, y) = (s *. x, s *. y)
let pp fmtr (x, y) = Format.fprintf fmtr "(%f, %f)" x y
let hash = Hashtbl.hash
let compare = Stdlib.compare
let equal (x, y) (x', y') = x =. x' && y =. y'
end
module FM =
Basic_structures.Basic_impl.Free_module.Float_valued.Make_with_map (R2)
module Table = Hashtbl.Make (R2)
let eval nsamples =
run
(Smc_inference.stratified_resampling ~ess_threshold:0.5)
()
~npart:nsamples
model
rng_state
let average pop =
let pop =
pop.terminated
|> Array.map (fun (p, w) -> (p, w /. pop.total_mass))
|> Stats.Fin.Float.of_assoc (module Table)
in
Stats.Fin.Float.measure pop
|> Stats.Fin.Float.normalize |> Stats.Fin.Float.as_measure
|> Stats.Fin.Float.mean_generic (module R2)
let test =
QCheck.Test.make ~name:"linear regression, smc" ~count:1 QCheck.unit
@@ fun () ->
let t0 = Unix.gettimeofday () in
let pop =
eval 2000
|> Seq.filter_map (fun pop ->
if Array.length pop.active = 0 then Some pop else None)
|> List.of_seq |> List.hd
in
let t1 = Unix.gettimeofday () in
Format.printf "data generated in %f seconds@." (t1 -. t0) ;
let res = average pop in
let () =
plot
(Printf.sprintf "regression, smc (y0=%f, coeff=%f)" (fst res) (snd res))
[| res |]
in
Array.for_all
(fun (a, b) ->
abs_float (a -. intercept) <. 3.0 && abs_float (b -. coeff) <. 0.5)
[| res |]
end
let tests = [Traced.test; Traced_incremental.test; Smc.test]