22 #ifndef EMP_INFO_THEORY_H 23 #define EMP_INFO_THEORY_H 25 #include "../base/vector.h" 31 template<
typename CONTAINER>
32 double Entropy(
const CONTAINER & weights) {
35 for (
auto w : weights) total += w;
36 for (
auto w : weights) {
37 double p = ((double) w) / total;
38 entropy -= p *
Log2(p);
45 template<
typename CONTAINER,
typename WEIGHT_FUN>
46 double Entropy(
const CONTAINER & objs, WEIGHT_FUN fun,
double total=0.0) {
48 if (total == 0.0)
for (
auto & o : objs) total += (double) fun(o);
52 for (
auto & o : objs) {
53 double p = ((double) fun(o)) / total;
54 entropy -= p *
Log2(p);
61 return -(p *
Log2(p) + (1.0-p)*
Log2(1.0-p));
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) {
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);
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's probability.
Definition: info_theory.h:60