Class TheilSenRegression

  • All Implemented Interfaces:
    IsRegression

    public final class TheilSenRegression
    extends Object
    In TheilSen Regression, the estimation of the model is done by calculating the slopes and intercepts of a subpopulation of all possible combinations of subsample points.
    The final slope and intercept is then defined as the spatial median of these slopes and intercepts.
    The TheilSen Regression model can be represented using the below equation:

    y = a0 + a1x

    where
    • a0 is the intercept of the Regression line (can be obtained putting x=0)
    • a1 is the slope of the regression line, which tells whether the line is increasing or decreasing

    This regression can be computed efficiently, and is insensitive to outliers.
    It can be significantly more accurate than non-robust simple linear regression (least squares) for skewed and heteroskedastic data, and competes well against least squares even for normally distributed data in terms of statistical power.
    It has been called "the most popular nonparametric technique for estimating a linear trend".
    See details on Theil–Sen estimator.
    Author:
    Andrea "Stock" Stocchero
    • Field Detail

      • DEFAULT_FORMULA_PRECISION

        public static final int DEFAULT_FORMULA_PRECISION
        Default precision to get the formula, 2.
        See Also:
        Constant Field Values
    • Method Detail

      • getSlope

        public final double getSlope()
        Returns the slope coefficient.
        Returns:
        the slope coefficient
      • getIntercept

        public final double getIntercept()
        Returns the intercept coefficient.
        Returns:
        the intercept coefficient
      • getCoefficients

        public final List<Double> getCoefficients()
        Returns all calculated coefficients as a list.
        Returns:
        all calculated coefficients as a list
      • computeX

        public final double computeX​(double y)
        Returns a calculated X value by the Y value.
        Parameters:
        y - Y value to use to calculate the X value
        Returns:
        a calculated X value by the Y value
      • computeX

        public final double computeX​(FloatingData y)
        Returns a calculated X value by the Y value.
        Parameters:
        y - Y value to use to calculate the X value
        Returns:
        a calculated X value by the Y value
      • computeX

        public final double computeX​(DataPoint dataPoint)
        Returns a calculated X value by the Y value.
        Parameters:
        dataPoint - Y value to use to calculate the X value
        Returns:
        a calculated X value by the Y value
      • computeX

        public final Date computeX​(TimeSeriesItem item)
        Returns a calculated X value by the Y value.
        Parameters:
        item - Y value to use to calculate the X value
        Returns:
        a calculated X value by the Y value
      • computeXToDate

        public final Date computeXToDate​(double y)
        Returns a calculated X value by the Y value.
        Parameters:
        y - Y value to use to calculate the X value
        Returns:
        a calculated X value by the Y value
      • computeXToDate

        public final Date computeXToDate​(FloatingData y)
        Returns a calculated X value by the Y value.
        Parameters:
        y - Y value to use to calculate the X value
        Returns:
        a calculated X value by the Y value
      • computeXToDate

        public final Date computeXToDate​(DataPoint dataPoint)
        Returns a calculated X value by the Y value.
        Parameters:
        dataPoint - Y value to use to calculate the X value
        Returns:
        a calculated X value by the Y value
      • isConsistent

        public final boolean isConsistent()
        Description copied from interface: IsRegression
        Returns true if the regression is consistent and usable.
        Specified by:
        isConsistent in interface IsRegression
        Returns:
        true if the regression is consistent and usable
      • predict

        public final double predict​(double x)
        Description copied from interface: IsRegression
        Returns the Y value, calculated by the regression formula at specific X value.
        Specified by:
        predict in interface IsRegression
        Parameters:
        x - value to use to get the predicted value
        Returns:
        the Y value, calculated by the regression formula at specific X value
      • predict

        public final double predict​(Date x)
        Description copied from interface: IsRegression
        Returns the Y value, calculated by the regression formula at specific X value.
        Specified by:
        predict in interface IsRegression
        Parameters:
        x - value to use to get the predicted value
        Returns:
        the Y value, calculated by the regression formula at specific X value
      • predict

        public final double predict​(DataPoint dataPoint)
        Description copied from interface: IsRegression
        Returns the Y value, calculated by the regression formula at specific X value.
        Specified by:
        predict in interface IsRegression
        Parameters:
        dataPoint - value to use to get the predicted value
        Returns:
        the Y value, calculated by the regression formula at specific X value
      • predict

        public final double predict​(TimeSeriesItem item)
        Description copied from interface: IsRegression
        Returns the Y value, calculated by the regression formula at specific X value.
        Specified by:
        predict in interface IsRegression
        Parameters:
        item - value to use to get the predicted value
        Returns:
        the Y value, calculated by the regression formula at specific X value
      • predict

        public final List<Double> predict​(double... xValues)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predict in interface IsRegression
        Parameters:
        xValues - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • predict

        public final List<Double> predict​(List<Double> xValues)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predict in interface IsRegression
        Parameters:
        xValues - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • predictByDates

        public final List<Double> predictByDates​(Date... xValues)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predictByDates in interface IsRegression
        Parameters:
        xValues - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • predictByDates

        public final List<Double> predictByDates​(List<Date> xValues)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predictByDates in interface IsRegression
        Parameters:
        xValues - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • predictByDataPoints

        public final List<Double> predictByDataPoints​(DataPoint... dataPoints)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predictByDataPoints in interface IsRegression
        Parameters:
        dataPoints - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • predictByDataPoints

        public final List<Double> predictByDataPoints​(List<DataPoint> dataPoints)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predictByDataPoints in interface IsRegression
        Parameters:
        dataPoints - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • predictByTimeSeriesItems

        public final List<Double> predictByTimeSeriesItems​(TimeSeriesItem... items)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predictByTimeSeriesItems in interface IsRegression
        Parameters:
        items - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • predictByTimeSeriesItems

        public final List<Double> predictByTimeSeriesItems​(List<TimeSeriesItem> items)
        Description copied from interface: IsRegression
        Returns a list of Y values, calculated by the regression formula for specific X values.
        Specified by:
        predictByTimeSeriesItems in interface IsRegression
        Parameters:
        items - values to use to get the predicted values
        Returns:
        a list of Y values, calculated by the regression formula for specific X values
      • score

        public final RegressionScore score​(List<Double> y)
        Description copied from interface: IsRegression
        Evaluates the regression formula if the model fits enough.
        X values are creating starting from 0 with increment of 1.
        Specified by:
        score in interface IsRegression
        Parameters:
        y - Y values to use for evaluation
        Returns:
        the score of the regression
      • score

        public final RegressionScore score​(List<Double> y,
                                           double starting)
        Description copied from interface: IsRegression
        Evaluates the regression formula if the model fits enough.
        X values are creating starting from passed argument with increment of 1.
        Specified by:
        score in interface IsRegression
        Parameters:
        y - Y values to use for evaluation
        starting - starting value to create X values
        Returns:
        the score of the regression
      • score

        public final RegressionScore score​(List<Double> y,
                                           double starting,
                                           double increment)
        Description copied from interface: IsRegression
        Evaluates the regression formula if the model fits enough.
        X values are creating starting from passed argument with increment of passed argument.
        Specified by:
        score in interface IsRegression
        Parameters:
        y - Y values to use for evaluation
        starting - starting value to create X values
        increment - increment to apply to the starting value
        Returns:
        the score of the regression
      • score

        public final RegressionScore score​(List<Double> x,
                                           List<Double> y)
        Description copied from interface: IsRegression
        Evaluates the regression formula if the model fits enough.
        Specified by:
        score in interface IsRegression
        Parameters:
        x - X values to use for evaluation
        y - Y values to use for evaluation
        Returns:
        the score of the regression
      • scoreByDate

        public final RegressionScore scoreByDate​(List<Date> x,
                                                 List<Double> y)
        Description copied from interface: IsRegression
        Evaluates the regression formula if the model fits enough.
        Specified by:
        scoreByDate in interface IsRegression
        Parameters:
        x - X values to use for evaluation
        y - Y values to use for evaluation
        Returns:
        the score of the regression
      • scoreByDataPoints

        public final RegressionScore scoreByDataPoints​(List<DataPoint> dataPoints)
        Description copied from interface: IsRegression
        Evaluates the regression formula if the model fits enough.
        Specified by:
        scoreByDataPoints in interface IsRegression
        Parameters:
        dataPoints - X and Y values to use for evaluation
        Returns:
        the score of the regression
      • toFormula

        public final String toFormula()
        Description copied from interface: IsRegression
        Returns the formula of the regression.
        Specified by:
        toFormula in interface IsRegression
        Returns:
        the formula of the regression
      • toFormula

        public final String toFormula​(int precision)
        Description copied from interface: IsRegression
        Returns the formula of the regression, using the requested precision.
        Specified by:
        toFormula in interface IsRegression
        Parameters:
        precision - precision to apply to the numbers of the formula
        Returns:
        the formula of the regression
      • toLaTeX

        public final String toLaTeX()
        Description copied from interface: IsRegression
        Returns the formula of the regression.
        Specified by:
        toLaTeX in interface IsRegression
        Returns:
        the formula of the regression
      • toLaTeX

        public final String toLaTeX​(int precision)
        Description copied from interface: IsRegression
        Returns the formula of the regression, using the requested precision.
        Specified by:
        toLaTeX in interface IsRegression
        Parameters:
        precision - precision to apply to the numbers of the formula
        Returns:
        the formula of the regression