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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
package entropy
import (
"database/sql"
"math"
)
// CREATE TABLE context(uid INTEGER, token INTEGER, logprob FLOAT, pos INTEGER)
//
// sqlMeanLogProbs matches an exact token id sequence of any length, bound as a
// single list parameter, and returns one row per position: the position, the
// mean logprob over all occurrences, and the occurrence count.
//
// seq unrolls the parameter into (position, token) pairs. Joining context on
// token alone gives every position that matches some element of the sequence;
// pos - i + 1 is the start a match would have to begin at for that element to
// line up, so counting rows per (uid, start) says how many elements agree
// there. A start where all of them agree is an occurrence.
const sqlMeanLogProbs = `
WITH seq AS (
SELECT i, s[i] AS token
FROM (SELECT ?::INTEGER[] AS s) t, range(1, len(s) + 1) r(i)
),
cand AS (
SELECT s.i AS i,
c.logprob AS logprob,
count(*) OVER (PARTITION BY c.uid, c.pos - s.i + 1) AS matched
FROM context c
JOIN seq s ON c.token = s.token
)
SELECT i, avg(logprob) AS mean, count(*) AS occurrences
FROM cand
WHERE matched = (SELECT count(*) FROM seq)
GROUP BY i
ORDER BY i`
// sqlLogProbs matches the same sequences as sqlMeanLogProbs but skips the
// aggregation, returning one row per position per occurrence.
const sqlLogProbs = `
WITH seq AS (
SELECT i, s[i] AS token
FROM (SELECT ?::INTEGER[] AS s) t, range(1, len(s) + 1) r(i)
),
cand AS (
SELECT c.uid AS uid,
c.pos - s.i + 1 AS start,
s.i AS i,
c.logprob AS logprob,
count(*) OVER (PARTITION BY c.uid, c.pos - s.i + 1) AS matched
FROM context c
JOIN seq s ON c.token = s.token
)
SELECT uid, start, i, logprob
FROM cand
WHERE matched = (SELECT count(*) FROM seq)
ORDER BY uid, start, i`
// Occurrence is one match of a sequence, holding the logprob of every position
// in the context it was found in.
type Occurrence struct {
Document int
Offset int
Values []float64
}
// LogProbs returns every occurrence of the exact id sequence separately,
// leaving the contexts uncombined.
func LogProbs(ids []int, db *sql.DB) ([]Occurrence, error) {
seq := make([]int32, len(ids)) // the driver panics on []int
for i, id := range ids {
seq[i] = int32(id)
}
rows, err := db.Query(sqlLogProbs, seq)
if err != nil {
return nil, err
}
defer rows.Close()
r := make([]Occurrence, 0)
for rows.Next() {
var document, offset, pos int
var value float64
if err := rows.Scan(&document, &offset, &pos, &value); err != nil {
return nil, err
}
if pos == 1 {
r = append(r, Occurrence{
Document: document,
Offset: offset,
Values: make([]float64, 0, len(ids)),
})
}
if len(r) == 0 {
continue
}
last := &r[len(r)-1]
last.Values = append(last.Values, value)
}
if err := rows.Err(); err != nil {
return nil, err
}
return r, nil
}
// MeanLogProbs returns the mean logprob per position over all occurrences of
// the exact id sequence, together with the number of occurrences. Occurrences
// of zero yield a nil slice: the sequence was never seen.
func MeanLogProbs(ids []int, db *sql.DB) ([]float64, int64, error) {
seq := make([]int32, len(ids)) // the driver panics on []int
for i, id := range ids {
seq[i] = int32(id)
}
rows, err := db.Query(sqlMeanLogProbs, seq)
if err != nil {
return nil, 0, err
}
defer rows.Close()
values := make([]float64, 0, len(ids))
var occurrences int64
for rows.Next() {
var pos int
var mean float64
var n int64
if err := rows.Scan(&pos, &mean, &n); err != nil {
return nil, 0, err
}
values = append(values, mean)
occurrences = n
}
if err := rows.Err(); err != nil {
return nil, 0, err
}
if len(values) == 0 {
return nil, 0, nil
}
return values, occurrences, nil
}
// Excess weights each position by how far it falls below threshold, in nats.
// Fires wherever surprisal is high in absolute terms, which in practice means
// the uncertain zone at the start of a chunk.
func Excess(values []float64, threshold float64) []float64 {
r := make([]float64, len(values))
for i, v := range values {
if d := threshold - v; d > 0 {
r[i] = d
}
}
return r
}
// Spikes weights each position by how much more surprising it is than the one
// before it, in nats. Catches boundaries an absolute threshold misses, since
// surprisal decays inside a unit and a boundary shows up as uncertainty rising
// again, however low the preceding tail got. Position 0 has no predecessor and
// is always 0.
func Spikes(values []float64) []float64 {
r := make([]float64, len(values))
for i := 1; i < len(values); i++ {
if d := values[i-1] - values[i]; d > 0 {
r[i] = d
}
}
return r
}
// Baseline accumulates mean logprobs per position index across sequences.
// Surprisal decays sharply with position -- onsets are uncertain, tails are
// forced -- so the raw value at a position says little on its own. Deviations
// measures how far a sequence departs from what its positions usually look
// like, which also cancels the downward pull the exact-sequence lookup puts on
// interior positions.
type Baseline struct {
sum []float64
sumSq []float64
noise []float64
counts []int
}
// MinVarianceShare floors the noise correction: however much sampling noise is
// estimated, a position keeps at least this share of its observed variance.
// Without it a position where every candidate agrees would correct to near-zero
// variance and produce enormous deviations from nothing.
const MinVarianceShare = 0.1
func NewBaseline() *Baseline {
return &Baseline{}
}
func (b *Baseline) grow(n int) {
for len(b.counts) < n {
b.sum = append(b.sum, 0)
b.sumSq = append(b.sumSq, 0)
b.noise = append(b.noise, 0)
b.counts = append(b.counts, 0)
}
}
func (b *Baseline) Add(values []float64) {
b.grow(len(values))
for i, v := range values {
b.sum[i] += v
b.sumSq[i] += v * v
b.counts[i]++
}
}
// AddCandidate is Add for a candidate whose means were estimated from n
// occurrences with the given per-position variances across those occurrences.
// The spread of candidate means overstates how much candidates really differ,
// by the sampling variance of the means themselves; recording it lets stats
// subtract it instead of passing the inflation on to every deviation.
func (b *Baseline) AddCandidate(values, variances []float64, n int) {
b.Add(values)
if n < 2 {
return
}
for i, v := range variances {
if i >= len(b.noise) {
break
}
b.noise[i] += v / float64(n)
}
}
func (b *Baseline) stats(pos int) (float64, float64, bool) {
if pos >= len(b.counts) || b.counts[pos] < 2 {
return 0, 0, false
}
n := float64(b.counts[pos])
mean := b.sum[pos] / n
variance := (b.sumSq[pos] - n*mean*mean) / (n - 1)
if variance <= 0 {
return mean, 0, false
}
if corrected := variance - b.noise[pos]/n; corrected > variance*MinVarianceShare {
variance = corrected
} else {
variance = variance * MinVarianceShare
}
return mean, math.Sqrt(variance), true
}
// Summarize reduces the occurrences of one candidate to per-position means and
// variances, the inputs AddCandidate needs.
func Summarize(occurrences []Occurrence) ([]float64, []float64, int) {
if len(occurrences) == 0 {
return nil, nil, 0
}
n := len(occurrences[0].Values)
means := make([]float64, n)
variances := make([]float64, n)
for _, o := range occurrences {
for i, v := range o.Values {
if i >= n {
break
}
means[i] += v
variances[i] += v * v
}
}
for i := range means {
sum := means[i]
sumSq := variances[i]
means[i] = sum / float64(len(occurrences))
if len(occurrences) < 2 {
variances[i] = 0
continue
}
variances[i] = (sumSq - float64(len(occurrences))*means[i]*means[i]) / float64(len(occurrences)-1)
if variances[i] < 0 {
variances[i] = 0
}
}
return means, variances, len(occurrences)
}
// Deviations scores each position by how many standard deviations more
// surprising it is than the same position usually is. Positions at or below
// their positional mean score 0, as they carry no evidence of a boundary.
func (b *Baseline) Deviations(values []float64) []float64 {
r := make([]float64, len(values))
for i, v := range values {
mean, std, ok := b.stats(i)
if !ok {
continue
}
if d := (mean - v) / std; d > 0 {
r[i] = d
}
}
return r
}
|