Class SimpleLinearRegressionRTIndicator

All Implemented Interfaces:
Resettable, RTIndicator

public class SimpleLinearRegressionRTIndicator extends AbstractWindowSeriesRTIndicator
Full rolling OLS regression with O(1) per-tick updates.

Fits a least-squares line to the last n values using x-positions 0, 1, ..., n-1. Returns the slope as the primary value, with accessors for all regression statistics:

Maintains three accumulators (sumY, sumYSq, sumXY) over a single window. The x-axis constants (sumX, sumXSq, denom) are precomputed and constant once the window is full.

R² is computed as:

  SSreg = (n*sumXY - sumX*sumY)² / denom
  SStot = n*sumYSq - sumY²
  R²    = SSreg / SStot

Standard Error is:

  SE = sqrt((SStot - SSreg) / (n * (n - 2)))
  • Constructor Details

    • SimpleLinearRegressionRTIndicator

      public SimpleLinearRegressionRTIndicator(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
    • getSlope

      public double getSlope()
      Returns the slope (same as AbstractRTIndicator.getValue()).
    • getMean

      public double getMean()
      Returns the current rolling mean of y values.
    • getIntercept

      public double getIntercept()
      Returns the y-intercept: meanY - slope * meanX.
    • getRSquared

      public double getRSquared()
      Returns the coefficient of determination (R²).

      Measures how well the linear fit explains the variance in y. R² = 1 means perfect linear fit; R² = 0 means no linear relationship. Returns 0 when the window has fewer than 2 values or y is constant.

    • getStandardError

      public double getStandardError()
      Returns the standard error of the estimate.

      SE = sqrt(SSres / (n - 2)) where SSres = SStot - SSreg. Returns 0 when window has fewer than 3 values.

    • forecast

      public double forecast(int ahead)
      Projects the regression line forward. forecast = intercept + slope * (n - 1 + ahead)
    • update

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