// and Longley OLS beta vector as "true" beta. Generate
// Y values by XB + u where u is a CorrelatedRandomVector generated
// from cov.
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
ols.newSampleData(longley, nObs, 6);
final RealVector b = ols.calculateBeta().copy();
final RealMatrix x = ols.X.copy();
// Create a GLS model to reuse
GLSMultipleLinearRegression gls = new GLSMultipleLinearRegression();
gls.newSampleData(longley, nObs, 6);
gls.newCovarianceData(cov.getData());
// Create aggregators for stats measuring model performance
DescriptiveStatistics olsBetaStats = new DescriptiveStatistics();
DescriptiveStatistics glsBetaStats = new DescriptiveStatistics();
// Generate Y vectors for 10000 models, estimate GLS and OLS and
// Verify that OLS estimates are better
final int nModels = 10000;
for (int i = 0; i < nModels; i++) {
// Generate y = xb + u with u cov
RealVector u = MatrixUtils.createRealVector(gen.nextVector());
double[] y = u.add(x.operate(b)).getData();
// Estimate OLS parameters
ols.newYSampleData(y);
RealVector olsBeta = ols.calculateBeta();
// Estimate GLS parameters
gls.newYSampleData(y);
RealVector glsBeta = gls.calculateBeta();
// Record deviations from "true" beta
double dist = olsBeta.getDistance(b);
olsBetaStats.addValue(dist * dist);
dist = glsBeta.getDistance(b);
glsBetaStats.addValue(dist * dist);
}
// Verify that GLS is on average more efficient, lower variance