Empirical
info_theory.h
Go to the documentation of this file.
1 // This file is part of Empirical, https://github.com/devosoft/Empirical
2 // Copyright (C) Michigan State University, 2016-2017.
3 // Released under the MIT Software license; see doc/LICENSE
4 //
5 //
6 // Tools to calculate Information Theory metrics.
7 // Status: ALPHA
8 //
9 //
10 // Info-theory formulas:
11 // H(X) = -SUM(X: p[x] log2 p[x])
12 // H(X|Y) = H(XY) - H(Y)
13 // I(X:Y) = H(X) - H(X|Y)
14 // H2(p) = -p log2(p) - (1-p)log2(1-p) = H({p, 1-p})
15 //
16 // Developer notes:
17 // * Input may come as WEIGHTS or as ELEMENTS (or both!).
18 // ELEMENTS need to be converted to WEIGHTS for calculations.
19 // * Want basic info theory functions, as well as tools (for channels, error-correction,
20 // compression, etc.)
21 
22 #ifndef EMP_INFO_THEORY_H
23 #define EMP_INFO_THEORY_H
24 
25 #include "../base/vector.h"
26 #include "math.h"
27 
28 namespace emp {
29 
31  template<typename CONTAINER>
32  double Entropy(const CONTAINER & weights) {
33  double total = 0.0;
34  double entropy = 0.0;
35  for (auto w : weights) total += w;
36  for (auto w : weights) {
37  double p = ((double) w) / total;
38  entropy -= p * Log2(p);
39  }
40  return entropy;
41  }
42 
45  template<typename CONTAINER, typename WEIGHT_FUN>
46  double Entropy(const CONTAINER & objs, WEIGHT_FUN fun, double total=0.0) {
47  // If we don't know the total, calculate it.
48  if (total == 0.0) for (auto & o : objs) total += (double) fun(o);
49  emp_assert(total > 0.0);
50 
51  double entropy = 0.0;
52  for (auto & o : objs) {
53  double p = ((double) fun(o)) / total;
54  entropy -= p * Log2(p);
55  }
56  return entropy;
57  }
58 
60  constexpr double Entropy2(const double p) {
61  return -(p * Log2(p) + (1.0-p)*Log2(1.0-p));
62  }
63 
66  template<typename CONTAINER, typename CAT_FUN_X, typename CAT_FUN_Y, typename WEIGHT_FUN>
67  double Entropy(const CONTAINER & objs, CAT_FUN_X funX, CAT_FUN_Y funY, WEIGHT_FUN funW) {
68  // @CAO Categorize all XY and all Y (maybe with helper function?) and count each.
69  // @CAO Run each through Entropy function.
70 
71  double total = 0.0;
72  double entropy = 0.0;
73  for (auto & o : objs) total += fun(o);
74  for (auto & o : objs) {
75  double p = ((double) fun(o)) / total;
76  entropy -= p * Log2(p);
77  }
78  return entropy;
79  }
80 }
81 
82 #endif
Useful mathematical functions (that are constexpr when possible.)
double Entropy(const CONTAINER &weights)
Convert a vector of weights to probabilities and return the entropy of the system.
Definition: info_theory.h:32
static constexpr double Log2(double x)
Compile-time log base 2 calculator.
Definition: math.h:120
If we are in emscripten, make sure to include the header.
Definition: array.h:37
#define emp_assert(...)
Definition: assert.h:199
constexpr double Entropy2(const double p)
Calculate the entropy when their are two possibile states based on one state&#39;s probability.
Definition: info_theory.h:60