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 --- format.lua | 5 +++++ ioc-delta.lua | 22 +++++++++++++++++++ ioc.lua | 54 ++--------------------------------------------- reading.lua | 9 ++++++++ stats.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 52 deletions(-) create mode 100755 ioc-delta.lua create mode 100644 stats.lua diff --git a/format.lua b/format.lua index 3a126a9..67379a8 100644 --- a/format.lua +++ b/format.lua @@ -10,6 +10,11 @@ function format.csv (tvalues) end +function format.float (float) + return string.format("%.5f", float) +end + + function format.ascii32 (tvalues) local str = "" for _, value in ipairs(tvalues) do diff --git a/ioc-delta.lua b/ioc-delta.lua new file mode 100755 index 0000000..e6dd656 --- /dev/null +++ b/ioc-delta.lua @@ -0,0 +1,22 @@ +#!/usr/bin/env lua +local eyes = require "eyes" +local reading = require "reading" +local stats = require "stats" +local messages = reading.all_tvalues(eyes) +for period=2, 124 do -- Second longest message + local global = stats(tostring(period)) + local positions = {} + for i=1, period do + positions[i - 1] = global:sub(string.format(" > %d", i)) + end + for _, message in ipairs(messages) do + for index, value in ipairs(message) do + positions[(index - 1) % period]:add(value) + end + end + local sum = 0 + for _, pos in pairs(positions) do + sum = sum + pos:ioc(global.letters) + end + io.write(period, ",", sum / period, "\n") +end diff --git a/ioc.lua b/ioc.lua index 51c99e2..41e3d87 100755 --- a/ioc.lua +++ b/ioc.lua @@ -1,62 +1,12 @@ #!/usr/bin/env lua local eyes = require "eyes" local reading = require "reading" - - -local function fmt (float) - return string.format("%.5f", float) -end - - -local function stats (name, parent) - return { - name=name, - parent=parent, - letters=0, - length=0, - counts={}, - - add = function (self, 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 - end, - - ioc = function (self, 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, - - dump = function (self) - local letters = (self.parent or {}).letters - io.write(name, "\t", self.length, "\t", self.letters, "\t", fmt(self:ioc(letters))) - if letters then - io.write("\t", fmt(self:ioc())) - end - print() - end - } -end - - +local stats = require "stats" local global = stats"global" -local lokals = {} for index, message in ipairs(eyes) do - local lokal = stats("#"..index, global) + local lokal = global:sub("#"..index) for _, value in reading.values(message) do - global:add(value) lokal:add(value) end - table.insert(lokals, lokal) -- Global is incomplete, can't dump yet. end global:dump() -for _, lokal in ipairs(lokals) do - lokal:dump() -end diff --git a/reading.lua b/reading.lua index 6c910e2..7ab8bc1 100644 --- a/reading.lua +++ b/reading.lua @@ -50,4 +50,13 @@ function reading.tvalues (message) end +function reading.all_tvalues (eyes) + local messages = {} + for _, message in ipairs(eyes) do + table.insert(messages, reading.tvalues(message)) + end + return messages +end + + return reading 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