-
Notifications
You must be signed in to change notification settings - Fork 151
/
linear_regression.go
232 lines (194 loc) · 6.38 KB
/
linear_regression.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
package promql
import (
"fmt"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
const LinearRegressionKind = "linearRegression"
type LinearRegressionOpSpec struct {
Predict bool `json:"predict"`
// Stored as seconds in float64 to avoid back-and-forth duration conversions from PromQL.
FromNow float64 `json:"fromNow"`
}
func init() {
linearRegressionSignature := runtime.MustLookupBuiltinType("internal/promql", LinearRegressionKind)
runtime.RegisterPackageValue("internal/promql", LinearRegressionKind, flux.MustValue(flux.FunctionValue(LinearRegressionKind, createLinearRegressionOpSpec, linearRegressionSignature)))
flux.RegisterOpSpec(LinearRegressionKind, newLinearRegressionOp)
plan.RegisterProcedureSpec(LinearRegressionKind, newLinearRegressionProcedure, LinearRegressionKind)
execute.RegisterTransformation(LinearRegressionKind, createLinearRegressionTransformation)
}
func createLinearRegressionOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(LinearRegressionOpSpec)
if p, ok, err := args.GetBool("predict"); err != nil {
return nil, err
} else if ok {
spec.Predict = p
}
if d, ok, err := args.GetFloat("fromNow"); err != nil {
return nil, err
} else if ok {
spec.FromNow = d
}
return spec, nil
}
func newLinearRegressionOp() flux.OperationSpec {
return new(LinearRegressionOpSpec)
}
func (s *LinearRegressionOpSpec) Kind() flux.OperationKind {
return LinearRegressionKind
}
type LinearRegressionProcedureSpec struct {
plan.DefaultCost
Predict bool
FromNow float64
}
func newLinearRegressionProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*LinearRegressionOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &LinearRegressionProcedureSpec{
Predict: spec.Predict,
FromNow: spec.FromNow,
}, nil
}
func (s *LinearRegressionProcedureSpec) Kind() plan.ProcedureKind {
return LinearRegressionKind
}
func (s *LinearRegressionProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(LinearRegressionProcedureSpec)
*ns = *s
return ns
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *LinearRegressionProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
func createLinearRegressionTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*LinearRegressionProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t := NewLinearRegressionTransformation(d, cache, s)
return t, d, nil
}
type linearRegressionTransformation struct {
execute.ExecutionNode
d execute.Dataset
cache execute.TableBuilderCache
predict bool
fromNow float64
}
func NewLinearRegressionTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *LinearRegressionProcedureSpec) *linearRegressionTransformation {
return &linearRegressionTransformation{
d: d,
cache: cache,
predict: spec.Predict,
fromNow: spec.FromNow,
}
}
func (t *linearRegressionTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func (t *linearRegressionTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
// TODO: Check that all columns are part of the key, except _value and _time.
key := tbl.Key()
builder, created := t.cache.TableBuilder(key)
if !created {
return fmt.Errorf("linearRegression found duplicate table with key: %v", tbl.Key())
}
if err := execute.AddTableKeyCols(key, builder); err != nil {
return err
}
cols := tbl.Cols()
timeIdx := execute.ColIdx(execute.DefaultTimeColLabel, cols)
if timeIdx < 0 {
return fmt.Errorf("time column not found (cols: %v): %s", cols, execute.DefaultTimeColLabel)
}
stopIdx := execute.ColIdx(execute.DefaultStopColLabel, cols)
if stopIdx < 0 {
return fmt.Errorf("stop column not found (cols: %v): %s", cols, execute.DefaultStopColLabel)
}
valIdx := execute.ColIdx(execute.DefaultValueColLabel, cols)
if valIdx < 0 {
return fmt.Errorf("value column not found (cols: %v): %s", cols, execute.DefaultValueColLabel)
}
if key.Value(stopIdx).Type().Nature() != semantic.Time {
return fmt.Errorf("stop column is not of time type")
}
var (
numVals int
sumX, sumY float64
sumXY, sumX2 float64
firstTime time.Time
)
err := tbl.Do(func(cr flux.ColReader) error {
vs := cr.Floats(valIdx)
times := cr.Times(timeIdx)
for i := 0; i < cr.Len(); i++ {
if !vs.IsValid(i) || !times.IsValid(i) {
continue
}
v := vs.Value(i)
ts := values.Time(times.Value(i)).Time()
if numVals == 0 {
// Subtle difference between deriv() and predict_linear() intercept time.
if t.predict {
firstTime = key.ValueTime(stopIdx).Time()
} else {
firstTime = ts
}
}
x := float64(ts.Sub(firstTime).Seconds())
numVals++
sumY += v
sumX += x
sumXY += x * v
sumX2 += x * x
}
return nil
})
if err != nil {
return err
}
// Omit output table if there are not at least two samples to compute a rate from.
if numVals < 2 {
return nil
}
n := float64(numVals)
covXY := sumXY - sumX*sumY/n
varX := sumX2 - sumX*sumX/n
slope := covXY / varX
resultValue := slope
if t.predict {
intercept := sumY/n - slope*sumX/n
resultValue = slope*t.fromNow + intercept
}
outValIdx, err := builder.AddCol(flux.ColMeta{Label: execute.DefaultValueColLabel, Type: flux.TFloat})
if err != nil {
return fmt.Errorf("error appending value column: %s", err)
}
if err := builder.AppendFloat(outValIdx, resultValue); err != nil {
return err
}
return execute.AppendKeyValues(key, builder)
}
func (t *linearRegressionTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *linearRegressionTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *linearRegressionTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}