 Entropy - Maple Help

StringTools

 Entropy
 compute the Entropy of a string Calling Sequence Entropy( s ) Parameters

 s - Maple string Description

 • The Entropy(s) command returns the Shannon entropy of the string s. A floating-point number, the entropy of the string, is returned.
 • Shannon's entropy is defined as -add( P( ch ) * log[ 2 ]( P( ch ) ), ch = Support( s ) ), where $P\left(\mathrm{ch}\right)=\frac{\mathrm{CountCharacterOccurrences}\left(s,\mathrm{ch}\right)}{\mathrm{length}\left(s\right)}$. It is a measure of the information content of the string, and can be interpreted as the number of bits required to encode each character of the string given perfect compression. The entropy is maximal when each character is equally likely. For arbitrary non-null characters, this maximal value is ${\mathrm{log}}_{2}\left(255\right)=7.99435$.
 (The null byte, with code point $0$, cannot appear in a Maple string. If all 256 single byte code points could appear, then the maximal entropy would be ${\mathrm{log}}_{2}\left(256\right)=8$, which is the number of bits per byte).
 • Note that the entropy is computed as a floating-point number, at hardware (double) precision.
 • All of the StringTools package commands treat strings as (null-terminated) sequences of $8$-bit (ASCII) characters.  Thus, there is no support for multibyte character encodings, such as unicode encodings. Examples

 > $\mathbf{use}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{StringTools}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{Entropy}\left("Mathematics"\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end use}$
 ${3.09579525500093355}$ (1)
 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{Entropy}\left("aaaaaaaaaaaaaaaaaaaaaaaaaa"\right)$
 ${-0.}$ (2)
 > $\mathrm{Entropy}\left("aaaaaaaaaaaaaaaaaaaaaaaaaaB"\right)$
 ${0.228538143953528006}$ (3)
 > Entropy( Iota( 1, 255 ) );
 ${7.99435343685886934}$ (4)
 > $\mathrm{Entropy}\left(\mathrm{Random}\left(1000000\right)\right)$
 ${7.99417106407216149}$ (5)
 > $\mathrm{evalf}\left({\mathrm{log}}_{2}\left(255\right)\right)$
 ${7.994353436}$ (6)
 > $\mathrm{Entropy}\left(\mathrm{Random}\left(1000000,'\mathrm{lower}'\right)\right)$
 ${4.70042263084046397}$ (7)
 > $\mathrm{evalf}\left({\mathrm{log}}_{2}\left(26\right)\right)$
 ${4.700439718}$ (8)
 > $\mathrm{Entropy}\left(\mathrm{Repeat}\left("ab",100\right)\right)$
 ${1.}$ (9)
 > $\mathrm{Entropy}\left(\mathrm{Repeat}\left("abc",100\right)\right)$
 ${1.58496250072115585}$ (10)
 > $\mathrm{Entropy}\left(\mathrm{Repeat}\left("abcde",100\right)\right)$
 ${2.32192809488736218}$ (11)
 > $\mathrm{Entropy}\left(\mathrm{Repeat}\left(\mathrm{Random}\left(10\right),10000\right)\right)$
 ${3.32192809488736218}$ (12)

The following steps illustrate the definition of Entropy.

 > $s≔\mathrm{Random}\left(30,'\mathrm{lower}'\right)$
 ${s}{≔}{"rbygsggdjijjtiqelzxehfnojeorwr"}$ (13)
 > $\mathrm{occ}≔\left[\mathrm{seq}\right]\left(\mathrm{CountCharacterOccurrences}\left(s,\mathrm{ch}\right),\mathrm{ch}=\mathrm{Support}\left(s\right)\right)$
 ${\mathrm{occ}}{≔}\left[{1}{,}{1}{,}{3}{,}{1}{,}{3}{,}{1}{,}{2}{,}{4}{,}{1}{,}{1}{,}{2}{,}{1}{,}{3}{,}{1}{,}{1}{,}{1}{,}{1}{,}{1}{,}{1}\right]$ (14)
 > $L≔\mathrm{map}\left(\mathrm{/},\mathrm{occ},\mathrm{length}\left(s\right)\right)$
 ${L}{≔}\left[\frac{{1}}{{30}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{10}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{10}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{15}}{,}\frac{{2}}{{15}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{15}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{10}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{30}}{,}\frac{{1}}{{30}}\right]$ (15)
 > $U≔\mathrm{map}\left(p→-\mathrm{evalf}\left(p{\mathrm{log}}_{2}\left(p\right)\right),L\right)$
 ${U}{≔}\left[{0.1635630199}{,}{0.1635630199}{,}{0.3321928095}{,}{0.1635630199}{,}{0.3321928095}{,}{0.1635630199}{,}{0.2604593730}{,}{0.3875854127}{,}{0.1635630199}{,}{0.1635630199}{,}{0.2604593730}{,}{0.1635630199}{,}{0.3321928095}{,}{0.1635630199}{,}{0.1635630199}{,}{0.1635630199}{,}{0.1635630199}{,}{0.1635630199}{,}{0.1635630199}\right]$ (16)
 > $\mathrm{convert}\left(U,'\mathrm{+}'\right)$
 ${4.031401848}$ (17)
 > $\mathrm{Entropy}\left(s\right)$
 ${4.03140184539217117}$ (18)