-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregator_fraud.go
More file actions
290 lines (252 loc) · 5.51 KB
/
aggregator_fraud.go
File metadata and controls
290 lines (252 loc) · 5.51 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
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
package gofeat
import (
"math"
"sort"
"time"
)
// Velocity computes events per minute within a time window.
// Use this to detect velocity abuse (e.g., too many transactions per minute).
func Velocity(window time.Duration) AggregatorFactory {
return func() Aggregator {
return &velocityAgg{
window: window,
firstTime: time.Time{},
lastTime: time.Time{},
}
}
}
type velocityAgg struct {
count int
window time.Duration
firstTime time.Time
lastTime time.Time
}
func (a *velocityAgg) Add(e Event) {
a.count++
if a.firstTime.IsZero() || e.Timestamp.Before(a.firstTime) {
a.firstTime = e.Timestamp
}
if a.lastTime.IsZero() || e.Timestamp.After(a.lastTime) {
a.lastTime = e.Timestamp
}
}
func (a *velocityAgg) Result() any {
if a.count == 0 || a.window == 0 {
return 0.0
}
// If we have only one event or all events at same time
duration := a.lastTime.Sub(a.firstTime)
if duration == 0 {
// All events at same time - return count per window
return float64(a.count) / a.window.Minutes()
}
// Calculate events per minute based on actual duration
minutes := duration.Minutes()
if minutes == 0 {
return float64(a.count)
}
return float64(a.count) / minutes
}
// Entropy computes Shannon entropy for a field's values.
// High entropy = many different values (suspicious for device_id, IP, etc).
func Entropy(field string) AggregatorFactory {
return func() Aggregator {
return &entropyAgg{
field: field,
counts: make(map[any]int),
}
}
}
type entropyAgg struct {
field string
counts map[any]int
total int
}
func (a *entropyAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
a.counts[v]++
a.total++
}
func (a *entropyAgg) Result() any {
if a.total == 0 {
return 0.0
}
var entropy float64
for _, count := range a.counts {
if count > 0 {
p := float64(count) / float64(a.total)
entropy -= p * math.Log2(p)
}
}
return entropy
}
// UniqueRatio computes the ratio of unique values to total events.
// Returns float64 from 0.0 to 1.0.
// 1.0 = all values unique (suspicious for cards, emails, etc)
// 0.0 = all values same.
func UniqueRatio(field string) AggregatorFactory {
return func() Aggregator {
return &uniqueRatioAgg{
field: field,
seen: make(map[any]struct{}),
}
}
}
type uniqueRatioAgg struct {
field string
seen map[any]struct{}
total int
}
func (a *uniqueRatioAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
a.seen[v] = struct{}{}
a.total++
}
func (a *uniqueRatioAgg) Result() any {
if a.total == 0 {
return 0.0
}
return float64(len(a.seen)) / float64(a.total)
}
// TimeSinceFirst returns duration since the first event.
// Useful for account age, time since first transaction, etc.
func TimeSinceFirst() AggregatorFactory {
return func() Aggregator {
return &timeSinceFirstAgg{
firstTime: time.Time{},
lastTime: time.Time{},
}
}
}
type timeSinceFirstAgg struct {
firstTime time.Time
lastTime time.Time
}
func (a *timeSinceFirstAgg) Add(e Event) {
if a.firstTime.IsZero() || e.Timestamp.Before(a.firstTime) {
a.firstTime = e.Timestamp
}
if a.lastTime.IsZero() || e.Timestamp.After(a.lastTime) {
a.lastTime = e.Timestamp
}
}
func (a *timeSinceFirstAgg) Result() any {
if a.firstTime.IsZero() {
return time.Duration(0)
}
return a.lastTime.Sub(a.firstTime)
}
// Percentile computes the percentile value for a numeric field.
// p should be between 0.0 and 1.0 (e.g., 0.95 for p95, 0.99 for p99).
// Use this for outlier detection.
func Percentile(field string, p float64) AggregatorFactory {
return func() Aggregator {
return &percentileAgg{
field: field,
p: p,
}
}
}
type percentileAgg struct {
field string
p float64
values []float64
}
func (a *percentileAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
if f, okF := toFloat64(v); okF {
a.values = append(a.values, f)
}
}
func (a *percentileAgg) Result() any {
if len(a.values) == 0 {
return 0.0
}
sorted := make([]float64, len(a.values))
copy(sorted, a.values)
sort.Float64s(sorted)
index := int(float64(len(sorted)-1) * a.p)
if index < 0 {
index = 0
}
if index >= len(sorted) {
index = len(sorted) - 1
}
return sorted[index]
}
// StandardDeviation computes the standard deviation for a numeric field.
// Use this for anomaly detection (e.g., Z-score calculation).
func StandardDeviation(field string) AggregatorFactory {
return func() Aggregator {
return &stdDevAgg{field: field}
}
}
type stdDevAgg struct {
field string
values []float64
}
func (a *stdDevAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
if f, okF := toFloat64(v); okF {
a.values = append(a.values, f)
}
}
func (a *stdDevAgg) Result() any {
if len(a.values) == 0 {
return 0.0
}
// Calculate mean
var sum float64
for _, v := range a.values {
sum += v
}
mean := sum / float64(len(a.values))
// Calculate variance
var variance float64
for _, v := range a.values {
diff := v - mean
variance += diff * diff
}
variance /= float64(len(a.values))
// Standard deviation is square root of variance
return math.Sqrt(variance)
}
// Mean computes the average value for a numeric field.
func Mean(field string) AggregatorFactory {
return func() Aggregator {
return &meanAgg{field: field}
}
}
type meanAgg struct {
field string
sum float64
count int
}
func (a *meanAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
if f, okF := toFloat64(v); okF {
a.sum += f
a.count++
}
}
func (a *meanAgg) Result() any {
if a.count == 0 {
return 0.0
}
return a.sum / float64(a.count)
}