Skip to content

Commit

Permalink
Merge 88d67fe into fa236c6
Browse files Browse the repository at this point in the history
  • Loading branch information
saromanov committed Jan 26, 2019
2 parents fa236c6 + 88d67fe commit 24a11b0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
31 changes: 31 additions & 0 deletions entropy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package stats

import "math"

// Entropy provides calculation of the entropy
func Entropy(input Float64Data) (float64, error) {
input, err := normalize(input)
if err != nil {
return math.NaN(), err
}
var result float64
for i := 0; i < input.Len(); i++ {
v := input.Get(i)
if v == 0 {
continue
}
result += (v * math.Log(v))
}
return -result, nil
}

func normalize(input Float64Data) (Float64Data, error) {
sum, err := input.Sum()
if err != nil {
return Float64Data{}, err
}
for i := 0; i < input.Len(); i++ {
input[i] = input[i] / sum
}
return input, nil
}
26 changes: 26 additions & 0 deletions entropy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package stats

import "testing"

func TestEntropy(t *testing.T) {
for _, c := range []struct {
in Float64Data
out float64
}{
{Float64Data{4, 8, 5, 1}, 1.2110440167801229},
{Float64Data{0.8, 0.01, 0.4}, 0.6791185708986585},
{Float64Data{0.8, 1.1, 0, 5}, 0.7759393943707658},
} {
got, err := Entropy(c.in)
if err != nil {
t.Errorf("Returned an error")
}
if got != c.out {
t.Errorf("Max(%.1f) => %.1f != %.1f", c.in, got, c.out)
}
}
_, err := Entropy([]float64{})
if err == nil {
t.Errorf("Empty slice didn't return an error")
}
}

0 comments on commit 24a11b0

Please sign in to comment.