print mean at the end

This commit is contained in:
NunoSempere 2023-10-15 00:44:20 +01:00
parent 3950946d68
commit 4c4d053ab9
5 changed files with 18 additions and 2 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,15 +4,18 @@ let normal_95_ci_length = 1.6448536269514722
(* List manipulation helpers *) (* List manipulation helpers *)
let sumFloats xs = List.fold_left(fun acc x -> acc +. x) 0.0 xs let sumFloats xs = List.fold_left(fun acc x -> acc +. x) 0.0 xs
let normalizeXs xs = let normalizeXs xs =
let sum_xs = sumFloats xs in let sum_xs = sumFloats xs in
List.map(fun x -> x /. sum_xs) xs List.map(fun x -> x /. sum_xs) xs
let cumsumXs xs = let cumsumXs xs =
let _, cum_sum = List.fold_left(fun (sum, ys) x -> let _, cum_sum = List.fold_left(fun (sum, ys) x ->
let new_sum = sum +. x in let new_sum = sum +. x in
new_sum, ys @ [new_sum] new_sum, ys @ [new_sum]
) (0.0, []) xs in ) (0.0, []) xs in
cum_sum cum_sum
let rec nth xs (n: int) = let rec nth xs (n: int) =
match xs with match xs with
| [] -> None | [] -> None
@ -31,6 +34,15 @@ let findIndex xs test =
in in
recursiveHelper xs 0 recursiveHelper xs 0
let rec unwind xs =
match xs with
| Some(y) :: ys -> (
match unwind ys with
| Some(zs) -> Some(y :: zs)
| None -> None
)
| None :: ys -> None
(* Basic samplers *) (* Basic samplers *)
let sampleZeroToOne () : float = Random.float 1.0 let sampleZeroToOne () : float = Random.float 1.0
@ -81,5 +93,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
(* let mean = sumFloats samples /. n in *) match unwind samples with
Printf.printf "Hello world\n" | None -> Printf.printf "error"
| Some(xs) -> (
let mean = sumFloats xs /. float_of_int(n) in
Printf.printf "Mean: %f" mean
)