Module:Harmonic entropy: Difference between revisions

Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
m harmonic_entropy() improvements
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local u = require('Module:Utils')
local u = require('Module:Utils')
local l = require('Module:Limits')
local rat = require('Module:Rational')
local rat = require('Module:Rational')
local p = {}
local p = {}
Line 28: Line 29:
-- `norm`: a function of rational numbers
-- `norm`: a function of rational numbers
function p.harmonic_entropy(c, ratios, deviation, norm)
function p.harmonic_entropy(c, ratios, deviation, norm)
deviation = deviation or 17
norm = norm or function(ratio)
norm = norm or function(ratio)
return math.sqrt(rat.beneditti_height(ratio))
return math.sqrt(rat.beneditti_height(ratio))
end
end
deviation = deviation or 17
ratios = ratios or l.integer_limit(
200,
function(ratio)
if math.abs(rat.cents(ratio) - c) > 3 * deviation then
return 1/0
end
return norm(ratio)
end,
100
)
local function S(x)
local function S(x)
Line 41: Line 52:
local Q_norm = 0
local Q_norm = 0
for i, ratio in ipairs(ratios) do
for i, ratio in pairs(ratios) do
Q_norm = Q_norm + Q(ratio)
Q_norm = Q_norm + Q(ratio)
end
end
Line 50: Line 61:
local entropy = 0
local entropy = 0
for i, ratio in ipairs(ratios) do
for i, ratio in pairs(ratios) do
local P_i = P(ratio)
local P_i = P(ratio)
if P_i > 1e-5 then
if P_i > 1e-5 then
entropy = entropy - P_i * math.log(P_i) / math.log(2)
entropy = entropy - P_i * math.log(P_i)
end
end
end
end