Last active
March 30, 2021 11:43
-
-
Save joaofnds/e64fa7165d575ca575fd7529e3236092 to your computer and use it in GitHub Desktop.
babashka script to generate a frequency distribution table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns script | |
(:require [clojure.pprint :as pprint])) | |
(def sequence (sort (map #(Float/parseFloat %) *command-line-args*))) | |
(def n (count sequence)) | |
(def k (int (Math/ceil (Math/sqrt n)))) | |
(def es (apply min sequence)) | |
(def ei (apply max sequence)) | |
(def i (/ (- ei es) k)) | |
(def classes (map #(+ es (* i %)) (range k))) | |
(def table | |
(map-indexed | |
(fn [j _class] | |
(let [lower-bound (+ es (* j i)) | |
upper-bound (+ es (* (inc j) i)) | |
range-fmt (str "[%5.2f, %.2f" (if (= (inc j) k) "]" ")")) | |
max-comp (if (= (inc j) k) <= <) | |
numbers (filter #(and (<= lower-bound %) (max-comp % upper-bound)) sequence) | |
F (count numbers) | |
F-prime (count (filter #(max-comp % upper-bound) sequence)) | |
f (float (/ F n)) | |
f-prime (float (/ F-prime n)) | |
c (float (/ (+ lower-bound upper-bound) 2))] | |
{"j" (inc j) | |
"class" (format range-fmt lower-bound upper-bound) | |
"F" F | |
"F-prime" F-prime | |
"f" f | |
"f-prime" f-prime | |
"c" (format "%.2f" c)})) | |
classes)) | |
(pprint/print-table table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment