Class LinearRegressionSlopeRTIndicator

All Implemented Interfaces:
Resettable, RTIndicator

public class LinearRegressionSlopeRTIndicator extends AbstractWindowSeriesRTIndicator
Rolling Linear Regression Slope with O(1) per-tick updates.

Fits a least-squares line to the last n values using x-positions 0, 1, ..., n-1. The slope measures the rate of change per tick.

Maintains two accumulators (sumY and sumXY) over a single window. The denominator n*sumXSq - sumX^2 is constant once the window is full, so the per-tick cost is O(1).

Rolling update when the window evicts oldest value y0 and adds yNew:

  sumXY = sumXY - sumY + evicted + (n-1) * newValue
  sumY  = sumY - evicted + newValue
  slope = (n * sumXY - sumX * sumY) / denom

The shift identity: when all values move one index down, each value's x-contribution decreases by 1, so Sigma(i*yi) decreases by Sigma(yi).

Bonus accessors at zero extra cost:

  • Constructor Details

    • LinearRegressionSlopeRTIndicator

      public LinearRegressionSlopeRTIndicator(int periods)
  • Method Details

    • onEvict

      protected void onEvict(double evicted)
      Description copied from class: AbstractWindowSeriesRTIndicator
      Called when the window evicts the oldest value. Override to update accumulators.
      Overrides:
      onEvict in class AbstractWindowSeriesRTIndicator
    • onReset

      protected void onReset()
      Description copied from class: AbstractWindowSeriesRTIndicator
      Called after window is cleared. Override to zero accumulators.
      Overrides:
      onReset in class AbstractWindowSeriesRTIndicator
    • getMean

      public double getMean()
      Returns the current rolling mean (sumY / n).
    • getIntercept

      public double getIntercept()
      Returns the y-intercept of the fitted regression line. intercept = meanY - slope * meanX
    • forecast

      public double forecast(int ahead)
      Projects the regression line forward by ahead ticks beyond the last value in the window. forecast = intercept + slope * (n - 1 + ahead)
      Parameters:
      ahead - number of ticks ahead (1 = next tick)
    • update

      public double update(double newValue)
      Specified by:
      update in interface RTIndicator
      Overrides:
      update in class AbstractRTIndicator