FENCEPOSTS.
This commit is contained in:
parent
fc0f274f51
commit
ec4d92fc5e
|
@ -34,22 +34,20 @@ func (lrs LinearRegressionSeries) GetYAxis() YAxisType {
|
||||||
|
|
||||||
// Len returns the number of elements in the series.
|
// Len returns the number of elements in the series.
|
||||||
func (lrs LinearRegressionSeries) Len() int {
|
func (lrs LinearRegressionSeries) Len() int {
|
||||||
return lrs.InnerSeries.Len()
|
return MinInt(lrs.GetWindow(), lrs.InnerSeries.Len()-lrs.GetOffset())
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWindow returns the window size.
|
// GetWindow returns the window size.
|
||||||
func (lrs LinearRegressionSeries) GetWindow() int {
|
func (lrs LinearRegressionSeries) GetWindow() int {
|
||||||
if lrs.Window == 0 {
|
if lrs.Window == 0 {
|
||||||
return lrs.InnerSeries.Len() - lrs.GetOffset()
|
return lrs.InnerSeries.Len()
|
||||||
}
|
}
|
||||||
return lrs.Window
|
return lrs.Window
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetEffectiveWindowEnd returns the effective window end.
|
// GetEndIndex returns the effective window end.
|
||||||
func (lrs LinearRegressionSeries) GetEffectiveWindowEnd() int {
|
func (lrs LinearRegressionSeries) GetEndIndex() int {
|
||||||
offset := lrs.GetOffset()
|
return MinInt(lrs.GetOffset()+(lrs.Len()), (lrs.InnerSeries.Len() - 1))
|
||||||
windowEnd := offset + lrs.GetWindow()
|
|
||||||
return MinInt(windowEnd, lrs.Len()-1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOffset returns the data offset.
|
// GetOffset returns the data offset.
|
||||||
|
@ -84,7 +82,7 @@ func (lrs *LinearRegressionSeries) GetLastValue() (x, y float64) {
|
||||||
if lrs.m == 0 && lrs.b == 0 {
|
if lrs.m == 0 && lrs.b == 0 {
|
||||||
lrs.computeCoefficients()
|
lrs.computeCoefficients()
|
||||||
}
|
}
|
||||||
endIndex := lrs.GetEffectiveWindowEnd()
|
endIndex := lrs.GetEndIndex()
|
||||||
x, y = lrs.InnerSeries.GetValue(endIndex)
|
x, y = lrs.InnerSeries.GetValue(endIndex)
|
||||||
y = (lrs.m * lrs.normalize(x)) + lrs.b
|
y = (lrs.m * lrs.normalize(x)) + lrs.b
|
||||||
return
|
return
|
||||||
|
@ -96,16 +94,14 @@ func (lrs *LinearRegressionSeries) normalize(xvalue float64) float64 {
|
||||||
|
|
||||||
// computeCoefficients computes the `m` and `b` terms in the linear formula given by `y = mx+b`.
|
// computeCoefficients computes the `m` and `b` terms in the linear formula given by `y = mx+b`.
|
||||||
func (lrs *LinearRegressionSeries) computeCoefficients() {
|
func (lrs *LinearRegressionSeries) computeCoefficients() {
|
||||||
|
|
||||||
startIndex := lrs.GetOffset()
|
startIndex := lrs.GetOffset()
|
||||||
endIndex := lrs.GetEffectiveWindowEnd()
|
endIndex := lrs.GetEndIndex()
|
||||||
|
|
||||||
valueCount := endIndex - startIndex
|
|
||||||
|
|
||||||
p := float64(endIndex - startIndex)
|
p := float64(endIndex - startIndex)
|
||||||
|
|
||||||
xvalues := NewRingBufferWithCapacity(valueCount)
|
xvalues := NewRingBufferWithCapacity(lrs.Len())
|
||||||
for index := startIndex; index < endIndex; index++ {
|
for index := startIndex; index < endIndex; index++ {
|
||||||
|
|
||||||
x, _ := lrs.InnerSeries.GetValue(index)
|
x, _ := lrs.InnerSeries.GetValue(index)
|
||||||
xvalues.Enqueue(x)
|
xvalues.Enqueue(x)
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,8 @@ func TestLinearRegressionSeriesWindowAndOffset(t *testing.T) {
|
||||||
Window: 10,
|
Window: 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert.Equal(10, linRegSeries.Len())
|
||||||
|
|
||||||
lrx0, lry0 := linRegSeries.GetValue(0)
|
lrx0, lry0 := linRegSeries.GetValue(0)
|
||||||
assert.InDelta(90.0, lrx0, 0.0000001)
|
assert.InDelta(90.0, lrx0, 0.0000001)
|
||||||
assert.InDelta(90.0, lry0, 0.0000001)
|
assert.InDelta(90.0, lry0, 0.0000001)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user