tweak: in ocaml, do the unwinding and the sum at the same time
This commit is contained in:
parent
37a2dab610
commit
2d5378fd53
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -54,6 +54,15 @@ let unwind xs =
|
||||||
)
|
)
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
let unwindSum xs =
|
||||||
|
let rec tailRecursiveHelper ys sum =
|
||||||
|
match ys with
|
||||||
|
| [] -> Ok(sum)
|
||||||
|
| Error e :: _ -> Error e
|
||||||
|
| Ok(y) :: ys -> tailRecursiveHelper ys (y +. sum)
|
||||||
|
in
|
||||||
|
tailRecursiveHelper xs 0.0
|
||||||
|
|
||||||
(* Basic samplers *)
|
(* Basic samplers *)
|
||||||
let sampleZeroToOne () : float = Random.float 1.0
|
let sampleZeroToOne () : float = Random.float 1.0
|
||||||
|
|
||||||
|
@ -104,9 +113,9 @@ let () =
|
||||||
let sampler () = mixture [ sample0; sample1; sampleFew; sampleMany ] weights in
|
let sampler () = mixture [ sample0; sample1; sampleFew; sampleMany ] weights in
|
||||||
let n = 1_000_000 in
|
let n = 1_000_000 in
|
||||||
let samples = List.init n (fun _ -> sampler ()) in
|
let samples = List.init n (fun _ -> sampler ()) in
|
||||||
match unwind samples with
|
match unwindSum samples with
|
||||||
| Error err -> Printf.printf "Error %s\n" err
|
| Error err -> Printf.printf "Error %s\n" err
|
||||||
| Ok(xs) -> (
|
| Ok(sum) -> (
|
||||||
let mean = sumFloats xs /. float_of_int(n) in
|
let mean = sum /. float_of_int(n) in
|
||||||
Printf.printf "Mean: %f\n" mean
|
Printf.printf "Mean: %f\n" mean
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user