adds last value for poly reg

This commit is contained in:
Will Charczuk 2017-04-21 13:31:19 -07:00
parent 0cbbd0887a
commit b41d05a2f4
2 changed files with 20 additions and 2 deletions

View File

@ -77,8 +77,7 @@ func (lrs *LinearRegressionSeries) GetValue(index int) (x, y float64) {
return return
} }
// GetLastValue computes the last moving average value but walking back window size samples, // GetLastValue computes the last linear regression value.
// and recomputing the last moving average chunk.
func (lrs *LinearRegressionSeries) GetLastValue() (x, y float64) { func (lrs *LinearRegressionSeries) GetLastValue() (x, y float64) {
if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 { if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
return return

View File

@ -100,6 +100,25 @@ func (prs *PolynomialRegressionSeries) GetValue(index int) (x, y float64) {
return return
} }
// GetLastValue computes the last moving average value but walking back window size samples,
// and recomputing the last moving average chunk.
func (prs *PolynomialRegressionSeries) GetLastValue() (x, y float64) {
if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 {
return
}
if prs.coeffs == nil {
coeffs, err := prs.computeCoefficients()
if err != nil {
panic(err)
}
prs.coeffs = coeffs
}
endIndex := prs.GetEndIndex()
x, y = prs.InnerSeries.GetValue(endIndex)
y = prs.apply(x)
return
}
func (prs *PolynomialRegressionSeries) apply(v float64) (out float64) { func (prs *PolynomialRegressionSeries) apply(v float64) (out float64) {
for index, coeff := range prs.coeffs { for index, coeff := range prs.coeffs {
out = out + (coeff * math.Pow(v, float64(index))) out = out + (coeff * math.Pow(v, float64(index)))