2016-07-27 15:34:15 -04:00
|
|
|
package chart
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
2017-02-03 14:26:53 -05:00
|
|
|
|
2018-09-07 15:52:30 -04:00
|
|
|
// Interface Assertions.
|
|
|
|
var (
|
2018-09-10 16:08:20 -04:00
|
|
|
_ Series = (*LinearRegressionSeries)(nil)
|
|
|
|
_ FirstValuesProvider = (*LinearRegressionSeries)(nil)
|
|
|
|
_ LastValuesProvider = (*LinearRegressionSeries)(nil)
|
|
|
|
_ LinearCoefficientProvider = (*LinearRegressionSeries)(nil)
|
2018-09-07 15:52:30 -04:00
|
|
|
)
|
|
|
|
|
2016-07-27 15:34:15 -04:00
|
|
|
// LinearRegressionSeries is a series that plots the n-nearest neighbors
|
|
|
|
// linear regression for the values.
|
|
|
|
type LinearRegressionSeries struct {
|
|
|
|
Name string
|
|
|
|
Style Style
|
2016-07-31 19:54:09 -04:00
|
|
|
YAxis YAxisType
|
2016-07-27 15:34:15 -04:00
|
|
|
|
2017-04-18 23:20:29 -04:00
|
|
|
Limit int
|
2016-07-27 15:34:15 -04:00
|
|
|
Offset int
|
2017-05-12 20:12:23 -04:00
|
|
|
InnerSeries ValuesProvider
|
2016-07-27 15:34:15 -04:00
|
|
|
|
|
|
|
m float64
|
|
|
|
b float64
|
|
|
|
avgx float64
|
|
|
|
stddevx float64
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:08:20 -04:00
|
|
|
// Coefficients returns the linear coefficients for the series.
|
|
|
|
func (lrs LinearRegressionSeries) Coefficients() (m, b, stdev, avg float64) {
|
|
|
|
if lrs.IsZero() {
|
|
|
|
lrs.computeCoefficients()
|
|
|
|
}
|
|
|
|
|
|
|
|
m = lrs.m
|
|
|
|
b = lrs.b
|
|
|
|
stdev = lrs.stddevx
|
|
|
|
avg = lrs.avgx
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:34:15 -04:00
|
|
|
// GetName returns the name of the time series.
|
|
|
|
func (lrs LinearRegressionSeries) GetName() string {
|
|
|
|
return lrs.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStyle returns the line style.
|
|
|
|
func (lrs LinearRegressionSeries) GetStyle() Style {
|
|
|
|
return lrs.Style
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYAxis returns which YAxis the series draws on.
|
2016-07-31 19:54:09 -04:00
|
|
|
func (lrs LinearRegressionSeries) GetYAxis() YAxisType {
|
2016-07-27 15:34:15 -04:00
|
|
|
return lrs.YAxis
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the series.
|
|
|
|
func (lrs LinearRegressionSeries) Len() int {
|
2022-01-10 18:52:32 -05:00
|
|
|
return Min(lrs.GetLimit(), lrs.InnerSeries.Len()-lrs.GetOffset())
|
2016-07-27 15:34:15 -04:00
|
|
|
}
|
|
|
|
|
2017-04-18 23:20:29 -04:00
|
|
|
// GetLimit returns the window size.
|
|
|
|
func (lrs LinearRegressionSeries) GetLimit() int {
|
|
|
|
if lrs.Limit == 0 {
|
2016-07-27 15:54:40 -04:00
|
|
|
return lrs.InnerSeries.Len()
|
2016-07-27 15:34:15 -04:00
|
|
|
}
|
2017-04-18 23:20:29 -04:00
|
|
|
return lrs.Limit
|
2016-07-27 15:34:15 -04:00
|
|
|
}
|
|
|
|
|
2017-04-18 23:20:29 -04:00
|
|
|
// GetEndIndex returns the effective limit end.
|
2016-07-27 15:54:40 -04:00
|
|
|
func (lrs LinearRegressionSeries) GetEndIndex() int {
|
2017-04-25 02:00:28 -04:00
|
|
|
windowEnd := lrs.GetOffset() + lrs.GetLimit()
|
2017-03-05 19:54:40 -05:00
|
|
|
innerSeriesLastIndex := lrs.InnerSeries.Len() - 1
|
2022-01-10 18:52:32 -05:00
|
|
|
return Min(windowEnd, innerSeriesLastIndex)
|
2016-07-27 15:34:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOffset returns the data offset.
|
|
|
|
func (lrs LinearRegressionSeries) GetOffset() int {
|
|
|
|
if lrs.Offset == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return lrs.Offset
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
// GetValues gets a value at a given index.
|
|
|
|
func (lrs *LinearRegressionSeries) GetValues(index int) (x, y float64) {
|
2017-03-05 19:54:40 -05:00
|
|
|
if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
|
2016-07-27 15:34:15 -04:00
|
|
|
return
|
|
|
|
}
|
2018-09-10 16:08:20 -04:00
|
|
|
if lrs.IsZero() {
|
2016-07-27 15:34:15 -04:00
|
|
|
lrs.computeCoefficients()
|
|
|
|
}
|
|
|
|
offset := lrs.GetOffset()
|
2022-01-10 18:52:32 -05:00
|
|
|
effectiveIndex := Min(index+offset, lrs.InnerSeries.Len())
|
2017-05-12 20:12:23 -04:00
|
|
|
x, y = lrs.InnerSeries.GetValues(effectiveIndex)
|
2016-07-27 15:34:15 -04:00
|
|
|
y = (lrs.m * lrs.normalize(x)) + lrs.b
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:52:30 -04:00
|
|
|
// GetFirstValues computes the first linear regression value.
|
|
|
|
func (lrs *LinearRegressionSeries) GetFirstValues() (x, y float64) {
|
|
|
|
if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
2018-09-10 16:08:20 -04:00
|
|
|
if lrs.IsZero() {
|
2018-09-07 15:52:30 -04:00
|
|
|
lrs.computeCoefficients()
|
|
|
|
}
|
|
|
|
x, y = lrs.InnerSeries.GetValues(0)
|
|
|
|
y = (lrs.m * lrs.normalize(x)) + lrs.b
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
// GetLastValues computes the last linear regression value.
|
|
|
|
func (lrs *LinearRegressionSeries) GetLastValues() (x, y float64) {
|
2017-03-05 19:54:40 -05:00
|
|
|
if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
|
2016-07-27 15:34:15 -04:00
|
|
|
return
|
|
|
|
}
|
2018-09-10 16:08:20 -04:00
|
|
|
if lrs.IsZero() {
|
2016-07-27 15:34:15 -04:00
|
|
|
lrs.computeCoefficients()
|
|
|
|
}
|
2016-07-27 15:54:40 -04:00
|
|
|
endIndex := lrs.GetEndIndex()
|
2017-05-12 20:12:23 -04:00
|
|
|
x, y = lrs.InnerSeries.GetValues(endIndex)
|
2016-07-27 15:34:15 -04:00
|
|
|
y = (lrs.m * lrs.normalize(x)) + lrs.b
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:08:20 -04:00
|
|
|
// Render renders the series.
|
|
|
|
func (lrs *LinearRegressionSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
|
|
|
|
style := lrs.Style.InheritFrom(defaults)
|
|
|
|
Draw.LineSeries(r, canvasBox, xrange, yrange, style, lrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the series.
|
|
|
|
func (lrs *LinearRegressionSeries) Validate() error {
|
|
|
|
if lrs.InnerSeries == nil {
|
|
|
|
return fmt.Errorf("linear regression series requires InnerSeries to be set")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsZero returns if we've computed the coefficients or not.
|
|
|
|
func (lrs *LinearRegressionSeries) IsZero() bool {
|
|
|
|
return lrs.m == 0 && lrs.b == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// internal helpers
|
|
|
|
//
|
|
|
|
|
2016-07-27 15:34:15 -04:00
|
|
|
func (lrs *LinearRegressionSeries) normalize(xvalue float64) float64 {
|
|
|
|
return (xvalue - lrs.avgx) / lrs.stddevx
|
|
|
|
}
|
|
|
|
|
|
|
|
// computeCoefficients computes the `m` and `b` terms in the linear formula given by `y = mx+b`.
|
|
|
|
func (lrs *LinearRegressionSeries) computeCoefficients() {
|
|
|
|
startIndex := lrs.GetOffset()
|
2016-07-27 15:54:40 -04:00
|
|
|
endIndex := lrs.GetEndIndex()
|
2016-07-27 15:34:15 -04:00
|
|
|
|
|
|
|
p := float64(endIndex - startIndex)
|
|
|
|
|
2022-01-10 18:52:32 -05:00
|
|
|
xvalues := NewValueBufferWithCapacity[float64](lrs.Len())
|
2016-07-27 15:34:15 -04:00
|
|
|
for index := startIndex; index < endIndex; index++ {
|
2017-05-12 20:12:23 -04:00
|
|
|
x, _ := lrs.InnerSeries.GetValues(index)
|
2016-07-27 15:34:15 -04:00
|
|
|
xvalues.Enqueue(x)
|
|
|
|
}
|
|
|
|
|
2022-01-10 18:52:32 -05:00
|
|
|
lrs.avgx = Seq[float64]{xvalues}.Average()
|
|
|
|
lrs.stddevx = Seq[float64]{xvalues}.StdDev()
|
2016-07-27 15:34:15 -04:00
|
|
|
|
|
|
|
var sumx, sumy, sumxx, sumxy float64
|
|
|
|
for index := startIndex; index < endIndex; index++ {
|
2017-05-12 20:12:23 -04:00
|
|
|
x, y := lrs.InnerSeries.GetValues(index)
|
2016-07-27 15:34:15 -04:00
|
|
|
|
|
|
|
x = lrs.normalize(x)
|
|
|
|
|
|
|
|
sumx += x
|
|
|
|
sumy += y
|
|
|
|
sumxx += x * x
|
|
|
|
sumxy += x * y
|
|
|
|
}
|
|
|
|
|
|
|
|
lrs.m = (p*sumxy - sumx*sumy) / (p*sumxx - sumx*sumx)
|
|
|
|
lrs.b = (sumy / p) - (lrs.m * sumx / p)
|
|
|
|
}
|