This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.go
73 lines (60 loc) · 1.73 KB
/
utils.go
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
64
65
66
67
68
69
70
71
72
73
package log
import (
"math"
"time"
)
//Round rounds up a float64, without digits after the comma
func Round(f float64) float64 {
return math.Floor(f + .5)
}
//RoundWithPrecision rounds up a float64, with a specified amount of digits after the comma
func RoundWithPrecision(f float64, places int) float64 {
shift := math.Pow(10, float64(places))
return Round(f*shift) / shift
}
//MeanInt64 returns the mean for a []int64
func MeanInt64(data []int64) float64 {
sum := int64(0)
for i := 0; i < len(data); i++ {
sum += data[i]
}
mean := float64(sum) / float64(len(data))
return mean
}
//MeanFloat64 returns the mean for a []float64
func MeanFloat64(data []float64) float64 {
sum := float64(0)
for i := 0; i < len(data); i++ {
sum += data[i]
}
mean := float64(sum) / float64(len(data))
return mean
}
//Confidence95Percentiles returns the delta for the 95% confidence interval. The actual interval is [mean(x)-delta; mean(x)+delta]
func ConfidenceInterval95(data []int64) float64 {
n := len(data)
if n == 0 {
return 0
}
mean_val := MeanInt64(data)
var deviations []float64
for i := 0; i < n; i++ {
diff := mean_val - float64(data[i])
deviations = append(deviations, diff*diff)
}
variance := MeanFloat64(deviations)
stddev := math.Sqrt(variance)
sigma := stddev / math.Sqrt(float64(n))
z_value_95 := 1.96
confidenceDelta := z_value_95 * sigma
return confidenceDelta
}
// MsTimeStampNow returns the current timestamp, in milliseconds.
func MsTimeStampNow() int64 {
return MsTimeStamp(time.Now())
}
// MsTimeStamp converts time.Time into int64
func MsTimeStamp(t time.Time) int64 {
//http://stackoverflow.com/questions/24122821/go-golang-time-now-unixnano-convert-to-milliseconds
return t.UnixNano() / int64(time.Millisecond)
}