How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the Hy programming language

Published on 12 May 2024 09:40 PM
#Hy

How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the Hy 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 Hy programming language

Source code in the hy programming language

(import
  [scipy.stats [chisquare]]
  [collections [Counter]])

(defn uniform? [f repeats &optional [alpha .05]]
  "Call 'f' 'repeats' times and do a chi-squared test for uniformity
  of the resulting discrete distribution. Return false iff the
  null hypothesis of uniformity is rejected for the test with
  size 'alpha'."
  (<= alpha (second (chisquare
    (.values (Counter (take repeats (repeatedly f))))))))


(import [random [randint]])

(for [f [
    (fn [] (randint 1 10))
    (fn [] (if (randint 0 1) (randint 1 9) (randint 1 10)))]]
  (print (uniform? f 5000)))


  

You may also check:How to resolve the algorithm Longest string challenge step by step in the Pike programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Price fraction step by step in the Rust programming language
You may also check:How to resolve the algorithm Start from a main routine step by step in the sed programming language
You may also check:How to resolve the algorithm Stack step by step in the 8086 Assembly programming language