Skip to content

Commit

Permalink
Add stddev (#164)
Browse files Browse the repository at this point in the history
Issue #41
  • Loading branch information
AlessandroLorenzi committed Oct 3, 2020
1 parent 172cf51 commit 2a685ad
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -225,6 +225,7 @@ The letters in brackets indicate:
| [`Sort`](#sort) ||| | | n⋅log(n) | Sort works similar to sort.SliceType(). However, unlike sort.SliceType the slice returned will be reallocated as to not modify the input slice. |
| [`SortStableUsing`](#sortstableusing) || || | n⋅log(n) | SortStableUsing works similar to sort.SliceStable. However, unlike sort.SliceStable the slice returned will be reallocated as to not modify the input slice. |
| [`SortUsing`](#sortusing) || || | n⋅log(n) | SortUsing works similar to sort.Slice. However, unlike sort.Slice the slice returned will be reallocated as to not modify the input slice. |
| [`Stddev`](#stddev) | || | | n | Stddev is the standard deviation |
| [`Strings`](#strings) (S) |||| | n | Strings transforms each element to a string. |
| [`SubSlice`](#subslice) |||| | n | SubSlice will return the subSlice from start to end(excluded) |
| [`Sum`](#sum) | || | | n | Sum is the sum of all of the elements. |
Expand Down Expand Up @@ -680,6 +681,11 @@ SortUsing works similar to sort.Slice. However, unlike sort.Slice the
slice returned will be reallocated as to not modify the input slice.


## Stddev

Stddev is the standard deviation


## Strings

Strings transforms each element to a string.
Expand Down
1 change: 1 addition & 0 deletions functions/main.go
Expand Up @@ -72,6 +72,7 @@ var Functions = []struct {
{"Sort", "sort.go", ForNumbersAndStrings, "n⋅log(n)"},
{"SortStableUsing", "sort_stable_using.go", ForStrings | ForStructs, "n⋅log(n)"},
{"SortUsing", "sort_using.go", ForStrings | ForStructs, "n⋅log(n)"},
{"Stddev", "stddev.go", ForNumbers, "n"},
{"Strings", "strings.go", ForAll, "n"},
{"SubSlice", "sub_slice.go", ForAll, "n"},
{"Sum", "sum.go", ForNumbers, "n"},
Expand Down
2 changes: 1 addition & 1 deletion functions/mode.go
Expand Up @@ -8,7 +8,7 @@ func (ss SliceType) Mode() SliceType {
if len(ss) == 0 {
return nil
}
values := make(map[ElementType]int, 0)
values := make(map[ElementType]int)
for _, s := range ss {
values[s]++
}
Expand Down
20 changes: 20 additions & 0 deletions functions/stddev.go
@@ -0,0 +1,20 @@
package functions

import "math"

// Stddev is the standard deviation
func (ss SliceType) Stddev() float64 {
if len(ss) == 0 {
return 0.0
}

avg := ss.Average()

var sd float64
for i := range ss {
sd += math.Pow(float64(ss[i])-avg, 2)
}
sd = math.Sqrt(sd / float64(len(ss)))

return sd
}
2 changes: 1 addition & 1 deletion pie/carpointers_pie.go
Expand Up @@ -420,7 +420,7 @@ func (ss carPointers) Mode() carPointers {
if len(ss) == 0 {
return nil
}
values := make(map[*car]int, 0)
values := make(map[*car]int)
for _, s := range ss {
values[s]++
}
Expand Down
2 changes: 1 addition & 1 deletion pie/cars_pie.go
Expand Up @@ -420,7 +420,7 @@ func (ss cars) Mode() cars {
if len(ss) == 0 {
return nil
}
values := make(map[car]int, 0)
values := make(map[car]int)
for _, s := range ss {
values[s]++
}
Expand Down
20 changes: 19 additions & 1 deletion pie/float64s_pie.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/elliotchance/pie/pie/util"
"math"
"math/rand"
"sort"
"strconv"
Expand Down Expand Up @@ -600,7 +601,7 @@ func (ss Float64s) Mode() Float64s {
if len(ss) == 0 {
return nil
}
values := make(map[float64]int, 0)
values := make(map[float64]int)
for _, s := range ss {
values[s]++
}
Expand Down Expand Up @@ -838,6 +839,23 @@ func (ss Float64s) Sort() Float64s {
return sorted
}

// Stddev is the standard deviation
func (ss Float64s) Stddev() float64 {
if len(ss) == 0 {
return 0.0
}

avg := ss.Average()

var sd float64
for i := range ss {
sd += math.Pow(float64(ss[i])-avg, 2)
}
sd = math.Sqrt(sd / float64(len(ss)))

return sd
}

// Strings transforms each element to a string.
//
// If the element type implements fmt.Stringer it will be used. Otherwise it
Expand Down
8 changes: 8 additions & 0 deletions pie/float64s_test.go
Expand Up @@ -1521,3 +1521,11 @@ func TestFloat64s_Insert(t *testing.T) {
assert.Equal(t, Float64s{1.0, 2.0}, Float64s{1.0}.Insert(1, 2.0))
assert.Equal(t, Float64s{1.0, 2.0, 3.3}, Float64s{1.0, 3.3}.Insert(1, 2.0))
}

func TestFloat64s_Stddev(t *testing.T) {

assert.Equal(t, 0.0, Float64s{}.Stddev())
assert.Equal(t, 0.0, Float64s{1}.Stddev())
assert.Equal(t, 4.8587389053127765, Float64s{10.0, 12.5, 23.3, 23.1, 16.5, 23.1, 21.2, 16.4}.Stddev())

}
20 changes: 19 additions & 1 deletion pie/ints_pie.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/elliotchance/pie/pie/util"
"math"
"math/rand"
"sort"
"strconv"
Expand Down Expand Up @@ -600,7 +601,7 @@ func (ss Ints) Mode() Ints {
if len(ss) == 0 {
return nil
}
values := make(map[int]int, 0)
values := make(map[int]int)
for _, s := range ss {
values[s]++
}
Expand Down Expand Up @@ -838,6 +839,23 @@ func (ss Ints) Sort() Ints {
return sorted
}

// Stddev is the standard deviation
func (ss Ints) Stddev() float64 {
if len(ss) == 0 {
return 0.0
}

avg := ss.Average()

var sd float64
for i := range ss {
sd += math.Pow(float64(ss[i])-avg, 2)
}
sd = math.Sqrt(sd / float64(len(ss)))

return sd
}

// Strings transforms each element to a string.
//
// If the element type implements fmt.Stringer it will be used. Otherwise it
Expand Down
8 changes: 8 additions & 0 deletions pie/ints_test.go
Expand Up @@ -1510,3 +1510,11 @@ func TestInts_Insert(t *testing.T) {
assert.Equal(t, Ints{1, 2}, Ints{1}.Insert(1, 2))
assert.Equal(t, Ints{1, 2, 3}, Ints{1, 3}.Insert(1, 2))
}

func TestInts_Stddev(t *testing.T) {

assert.Equal(t, 0.0, Ints{}.Stddev())
assert.Equal(t, 0.0, Ints{1}.Stddev())
assert.Equal(t, 4.898979485566356, Ints{10, 12, 23, 23, 16, 23, 21, 16}.Stddev())

}
2 changes: 1 addition & 1 deletion pie/strings_pie.go
Expand Up @@ -511,7 +511,7 @@ func (ss Strings) Mode() Strings {
if len(ss) == 0 {
return nil
}
values := make(map[string]int, 0)
values := make(map[string]int)
for _, s := range ss {
values[s]++
}
Expand Down
23 changes: 22 additions & 1 deletion template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a685ad

Please sign in to comment.