Module:Harmonic entropy: Difference between revisions
Jump to navigation
Jump to search
ArrowHead294 (talk | contribs) No edit summary |
ArrowHead294 (talk | contribs) No edit summary |
||
| Line 15: | Line 15: | ||
ratios = ratios | ratios = ratios | ||
or limits.integer_limit(200, function(ratio) | or limits.integer_limit(200, function(ratio) | ||
if math.abs(ratio[ | if math.abs(ratio[1] * 2^(c / 1200)) > 3 * deviation then | ||
return 1 / 0 | return 1 / 0 | ||
end | end | ||
| Line 26: | Line 26: | ||
local function weighted_gaussian(ratio) | local function weighted_gaussian(ratio) | ||
return gaussian(ratio[ | return gaussian(ratio[1] * 2^(c / 1200)) / norm(ratio) | ||
end | end | ||
Revision as of 13:04, 10 May 2024
- This module primarily serves as a library for other modules and has no corresponding template.
This module provides a means to calculate harmonic Shannon entropy of a particular interval.
| Introspection summary for Module:Harmonic entropy | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||||||||||
-- TODO: this module should just be called harmonic entropy!
local limits = require("Module:Limits")
local rat = require("Module:Rational")
local p = {}
-- compute harmonic Shannon entropy for an interval of `c` cents
-- `c`, `deviation`: in cents
-- `ratios`: an array of rational numbers
-- `norm`: a function of rational numbers
function p.harmonic_entropy(c, ratios, deviation, norm)
norm = norm or function(ratio)
return math.sqrt(rat.benedetti_height(ratio))
end
deviation = deviation or 0.01
ratios = ratios
or limits.integer_limit(200, function(ratio)
if math.abs(ratio[1] * 2^(c / 1200)) > 3 * deviation then
return 1 / 0
end
return norm(ratio)
end, 100)
local function gaussian(x)
return math.exp(-x * x / (2 * deviation * deviation)) / (deviation * math.sqrt(2 * math.pi))
end
local function weighted_gaussian(ratio)
return gaussian(ratio[1] * 2^(c / 1200)) / norm(ratio)
end
local q_norm = 0
for _, ratio in pairs(ratios) do
q_norm = q_norm + weighted_gaussian(ratio)
end
local function probability(ratio)
return weighted_gaussian(ratio) / q_norm
end
local entropy = 0
for _, ratio in pairs(ratios) do
local p_i = probability(ratio)
if p_i > 1e-5 then
entropy = entropy - p_i * math.log(p_i)
end
end
return entropy
end
return p