Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,9 +1,13 @@
Calculate the [[wp:Entropy (information theory)|information entropy]] (Shannon entropy) of a given input string.
Entropy is the [[wp:Expected value|expected value]] of the measure of [[wp:Self-information|information]] content in a system. In general, the Shannon entropy of a variable <math>X</math> is defined as:
Entropy is the [[wp:Expected value|expected value]] of the measure of [[wp:Self-information|information]] content in a system.
In general, the Shannon entropy of a variable <math>X</math> is defined as:
:<math>H(X) = \sum_{x\in\Omega} P(x) I(x)</math>
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:
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.
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,43 @@
# calculate the shannon entropy of a string #
PROC shannon entropy = ( STRING s )REAL:
BEGIN
INT string length = ( UPB s - LWB s ) + 1;
# count the occurances of each character #
[ 0 : max abs char ]INT char count;
FOR char pos FROM LWB char count TO UPB char count DO
char count[ char pos ] := 0
OD;
FOR char pos FROM LWB s TO UPB s DO
char count[ ABS s[ char pos ] ] +:= 1
OD;
# calculate the entropy, we use log base 10 and then convert #
# to log base 2 after calculating the sum #
REAL entropy := 0;
FOR char pos FROM LWB char count TO UPB char count DO
IF char count[ char pos ] /= 0
THEN
# have a character that occurs in the string #
REAL probability = char count[ char pos ] / string length;
entropy -:= probability * log( probability )
FI
OD;
entropy / log( 2 )
END; # shannon entropy #
main:
(
# test the shannon entropy routine #
print( ( shannon entropy( "1223334444" ), newline ) )
)

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
namespace Entropy
{
class Program
{
public static double logtwo(double num)
{
return Math.Log(num)/Math.Log(2);
}
public static void Main(string[] args)
{
label1:
string input = Console.ReadLine();
double infoC=0;
Dictionary<char,double> table = new Dictionary<char, double>();
foreach (char c in input)
{
if (table.ContainsKey(c))
table[c]++;
else
table.Add(c,1);
}
double freq;
foreach (KeyValuePair<char,double> letter in table)
{
freq=letter.Value/input.Length;
infoC+=freq*logtwo(freq);
}
infoC*=-1;
Console.WriteLine("The Entropy of {0} is {1}",input,infoC);
goto label1;
}
}
}

View file

@ -0,0 +1,43 @@
using System;
namespace Entropy
{
class Program
{
public static double logtwo(double num)
{
return Math.Log(num)/Math.Log(2);
}
static double Contain(string x,char k)
{
double count=0;
foreach (char Y in x)
{
if(Y.Equals(k))
count++;
}
return count;
}
public static void Main(string[] args)
{
label1:
string input = Console.ReadLine();
double infoC=0;
double freq;
string k="";
foreach (char c1 in input)
{
if (!(k.Contains(c1.ToString())))
k+=c1;
}
foreach (char c in k)
{
freq=Contain(input,c)/(double)input.Length;
infoC+=freq*logtwo(freq);
}
infoC/=-1;
Console.WriteLine("The Entropy of {0} is {1}",input,infoC);
goto label1;
}
}
}

View file

@ -1,9 +1,7 @@
(defn entropy [s]
(let [len (count s)
freqs (frequencies s)
log-of-2 (Math/log 2)]
(->> (keys freqs)
(map (fn [c]
(let [rf (/ (get freqs c) len)]
(* -1 rf (/ (Math/log rf) log-of-2)))))
(let [len (count s), log-2 (Math/log 2)]
(->> (frequencies s)
(map (fn [[_ v]]
(let [rf (/ v len)]
(-> (Math/log rf) (/ log-2) (* rf) Math/abs))))
(reduce +))))

View file

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

View file

@ -1,11 +1,12 @@
import std.stdio, std.algorithm, std.math;
double entropy(T)(T[] s)
/*pure nothrow*/ if (__traits(compiles, s.sort())) {
pure nothrow if (__traits(compiles, s.sort())) {
immutable sLen = s.length;
return s
.sort()
.group
.map!(g => g[1] / double(s.length))
.map!(g => g[1] / double(sLen))
.map!(p => -p * p.log2)
.sum;
}

View file

@ -5,4 +5,4 @@ main = print $ entropy "1223334444"
entropy s =
sum . map lg' . fq' . map (fromIntegral.length) . group . sort $ s
where lg' c = (c * ) . logBase 2 $ 1.0 / c
fq' c = map (\x -> x / (sum c)) c
fq' c = let sc = sum c in map (/ sc) c

View file

@ -0,0 +1,31 @@
(function(shannon) {
// Create a dictionary of character frequencies and iterate over it.
function process(s, evaluator) {
var h = Object.create(null), k;
s.split('').forEach(function(c) {
h[c] && h[c]++ || (h[c] = 1); });
if (evaluator) for (k in h) evaluator(k, h[k]);
return h;
};
// Measure the entropy of a string in bits per symbol.
shannon.entropy = function(s) {
var sum = 0,len = s.length;
process(s, function(k, f) {
var p = f/len;
sum -= p * Math.log(p) / Math.log(2);
});
return sum;
};
})(window.shannon = window.shannon || {});
// Log the Shannon entropy of a string.
function logEntropy(s) {
console.log('Entropy of "' + s + '" in bits per symbol:', shannon.entropy(s));
}
logEntropy('1223334444');
logEntropy('0');
logEntropy('01');
logEntropy('0123');
logEntropy('01234567');
logEntropy('0123456789abcdef');

View file

@ -0,0 +1 @@
entropy(s)=-sum(x->x*log(2,x), [count(x->x==c,s)/length(s) for c in unique(s)])

View file

@ -0,0 +1,30 @@
(* generic OCaml, using a mutable Hashtbl *)
(* pre-bake & return an inner-loop function to bin & assemble a character frequency map *)
let get_fproc (m: (char, int) Hashtbl.t) :(char -> unit) =
(fun (c:char) -> try
Hashtbl.replace m c ( (Hashtbl.find m c) + 1)
with Not_found -> Hashtbl.add m c 1)
(* pre-bake and return an inner-loop function to do the actual entropy calculation *)
let get_calc (slen:int) :(float -> float) =
let slen_float = float_of_int slen in
let log_2 = log 2.0 in
(fun v -> let pt = v /. slen_float in
pt *. ((log pt) /. log_2) )
(* main function, given a string argument it:
builds a (mutable) frequency map (initial alphabet size of 255, but it's auto-expanding),
extracts the relative probability values into a list,
folds-in the basic entropy calculation and returns the result. *)
let shannon (s:string) :float =
let freq_hash = Hashtbl.create 255 in
String.iter (get_fproc freq_hash) s;
let relative_probs = Hashtbl.fold (fun k v b -> (float v)::b) freq_hash [] in
let calc = get_calc (String.length s) in
-1.0 *. List.fold_left (fun b x -> b +. calc x) 0.0 relative_probs

View file

@ -0,0 +1,34 @@
*process source xref attributes or(!);
/*--------------------------------------------------------------------
* 08.08.2014 Walter Pachl translated from REXX version 1
*-------------------------------------------------------------------*/
ent: Proc Options(main);
Dcl (index,length,log2,substr) Builtin;
Dcl sysprint Print;
Dcl occ(100) Bin fixed(31) Init((100)0);
Dcl (n,cn,ci,i,pos) Bin fixed(31) Init(0);
Dcl chars Char(100) Var Init('');
Dcl s Char(100) Var Init('1223334444');
Dcl c Char(1);
Dcl (occf,p(100)) Dec Float(18);
Dcl e Dec Float(18) Init(0);
Do i=1 To length(s);
c=substr(s,i,1);
pos=index(chars,c);
If pos=0 Then Do;
pos=length(chars)+1;
cn+=1;
chars=chars!!c;
End;
occ(pos)+=1;
n+=1;
End;
do ci=1 To cn;
occf=occ(ci);
p(ci)=occf/n;
End;
Do ci=1 To cn;
e=e+p(ci)*log2(p(ci));
End;
Put Edit('s='''!!s!!''' Entropy=',-e)(Skip,a,f(15,12));
End;

View file

@ -0,0 +1,50 @@
PROGRAM entropytest;
USES StrUtils, Math;
TYPE FArray = ARRAY of CARDINAL;
VAR strng: STRING = '1223334444';
// list unique characters in a string
FUNCTION uniquechars(str: STRING): STRING;
VAR n: CARDINAL;
BEGIN
uniquechars := '';
FOR n := 1 TO length(str) DO
IF (PosEx(str[n],str,n)>0)
AND (PosEx(str[n],uniquechars,1)=0)
THEN uniquechars += str[n];
END;
// obtain a list of character-frequencies for a string
// given a string containing its unique characters
FUNCTION frequencies(str,ustr: STRING): FArray;
VAR u,s,p,o: CARDINAL;
BEGIN
SetLength(frequencies, Length(ustr)+1);
p := 0;
FOR u := 1 TO length(ustr) DO
FOR s := 1 TO length(str) DO BEGIN
o := p; p := PosEx(ustr[u],str,s);
IF (p>o) THEN INC(frequencies[u]);
END;
END;
// Obtain the Shannon entropy of a string
FUNCTION entropy(s: STRING): EXTENDED;
VAR pf : FArray;
us : STRING;
i,l: CARDINAL;
BEGIN
us := uniquechars(s);
pf := frequencies(s,us);
l := length(s);
entropy := 0.0;
FOR i := 1 TO length(us) DO
entropy -= pf[i]/l * log2(pf[i]/l);
END;
BEGIN
Writeln('Entropy of "',strng,'" is ',entropy(strng):2:5, ' bits.');
END.