final double regressionSumOfSquares = totalSumOfSquares - errorSumOfSquares;
final double[][] covarianceBetas = convertArray(_algebra.inverse(_algebra.mult(transpose, matrix)).toArray());
final double rSquared = regressionSumOfSquares / totalSumOfSquares;
final double adjustedRSquared = 1. - (1 - rSquared) * (n - 1.) / (n - k);
final double meanSquareError = errorSumOfSquares / (n - k);
final ContinuousDistribution studentT = new TDistributionImpl(n - k);
// final ProbabilityDistribution<Double> studentT = new
// StudentTDistribution(n - k);
for (int i = 0; i < k; i++) {
stdErrorBetas[i] = Math.sqrt(meanSquareError * covarianceBetas[i][i]);
tStats[i] = betas[i] / stdErrorBetas[i];
try {
pValues[i] = 1 - studentT.cumulativeProbability(Math.abs(tStats[i]));
} catch (final org.apache.commons.math.MathException e) {
throw new com.opengamma.analytics.math.MathException(e);
}
}
return new LeastSquaresRegressionResult(betas, residuals, meanSquareError, stdErrorBetas, rSquared, adjustedRSquared, tStats, pValues, useIntercept);