double beta1 = 0; // coefficient value of the first cluster
int k2 = 20; // number of coefficients of the second cluster
double beta2 = 5; // coefficient value of the second cluster
int k = 1 + k1 + k2;
DoubleVector beta = new DoubleVector( 1 + k1 + k2 );
beta.set( 0, beta0 );
beta.set( 1, k1, beta1 );
beta.set( k1+1, k1+k2, beta2 );
System.out.println("The data set contains " + n +
" observations plus " + (k1 + k2) +
" variables.\n\nThe coefficients of the true model"
+ " are:\n\n" + beta );
System.out.println("\nThe standard deviation of the error term is " +
sd );
System.out.println("==============================================="
+ "============");
PaceMatrix X = new PaceMatrix( n, k1+k2+1 );
X.setMatrix( 0, n-1, 0, 0, 1 );
X.setMatrix( 0, n-1, 1, k1+k2, random(n, k1+k2) );
PaceMatrix Y = new
PaceMatrix( X.times( new PaceMatrix(beta) ).
plusEquals( randomNormal(n,1).times(sd) ) );
IntVector pvt = (IntVector) IntVector.seq(0, k1+k2);
/*System.out.println( "The OLS estimate (by jama.Matrix.solve()) is:\n\n" +
(new PaceMatrix(X.solve(Y))).getColumn(0) );*/
X.lsqrSelection( Y, pvt, 1 );
X.positiveDiagonal( Y, pvt );
PaceMatrix sol = (PaceMatrix) Y.clone();
X.rsolve( sol, pvt, pvt.size() );
DoubleVector betaHat = sol.getColumn(0).unpivoting( pvt, k );
System.out.println( "\nThe OLS estimate (through lsqr()) is: \n\n" +
betaHat );
System.out.println( "\nQuadratic loss of the OLS estimate (||X b - X bHat||^2) = " +
( new PaceMatrix( X.times( new
PaceMatrix(beta.minus(betaHat)) )))
.getColumn(0).sum2() );
System.out.println("=============================================" +
"==============");
System.out.println(" *** Pace estimation *** \n");
DoubleVector r = Y.getColumn( pvt.size(), n-1, 0);
double sde = Math.sqrt(r.sum2() / r.size());
System.out.println( "Estimated standard deviation = " + sde );
DoubleVector aHat = Y.getColumn( 0, pvt.size()-1, 0).times( 1./sde );
System.out.println("\naHat = \n" + aHat );
System.out.println("\n========= Based on chi-square mixture ============");
ChisqMixture d2 = new ChisqMixture();
int method = MixtureDistribution.NNMMethod;
DoubleVector AHat = aHat.square();
d2.fit( AHat, method );
System.out.println( "\nEstimated mixing distribution is:\n" + d2 );
DoubleVector ATilde = d2.pace2( AHat );
DoubleVector aTilde = ATilde.sqrt().times(aHat.sign());
PaceMatrix YTilde = new
PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
X.rsolve( YTilde, pvt, pvt.size() );
DoubleVector betaTilde =
YTilde.getColumn(0).unpivoting( pvt, k );
System.out.println( "\nThe pace2 estimate of coefficients = \n" +
betaTilde );
System.out.println( "Quadratic loss = " +
( new PaceMatrix( X.times( new