Top Description Fields Constructors Methods
org.apache.commons.math3.stat.regression

public Class SimpleRegression

extends Object
implements Serializable, UpdatingMultipleLinearRegression
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression, java.io.Serializable
Imports
java.io.Serializable, org.apache.commons.math3.distribution.TDistribution, org.apache.commons.math3.exception.MathIllegalArgumentException, .NoDataException, .OutOfRangeException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.util.FastMath, .Precision

Estimates an ordinary least squares regression model with one independent variable.

y = intercept + slope * x

Standard errors for intercept and slope are available as well as ANOVA, r-square and Pearson's r statistics.

Observations (x,y pairs) can be added to the model one at a time or they can be provided in a 2-dimensional array. The observations are not stored in memory, so there is no limit to the number of observations that can be added to the model.

Usage Notes:

Field Summary

Modifier and TypeField and Description
private final boolean
hasIntercept

include an intercept or not

private long
n

number of observations

private static final long
serialVersionUID

Serializable version identifier

private double
sumX

sum of x values

private double
sumXX

total variation in x (sum of squared deviations from xbar)

private double
sumXY

sum of products

private double
sumY

sum of y values

private double
sumYY

total variation in y (sum of squared deviations from ybar)

private double
xbar

mean of accumulated x values, used in updating formulas

private double
ybar

mean of accumulated y values, used in updating formulas

Constructor Summary

AccessConstructor and Description
public
SimpleRegression()

Create an empty SimpleRegression instance

public
SimpleRegression(boolean
whether or not to include an intercept term in the regression model
includeIntercept
)

Create a SimpleRegression instance, specifying whether or not to estimate an intercept.

Method Summary

Modifier and TypeMethod and Description
public void
addData(final double
independent variable value
x
,
final double
dependent variable value
y
)

Adds the observation (x,y) to the regression data set.

public void
addData(final double[][]
array of observations to be added
data
)

Adds the observations represented by the elements in data.

public void
addObservation(final double[]
the independent variables which form the design matrix
x
,
final double
the dependent or response variable
y
)

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.addObservation.

Adds one observation to the regression model.

public void
addObservations(final double[][]
a series of observations on the independent variables
x
,
final double[]
a series of observations on the dependent variable The length of x and y must be the same
y
)

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.addObservations.

Adds a series of observations to the regression model.

public void
append(SimpleRegression
model to append data from
reg
)

Appends data from another regression calculation to this one.

public void
public double

Returns:

the intercept of the regression line if the model includes an intercept; 0 otherwise
getIntercept
()

Returns the intercept of the estimated regression line, if hasIntercept() is true; otherwise 0.

private double

Returns:

the intercept of the regression line
getIntercept
(final double
current slope
slope
)

Returns the intercept of the estimated regression line, given the slope.

public double

Returns:

standard error associated with intercept estimate
getInterceptStdErr
()

Returns the standard error of the intercept estimate, usually denoted s(b0).

public double

Returns:

sum of squared deviations of y values
getMeanSquareError
()

Returns the sum of squared errors divided by the degrees of freedom, usually abbreviated MSE.

public long

Returns:

n number of observations that have been added.
getN
()

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.getN.

Returns the number of observations that have been added to the model.

public double

Returns:

Pearson's r
getR
()

Returns Pearson's product moment correlation coefficient, usually denoted r.

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double,NaN is returned.
public double

Returns:

sum of squared deviations of predicted y values
getRegressionSumSquares
()

Returns the sum of squared deviations of the predicted y values about their mean (which equals the mean of y).

private double

Returns:

sum of squared deviations of predicted y values
getRegressionSumSquares
(final double
regression slope estimate
slope
)

Computes SSR from b1.

public double

Returns:

r-square
getRSquare
()

Returns the coefficient of determination, usually denoted r-square.

public double

Returns:

significance level for slope/correlation
getSignificance
()

Returns the significance level of the slope (equiv) correlation.

public double

Returns:

the slope of the regression line
getSlope
()

Returns the slope of the estimated regression line.

public double

Returns:

half-width of 95% confidence interval for the slope estimate
getSlopeConfidenceInterval
()

Returns the half-width of a 95% confidence interval for the slope estimate.

public double

Returns:

half-width of 95% confidence interval for the slope estimate
getSlopeConfidenceInterval
(final double
the desired significance level
alpha
)

Returns the half-width of a (100-100*alpha)% confidence interval for the slope estimate.

public double

Returns:

standard error associated with slope estimate
getSlopeStdErr
()

Returns the standard error of the slope estimate, usually denoted s(b1).

public double

Returns:

sum of cross products
getSumOfCrossProducts
()

Returns the sum of crossproducts, xi*yi.

public double

Returns:

sum of squared errors associated with the regression model
getSumSquaredErrors
()

Returns the sum of squared errors (SSE) associated with the regression model.

public double

Returns:

sum of squared deviations of y values
getTotalSumSquares
()

Returns the sum of squared deviations of the y values about their mean.

public double

Returns:

sum of squared deviations of x values
getXSumSquares
()

Returns the sum of squared deviations of the x values about their mean.

public boolean

Returns:

true if the regression includes an intercept; false otherwise
hasIntercept
()

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.hasIntercept.

Returns true if the model includes an intercept term.

public double

Returns:

predicted y value
predict
(final double
input x value
x
)

Returns the "predicted" y value associated with the supplied x value, based on the data that has been added to the model when this method is activated.

public RegressionResults

Returns:

RegressionResults acts as a container of regression output
regress
()

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.regress.

Performs a regression on data present in buffers and outputs a RegressionResults object.

public RegressionResults

Returns:

RegressionResults acts as a container of regression output
regress
(int[]
an array of indices of regressors to include
variablesToInclude
)

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.regress.

Performs a regression on data present in buffers including only regressors indexed in variablesToInclude and outputs a RegressionResults object

public void
removeData(final double
independent variable value
x
,
final double
dependent variable value
y
)

Removes the observation (x,y) from the regression data set.

public void
removeData(double[][]
array of observations to be removed
data
)

Removes observations represented by the elements in data.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hasInterceptback to summary
private final boolean hasIntercept

include an intercept or not

nback to summary
private long n

number of observations

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier

sumXback to summary
private double sumX

sum of x values

sumXXback to summary
private double sumXX

total variation in x (sum of squared deviations from xbar)

sumXYback to summary
private double sumXY

sum of products

sumYback to summary
private double sumY

sum of y values

sumYYback to summary
private double sumYY

total variation in y (sum of squared deviations from ybar)

xbarback to summary
private double xbar

mean of accumulated x values, used in updating formulas

ybarback to summary
private double ybar

mean of accumulated y values, used in updating formulas

Constructor Detail

SimpleRegressionback to summary
public SimpleRegression()

Create an empty SimpleRegression instance

SimpleRegressionback to summary
public SimpleRegression(boolean includeIntercept)

Create a SimpleRegression instance, specifying whether or not to estimate an intercept.

Use false to estimate a model with no intercept. When the hasIntercept property is false, the model is estimated without a constant term and getIntercept() returns 0.

Parameters
includeIntercept:boolean

whether or not to include an intercept term in the regression model

Method Detail

addDataback to summary
public void addData(final double x, final double y)

Adds the observation (x,y) to the regression data set.

Uses updating formulas for means and sums of squares defined in "Algorithms for Computing the Sample Variance: Analysis and Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. 1983, American Statistician, vol. 37, pp. 242-247, referenced in Weisberg, S. "Applied Linear Regression". 2nd Ed. 1985.

Parameters
x:double

independent variable value

y:double

dependent variable value

addDataback to summary
public void addData(final double[][] data) throws ModelSpecificationException

Adds the observations represented by the elements in data.

(data[0][0],data[0][1]) will be the first observation, then (data[1][0],data[1][1]), etc.

This method does not replace data that has already been added. The observations represented by data are added to the existing dataset.

To replace all data, use clear() before adding the new data.

Parameters
data:double[][]

array of observations to be added

Exceptions
ModelSpecificationException:
if the length of data[i] is not greater than or equal to 2
addObservationback to summary
public void addObservation(final double[] x, final double y) throws ModelSpecificationException

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.addObservation.

Adds one observation to the regression model.

Parameters
x:double[]

the independent variables which form the design matrix

y:double

the dependent or response variable

Exceptions
ModelSpecificationException:
if the length of x does not equal the number of independent variables in the model
addObservationsback to summary
public void addObservations(final double[][] x, final double[] y) throws ModelSpecificationException

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.addObservations.

Adds a series of observations to the regression model. The lengths of x and y must be the same and x must be rectangular.

Parameters
x:double[][]

a series of observations on the independent variables

y:double[]

a series of observations on the dependent variable The length of x and y must be the same

Exceptions
ModelSpecificationException:
if x is not rectangular, does not match the length of y or does not contain sufficient data to estimate the model
appendback to summary
public void append(SimpleRegression reg)

Appends data from another regression calculation to this one.

The mean update formulae are based on a paper written by Philippe Pébay: Formulas for Robust, One-Pass Parallel Computation of Covariances and Arbitrary-Order Statistical Moments, 2008, Technical Report SAND2008-6212, Sandia National Laboratories.

Parameters
reg:SimpleRegression

model to append data from

Since
3.3
clearback to summary
public void clear()

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.clear.

Clears all data from the model.

getInterceptback to summary
public double getIntercept()

Returns the intercept of the estimated regression line, if hasIntercept() is true; otherwise 0.

The least squares estimate of the intercept is computed using the normal equations. The intercept is sometimes denoted b0.

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double,NaN is returned.

Returns:double

the intercept of the regression line if the model includes an intercept; 0 otherwise

See Also
SimpleRegression(boolean)
getInterceptback to summary
private double getIntercept(final double slope)

Returns the intercept of the estimated regression line, given the slope.

Will return NaN if slope is NaN.

Parameters
slope:double

current slope

Returns:double

the intercept of the regression line

getInterceptStdErrback to summary
public double getInterceptStdErr()

Returns the standard error of the intercept estimate, usually denoted s(b0).

If there are fewer that three observations in the model, or if there is no variation in x, this returns Double.NaN.

Additionally, a Double.NaN is returned when the intercept is constrained to be zero
Returns:double

standard error associated with intercept estimate

getMeanSquareErrorback to summary
public double getMeanSquareError()

Returns the sum of squared errors divided by the degrees of freedom, usually abbreviated MSE.

If there are fewer than three data pairs in the model, or if there is no variation in x, this returns Double.NaN.

Returns:double

sum of squared deviations of y values

getNback to summary
public long getN()

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.getN.

Returns the number of observations that have been added to the model.

Returns:long

n number of observations that have been added.

getRback to summary
public double getR()

Returns Pearson's product moment correlation coefficient, usually denoted r.

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double,NaN is returned.

Returns:double

Pearson's r

getRegressionSumSquaresback to summary
public double getRegressionSumSquares()

Returns the sum of squared deviations of the predicted y values about their mean (which equals the mean of y).

This is usually abbreviated SSR or SSM. It is defined as SSM here

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double.NaN is returned.

Returns:double

sum of squared deviations of predicted y values

getRegressionSumSquaresback to summary
private double getRegressionSumSquares(final double slope)

Computes SSR from b1.

Parameters
slope:double

regression slope estimate

Returns:double

sum of squared deviations of predicted y values

getRSquareback to summary
public double getRSquare()

Returns the coefficient of determination, usually denoted r-square.

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double,NaN is returned.

Returns:double

r-square

getSignificanceback to summary
public double getSignificance()

Returns the significance level of the slope (equiv) correlation.

Specifically, the returned value is the smallest alpha such that the slope confidence interval with significance level equal to alpha does not include 0. On regression output, this is often denoted Prob(|t| > 0)

Usage Note:
The validity of this statistic depends on the assumption that the observations included in the model are drawn from a Bivariate Normal Distribution.

If there are fewer that three observations in the model, or if there is no variation in x, this returns Double.NaN.

Returns:double

significance level for slope/correlation

Exceptions
MaxCountExceededException:
if the significance level can not be computed.
getSlopeback to summary
public double getSlope()

Returns the slope of the estimated regression line.

The least squares estimate of the slope is computed using the normal equations. The slope is sometimes denoted b1.

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double.NaN is returned.

Returns:double

the slope of the regression line

getSlopeConfidenceIntervalback to summary
public double getSlopeConfidenceInterval() throws OutOfRangeException

Returns the half-width of a 95% confidence interval for the slope estimate.

The 95% confidence interval is

(getSlope() - getSlopeConfidenceInterval(), getSlope() + getSlopeConfidenceInterval())

If there are fewer that three observations in the model, or if there is no variation in x, this returns Double.NaN.

Usage Note:
The validity of this statistic depends on the assumption that the observations included in the model are drawn from a Bivariate Normal Distribution.

Returns:double

half-width of 95% confidence interval for the slope estimate

Exceptions
OutOfRangeException:
if the confidence interval can not be computed.
getSlopeConfidenceIntervalback to summary
public double getSlopeConfidenceInterval(final double alpha) throws OutOfRangeException

Returns the half-width of a (100-100*alpha)% confidence interval for the slope estimate.

The (100-100*alpha)% confidence interval is

(getSlope() - getSlopeConfidenceInterval(), getSlope() + getSlopeConfidenceInterval())

To request, for example, a 99% confidence interval, use alpha = .01

Usage Note:
The validity of this statistic depends on the assumption that the observations included in the model are drawn from a Bivariate Normal Distribution.

Preconditions:

  • If there are fewer that three observations in the model, or if there is no variation in x, this returns Double.NaN.
  • (0 < alpha < 1); otherwise an OutOfRangeException is thrown.

Parameters
alpha:double

the desired significance level

Returns:double

half-width of 95% confidence interval for the slope estimate

Exceptions
OutOfRangeException:
if the confidence interval can not be computed.
getSlopeStdErrback to summary
public double getSlopeStdErr()

Returns the standard error of the slope estimate, usually denoted s(b1).

If there are fewer that three data pairs in the model, or if there is no variation in x, this returns Double.NaN.

Returns:double

standard error associated with slope estimate

getSumOfCrossProductsback to summary
public double getSumOfCrossProducts()

Returns the sum of crossproducts, xi*yi.

Returns:double

sum of cross products

getSumSquaredErrorsback to summary
public double getSumSquaredErrors()

Returns the sum of squared errors (SSE) associated with the regression model.

The sum is computed using the computational formula

SSE = SYY - (SXY * SXY / SXX)

where SYY is the sum of the squared deviations of the y values about their mean, SXX is similarly defined and SXY is the sum of the products of x and y mean deviations.

The sums are accumulated using the updating algorithm referenced in addData.

The return value is constrained to be non-negative - i.e., if due to rounding errors the computational formula returns a negative result, 0 is returned.

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double,NaN is returned.

Returns:double

sum of squared errors associated with the regression model

getTotalSumSquaresback to summary
public double getTotalSumSquares()

Returns the sum of squared deviations of the y values about their mean.

This is defined as SSTO here.

If n < 2, this returns Double.NaN.

Returns:double

sum of squared deviations of y values

getXSumSquaresback to summary
public double getXSumSquares()

Returns the sum of squared deviations of the x values about their mean. If n < 2, this returns Double.NaN.

Returns:double

sum of squared deviations of x values

hasInterceptback to summary
public boolean hasIntercept()

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.hasIntercept.

Returns true if the model includes an intercept term.

Returns:boolean

true if the regression includes an intercept; false otherwise

See Also
SimpleRegression(boolean)
predictback to summary
public double predict(final double x)

Returns the "predicted" y value associated with the supplied x value, based on the data that has been added to the model when this method is activated.

predict(x) = intercept + slope * x

Preconditions:

  • At least two observations (with at least two different x values) must have been added before invoking this method. If this method is invoked before a model can be estimated, Double,NaN is returned.

Parameters
x:double

input x value

Returns:double

predicted y value

regressback to summary
public RegressionResults regress() throws ModelSpecificationException, NoDataException

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.regress.

Performs a regression on data present in buffers and outputs a RegressionResults object.

If there are fewer than 3 observations in the model and hasIntercept is true a NoDataException is thrown. If there is no intercept term, the model must contain at least 2 observations.

Returns:RegressionResults

RegressionResults acts as a container of regression output

Exceptions
ModelSpecificationException:
if the model is not correctly specified
NoDataException:
if there is not sufficient data in the model to estimate the regression parameters
regressback to summary
public RegressionResults regress(int[] variablesToInclude) throws MathIllegalArgumentException

Implements org.apache.commons.math3.stat.regression.UpdatingMultipleLinearRegression.regress.

Performs a regression on data present in buffers including only regressors indexed in variablesToInclude and outputs a RegressionResults object

Parameters
variablesToInclude:int[]

an array of indices of regressors to include

Returns:RegressionResults

RegressionResults acts as a container of regression output

Exceptions
MathIllegalArgumentException:
if the variablesToInclude array is null or zero length
OutOfRangeException:
if a requested variable is not present in model
removeDataback to summary
public void removeData(final double x, final double y)

Removes the observation (x,y) from the regression data set.

Mirrors the addData method. This method permits the use of SimpleRegression instances in streaming mode where the regression is applied to a sliding "window" of observations, however the caller is responsible for maintaining the set of observations in the window.

The method has no effect if there are no points of data (i.e. n=0)
Parameters
x:double

independent variable value

y:double

dependent variable value

removeDataback to summary
public void removeData(double[][] data)

Removes observations represented by the elements in data.

If the array is larger than the current n, only the first n elements are processed. This method permits the use of SimpleRegression instances in streaming mode where the regression is applied to a sliding "window" of observations, however the caller is responsible for maintaining the set of observations in the window.

To remove all data, use clear().

Parameters
data:double[][]

array of observations to be removed