-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypercube.go
311 lines (265 loc) · 7.28 KB
/
hypercube.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
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
package weighting
import (
"fmt"
"math"
)
// HyperCube is an implementation of the Weight interface
type HyperCube struct {
Dimensions map[string]*Dimension
Stats *Stats
Initspace []float64
Workspace []float64
DimValues map[string]map[string]map[int]bool
WorkRows map[string]map[string]string
Options *Options
}
// Weight will register all responses at once
func (hc *HyperCube) Weight(responses []*Response) (*Result, error) {
for _, res := range responses {
hc.analyzeDimensionsFromResponse(res)
}
hc.configureBlockSize()
var err error
for _, res := range responses {
err = hc.addRow(res)
if err != nil {
return nil, err
}
}
hc.rakeDimensions()
return &Result{Weights: hc.getWeights(), Stats: hc.getStats()}, nil
}
func (hc *HyperCube) analyzeDimensionsFromResponse(r *Response) {
for col, val := range r.Values {
dim, found := hc.Dimensions[col]
if !found {
continue
}
if dim.Values == nil {
dim.Values = make(map[string]*DimensionValue)
}
_, found = dim.Values[val]
if !found {
dim.Values[val] = NewDimensionValue(dim.Size)
dim.Size++
}
}
}
func (hc *HyperCube) configureBlockSize() error {
blockSize := int(1)
for _, col := range hc.Options.Columns {
dim := hc.Dimensions[col]
dim.BlockSize = blockSize
blockSize = dim.BlockSize * dim.Size
}
hc.Workspace = make([]float64, blockSize)
hc.WorkRows = make(map[string]map[string]string)
return nil
}
func (hc *HyperCube) addRow(r *Response) error {
var isWork bool
switch r.Values[hc.Options.GroupColumn] {
case hc.Options.GoalGroupValue: // Exposed
isWork = false
case hc.Options.WorkGroupValue: // Control
isWork = true
default:
return fmt.Errorf("invalid group value %s", r.Values[hc.Options.GroupColumn])
}
var index int
for _, col := range hc.Options.Columns {
val := hc.v(r.Values[col])
dval, found := hc.Dimensions[col].Values[val]
if !found {
return fmt.Errorf("invalid value %s for column %s", val, col)
}
if isWork {
dval.Initial++
dval.WorkSum += 1.0
hc.Dimensions[col].Initial++
index += dval.Index * hc.Dimensions[col].BlockSize
} else {
dval.GoalSum += 1.0
}
}
if isWork {
hc.Stats.WorkRows++
hc.Workspace[index] += 1.0
hc.WorkRows[r.RespondentID] = make(map[string]string)
for _, col := range hc.Options.Columns {
_, found := hc.DimValues[col]
if !found {
hc.DimValues[col] = make(map[string]map[int]bool)
}
val := hc.v(r.Values[col])
if hc.DimValues[col][val] == nil {
hc.DimValues[col][val] = make(map[int]bool)
}
hc.DimValues[col][val][index] = true
hc.WorkRows[r.RespondentID][col] = hc.v(r.Values[col])
}
} else {
hc.Stats.GoalRows++
}
return nil
}
func (hc *HyperCube) rakeDimensions() {
hc.Initspace = make([]float64, len(hc.Workspace))
copy(hc.Initspace, hc.Workspace)
iter := 0
for iter < hc.Options.MaxIterations {
dimAdjust := 1.0
rmse := 0.0
rmsn := 0
hc.Stats.MinWeight = defaultWeight
hc.Stats.MaxWeight = defaultWeight
for _, c := range hc.Options.Columns {
totalGoalSum := 0.0
totalWorkSum := 0.0
for k, data := range hc.Dimensions[c].Values {
dv, found := hc.DimValues[c][k]
if !found {
continue
}
newWorkSum := 0.0
for dvIndex := range dv {
newWorkSum += hc.Workspace[dvIndex]
}
data.WorkSum = newWorkSum
data.Weight, _ = hc.curbWeight(data.GoalSum / data.WorkSum)
totalWorkSum += data.WorkSum
totalGoalSum += data.GoalSum
}
hc.Dimensions[c].GoalSum = totalGoalSum
hc.Dimensions[c].WorkSum = totalWorkSum
dimAdjust *= hc.Dimensions[c].Adjustment()
for k, data := range hc.Dimensions[c].Values {
dv, found := hc.DimValues[c][k]
if !found {
continue
}
newWorkSum := 0.0
for dvIndex := range dv {
if hc.Workspace[dvIndex] < 0.00001 {
hc.Workspace[dvIndex] = 0.00001
}
hc.Workspace[dvIndex] *= data.Weight
newWorkSum += hc.Workspace[dvIndex]
}
data.WorkSum = newWorkSum / dimAdjust
data.Weight, _ = hc.curbWeight(data.GoalSum / data.WorkSum)
d2 := math.Pow(data.GoalSum-data.WorkSum, 2.0)
data.Rmse += d2
data.Rmsn++
hc.Dimensions[c].Rmse += d2
hc.Dimensions[c].Rmsn++
rmse += d2
rmsn++
}
}
iter++
hc.Stats.Iterations++
hc.Stats.Rmse = math.Sqrt(rmse / float64(rmsn))
if hc.Stats.Rmse <= hc.Options.RootMeanSquareError {
break
}
}
hc.setEffectiveBaseSize()
}
func (hc *HyperCube) getStats() *Stats {
return hc.Stats
}
func (hc *HyperCube) getWeights() map[string]float64 {
results := make(map[string]float64)
for rid := range hc.WorkRows {
_, _, weight := hc.weight(rid)
results[rid] = weight
}
return results
}
func (hc *HyperCube) setEffectiveBaseSize() {
for _, col := range hc.Options.Columns {
hc.Dimensions[col].WorkSum = 0
for _, dv := range hc.Dimensions[col].Values {
dv.WorkSum = 0.0
}
}
var totalCount, curbedCount int
var totalSum, totalSum2 float64
for rid, target := range hc.WorkRows {
weighted, curbed, weight := hc.weight(rid)
for _, col := range hc.Options.Columns {
value := target[col]
hc.Dimensions[col].WorkSum += weight
hc.Dimensions[col].Values[value].WorkSum += weight
}
if weighted {
if curbed {
curbedCount++
}
totalSum += weight
totalSum2 += math.Pow(weight, 2.0)
totalCount++
}
}
grandSum := totalSum + float64(hc.Stats.GoalRows)
grandSum2 := totalSum2 + float64(hc.Stats.GoalRows)
grandCount := totalCount + hc.Stats.GoalRows
hc.Stats.AverageWeight = totalSum / float64(totalCount)
hc.Stats.DesignEffect = float64(grandCount) * (grandSum2 / (math.Pow(grandSum, 2.0)))
hc.Stats.EffectiveBaseSize = math.Pow(totalSum, 2.0) / totalSum2
hc.Stats.Curbed = (100.0 * float64(curbedCount)) / float64(totalCount)
}
func (hc *HyperCube) weight(responseID string) (weighted, curbed bool, weight float64) {
target, found := hc.WorkRows[responseID]
if !found || target == nil {
return false, false, defaultWeight
}
index := hc.rowIndex(target)
weight = (hc.Workspace[index] * float64(hc.Stats.WorkRows)) /
(hc.Initspace[index] * float64(hc.Stats.GoalRows)) // / hc.Stats.AverageWeight
if weight < hc.Stats.MinWeight {
hc.Stats.MinWeight = weight
}
if weight > hc.Stats.MaxWeight {
hc.Stats.MaxWeight = weight
}
weight, curbed = hc.curbWeight(weight)
return true, curbed, weight
}
func (hc *HyperCube) rowIndex(res map[string]string) int {
var index int
for _, c := range hc.Options.Columns {
val := hc.v(res[c])
index += hc.Dimensions[c].Values[val].Index * hc.Dimensions[c].BlockSize
}
return index
}
func (hc *HyperCube) curbWeight(weight float64) (float64, bool) {
if weight < hc.Options.LowerWeightCap {
return hc.Options.LowerWeightCap, true
}
if weight > hc.Options.UpperWeightCap {
return hc.Options.UpperWeightCap, true
}
return weight, false
}
func (hc *HyperCube) v(value string) string {
// TODO: Placeholder for value translations
return value
}
// NewGroupedWeighter returns an instance of the HyperCube implementation
func NewGroupedWeighter(ops *Options) *HyperCube {
hc := &HyperCube{Options: ops}
hc.Dimensions = make(map[string]*Dimension)
hc.DimValues = make(map[string]map[string]map[int]bool)
hc.Stats = &Stats{
AverageWeight: defaultWeight,
MinWeight: defaultWeight,
MaxWeight: defaultWeight,
}
for _, c := range ops.Columns {
hc.Dimensions[c] = NewDimension()
}
return hc
}