start adding mixture function

This commit is contained in:
NunoSempere 2023-10-14 20:50:25 +01:00
parent ffec4663fc
commit 2cddf557bf
5 changed files with 16 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -2,6 +2,17 @@
let pi = acos (-1.) let pi = acos (-1.)
let normal_95_ci_length = 1.6448536269514722 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 *) (* Basic samplers *)
let sampleZeroToOne () : float = Random.float 1.0 let sampleZeroToOne () : float = Random.float 1.0
let sampleStandardNormal (): float = let sampleStandardNormal (): float =
@ -17,6 +28,11 @@ let sampleTo low high =
let logmean = (loglow +. loghigh) /. 2.0 in let logmean = (loglow +. loghigh) /. 2.0 in
let logstd = (loghigh -. loglow) /. (2.0 -. normal_95_ci_length ) in let logstd = (loghigh -. loglow) /. (2.0 -. normal_95_ci_length ) in
sampleLognormal logmean logstd 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 () = let () =
Random.init 1; Random.init 1;