Linear Regression

Linear regression analyzes the relationship between two variables, X and Y. For each subject (or experimental unit), you know both X and Y and you want to find the best straight curve (line in the simplest case) through the data.

Because Ordinary Least Square (OLS) are sensitive to spurious data more robust regressions methods need to be used such as Median Fit or robust OLS based on data rejection algorithm.

The sensitivity of the OLS method and the robustness of the other methods are illustrated with the example below.

 

The Linear Regression chart showing Series with Spurious Points series, Regression series.
Fit



C# Code Sample
double[] wYs = YSpurious;

// Define the regression object
QuantExpress.Maths.Regression.Regression1DBase oReg;

// Create the instance of the regression according to the user's selection
if (RBListRegType.SelectedIndex == 0)
{
    oReg = new QuantExpress.Maths.Regression.LinearRegression1D();
    ((QuantExpress.Maths.Regression.LinearRegression1D)oReg).IsOLSFit = true;
}
else 
    oReg = new QuantExpress.Maths.Regression.LinearRegression1D();

// sets the input Xs and Ys values
oReg.SetValues(X, wYs);
// Fits the regression parameters
oReg.Fit();

System.Data.DataTable oDT = new System.Data.DataTable("tblReg");
oDT.Columns.Add(new System.Data.DataColumn("X", typeof(double)));
oDT.Columns.Add(new System.Data.DataColumn("Y", typeof(double)));

System.Data.DataRow oDRow = oDT.NewRow();
oDRow["X"] = X[0];
oDRow["Y"] = oReg.Eval(X[0]); // estimates the regression Y for X[0]
oDT.Rows.Add(oDRow);

oDRow = oDT.NewRow();
oDRow["X"] = X[X.Length - 1];
oDRow["Y"] = oReg.Eval(X[X.Length - 1]); // estimates the regression Y for X[X.Length - 1]
oDT.Rows.Add(oDRow);