summaryrefslogtreecommitdiff
path: root/utilities/math.go
blob: 1ecca610e731933c7db90268fb8170715b94a00f (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * This file is part of Go Responsiveness.
 *
 * Go Responsiveness is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * either version 2 of the License, or (at your option) any later version.
 * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>.
 */

package utilities

import (
	"math"
	"sort"

	"golang.org/x/exp/constraints"
	"golang.org/x/exp/slices"
)

type Number interface {
	constraints.Float | constraints.Integer
}

func CalculateAverage[T Number](elements []T) float64 {
	total := T(0)
	for i := 0; i < len(elements); i++ {
		total += elements[i]
	}
	return float64(total) / float64(len(elements))
}

func CalculatePercentile[T Number](
	elements []T,
	p int,
) (result T) {
	result = T(0)
	if p < 0 || p > 100 {
		return
	}

	sort.Slice(elements, func(l int, r int) bool { return elements[l] < elements[r] })
	pindex := int64((float64(p) / float64(100)) * float64(len(elements)))
	result = elements[pindex]
	return
}

func Max(x, y uint64) uint64 {
	if x > y {
		return x
	}
	return y
}

func SignedPercentDifference[T constraints.Float | constraints.Integer](
	current T,
	previous T,
) (difference float64) {
	fCurrent := float64(current)
	fPrevious := float64(previous)
	return ((fCurrent - fPrevious) / fPrevious) * 100.0
}

func AbsPercentDifference(
	current float64,
	previous float64,
) (difference float64) {
	return (math.Abs(current-previous) / (float64(current+previous) / 2.0)) * float64(
		100,
	)
}

func CalculateStandardDeviation[T constraints.Float | constraints.Integer](elements []T) float64 {
	// From https://www.mathsisfun.com/data/standard-deviation-calculator.html
	// Yes, for real!

	// Calculate the average of the numbers ...
	average := CalculateAverage(elements)

	// Calculate the square of each of the elements' differences from the mean.
	differences_squared := make([]float64, len(elements))
	for index, value := range elements {
		differences_squared[index] = math.Pow(float64(value-T(average)), 2)
	}

	// The variance is the average of the squared differences.
	// So, we need to ...

	// Accumulate all those squared differences.
	sds := float64(0)
	for _, dss := range differences_squared {
		sds += dss
	}

	// And then divide that total by the number of elements
	variance := sds / float64(len(elements))

	// Finally, the standard deviation is the square root
	// of the variance.
	sd := float64(math.Sqrt(variance))
	// sd := T(variance)

	return sd
}

func AllSequentialIncreasesLessThan[T Number](elements []T, limit float64) (bool, float64) {
	if len(elements) < 2 {
		return false, 0.0
	}

	maximumSequentialIncrease := float64(0)
	for i := 1; i < len(elements); i++ {
		current := elements[i]
		previous := elements[i-1]
		percentChange := SignedPercentDifference(current, previous)
		if percentChange > limit {
			return false, percentChange
		}
		if percentChange > float64(maximumSequentialIncrease) {
			maximumSequentialIncrease = percentChange
		}
	}
	return true, maximumSequentialIncrease
}

// elements must already be sorted!
func TrimBy[T Number](elements []T, trim int) []T {
	numberToKeep := int(float32(len(elements)) * (float32(trim) / 100.0))

	return elements[:numberToKeep]
}

func TrimmedMean[T Number](elements []T, trim int) (float64, []T) {
	sortedElements := make([]T, len(elements))
	copy(sortedElements, elements)
	slices.Sort(sortedElements)

	trimmedElements := TrimBy(sortedElements, trim)
	return CalculateAverage(trimmedElements), trimmedElements
}