commit 23bd623b6630121b2e99c693ced37ded1e236998
parent fa73c33c27a324c74ee2ea0102624e8459734054
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sat, 14 Oct 2023 23:31:58 +0100
reformat, move to using arrays instead of list.
Diffstat:
5 files changed, 11 insertions(+), 6 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
@@ -3,12 +3,12 @@ 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 sumFloats xs = Array.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
+ Array.map(fun x -> x /. sum_xs) xs
let cumsumXs xs =
- let _, cum_sum = List.fold_left(fun (sum, ys) x ->
+ let _, cum_sum = Array.fold_left(fun (sum, ys) x ->
let new_sum = sum +. x in
new_sum, ys @ [new_sum]
) (0.0, []) xs in
@@ -16,27 +16,32 @@ let cumsumXs xs =
(* Basic samplers *)
let sampleZeroToOne () : float = Random.float 1.0
+
let sampleStandardNormal (): float =
let u1 = sampleZeroToOne () in
let u2 = sampleZeroToOne () in
let z = sqrt(-2.0 *. log(u1)) *. sin(2.0 *. pi *. u2) in
z
+
let sampleNormal mean std = mean +. std *. (sampleStandardNormal ())
+
let sampleLognormal logmean logstd = exp(sampleNormal logmean logstd)
+
let sampleTo low high =
let loglow = log(low) in
let loghigh = log(high) in
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
+
+let mixture (samplers: (unit -> float) array) (weights: float array) =
+ match (Array.length samplers == Array.length weights) with
| false -> None
| true ->
let normalized_weights = normalizeXs weights in
let cumsummed_normalized_weights = cumsumXs normalized_weights in
+ let answer = 1.1 in
Some(1.0)
-
let () =
Random.init 1;