feat: move to using an array for samples

This commit is contained in:
NunoSempere 2023-10-15 02:21:45 +01:00
parent cf76c92803
commit 3befb4af4d
5 changed files with 15 additions and 13 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -43,17 +43,6 @@ let unwind xs =
in in
tailRecursiveHelper xs [] tailRecursiveHelper xs []
(* previous version, which wasn't tail-recursive
match xs with
| [] -> Ok([])
| Error e:: ys -> Error e
| Ok(y) :: ys -> (
match unwind ys with
| Ok(zs) -> Ok(y :: zs)
| Error e -> Error e
)
*)
let unwindSum xs = let unwindSum xs =
let rec tailRecursiveHelper ys sum = let rec tailRecursiveHelper ys sum =
match ys with match ys with
@ -63,6 +52,19 @@ let unwindSum xs =
in in
tailRecursiveHelper xs 0.0 tailRecursiveHelper xs 0.0
(* Array helpers *)
let unwindSumArray xs =
Array.fold_left(fun acc x ->
(
match (acc, x) with
| (Error e, _) -> Error e
| (_, Error e) -> Error e
| (Ok(sum), Ok(y)) -> Ok(sum +. y)
)
) (Ok 0.0) xs
let sumFloats xs = List.fold_left(fun acc x -> acc +. x) 0.0 xs
(* Basic samplers *) (* Basic samplers *)
let sampleZeroToOne () : float = Random.float 1.0 let sampleZeroToOne () : float = Random.float 1.0
@ -112,8 +114,8 @@ let () =
let weights = [ 1. -. p3; p3 /. 2.; p3 /. 4.; p3/. 4. ] in let weights = [ 1. -. p3; p3 /. 2.; p3 /. 4.; p3/. 4. ] in
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 = Array.init n (fun _ -> sampler ()) in
match unwindSum samples with match unwindSumArray samples with
| Error err -> Printf.printf "Error %s\n" err | Error err -> Printf.printf "Error %s\n" err
| Ok(sum) -> ( | Ok(sum) -> (
let mean = sum /. float_of_int(n) in let mean = sum /. float_of_int(n) in