commit 2cddf557bf56a38ec20fdbad4bc2ca80f4aeb833
parent ffec4663fc601750b72de1d0768dcf1db10449fe
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sat, 14 Oct 2023 20:50:25 +0100
start adding mixture function
Diffstat:
5 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/ocaml/out/samples b/ocaml/out/samples
Binary files differ.
diff --git a/ocaml/out/samples.cmi b/ocaml/out/samples.cmi
Binary files differ.
diff --git a/ocaml/out/samples.cmx b/ocaml/out/samples.cmx
Binary files differ.
diff --git a/ocaml/out/samples.o b/ocaml/out/samples.o
Binary files differ.
diff --git a/ocaml/samples.ml b/ocaml/samples.ml
@@ -2,6 +2,17 @@
let pi = acos (-1.)
let normal_95_ci_length = 1.6448536269514722
+(* Array manipulation helpers *)
+let sumFloats xs = List.fold_left(fun acc x -> acc +. x) 0.0 xs
+let normalizeXs xs = let sum_xs = sumFloats xs in
+ List.map(fun x -> x /. sum_xs) xs
+let cumsumXs xs =
+ let _, cum_sum = List.fold_left(fun (sum, ys) x ->
+ let new_sum = sum +. x in
+ new_sum, ys @ [new_sum]
+ ) (0.0, []) xs in
+ cum_sum
+
(* Basic samplers *)
let sampleZeroToOne () : float = Random.float 1.0
let sampleStandardNormal (): float =
@@ -17,6 +28,11 @@ let sampleTo low high =
let logmean = (loglow +. loghigh) /. 2.0 in
let logstd = (loghigh -. loglow) /. (2.0 -. normal_95_ci_length ) in
sampleLognormal logmean logstd
+let mixture (samplers: (unit -> float) list) (weights: float list) =
+ match (List.length samplers == List.length weights) with
+ | false -> None
+ | true -> let normalized_weights = cumsumXs (normalizeXs weights) in Some(1.0)
+
let () =
Random.init 1;