-
Notifications
You must be signed in to change notification settings - Fork 3
/
sample.go
68 lines (63 loc) · 1.61 KB
/
sample.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
// Package sample solves problem of choosing k random elements with weights
// from some(potentially big) sequence. Such problem is called Reservoir Sampling.
package sample
import (
"errors"
"fmt"
"math/rand"
)
var (
// ErrEmptyInput used when input sequence is empty - nothing to sample.
ErrEmptyInput = errors.New("input sequence empty")
// ErrTooBigSample returned when requested sample is bigger than input sequence.
ErrTooBigSample = errors.New("requested sample size exceeds size of sequence")
)
// Weighted is interface for "weighted" elements in sequence.
type Weighted interface {
Weight() float64
}
// Sample samples random k elements with respect to their weights from slice of
// Weighted elements.
//
// Complexity is O(n*log(k)), O(k) memory is used.
func Sample(input []Weighted, k int) ([]Weighted, error) {
if len(input) == 0 {
return nil, ErrEmptyInput
}
if k >= len(input) {
return nil, ErrTooBigSample
}
if k == 1 {
ch, err := Choice(input)
if err != nil {
return nil, err
}
return []Weighted{ch}, nil
}
sh := newSampleHeap(k)
for _, w := range input {
sh.Push(w)
}
return sh.Result(), nil
}
// Choice returns Weighted element with respect to its weight from slice of
// Weighted elements.
//
// Complexity is O(n), memory usage is O(1).
func Choice(input []Weighted) (Weighted, error) {
if len(input) == 0 {
return nil, ErrEmptyInput
}
var wsum float64
for _, w := range input {
wsum += w.Weight()
}
threshold := rand.Float64() * wsum
for _, w := range input {
threshold -= w.Weight()
if threshold <= 0 {
return w, nil
}
}
return nil, fmt.Errorf("internal error")
}