Debian Codegraph @

polynomial_regression_series.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 "math"
6
7 "github.com/wcharczuk/go-chart/v2/matrix"
8 )
9
10 // Interface Assertions.
11 var (
12 _ Series = (*PolynomialRegressionSeries)(nil)
13 _ FirstValuesProvider = (*PolynomialRegressionSeries)(nil)
14 _ LastValuesProvider = (*PolynomialRegressionSeries)(nil)
15 )
16
17 // PolynomialRegressionSeries implements a polynomial regression over a given
18 // inner series.
19 type PolynomialRegressionSeries struct {
20 Name string
21 Style Style
22 YAxis YAxisType
23
24 Limit int
25 Offset int
26 Degree int
27 InnerSeries ValuesProvider
28
29 coeffs []float64
30 }
31
32 // GetName returns the name of the time series.
33 func (prs PolynomialRegressionSeries) GetName() string {
34 return prs.Name
35 }
36
37 // GetStyle returns the line style.
38 func (prs PolynomialRegressionSeries) GetStyle() Style {
39 return prs.Style
40 }
41
42 // GetYAxis returns which YAxis the series draws on.
43 func (prs PolynomialRegressionSeries) GetYAxis() YAxisType {
44 return prs.YAxis
45 }
46
47 // Len returns the number of elements in the series.
48 func (prs PolynomialRegressionSeries) Len() int {
49 return MinInt(prs.GetLimit(), prs.InnerSeries.Len()-prs.GetOffset())
50 }
51
52 // GetLimit returns the window size.
53 func (prs PolynomialRegressionSeries) GetLimit() int {
54 if prs.Limit == 0 {
55 return prs.InnerSeries.Len()
56 }
57 return prs.Limit
58 }
59
60 // GetEndIndex returns the effective limit end.
61 func (prs PolynomialRegressionSeries) GetEndIndex() int {
62 windowEnd := prs.GetOffset() + prs.GetLimit()
63 innerSeriesLastIndex := prs.InnerSeries.Len() - 1
64 return MinInt(windowEnd, innerSeriesLastIndex)
65 }
66
67 // GetOffset returns the data offset.
68 func (prs PolynomialRegressionSeries) GetOffset() int {
69 if prs.Offset == 0 {
70 return 0
71 }
72 return prs.Offset
73 }
74
75 // Validate validates the series.
76 func (prs *PolynomialRegressionSeries) Validate() error {
77 if prs.InnerSeries == nil {
78 return fmt.Errorf("linear regression series requires InnerSeries to be set")
79 }
80
81 endIndex := prs.GetEndIndex()
82 if endIndex >= prs.InnerSeries.Len() {
83 return fmt.Errorf("invalid window; inner series has length %d but end index is %d", prs.InnerSeries.Len(), endIndex)
84 }
85
86 return nil
87 }
88
89 // GetValues returns the series value for a given index.
90 func (prs *PolynomialRegressionSeries) GetValues(index int) (x, y float64) {
91 if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 {
92 return
93 }
94
95 if prs.coeffs == nil {
96 coeffs, err := prs.computeCoefficients()
97 if err != nil {
98 panic(err)
99 }
100 prs.coeffs = coeffs
101 }
102
103 offset := prs.GetOffset()
104 effectiveIndex := MinInt(index+offset, prs.InnerSeries.Len())
105 x, y = prs.InnerSeries.GetValues(effectiveIndex)
106 y = prs.apply(x)
107 return
108 }
109
110 // GetFirstValues computes the first poly regression value.
111 func (prs *PolynomialRegressionSeries) GetFirstValues() (x, y float64) {
112 if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 {
113 return
114 }
115 if prs.coeffs == nil {
116 coeffs, err := prs.computeCoefficients()
117 if err != nil {
118 panic(err)
119 }
120 prs.coeffs = coeffs
121 }
122 x, y = prs.InnerSeries.GetValues(0)
123 y = prs.apply(x)
124 return
125 }
126
127 // GetLastValues computes the last poly regression value.
128 func (prs *PolynomialRegressionSeries) GetLastValues() (x, y float64) {
129 if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 {
130 return
131 }
132 if prs.coeffs == nil {
133 coeffs, err := prs.computeCoefficients()
134 if err != nil {
135 panic(err)
136 }
137 prs.coeffs = coeffs
138 }
139 endIndex := prs.GetEndIndex()
140 x, y = prs.InnerSeries.GetValues(endIndex)
141 y = prs.apply(x)
142 return
143 }
144
145 func (prs *PolynomialRegressionSeries) apply(v float64) (out float64) {
146 for index, coeff := range prs.coeffs {
147 out = out + (coeff * math.Pow(v, float64(index)))
148 }
149 return
150 }
151
152 func (prs *PolynomialRegressionSeries) computeCoefficients() ([]float64, error) {
153 xvalues, yvalues := prs.values()
154 return matrix.Poly(xvalues, yvalues, prs.Degree)
155 }
156
157 func (prs *PolynomialRegressionSeries) values() (xvalues, yvalues []float64) {
158 startIndex := prs.GetOffset()
159 endIndex := prs.GetEndIndex()
160
161 xvalues = make([]float64, endIndex-startIndex)
162 yvalues = make([]float64, endIndex-startIndex)
163
164 for index := startIndex; index < endIndex; index++ {
165 x, y := prs.InnerSeries.GetValues(index)
166 xvalues[index-startIndex] = x
167 yvalues[index-startIndex] = y
168 }
169
170 return
171 }
172
173 // Render renders the series.
174 func (prs *PolynomialRegressionSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
175 style := prs.Style.InheritFrom(defaults)
176 Draw.LineSeries(r, canvasBox, xrange, yrange, style, prs)
177 }