How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the OCaml programming language
Table of Contents
Problem Statement
Write a function to determine whether a given set of frequency counts could plausibly have come from a uniform distribution by using the
χ
2
{\displaystyle \chi ^{2}}
test with a significance level of 5%.
The function should return a boolean that is true if and only if the distribution is one that a uniform distribution (with appropriate number of degrees of freedom) may be expected to produce.
Note: normally a two-tailed test would be used for this kind of problem.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the OCaml programming language
Source code in the ocaml programming language
let sqr x = x *. x
let chi2UniformDistance distrib =
let count, len = Array.fold_left (fun (s, c) e -> s + e, succ c)
(0, 0) distrib in
let expected = float count /. float len in
let distance = Array.fold_left (fun s e ->
s +. sqr (float e -. expected) /. expected
) 0. distrib in
let dof = float (pred len) in
dof, distance
let chi2Proba dof distance =
Gsl_sf.gamma_inc_Q (0.5 *. dof) (0.5 *. distance)
let chi2IsUniform distrib significance =
let dof, distance = chi2UniformDistance distrib in
let likelihoodOfRandom = chi2Proba dof distance in
likelihoodOfRandom > significance
let _ =
List.iter (fun distrib ->
let dof, distance = chi2UniformDistance distrib in
Printf.printf "distribution ";
Array.iter (Printf.printf "\t%d") distrib;
Printf.printf "\tdistance %g" distance;
Printf.printf "\t[%g > 0.05]" (chi2Proba dof distance);
if chi2IsUniform distrib 0.05 then Printf.printf " fair\n"
else Printf.printf " unfair\n"
)
[
[| 199809; 200665; 199607; 200270; 199649 |];
[| 522573; 244456; 139979; 71531; 21461 |]
]
You may also check:How to resolve the algorithm Subtractive generator step by step in the Raku programming language
You may also check:How to resolve the algorithm Sorting algorithms/Radix sort step by step in the Scala programming language
You may also check:How to resolve the algorithm HTTPS step by step in the zkl programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Elementary cellular automaton step by step in the PicoLisp programming language