entropy

Functions

int main(int argc, char *argv[])

entropy [ option ] [ infile ]

  • -l int

    • number of elements \((1 \le N)\)

  • -o int

    • output format \(O\)

      • 0 bit

      • 1 nat

      • 2 dit

  • -f bool

    • output entropy frame by frame

  • infile str

    • double-type probability sequence

  • stdout

    • double-type entropy

The input is a set of probabilities:

\[ \begin{array}{ccc} \underbrace{p_0(1),\,p_0(2),\,\ldots,\,p_0(N)}_{\boldsymbol{p}(0)}, & \underbrace{p_1(1),\,p_1(2),\,\ldots,\,p_1(N)}_{\boldsymbol{p}(1)}, & \ldots, \end{array} \]
If -f option is given, the output sequence is
\[ \begin{array}{cccc} H(0), & H(1), & H(2), & \ldots, \end{array} \]
where \(H(t)\) is the entropy at \(t\)-th frame:
\[ H(t) = -\sum_{n=1}^{N} p_t(n) \log_b p_t(n) = -\boldsymbol{p}(t)^\mathsf{T} \log_b \boldsymbol{p}(t). \]
The base \(b\) depends on the value of \(O\):
\[\begin{split} b = \left\{\begin{array}{ll} 2,\quad & O = 0 \\ e, & O = 1 \\ 10. & O = 2 \\ \end{array}\right. \end{split}\]
If -f option is not given, only the average of the entropies, \(\bar{H}\), is sent to the standard output:
\[ \bar{H} = \frac{1}{T} \sum_{t=0}^{T-1} H(t), \]
where \(T\) is the number of the set of probabilities.

The below example calculates maximum value of entropy:

step -l 4 | sopr -d 4 | entropy -l 4 | x2x +da

Parameters
  • argc[in] Number of arguments.

  • argv[in] Argument vector.

Returns

0 on success, 1 on failure.

See also

huffman histogram

class sptk::EntropyCalculation

Calculate entropy.

The input is the probabilities of \(N\) events:

\[ \begin{array}{cccc} p(1), & p(2), & \ldots, & p(N), \end{array} \]
where
\[ \sum_{n=1}^N \, p(n) = 1. \]
The output is the entropy for the probabilities:
\[ \begin{array}{cccc} H = -\displaystyle\sum_{n=1}^N p(n) \log_b p(n) \end{array} \]
where \(b\) is 2, \(e\), or 10.

Public Types

enum EntropyUnits

Unit of entropy.

Values:

enumerator kBit
enumerator kNat
enumerator kDit
enumerator kNumUnits

Public Functions

EntropyCalculation(int num_element, EntropyUnits entropy_unit)
Parameters
  • num_element[in] Number of elements, \(N\).

  • entropy_unit[in] Unit of entropy.

inline int GetNumElement() const
Returns

Number of elements.

inline EntropyUnits GetEntropyUnit() const
Returns

Unit of entropy.

inline bool IsValid() const
Returns

True if this object is valid.

bool Run(const std::vector<double> &probability, double *entropy) const
Parameters
  • probability[in] Probabiltiy distribution.

  • entropy[out] Entropy.

Returns

True on success, false on failure.