-
Notifications
You must be signed in to change notification settings - Fork 1
/
weighted_test.go
81 lines (64 loc) · 2.11 KB
/
weighted_test.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
74
75
76
77
78
79
80
81
// Copyright 2019, LightStep Inc.
package varopt_test
import (
"fmt"
"math"
"math/rand"
"github.com/lightstep/varopt"
)
type packet struct {
size int
color string
protocol string
}
func ExampleNew() {
const totalPackets = 1e6
const sampleRatio = 0.01
colors := []string{"red", "green", "blue"}
protocols := []string{"http", "tcp", "udp"}
sizeByColor := map[string]int{}
sizeByProtocol := map[string]int{}
trueTotalWeight := 0.0
rnd := rand.New(rand.NewSource(32491))
sampler := varopt.New[packet](totalPackets*sampleRatio, rnd)
for i := 0; i < totalPackets; i++ {
packet := packet{
size: 1 + rnd.Intn(100000),
color: colors[rnd.Intn(len(colors))],
protocol: protocols[rnd.Intn(len(protocols))],
}
sizeByColor[packet.color] += packet.size
sizeByProtocol[packet.protocol] += packet.size
trueTotalWeight += float64(packet.size)
sampler.Add(packet, float64(packet.size))
}
estSizeByColor := map[string]float64{}
estSizeByProtocol := map[string]float64{}
estTotalWeight := 0.0
for i := 0; i < sampler.Size(); i++ {
packet, weight := sampler.Get(i)
estSizeByColor[packet.color] += weight
estSizeByProtocol[packet.protocol] += weight
estTotalWeight += weight
}
// Compute mean average percentage error for colors
colorMape := 0.0
for _, c := range colors {
colorMape += math.Abs(float64(sizeByColor[c])-estSizeByColor[c]) / float64(sizeByColor[c])
}
colorMape /= float64(len(colors))
// Compute mean average percentage error for protocols
protocolMape := 0.0
for _, p := range protocols {
protocolMape += math.Abs(float64(sizeByProtocol[p])-estSizeByProtocol[p]) / float64(sizeByProtocol[p])
}
protocolMape /= float64(len(protocols))
// Compute total sum error percentage
fmt.Printf("Total sum error %.2g%%\n", 100*math.Abs(estTotalWeight-trueTotalWeight)/trueTotalWeight)
fmt.Printf("Color mean absolute percentage error %.2f%%\n", 100*colorMape)
fmt.Printf("Protocol mean absolute percentage error %.2f%%\n", 100*protocolMape)
// Output:
// Total sum error 2.4e-11%
// Color mean absolute percentage error 0.73%
// Protocol mean absolute percentage error 1.62%
}