2016-07-27 15:34:15 -04:00
|
|
|
package chart
|
|
|
|
|
2017-02-03 14:26:53 -05:00
|
|
|
import "fmt"
|
|
|
|
|
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
|
|
|
|
|
|
|
Window int
|
|
|
|
Offset int
|
|
|
|
InnerSeries ValueProvider
|
|
|
|
|
|
|
|
m float64
|
|
|
|
b float64
|
|
|
|
avgx float64
|
|
|
|
stddevx float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-07-29 21:24:25 -04:00
|
|
|
return Math.MinInt(lrs.GetWindow(), lrs.InnerSeries.Len()-lrs.GetOffset())
|
2016-07-27 15:34:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWindow returns the window size.
|
|
|
|
func (lrs LinearRegressionSeries) GetWindow() int {
|
|
|
|
if lrs.Window == 0 {
|
2016-07-27 15:54:40 -04:00
|
|
|
return lrs.InnerSeries.Len()
|
2016-07-27 15:34:15 -04:00
|
|
|
}
|
|
|
|
return lrs.Window
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:54:40 -04:00
|
|
|
// GetEndIndex returns the effective window end.
|
|
|
|
func (lrs LinearRegressionSeries) GetEndIndex() int {
|
2017-03-05 19:54:40 -05:00
|
|
|
offset := lrs.GetOffset() + lrs.Len()
|
|
|
|
innerSeriesLastIndex := lrs.InnerSeries.Len() - 1
|
|
|
|
return Math.MinInt(offset, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue gets a value at a given index.
|
|
|
|
func (lrs *LinearRegressionSeries) GetValue(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
|
|
|
|
}
|
|
|
|
if lrs.m == 0 && lrs.b == 0 {
|
|
|
|
lrs.computeCoefficients()
|
|
|
|
}
|
|
|
|
offset := lrs.GetOffset()
|
2016-07-29 21:24:25 -04:00
|
|
|
effectiveIndex := Math.MinInt(index+offset, lrs.InnerSeries.Len())
|
2016-07-27 15:44:02 -04:00
|
|
|
x, y = lrs.InnerSeries.GetValue(effectiveIndex)
|
2016-07-27 15:34:15 -04:00
|
|
|
y = (lrs.m * lrs.normalize(x)) + lrs.b
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLastValue computes the last moving average value but walking back window size samples,
|
|
|
|
// and recomputing the last moving average chunk.
|
|
|
|
func (lrs *LinearRegressionSeries) GetLastValue() (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
|
|
|
|
}
|
|
|
|
if lrs.m == 0 && lrs.b == 0 {
|
|
|
|
lrs.computeCoefficients()
|
|
|
|
}
|
2016-07-27 15:54:40 -04:00
|
|
|
endIndex := lrs.GetEndIndex()
|
2016-07-27 15:34:15 -04:00
|
|
|
x, y = lrs.InnerSeries.GetValue(endIndex)
|
|
|
|
y = (lrs.m * lrs.normalize(x)) + lrs.b
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-07-27 15:54:40 -04:00
|
|
|
xvalues := NewRingBufferWithCapacity(lrs.Len())
|
2016-07-27 15:34:15 -04:00
|
|
|
for index := startIndex; index < endIndex; index++ {
|
2016-07-27 15:54:40 -04:00
|
|
|
|
2016-07-27 15:34:15 -04:00
|
|
|
x, _ := lrs.InnerSeries.GetValue(index)
|
|
|
|
xvalues.Enqueue(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
lrs.avgx = xvalues.Average()
|
|
|
|
lrs.stddevx = xvalues.StdDev()
|
|
|
|
|
|
|
|
var sumx, sumy, sumxx, sumxy float64
|
|
|
|
for index := startIndex; index < endIndex; index++ {
|
|
|
|
x, y := lrs.InnerSeries.GetValue(index)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the series.
|
|
|
|
func (lrs *LinearRegressionSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
|
|
|
|
style := lrs.Style.InheritFrom(defaults)
|
2016-07-29 19:36:29 -04:00
|
|
|
Draw.LineSeries(r, canvasBox, xrange, yrange, style, lrs)
|
2016-07-27 15:34:15 -04:00
|
|
|
}
|
2017-02-03 14:26:53 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|