September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,7 +1,7 @@
;Task:
Calculate the Shannon entropy   H   of a given input string.
Given the discreet random variable <math>X</math> that is a string of <math>N</math> "symbols" (total characters) consisting of <math>n</math> different characters (n=2 for binary), the Shannon entropy of X in '''bits/symbol''' is :
Given the discrete random variable <math>X</math> that is a string of <math>N</math> "symbols" (total characters) consisting of <math>n</math> different characters (n=2 for binary), the Shannon entropy of X in '''bits/symbol''' is :
:<math>H_2(X) = -\sum_{i=1}^n \frac{count_i}{N} \log_2 \left(\frac{count_i}{N}\right)</math>
where <math>count_i</math> is the count of character <math>n_i</math>.

View file

@ -0,0 +1,41 @@
import system'math.
import system'collections.
import system'routines.
import extensions.
extension $op
{
logTwo
= self ln / 2 ln.
}
symbol program =
[
var input := console readLine.
var infoC := 0.0r.
var table := Dictionary new.
input forEach(:ch)
[
var n := table[ch].
if ($nil == n)
[
table[ch] := 1.
];
[
table[ch] := n + 1.
]
].
var freq := 0.
table forEach(:letter)
[
freq := letter int; realDiv(input length).
infoC += (freq * freq logTwo).
].
infoC *= -1.
console printLine("The Entropy of ", input, " is ", infoC).
].

View file

@ -0,0 +1,21 @@
function entropy
for arg in $argv
set name count_$arg
if not count $$name > /dev/null
set $name 0
set values $values $arg
end
set $name (math $$name + 1)
end
set entropy 0
for value in $values
set name count_$value
set entropy (echo "
scale = 50
p = "$$name" / "(count $argv)"
$entropy - p * l(p)
" | bc -l)
end
echo "$entropy / l(2)" | bc -l
end
entropy (echo 1223334444 | fold -w1)

View file

@ -0,0 +1,24 @@
package main
import (
"fmt"
"math"
"strings"
)
func main(){
fmt.Println(H("1223334444"))
}
func H(data string) (entropy float64) {
if data == "" {
return 0
}
for i := 0; i < 256; i++ {
px := float64(strings.Count(data, string(byte(i)))) / float64(len(data))
if px > 0 {
entropy += -px * math.Log2(px)
}
}
return entropy
}

View file

@ -5,14 +5,14 @@ import (
"math"
)
const s = "1223334444"
func main() {
const s = "1223334444"
m := map[rune]float64{}
for _, r := range s {
m[r]++
}
hm := 0.
var fm float64
for _, c := range m {
hm += c * math.Log2(c)
}

View file

@ -0,0 +1,33 @@
// version 1.0.6
fun log2(d: Double) = Math.log(d) / Math.log(2.0)
fun shannon(s: String): Double {
val counters = mutableMapOf<Char, Int>()
for (c in s) {
if (counters.containsKey(c)) counters[c] = counters[c]!! + 1
else counters.put(c, 1)
}
val nn = s.length.toDouble()
var sum = 0.0
for (key in counters.keys) {
val term = counters[key]!! / nn
sum += term * log2(term)
}
return -sum
}
fun main(args: Array<String>) {
val samples = arrayOf(
"1223334444",
"1223334444555555555",
"122333",
"1227774444",
"aaBBcccDDDD",
"1234567890abcdefghijklmnopqrstuvwxyz",
"Rosetta Code"
)
println(" String Entropy")
println("------------------------------------ ------------------")
for (sample in samples) println("${sample.padEnd(36)} -> ${"%18.16f".format(shannon(sample))}")
}

View file

@ -0,0 +1,28 @@
function log2(atom v)
return log(v)/log(2)
end function
function entropy(sequence s)
sequence symbols = {},
counts = {}
integer N = length(s)
for i=1 to N do
object si = s[i]
integer k = find(si,symbols)
if k=0 then
symbols = append(symbols,si)
counts = append(counts,1)
else
counts[k] += 1
end if
end for
atom H = 0
integer n = length(counts)
for i=1 to n do
atom ci = counts[i]/N
H -= ci*log2(ci)
end for
return H
end function
?entropy("1223334444")

View file

@ -1,18 +1,18 @@
/*REXX program calculates the information entropy for a given character string. */
numeric digits 50 /*use 50 decimal digits for precision. */
parse arg $; if $='' then $=1223334444 /*obtain the optional input from the CL*/
#=0; @.=0; L=length($); $$= /*define handy-dandy REXX variables. */
do j=1 for L; _=substr($,j,1) /*process each character in $ string.*/
if @._==0 then do; #=#+1 /*Unique? Yes, bump character counter.*/
$$=$$ || _ /*add this character to the $$ list. */
end
@._=@._+1 /*keep track of this character's count.*/
end /*j*/
parse arg $; if $='' then $=1223334444 /*obtain the optional input from the CL*/
#=0; @.=0; L=length($) /*define handy-dandy REXX variables. */
$$= /*initialize the $$ list. */
do j=1 for L; _=substr($, j, 1) /*process each character in $ string.*/
if @._==0 then do; #=# + 1 /*Unique? Yes, bump character counter.*/
$$=$$ || _ /*add this character to the $$ list. */
end
@._=@._ + 1 /*keep track of this character's count.*/
end /*j*/
sum=0 /*calculate info entropy for each char.*/
do i=1 for #; _=substr($$,i,1) /*obtain a character from unique list. */
sum=sum - @._/L * log2(@._/L) /*add (negatively) the char entropies. */
end /*i*/
do i=1 for #; _=substr($$, i, 1) /*obtain a character from unique list. */
sum=sum - @._/L * log2(@._/L) /*add (negatively) the char entropies. */
end /*i*/
say ' input string: ' $
say 'string length: ' L
@ -23,8 +23,8 @@ exit /*stick a fork in it, we're al
log2: procedure; parse arg x 1 ox; ig= x>1.5; ii=0; is=1 - 2 * (ig\==1)
numeric digits digits()+5 /* [↓] precision of E must be≥digits()*/
e=2.71828182845904523536028747135266249775724709369995957496696762772407663035354759
do while ig & ox>1.5 | \ig&ox<.5; _=e; do j=-1; iz=ox* _**-is
do while ig & ox>1.5 | \ig&ox<.5; _=e; do j=-1; iz=ox * _ ** -is
if j>=0 & (ig & iz<1 | \ig&iz>.5) then leave; _=_*_; izz=iz; end /*j*/
ox=izz; ii=ii+is*2**j; end; x=x* e**-ii-1; z=0; _=-1; p=z
ox=izz; ii=ii+is*2**j; end /*while*/; x=x * e** -ii -1; z=0; _=-1; p=z
do k=1; _=-_*x; z=z+_/k; if z=p then leave; p=z; end /*k*/
r=z+ii; if arg()==2 then return r; return r/log2(2,.)

View file

@ -0,0 +1,32 @@
decimals(8)
entropy = 0
countOfChar = list(255)
source="1223334444"
charCount =len( source)
usedChar =""
for i =1 to len( source)
ch =substr(source, i, 1)
if not(substr( usedChar, ch)) usedChar =usedChar +ch ok
j =substr( usedChar, ch)
countOfChar[j] =countOfChar[j] +1
next
l =len(usedChar)
for i =1 to l
probability =countOfChar[i] /charCount
entropy =entropy - (probability *logBase(probability, 2))
next
see "Characters used and the number of occurrences of each " + nl
for i =1 to l
see "'" + substr(usedChar, i, 1) + "' " + countOfChar[i] + nl
next
see " Entropy of " + source + " is " + entropy + " bits." + nl
see " The result should be around 1.84644 bits." + nl
func logBase (x, b)
logBase =log( x) /log( 2)
return logBase

View file

@ -0,0 +1,42 @@
(define (entropy input)
(define (close? a b)
(define (norm x y)
(define (infinite_norm m n)
(define (absminus p q)
(cond ((null? p) '())
(else (cons (abs (- (car p) (car q))) (absminus (cdr p) (cdr q))))))
(define (mm l)
(cond ((null? (cdr l)) (car l))
((> (car l) (cadr l)) (mm (cons (car l) (cddr l))))
(else (mm (cdr l)))))
(mm (absminus m n)))
(if (pair? x) (infinite_norm x y) (abs (- x y))))
(let ((epsilon 0.2))
(< (norm a b) epsilon)))
(define (freq-list x)
(define (f x)
(define (count a b)
(cond ((null? b) 1)
(else (+ (if (close? a (car b)) 1 0) (count a (cdr b))))))
(let ((t (car x)) (tt (cdr x)))
(count t tt)))
(define (g x)
(define (filter a b)
(cond ((null? b) '())
((close? a (car b)) (filter a (cdr b)))
(else (cons (car b) (filter a (cdr b))))))
(let ((t (car x)) (tt (cdr x)))
(filter t tt)))
(cond ((null? x) '())
(else (cons (f x) (freq-list (g x))))))
(define (scale x)
(define (sum x)
(if (null? x) 0.0 (+ (car x) (sum (cdr x)))))
(let ((z (sum x)))
(map (lambda(m) (/ m z)) x)))
(define (cal x)
(if (null? x) 0 (+ (* (car x) (/ (log (car x)) (log 2))) (cal (cdr x)))))
(- (cal (scale (freq-list input)))))
(entropy (list 1 2 2 3 3 3 4 4 4 4))
(entropy (list (list 1 1) (list 1.1 1.1) (list 1.2 1.2) (list 1.3 1.3) (list 1.5 1.5) (list 1.6 1.6)))

View file

@ -0,0 +1,20 @@
function E = entropy(d)
d=strsplit(d);
n=unique(string(d));
N=size(d,'r');
count=zeros(n);
n_size = size(n,'r');
for i = 1:n_size
count(i) = sum ( d == n(i) );
end
E=0;
for i=1:length(count)
E = E - count(i)/N * log(count(i)/N) / log(2);
end
endfunction
word ='1223334444';
E = entropy(word);
disp('The entropy of '+word+' is '+string(E)+'.');

View file

@ -0,0 +1,10 @@
fcn entropy(text){
text.pump(Void,fcn(c,freq){ c=c.toAsc(); freq[c]+=1; freq }
.fp1( (0).pump(256,List,0.0).copy() )) // array[256] of 0.0
.filter() // remove all zero entries from array
.apply('/(text.len())) // (num of char)/len
.apply(fcn(p){-p*p.log()}) // |p*ln(p)|
.sum(0.0)/(2.0).log(); // sum * ln(e)/ln(2) to convert to log2
}
entropy("1223334444").println(" bits");