Class VarianceRTIndicator
- All Implemented Interfaces:
ClockAware, Resettable, RTIndicator
In periodic mode, by default (VariancePrecision.EXACT), computes the variance of the
window from the window itself on every tick: the mean first, then the squared deviations from it,
oldest value first. That is O(periods) per tick, exact to the last digits, and there is nothing
to drift. It replaces a running sum of squares with Var(X) = E(X²) - E(X)², which cancels
the deviation away when the mean is large next to it (a quiet window of prices in the tens of
thousands) and keeps the rounding of every add and eviction for good.
withPrecision(FAST) trades the last digits for O(1)
per tick: running sums of the deviations from a reference value near the window's mean, with a
bound on the rounding they gather and an exact pass over the window whenever that bound could
matter. It keeps the rounding of the variance below about one part in a thousand million (the
bound is a worst case; the tests hold it to ten times that), wherever the mean is.
-
Constructor Summary
ConstructorsConstructorDescriptionVarianceRTIndicator(int periods) VarianceRTIndicator(RTIndicator indicator) VarianceRTIndicator(RTIndicator indicator, int periods) -
Method Summary
Modifier and TypeMethodDescriptionintHow many times FAST worked the window out again from the window itself (for tests and diagnostics).getSma()protected doublenextValue(double prevValue, double newValue) Non-periodic (2-period) variance: running average of squared deviations.voidreset()protected doubletickValue(int tick, boolean init, boolean endPeriod, double newValue) Periodic variance computation from the window:mean = Σx/n, thenΣ(x-mean)²/n(population) orΣ(x-mean)²/(n-1)with Bessel's correction (sample), the sums added in the order the values arrived.withAverageSample(boolean averageSample) withPrecision(VariancePrecision precision) Chooses how the variance is worked out.Methods inherited from class AbstractDecorableIncrementalRTIndicator
getIndicator, isReady, setClockSupplier, updateMethods inherited from class AbstractIncrementalRTIndicator
getLastUpdateValue, getPeriodCycles, getPeriods, getPrevValue, initValue, isPeriodic, prevNextValue, setPeriodsMethods inherited from class AbstractRTIndicator
equals, getDisplayHint, getMeta, getValue, hashCode, hide, isPoison, setValue, toString, toString, withDisplayHint, withMeta
-
Constructor Details
-
VarianceRTIndicator
public VarianceRTIndicator() -
VarianceRTIndicator
public VarianceRTIndicator(int periods) -
VarianceRTIndicator
-
VarianceRTIndicator
-
-
Method Details
-
reset
public void reset()- Specified by:
resetin interfaceResettable- Overrides:
resetin classAbstractDecorableIncrementalRTIndicator
-
withAverageSample
-
withPrecision
Chooses how the variance is worked out.VariancePrecision.EXACT(the default) works it out from the window on every tick;VariancePrecision.FASTkeeps O(1) running sums with a guard. Safe to change at any point: FAST starts from the values the window holds at that moment. -
tickValue
protected double tickValue(int tick, boolean init, boolean endPeriod, double newValue) Periodic variance computation from the window:mean = Σx/n, thenΣ(x-mean)²/n(population) orΣ(x-mean)²/(n-1)with Bessel's correction (sample), the sums added in the order the values arrived. Returns 0 until the first complete period.- Overrides:
tickValuein classAbstractIncrementalRTIndicator- Parameters:
tick- current tick within the periodinit- true on the very first updateendPeriod- true when this tick completes a periodnewValue- the new input value- Returns:
- the computed variance, or 0 if insufficient data
-
nextValue
protected double nextValue(double prevValue, double newValue) Non-periodic (2-period) variance: running average of squared deviations.- Overrides:
nextValuein classAbstractDecorableIncrementalRTIndicator- Parameters:
prevValue- the previous variance valuenewValue- the new input value- Returns:
- the smoothed variance
-
getSma
-
getPrecision
-
getFastRecalculations
public int getFastRecalculations()How many times FAST worked the window out again from the window itself (for tests and diagnostics).
-