-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatistics.ml
More file actions
40 lines (29 loc) · 816 Bytes
/
Statistics.ml
File metadata and controls
40 lines (29 loc) · 816 Bytes
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
open Core.Std
open Random
module type STATISTICS =
sig
type t
type std_dev
(* Returns a pseudo-random normally distributed float
* between 0 and 1*)
val normal : unit -> t
(* Returns a pseudorandom t in the gaussian distribution *)
val gaussian_pick : std_dev -> t -> t
end
(* Implements Statistics of Floats *)
module Statistics : (STATISTICS with type t = float
with type std_dev = float) =
struct
type t = float
type std_dev = float
(* Uses Box-Muller to return a pseudo-random *)
let rec normal () =
let u = Random.float 2.0 -. 1.0 in
let v = Random.float 2.0 -. 1.0 in
let s = u *. u +. v *. v in
if (s >= 1.0 || s = 0.0) then normal ()
else u *. sqrt (-2.0 *. (log s) /. s)
let gaussian_pick s m =
let n = normal () in
n *. s +. m
end