Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -5,3 +5,5 @@ Entropy is the [[wp:Expected value|expected value]] of the measure of [[wp:Self-
where the information content <math>I(x) = -\log_{b} P(x)</math>. If the base of the logarithm <math>b = 2</math>, the result is expressed in ''bits'', a [[wp:Units of information|unit of information]]. Therefore, given a string <math>S</math> of length <math>n</math> where <math>P(s_i)</math> is the relative frequency of each character, the entropy of a string in bits is:
:<math>H(S) = -\sum_{i=0}^n P(s_i) \log_2 (P(s_i))</math>
For this task, use "<tt>1223334444</tt>" as an example. The result should be around 1.84644 bits.
Related Task: [[Fibonacci_word]]

View file

@ -0,0 +1,15 @@
(defun shannon-entropy (input)
(let ((freq-table (make-hash-table))
(entropy 0)
(length (+ (length input) 0.0)))
(mapcar (lambda (x)
(puthash x
(+ 1 (gethash x freq-table 0))
freq-table))
input)
(maphash (lambda (k v)
(set 'entropy (+ entropy
(* (/ v length)
(log (/ v length) 2)))))
freq-table)
(- entropy)))

View file

@ -0,0 +1,2 @@
(shannon-entropy "1223334444")
1.8464393446710154

View file

@ -0,0 +1,6 @@
String.metaClass.getShannonEntrophy = {
-delegate.inject([:]) { map, v -> map[v] = (map[v] ?: 0) + 1; map }.values().inject(0.0) { sum, v ->
def p = (BigDecimal)v / delegate.size()
sum + p * Math.log(p) / Math.log(2)
}
}

View file

@ -0,0 +1,11 @@
[ '1223334444': '1.846439344671',
'1223334444555555555': '1.969811065121',
'122333': '1.459147917061',
'1227774444': '1.846439344671',
aaBBcccDDDD: '1.936260027482',
'1234567890abcdefghijklmnopqrstuvwxyz': '5.169925004424',
'Rosetta Code': '3.084962500407' ].each { s, expected ->
println "Checking $s has a shannon entrophy of $expected"
assert sprintf('%.12f', s.shannonEntrophy) == expected
}

View file

@ -6,7 +6,7 @@
* 22.05.2013 Walter Pachl (I won't analyze the minor differences)
* 25.05.2013 I did now analyze and had to discover that
* 'my' log routine is apparently incorrect
* Correction is yet to come
* 25.05.2013 problem identified & corrected
*********************************************************************/
Call both '1223334444'
Call both '1223334444555555555'

View file

@ -0,0 +1,29 @@
$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
const func float: entropy (in string: stri) is func
result
var float: entropy is 0.0;
local
var hash [char] integer: count is (hash [char] integer).value;
var char: ch is ' ';
var float: p is 0.0;
begin
for ch range stri do
if ch in count then
incr(count[ch]);
else
count @:= [ch] 1;
end if;
end for;
for key ch range count do
p := flt(count[ch]) / flt(length(stri));
entropy -:= p * log(p) / log(2.0);
end for;
end func ;
const proc: main is func
begin
writeln(entropy("1223334444") digits 5);
end func;