-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add linear regression functions #1063
Conversation
} | ||
|
||
case DerivType: | ||
if len(args) != 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you forgot to assign duration here. Bug ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good- duration is not needed for deriv
Codecov Report
@@ Coverage Diff @@
## master #1063 +/- ##
==========================================
- Coverage 77.4% 77.39% -0.02%
==========================================
Files 545 546 +1
Lines 46068 46132 +64
==========================================
+ Hits 35659 35702 +43
- Misses 8156 8168 +12
- Partials 2253 2262 +9
Continue to review full report at Codecov.
|
src/query/functions/temporal/base.go
Outdated
@@ -299,6 +299,7 @@ func (c *baseNode) processSingleRequest(request processRequest) error { | |||
values = append(values, val) | |||
newVal := math.NaN() | |||
alignedTime, _ := bounds.TimeForIndex(i) | |||
// fmt.Println("align: ", alignedTime.String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit; delete
return emptyOp, fmt.Errorf("invalid number of args for %s: %d", PredictLinearType, len(args)) | ||
} | ||
|
||
duration, ok = args[1].(float64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's arg[0]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The duration for the actual query.
|
||
var slope, intercept float64 | ||
|
||
if l.op.operatorType == PredictLinearType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads a little weird to me, since you're splitting processing amongst two functions and doing related calculations in both.
Instead, can you pull the code from linearRegression()
into this Process
function, and let it do the switching on its own?
Alternatively, if you want to avoid dealing with operator type inside the regression function, you can pass a bool that signifies wether you're calculating regression or deriv into there, and have it just return a float, something like linearRegression(isDeriv bool, dps ts.Datapoints, interceptTime time.Time) float64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like separate linearRegression since it makes it easy to test. My above comment about having a separate func instead duration should fix this problem.
func linearRegression(dps ts.Datapoints, interceptTime time.Time) (float64, float64) { | ||
var ( | ||
n float64 | ||
sumX, sumY float64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are X and Y?
sumX, sumY float64 | ||
sumXY, sumX2 float64 | ||
|
||
nonNaNCount int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe valueCount
?
nonNaNCount++ | ||
|
||
// convert to milliseconds | ||
x := float64(dp.Timestamp.Sub(interceptTime).Nanoseconds()/1000000) / 1e3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this converts it to seconds, not millis. Can you just do float64(dp.Timestamp.Sub(interceptTime).Seconds())
?
src/query/functions/temporal/base.go
Outdated
@@ -360,7 +361,7 @@ func (c *baseNode) sweep(processedKeys []bool, maxBlocks int) { | |||
|
|||
// Processor is implemented by the underlying transforms | |||
type Processor interface { | |||
Process(values ts.Datapoints) float64 | |||
Process(values ts.Datapoints, alignedTime time.Time) float64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call it evaluationTime ?
) | ||
|
||
type linearRegressionProcessor struct { | ||
duration float64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of this, store a function here which takes in a slope and intercept, returns a float. This way you don't have to compare Op in Process.
|
||
var slope, intercept float64 | ||
|
||
if l.op.operatorType == PredictLinearType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like separate linearRegression since it makes it easy to test. My above comment about having a separate func instead duration should fix this problem.
fcdd004
to
57016fb
Compare
var ( | ||
n float64 | ||
sumX, sumY float64 | ||
sumXY, sumX2 float64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better name ?
n float64 | ||
sumX, sumY float64 | ||
sumXY, sumX2 float64 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove line ?
} | ||
|
||
valueCount++ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove line ?
} | ||
|
||
slope, intercept := linearRegression(dps, evaluationTime, l.isDeriv) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove line ?
13e976a
to
281f999
Compare
No description provided.