-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample.go
More file actions
194 lines (159 loc) · 3.86 KB
/
sample.go
File metadata and controls
194 lines (159 loc) · 3.86 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package sample
import (
"fmt"
"math/rand"
"strings"
"sync"
)
type Sampler struct {
rnd *rand.Rand
mu *sync.Mutex
}
// NewSampler instantiates a new Sampler, using the seed provided to create a new `rand.Rand` object.
func NewSampler(seed int64) *Sampler {
return &Sampler{
rand.New(rand.NewSource(seed)),
&sync.Mutex{},
}
}
type index []uint
func newIndex(n int) index {
xindex := make(index, n)
for i := 0; i < n; i++ {
xindex[i] = uint(i)
}
return xindex
}
func (i index) Remove(which uint) index {
if which >= uint(len(i)) {
return i
}
if which == uint(len(i)-1) {
return i[:which]
}
return append(i[:which], i[which+1:]...)
}
type vector []float64
func (v vector) Copy() vector {
v2 := make(vector, len(v))
for i, vi := range v {
v2[i] = vi
}
return v2
}
func (v vector) Remove(which uint) vector {
if which >= uint(len(v)) {
return v
}
if which == uint(len(v)-1) {
return v[:which]
}
return append(v[:which], v[which+1:]...)
}
func (v vector) Sum() float64 {
var s float64
for _, val := range v {
s += val
}
return s
}
// scale scales each element by the sum of the vector
func (v vector) Scale() vector {
scaled := make(vector, len(v))
sum := v.Sum()
for i, val := range v {
scaled[i] = val / sum
}
return scaled
}
func (v vector) CumProb() vector {
sum := v.Sum()
cumprob := make([]float64, len(v))
var cumsum float64
for i, val := range v {
cumsum += val / sum
cumprob[i] = cumsum
}
return cumprob
}
func (v vector) String() string {
parts := make([]string, len(v))
for i, val := range v {
parts[i] = fmt.Sprintf("%.3f", val)
}
return fmt.Sprintf("[%s]", strings.Join(parts, " "))
}
func find(w vector, val float64) int {
for i, weight := range w {
if val <= weight {
return i
}
}
return len(w) - 1
}
func (s *Sampler) randfloat() float64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.rnd.Float64()
}
// SampleFloats takes a slice of floats and returns a sample of n
// elements with probability proportional to the weights. `Replace`
// specifies whether or not to sample with replacement.
func (s *Sampler) SampleFloats(x []float64, n int, replace bool, weights vector) ([]float64, error) {
xindex := newIndex(len(x))
index, err := sample(xindex, n, replace, weights, s.randfloat)
if err != nil {
return nil, err
}
result := make([]float64, n)
for i, val := range index {
result[i] = x[val]
}
return result, nil
}
// SampleInts takes a slice of ints and returns a sample of n
// elements with probability proportional to the weights. `Replace`
// specifies whether or not to sample with replacement.
func (s *Sampler) SampleInts(x []int, n int, replace bool, weights vector) ([]int, error) {
xindex := newIndex(len(x))
index, err := sample(xindex, n, replace, weights, s.randfloat)
if err != nil {
return nil, err
}
result := make([]int, n)
for i, val := range index {
result[i] = x[val]
}
return result, nil
}
// return the sample index to the other public functions
func sample(x index, n int, replace bool, weights vector, rnd func() float64) ([]uint, error) {
if weights != nil && len(x) != len(weights) {
return nil, fmt.Errorf("length of x (%d) unequal to length of weights (%d)", len(x), len(weights))
}
if !replace && n > len(x) {
return nil, fmt.Errorf("cannot sample with replacement when n (%d) is greater than x (%d)", n, len(x))
}
// cumulative probabilities
if weights == nil {
weights = make(vector, len(x))
nx := float64(len(x))
for i, _ := range x {
weights[i] = 1 / nx
}
}
weights2 := weights.Copy()
cumprob := weights2.CumProb()
results := make([]uint, n)
for i := 0; i < n; i++ {
idx := uint(find(cumprob, rnd()))
results[i] = x[idx]
// if sampling w/o replacement, remove the index and re-scale weights
if !replace {
x = x.Remove(idx)
weights2 = weights2.Remove(idx)
cumprob = weights2.Scale().CumProb()
}
}
return results, nil
}