Class VarianceRTIndicator

All Implemented Interfaces:
ClockAware, Resettable, RTIndicator

public class VarianceRTIndicator extends AbstractDecorableIncrementalRTIndicator
Variance 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 Details

    • VarianceRTIndicator

      public VarianceRTIndicator()
    • VarianceRTIndicator

      public VarianceRTIndicator(int periods)
    • VarianceRTIndicator

      public VarianceRTIndicator(RTIndicator indicator)
    • VarianceRTIndicator

      public VarianceRTIndicator(RTIndicator indicator, int periods)
  • Method Details

    • reset

      public void reset()
      Specified by:
      reset in interface Resettable
      Overrides:
      reset in class AbstractDecorableIncrementalRTIndicator
    • withAverageSample

      public VarianceRTIndicator withAverageSample(boolean averageSample)
    • withPrecision

      public VarianceRTIndicator withPrecision(VariancePrecision precision)
      Chooses how the variance is worked out. VariancePrecision.EXACT (the default) works it out from the window on every tick; VariancePrecision.FAST keeps 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:
      tickValue in class AbstractIncrementalRTIndicator
      Parameters:
      tick - current tick within the period
      init - true on the very first update
      endPeriod - true when this tick completes a period
      newValue - 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:
      nextValue in class AbstractDecorableIncrementalRTIndicator
      Parameters:
      prevValue - the previous variance value
      newValue - the new input value
      Returns:
      the smoothed variance
    • getSma

      public SmaRTIndicator getSma()
    • getPrecision

      public VariancePrecision getPrecision()
    • getFastRecalculations

      public int getFastRecalculations()
      How many times FAST worked the window out again from the window itself (for tests and diagnostics).