RealMatrix cov = (new Covariance(errorSeeds)).getCovarianceMatrix();
// Create a CorrelatedRandomVectorGenerator to use to generate correlated errors
GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
double[] errorMeans = new double[nObs]; // Counting on init to 0 here
CorrelatedRandomVectorGenerator gen = new CorrelatedRandomVectorGenerator(errorMeans, cov,
1.0e-12 * cov.getNorm(), rawGenerator);
// Now start generating models. Use Longley X matrix on LHS
// 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();