Repository navigation
Expand file tree
/
Copy pathdeviation.go
More file actions
63 lines (47 loc) · 1.68 KB
/
Copy pathdeviation.go
File metadata and controls
63 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package stats
import "math"
// MedianAbsoluteDeviation finds the median of the absolute deviations from the dataset median
func MedianAbsoluteDeviation(input Float64Data) (mad float64, err error) {
return MedianAbsoluteDeviationPopulation(input)
}
// MedianAbsoluteDeviationPopulation finds the median of the absolute deviations from the population median.
// If the input contains a NaN the result is NaN.
func MedianAbsoluteDeviationPopulation(input Float64Data) (mad float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInputErr
}
i := copyslice(input)
m, _ := Median(i)
for key, value := range i {
i[key] = math.Abs(value - m)
}
return Median(i)
}
// StandardDeviation the amount of variation in the dataset
func StandardDeviation(input Float64Data) (sdev float64, err error) {
return StandardDeviationPopulation(input)
}
// StandardDeviationPopulation finds the amount of variation from the population
func StandardDeviationPopulation(input Float64Data) (sdev float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInputErr
}
// Get the scaled population variance
vp, e := scaledVariance(input, 0)
// Return the population standard deviation
return math.Ldexp(math.Sqrt(vp), e), nil
}
// StandardDeviationSample finds the amount of variation from a sample. It
// returns ErrTooFew for a single element.
func StandardDeviationSample(input Float64Data) (sdev float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInputErr
}
if input.Len() == 1 {
return math.NaN(), ErrTooFew
}
// Get the scaled sample variance
vs, e := scaledVariance(input, 1)
// Return the sample standard deviation
return math.Ldexp(math.Sqrt(vs), e), nil
}