Added standard errors.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@731356 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f2b72fc248
commit
e7911e6efa
|
@ -140,6 +140,20 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
return calculateBetaVariance().getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public double[] estimateRegressionParametersStandardErrors() {
|
||||
double[][] betaVariance = estimateRegressionParametersVariance();
|
||||
double sigma = calculateYVariance();
|
||||
int length = betaVariance[0].length;
|
||||
double[] result = new double[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = Math.sqrt(sigma * betaVariance[i][i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
|
|
@ -60,4 +60,11 @@ public interface MultipleLinearRegression {
|
|||
*/
|
||||
double estimateRegressandVariance();
|
||||
|
||||
/**
|
||||
* Returns the standard errors of the regression parameters.
|
||||
*
|
||||
* @return standard errors of estimated regression parameters
|
||||
*/
|
||||
double[] estimateRegressionParametersStandardErrors();
|
||||
|
||||
}
|
||||
|
|
|
@ -143,6 +143,7 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
|
|||
* @return The beta variance
|
||||
*/
|
||||
protected RealMatrix calculateBetaVariance() {
|
||||
//TODO: find a way to use QR decomp to avoid inverting XX' here
|
||||
RealMatrix XTX = X.transpose().multiply(X);
|
||||
return new LUDecompositionImpl(XTX).getSolver().getInverse();
|
||||
}
|
||||
|
|
|
@ -149,8 +149,17 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
|
|||
1E-8);
|
||||
|
||||
// Check standard errors from NIST
|
||||
double[][] errors = model.estimateRegressionParametersVariance();
|
||||
//TODO: translate this into std error vector and check
|
||||
double[] errors = model.estimateRegressionParametersStandardErrors();
|
||||
TestUtils.assertEquals(new double[] {890420.383607373,
|
||||
84.9149257747669,
|
||||
0.334910077722432E-01,
|
||||
0.488399681651699,
|
||||
0.214274163161675,
|
||||
0.226073200069370,
|
||||
455.478499142212}, errors, 1E-2); // Ugh..
|
||||
// Bad accuracy is in intercept std error estimate. Could be due to
|
||||
// Current impl inverting XX' to get standard errors.
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -246,6 +255,14 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
|
|||
15.0147574652763112,4.8625103516321015,-7.1597256413907706,
|
||||
-0.4515205619767598,-10.2916870903837587,-15.7812984571900063},
|
||||
1E-12);
|
||||
|
||||
// Check standard errors from R
|
||||
double[] errors = model.estimateRegressionParametersStandardErrors();
|
||||
TestUtils.assertEquals(new double[] {6.94881329475087,
|
||||
0.07360008972340,
|
||||
0.27410957467466,
|
||||
0.19454551679325,
|
||||
0.03726654773803}, errors, 1E-10);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue