From cc77d7ccc034aecba64278e43112a61cf8c7f12c Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 2 Nov 2024 22:24:01 +0100 Subject: Calculate delta bar IoC for 2-124 long periods --- stats.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 stats.lua (limited to 'stats.lua') diff --git a/stats.lua b/stats.lua new file mode 100644 index 0000000..1cb485d --- /dev/null +++ b/stats.lua @@ -0,0 +1,68 @@ +local format = require "format" +local stats = {} +local mt = {__index=stats} + + +local function new (name_or_obj, parent) + if type(name_or_obj) == "string" then + name_or_obj = { + name = name_or_obj, + parent = parent, + letters = 0, + length = 0, + counts = {}, + children = {}, + } + end + return setmetatable(name_or_obj, mt) +end + + +function stats:add (value) + local current = self.counts[value] or 0 + if current == 0 then + self.letters = self.letters + 1 + end + self.length = self.length + 1 + self.counts[value] = current + 1 + if self.parent then + self.parent:add(value) + end +end + + +function stats:sub (name) + local child = new(name, self) + table.insert(self.children, child) + return child +end + + +function stats:ioc (letters) + letters = letters or self.letters + local subtotal = 0 + for _, count in pairs(self.counts) do + subtotal = subtotal + count * (count - 1) + end + return subtotal / (self.length * (self.length - 1) / letters) +end + + +function stats:dump () + local function dump (stat) + local letters = (stat.parent or {}).letters + io.write(stat.name, "\t", stat.length, "\t", stat.letters, "\t", format.float(stat:ioc(letters))) + if letters then + io.write("\t", format.float(stat:ioc())) + end + print() + end + + dump(self) + for _, child in ipairs(self.children) do + dump(child) + end +end + + +return new -- cgit v1.1