/
zscoreAPI.go
138 lines (118 loc) · 3.57 KB
/
zscoreAPI.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
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
package zscore
import (
"log"
"math"
"github.com/tucobenedicto/mongoDBConfig"
"gopkg.in/mgo.v2/bson"
)
// apply logarithmic transformation to amounts and number of transactions to normalize
// will more closely resemble a normal distribution so we can take z-scores
// aggregate a sum, then divide by total count to find means
// return as a struct of means
func getMeans(id mongoDBConfig.BrandId) (mean ZscoreData) {
collections := mongoDBConfig.NewDBConn()
visitorsCollection := collections.C("visitors")
result := visitorsCollection.
Find(bson.M{
"brandid": id, // for which brand in our db are we computing z-scores
"summaries.amt": bson.M{"$gt": 0}, // don't include $0 transactions
}).
Select(bson.M{
"summaries.amt": 1,
"summaries.trn": 1}).
//Limit(100).
Iter()
visitor := Visitor{}
count := 0
// mgo next() will iterate through the dataset and unmarshal into vistor struct
for result.Next(&visitor) {
count += 1
amt := visitor.Summaries.Amount
amt = math.Log10(amt)
mean.Amt += amt
trn := float64(visitor.Summaries.NumberOfTransactions)
trn = math.Log10(trn)
mean.Trn += trn
}
mean.Amt = mean.Amt / float64(count)
mean.Trn = mean.Trn / float64(count)
return
}
// calculate standard deviation, also applying logarithmic transformation
func getStdDevs(id mongoDBConfig.BrandId, mean ZscoreData) (stdDev ZscoreData) {
collections := mongoDBConfig.NewDBConn()
visitorsCollection := collections.C("visitors")
result := visitorsCollection.
Find(bson.M{
"brandid": id,
"summaries.amt": bson.M{"$gt": 0},
}).
Select(bson.M{
"summaries.amt": 1,
"summaries.trn": 1}).
Iter()
visitor := Visitor{}
// let's get variances first, then standard deviations
variance := ZscoreData{}
count := 0
for result.Next(&visitor) {
count += 1
amt := visitor.Summaries.Amount
amt = math.Log10(amt)
amt = mean.Amt - amt
variance.Amt += (amt * amt)
trn := float64(visitor.Summaries.NumberOfTransactions)
trn = math.Log10(trn)
trn = mean.Trn - trn
variance.Trn += (trn * trn)
}
// divide by total count - 1 to find variance
variance.Amt = variance.Amt / float64(count-1)
variance.Trn = variance.Trn / float64(count-1)
stdDev.Amt = math.Sqrt(variance.Amt)
stdDev.Trn = math.Sqrt(variance.Trn)
return
}
// subtract from mean, divide by standard deviation to get z-score
// update db with new z-scores
func updateVisitorsWithZScore(id mongoDBConfig.BrandId, mean ZscoreData, stdDev ZscoreData) bool {
collections := mongoDBConfig.NewDBConn()
visitorsCollection := collections.C("visitors")
visitorsUpdater := collections.C("visitors")
result := visitorsCollection.
Find(bson.M{
"brandid": id,
"summaries.amt": bson.M{"$gt": 0},
}).
Select(bson.M{
"_id": 1,
"summaries.amt": 1,
"summaries.trn": 1}).
Iter()
v := Visitor{}
zscore := ZscoreData{}
for result.Next(&v) {
amt := v.Summaries.Amount
amt = math.Log10(amt)
zscore.Amt = (amt - mean.Amt) / stdDev.Amt
trn := float64(v.Summaries.NumberOfTransactions)
trn = math.Log10(trn)
zscore.Trn = (trn - mean.Trn) / stdDev.Trn
set := bson.M{
"zscore": zscore,
}
// mgo UpdateID() will update entry whose _id matches id argument
if err := visitorsUpdater.UpdateId(v.Id, bson.M{"$set": set}); err != nil {
log.Printf("Got an error while updating visitor: %v\n", err)
return false
}
}
return true
}
// public function to run all calculations and update db
func RunUpdate(id mongoDBConfig.BrandId) bool {
mean := getMeans(id)
stdDev := getStdDevs(id, mean)
result := updateVisitorsWithZScore(id, mean, stdDev)
return result
}