Debian Codegraph @

linear_regression_series.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 )
6
7 // Interface Assertions.
8 var (
9 _ Series = (*LinearRegressionSeries)(nil)
10 _ FirstValuesProvider = (*LinearRegressionSeries)(nil)
11 _ LastValuesProvider = (*LinearRegressionSeries)(nil)
12 _ LinearCoefficientProvider = (*LinearRegressionSeries)(nil)
13 )
14
15 // LinearRegressionSeries is a series that plots the n-nearest neighbors
16 // linear regression for the values.
17 type LinearRegressionSeries struct {
18 Name string
19 Style Style
20 YAxis YAxisType
21
22 Limit int
23 Offset int
24 InnerSeries ValuesProvider
25
26 m float64
27 b float64
28 avgx float64
29 stddevx float64
30 }
31
32 // Coefficients returns the linear coefficients for the series.
33 func (lrs LinearRegressionSeries) Coefficients() (m, b, stdev, avg float64) {
34 if lrs.IsZero() {
35 lrs.computeCoefficients()
36 }
37
38 m = lrs.m
39 b = lrs.b
40 stdev = lrs.stddevx
41 avg = lrs.avgx
42 return
43 }
44
45 // GetName returns the name of the time series.
46 func (lrs LinearRegressionSeries) GetName() string {
47 return lrs.Name
48 }
49
50 // GetStyle returns the line style.
51 func (lrs LinearRegressionSeries) GetStyle() Style {
52 return lrs.Style
53 }
54
55 // GetYAxis returns which YAxis the series draws on.
56 func (lrs LinearRegressionSeries) GetYAxis() YAxisType {
57 return lrs.YAxis
58 }
59
60 // Len returns the number of elements in the series.
61 func (lrs LinearRegressionSeries) Len() int {
62 return MinInt(lrs.GetLimit(), lrs.InnerSeries.Len()-lrs.GetOffset())
63 }
64
65 // GetLimit returns the window size.
66 func (lrs LinearRegressionSeries) GetLimit() int {
67 if lrs.Limit == 0 {
68 return lrs.InnerSeries.Len()
69 }
70 return lrs.Limit
71 }
72
73 // GetEndIndex returns the effective limit end.
74 func (lrs LinearRegressionSeries) GetEndIndex() int {
75 windowEnd := lrs.GetOffset() + lrs.GetLimit()
76 innerSeriesLastIndex := lrs.InnerSeries.Len() - 1
77 return MinInt(windowEnd, innerSeriesLastIndex)
78 }
79
80 // GetOffset returns the data offset.
81 func (lrs LinearRegressionSeries) GetOffset() int {
82 if lrs.Offset == 0 {
83 return 0
84 }
85 return lrs.Offset
86 }
87
88 // GetValues gets a value at a given index.
89 func (lrs *LinearRegressionSeries) GetValues(index int) (x, y float64) {
90 if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
91 return
92 }
93 if lrs.IsZero() {
94 lrs.computeCoefficients()
95 }
96 offset := lrs.GetOffset()
97 effectiveIndex := MinInt(index+offset, lrs.InnerSeries.Len())
98 x, y = lrs.InnerSeries.GetValues(effectiveIndex)
99 y = (lrs.m * lrs.normalize(x)) + lrs.b
100 return
101 }
102
103 // GetFirstValues computes the first linear regression value.
104 func (lrs *LinearRegressionSeries) GetFirstValues() (x, y float64) {
105 if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
106 return
107 }
108 if lrs.IsZero() {
109 lrs.computeCoefficients()
110 }
111 x, y = lrs.InnerSeries.GetValues(0)
112 y = (lrs.m * lrs.normalize(x)) + lrs.b
113 return
114 }
115
116 // GetLastValues computes the last linear regression value.
117 func (lrs *LinearRegressionSeries) GetLastValues() (x, y float64) {
118 if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
119 return
120 }
121 if lrs.IsZero() {
122 lrs.computeCoefficients()
123 }
124 endIndex := lrs.GetEndIndex()
125 x, y = lrs.InnerSeries.GetValues(endIndex)
126 y = (lrs.m * lrs.normalize(x)) + lrs.b
127 return
128 }
129
130 // Render renders the series.
131 func (lrs *LinearRegressionSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
132 style := lrs.Style.InheritFrom(defaults)
133 Draw.LineSeries(r, canvasBox, xrange, yrange, style, lrs)
134 }
135
136 // Validate validates the series.
137 func (lrs *LinearRegressionSeries) Validate() error {
138 if lrs.InnerSeries == nil {
139 return fmt.Errorf("linear regression series requires InnerSeries to be set")
140 }
141 return nil
142 }
143
144 // IsZero returns if we've computed the coefficients or not.
145 func (lrs *LinearRegressionSeries) IsZero() bool {
146 return lrs.m == 0 && lrs.b == 0
147 }
148
149 //
150 // internal helpers
151 //
152
153 func (lrs *LinearRegressionSeries) normalize(xvalue float64) float64 {
154 return (xvalue - lrs.avgx) / lrs.stddevx
155 }
156
157 // computeCoefficients computes the `m` and `b` terms in the linear formula given by `y = mx+b`.
158 func (lrs *LinearRegressionSeries) computeCoefficients() {
159 startIndex := lrs.GetOffset()
160 endIndex := lrs.GetEndIndex()
161
162 p := float64(endIndex - startIndex)
163
164 xvalues := NewValueBufferWithCapacity(lrs.Len())
165 for index := startIndex; index < endIndex; index++ {
166 x, _ := lrs.InnerSeries.GetValues(index)
167 xvalues.Enqueue(x)
168 }
169
170 lrs.avgx = Seq{xvalues}.Average()
171 lrs.stddevx = Seq{xvalues}.StdDev()
172
173 var sumx, sumy, sumxx, sumxy float64
174 for index := startIndex; index < endIndex; index++ {
175 x, y := lrs.InnerSeries.GetValues(index)
176
177 x = lrs.normalize(x)
178
179 sumx += x
180 sumy += y
181 sumxx += x * x
182 sumxy += x * y
183 }
184
185 lrs.m = (p*sumxy - sumx*sumy) / (p*sumxx - sumx*sumx)
186 lrs.b = (sumy / p) - (lrs.m * sumx / p)
187 }