Module:Harmonic entropy: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
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) | ||
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 | 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 | 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 | entropy = entropy - P_i * math.log(P_i) | ||
end | end | ||
end | end | ||
Revision as of 14:37, 10 October 2022
- 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 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||||||||||||||||||||||||
local getArgs = require('Module:Arguments').getArgs
local u = require('Module:Utils')
local l = require('Module:Limits')
local rat = require('Module:Rational')
local p = {}
-- return measure in cents of an interval ratio, rounded to prec decimal places
function p.to_cents(frame)
local args = getArgs(frame)
return p._to_cents(args[1], args[2])
end
function p._to_cents(ratio, prec)
-- ratio defaults to 1
ratio = u.eval_num_arg(ratio, 1)
-- prec defaults to nil
prec = u.eval_num_arg(prec)
local result = 1200*u._log(ratio), prec
if prec == nil then
return result
else
return u._round(result, prec)
end
end
-- 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.beneditti_height(ratio))
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)
return math.exp(-x*x / (2*deviation*deviation)) / (deviation * math.sqrt(2*math.pi))
end
local function Q(ratio)
return S(rat.cents(ratio) - c) / norm(ratio)
end
local Q_norm = 0
for i, ratio in pairs(ratios) do
Q_norm = Q_norm + Q(ratio)
end
local function P(ratio)
return Q(ratio) / Q_norm
end
local entropy = 0
for i, ratio in pairs(ratios) do
local P_i = P(ratio)
if P_i > 1e-5 then
entropy = entropy - P_i * math.log(P_i)
end
end
return entropy
end
return p