-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathindicator_variance.go
More file actions
33 lines (26 loc) · 869 Bytes
/
Copy pathindicator_variance.go
File metadata and controls
33 lines (26 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package techan
import "github.com/sdcoffey/big"
// NewVarianceIndicator provides a way to find the variance in a base indicator, where variances is the sum of squared
// deviations from the mean at any given index in the time series.
func NewVarianceIndicator(ind Indicator) Indicator {
return varianceIndicator{
Indicator: ind,
}
}
type varianceIndicator struct {
Indicator Indicator
}
// Calculate returns the Variance for this indicator at the given index
func (vi varianceIndicator) Calculate(index int) big.Decimal {
if index < 1 {
return big.ZERO
}
avgIndicator := NewSimpleMovingAverage(vi.Indicator, index+1)
avg := avgIndicator.Calculate(index)
variance := big.ZERO
for i := 0; i <= index; i++ {
pow := vi.Indicator.Calculate(i).Sub(avg).Pow(2)
variance = variance.Add(pow)
}
return variance.Div(big.NewDecimal(float64(index + 1)))
}