Add ‘math’ package for all mathematical functions
This commit is contained in:
parent
34a4132403
commit
121498e95e
28 changed files with 1066 additions and 0 deletions
21
util/math/abs.go
Normal file
21
util/math/abs.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Abs(val float64) float64 {
|
||||
return math.Abs(val)
|
||||
}
|
22
util/math/bool.go
Normal file
22
util/math/bool.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
func Bool(val bool) int {
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
32
util/math/bottom.go
Normal file
32
util/math/bottom.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
func Bottom(vals []float64, take int) []float64 {
|
||||
|
||||
var out []float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort := Sort(vals)
|
||||
|
||||
for i := 0; i < take && i < len(vals); i++ {
|
||||
out = append(out, sort[i])
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
21
util/math/ceil.go
Normal file
21
util/math/ceil.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Ceil(val float64) float64 {
|
||||
return math.Ceil(val)
|
||||
}
|
25
util/math/copy.go
Normal file
25
util/math/copy.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
func Copy(vals []float64) []float64 {
|
||||
|
||||
outs := make([]float64, len(vals))
|
||||
|
||||
copy(outs, vals)
|
||||
|
||||
return outs
|
||||
|
||||
}
|
43
util/math/correlation.go
Normal file
43
util/math/correlation.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Correlation(a, b []float64) float64 {
|
||||
|
||||
if len(a) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if len(b) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if len(a) != len(b) {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
sa := PopulationStandardDeviation(a)
|
||||
sb := PopulationStandardDeviation(b)
|
||||
co := PopulationCovariance(a, b)
|
||||
|
||||
if sa == 0 || sb == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return co / (sa * sb)
|
||||
|
||||
}
|
73
util/math/covariance.go
Normal file
73
util/math/covariance.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Covariance(a, b []float64) float64 {
|
||||
|
||||
var ss float64
|
||||
|
||||
if len(a) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if len(b) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if len(a) != len(b) {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
l, ma, mb := Size(a), Mean(a), Mean(b)
|
||||
|
||||
for i := 0; i < l; i++ {
|
||||
da := (a[i] - ma)
|
||||
db := (b[i] - mb)
|
||||
ss += (da*db - ss) / float64(i+1)
|
||||
}
|
||||
|
||||
return ss * float64(l) / float64(l-1)
|
||||
|
||||
}
|
||||
|
||||
func PopulationCovariance(a, b []float64) float64 {
|
||||
|
||||
var ss float64
|
||||
|
||||
if len(a) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if len(b) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if len(a) != len(b) {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
l, ma, mb := Size(a), Mean(a), Mean(b)
|
||||
|
||||
for i := 0; i < l; i++ {
|
||||
da := (a[i] - ma)
|
||||
db := (b[i] - mb)
|
||||
ss += da * db
|
||||
}
|
||||
|
||||
return ss / float64(l)
|
||||
|
||||
}
|
47
util/math/deviation.go
Normal file
47
util/math/deviation.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func StandardDeviation(vals []float64, sample bool) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
return math.Pow(Variance(vals, sample), 0.5)
|
||||
|
||||
}
|
||||
|
||||
func SampleStandardDeviation(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
return math.Pow(SampleVariance(vals), 0.5)
|
||||
|
||||
}
|
||||
|
||||
func PopulationStandardDeviation(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
return math.Pow(PopulationVariance(vals), 0.5)
|
||||
|
||||
}
|
21
util/math/floor.go
Normal file
21
util/math/floor.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Floor(val float64) float64 {
|
||||
return math.Floor(val)
|
||||
}
|
37
util/math/max.go
Normal file
37
util/math/max.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Max(vals []float64) float64 {
|
||||
|
||||
var max float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
max = vals[0]
|
||||
|
||||
for _, v := range vals {
|
||||
if v > max {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
|
||||
return max
|
||||
|
||||
}
|
64
util/math/mean.go
Normal file
64
util/math/mean.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Mean(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
return Sum(vals) / float64(len(vals))
|
||||
|
||||
}
|
||||
|
||||
func RollingMean(cnt int64, sum float64) float64 {
|
||||
|
||||
return sum / float64(cnt)
|
||||
|
||||
}
|
||||
|
||||
func GeometricMean(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
return math.Pow(Product(vals), 1/float64(len(vals)))
|
||||
|
||||
}
|
||||
|
||||
func HarmonicMean(vals []float64) float64 {
|
||||
|
||||
var out float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
for _, v := range vals {
|
||||
if v < 0 {
|
||||
return math.NaN()
|
||||
} else if v == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
out += (1 / v)
|
||||
}
|
||||
|
||||
return float64(len(vals)) / out
|
||||
|
||||
}
|
54
util/math/median.go
Normal file
54
util/math/median.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Median(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
l := len(vals)
|
||||
|
||||
switch {
|
||||
case l%2 == 0:
|
||||
sort := Sort(vals)
|
||||
return Mean(sort[l/2-1 : l/2+1])
|
||||
default:
|
||||
sort := Sort(vals)
|
||||
return float64(sort[l/2])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func MedianAbsoluteDeviation(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
dups := Copy(vals)
|
||||
|
||||
m := Median(dups)
|
||||
|
||||
for k, v := range dups {
|
||||
dups[k] = math.Abs(v - m)
|
||||
}
|
||||
|
||||
return Median(dups)
|
||||
|
||||
}
|
29
util/math/midhinge.go
Normal file
29
util/math/midhinge.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Midhinge(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
q1, _, q3 := Quartile(vals)
|
||||
|
||||
return (q1 + q3) / 2
|
||||
|
||||
}
|
37
util/math/min.go
Normal file
37
util/math/min.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Min(vals []float64) float64 {
|
||||
|
||||
var min float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
min = vals[0]
|
||||
|
||||
for _, v := range vals {
|
||||
if v < min {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
|
||||
return min
|
||||
|
||||
}
|
66
util/math/mode.go
Normal file
66
util/math/mode.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "sort"
|
||||
|
||||
func Mode(vals []float64) []float64 {
|
||||
|
||||
var out []float64
|
||||
|
||||
switch len(vals) {
|
||||
case 1:
|
||||
return vals
|
||||
case 0:
|
||||
return nil
|
||||
}
|
||||
|
||||
if !sort.Float64sAreSorted(vals) {
|
||||
vals = Sort(vals)
|
||||
}
|
||||
|
||||
out = make([]float64, 5)
|
||||
|
||||
cnt, max := 1, 1
|
||||
|
||||
for i := 1; i < len(vals); i++ {
|
||||
switch {
|
||||
case vals[i] == vals[i-1]:
|
||||
cnt++
|
||||
case cnt == max && max != 1:
|
||||
out = append(out, vals[i-1])
|
||||
cnt = 1
|
||||
case cnt > max:
|
||||
out = append(out[:0], vals[i-1])
|
||||
max, cnt = cnt, 1
|
||||
default:
|
||||
cnt = 1
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case cnt == max:
|
||||
out = append(out, vals[len(vals)-1])
|
||||
case cnt > max:
|
||||
out = append(out[:0], vals[len(vals)-1])
|
||||
max = cnt
|
||||
}
|
||||
|
||||
if max == 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
71
util/math/percentile.go
Normal file
71
util/math/percentile.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Percentile(vals []float64, percent float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if percent <= 0 || percent > 100 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
sort := Sort(vals)
|
||||
size := Size(vals)
|
||||
indx := (percent / 100) * float64(size)
|
||||
|
||||
switch {
|
||||
case indx == Whole(indx):
|
||||
i := int(indx)
|
||||
return sort[i-1]
|
||||
case indx > 1:
|
||||
i := int(indx)
|
||||
return Mean([]float64{sort[i-1], sort[i]})
|
||||
default:
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func NearestRankPercentile(vals []float64, percent float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
if percent <= 0 || percent > 100 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
sort := Sort(vals)
|
||||
size := Size(vals)
|
||||
|
||||
if percent == 100 {
|
||||
return sort[size-1]
|
||||
}
|
||||
|
||||
r := int(math.Ceil(float64(size) * percent / 100))
|
||||
|
||||
if r == 0 {
|
||||
return sort[0]
|
||||
}
|
||||
|
||||
return sort[r-1]
|
||||
|
||||
}
|
38
util/math/product.go
Normal file
38
util/math/product.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Product(vals []float64) float64 {
|
||||
|
||||
var out float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
for _, v := range vals {
|
||||
switch out {
|
||||
case 0:
|
||||
out = v
|
||||
default:
|
||||
out *= v
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
|
||||
}
|
52
util/math/quartile.go
Normal file
52
util/math/quartile.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Quartile(vals []float64) (float64, float64, float64) {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN(), math.NaN(), math.NaN()
|
||||
}
|
||||
|
||||
l := len(vals)
|
||||
|
||||
switch {
|
||||
case l%2 == 0:
|
||||
c1 := l / 2
|
||||
c2 := l / 2
|
||||
sort := Sort(vals)
|
||||
return Median(sort[:c1]), Median(sort), Median(sort[c2:])
|
||||
default:
|
||||
c1 := (l - 1) / 2
|
||||
c2 := c1 + 1
|
||||
sort := Sort(vals)
|
||||
return Median(sort[:c1]), Median(sort), Median(sort[c2:])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func InterQuartileRange(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
q1, _, q3 := Quartile(vals)
|
||||
|
||||
return q3 - q1
|
||||
|
||||
}
|
21
util/math/round.go
Normal file
21
util/math/round.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Round(val float64) float64 {
|
||||
return math.Floor(val + 0.5)
|
||||
}
|
37
util/math/sample.go
Normal file
37
util/math/sample.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math/rand"
|
||||
|
||||
func Sample(vals []float64, take int) []float64 {
|
||||
|
||||
var out []float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for k, v := range rand.Perm(len(vals)) {
|
||||
if k < take {
|
||||
out = append(out, vals[v])
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return out
|
||||
|
||||
}
|
19
util/math/size.go
Normal file
19
util/math/size.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
func Size(vals []float64) int {
|
||||
return len(vals)
|
||||
}
|
27
util/math/sort.go
Normal file
27
util/math/sort.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "sort"
|
||||
|
||||
func Sort(vals []float64) []float64 {
|
||||
|
||||
outs := Copy(vals)
|
||||
|
||||
sort.Float64s(outs)
|
||||
|
||||
return outs
|
||||
|
||||
}
|
41
util/math/spread.go
Normal file
41
util/math/spread.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Spread(vals []float64) float64 {
|
||||
|
||||
var min, max float64
|
||||
|
||||
switch len(vals) {
|
||||
case 0:
|
||||
return math.NaN()
|
||||
case 1:
|
||||
return 0
|
||||
}
|
||||
|
||||
for _, v := range vals {
|
||||
switch {
|
||||
case v < min:
|
||||
min = v
|
||||
case v > max:
|
||||
max = v
|
||||
}
|
||||
}
|
||||
|
||||
return max - min
|
||||
|
||||
}
|
33
util/math/sum.go
Normal file
33
util/math/sum.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Sum(vals []float64) float64 {
|
||||
|
||||
var sum float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
for _, v := range vals {
|
||||
sum += v
|
||||
}
|
||||
|
||||
return sum
|
||||
|
||||
}
|
32
util/math/top.go
Normal file
32
util/math/top.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
func Top(vals []float64, take int) []float64 {
|
||||
|
||||
var out []float64
|
||||
|
||||
if len(vals) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort := Sort(vals)
|
||||
|
||||
for i := 0; i < take && i < len(vals); i++ {
|
||||
out = append(out, sort[len(vals)-1-i])
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
29
util/math/trimean.go
Normal file
29
util/math/trimean.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Trimean(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
q1, q2, q3 := Quartile(vals)
|
||||
|
||||
return (q1 + (q2 * 2) + q3) / 4
|
||||
|
||||
}
|
55
util/math/variance.go
Normal file
55
util/math/variance.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
import "math"
|
||||
|
||||
func Variance(vals []float64, sample bool) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
var out float64
|
||||
|
||||
m := Mean(vals)
|
||||
|
||||
for _, v := range vals {
|
||||
out += (float64(v) - m) * (float64(v) - m)
|
||||
}
|
||||
|
||||
return out / float64((len(vals) - (1 * Bool(sample))))
|
||||
|
||||
}
|
||||
|
||||
func SampleVariance(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
return Variance(vals, true)
|
||||
|
||||
}
|
||||
|
||||
func PopulationVariance(vals []float64) float64 {
|
||||
|
||||
if len(vals) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
|
||||
return Variance(vals, false)
|
||||
|
||||
}
|
19
util/math/whole.go
Normal file
19
util/math/whole.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package math
|
||||
|
||||
func Whole(val float64) float64 {
|
||||
return float64(int64(val))
|
||||
}
|
Loading…
Reference in a new issue