How to resolve the algorithm Probabilistic choice step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Probabilistic choice step by step in the OCaml programming language

Table of Contents

Problem Statement

Given a mapping between items and their required probability of occurrence, generate a million items randomly subject to the given probabilities and compare the target probability of occurrence versus the generated values. The total of all the probabilities should equal one. (Because floating point arithmetic is involved, this is subject to rounding errors).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Probabilistic choice step by step in the OCaml programming language

Source code in the ocaml programming language

let p = [
    "Aleph",   1.0 /. 5.0;
    "Beth",    1.0 /. 6.0;
    "Gimel",   1.0 /. 7.0;
    "Daleth",  1.0 /. 8.0;
    "He",      1.0 /. 9.0;
    "Waw",     1.0 /. 10.0;
    "Zayin",   1.0 /. 11.0;
    "Heth", 1759.0 /. 27720.0;
  ]

let rec take k = function
  | (v, p)::tl -> if k < p then v else take (k -. p) tl
  | _ -> invalid_arg "take"
 
let () =
  let n = 1_000_000 in
  Random.self_init();
  let h = Hashtbl.create 3 in
  List.iter (fun (v, _) -> Hashtbl.add h v 0) p;
  let tot = List.fold_left (fun acc (_, p) -> acc +. p) 0.0 p in
  for i = 1 to n do
    let sel = take (Random.float tot) p in
    let n = Hashtbl.find h sel in
    Hashtbl.replace h sel (succ n)  (* count the number of each item *)
  done;
  List.iter (fun (v, p) ->
    let d = Hashtbl.find h v in
    Printf.printf "%s \t %f %f\n" v p (float d /. float n)
  ) p


  

You may also check:How to resolve the algorithm Map range step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Chat server step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the Clojure programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Erlang programming language
You may also check:How to resolve the algorithm Function definition step by step in the OOC programming language