Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -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]]
|
||||
|
|
|
|||
15
Task/Entropy/Emacs-Lisp/entropy-1.l
Normal file
15
Task/Entropy/Emacs-Lisp/entropy-1.l
Normal 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)))
|
||||
2
Task/Entropy/Emacs-Lisp/entropy-2.l
Normal file
2
Task/Entropy/Emacs-Lisp/entropy-2.l
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(shannon-entropy "1223334444")
|
||||
1.8464393446710154
|
||||
6
Task/Entropy/Groovy/entropy-1.groovy
Normal file
6
Task/Entropy/Groovy/entropy-1.groovy
Normal 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)
|
||||
}
|
||||
}
|
||||
11
Task/Entropy/Groovy/entropy-2.groovy
Normal file
11
Task/Entropy/Groovy/entropy-2.groovy
Normal 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
|
||||
}
|
||||
|
|
@ -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'
|
||||
|
|
|
|||
29
Task/Entropy/Seed7/entropy.seed7
Normal file
29
Task/Entropy/Seed7/entropy.seed7
Normal 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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue