/** * Test ensemble in regression * * @param global Global definition parameters * @param data Input data * @param npatterns No of patterns * @return Test ensemble in regression */ public double TestEnsembleInRegression( EnsembleParameters global, double data[][], int npatterns) { double fitness, RMS = 0.0, error; double[] outputs = new double[Noutputs]; for (int i = 0; i < npatterns; i++) { // Obtain network output EnsembleOutput(data[i], outputs); // Obtain RMS error error = 0.0; for (int j = 0; j < Noutputs; j++) { error += Math.pow(outputs[j] - data[i][Ninputs + j], 2.0); } RMS += Math.sqrt(error); } fitness = RMS / (npatterns * global.Noutputs); return fitness; }
/** * Calculate weights using GEM method * * @param global Global definition parameters * @param data Input data * @param n matrix order (no of rows and colms in data matrix) */ public void GetGEMWeights(EnsembleParameters global, double data[][], int n) { int xx, yy, offset, cols; double scalar; double[] module = new double[Nnetworks]; boolean[] collinear = new boolean[Nnetworks]; double[][] omega = new double[Nnetworks][Nnetworks]; double[][] inverse = new double[Nnetworks][Nnetworks]; double[][] toinvert = new double[Nnetworks][Nnetworks]; double[] output_i = new double[Noutputs]; double[] output_j = new double[Noutputs]; // Weight for every output. for (int out = 0; out < Noutputs; out++) { // Obtain omega matrix. for (int i = 0; i < Nnetworks; i++) { for (int j = 0; j <= i; j++) { omega[i][j] = 0.0; for (int k = 0; k < n; k++) { nets[i].GenerateOutput(data[k], output_i); nets[j].GenerateOutput(data[k], output_j); omega[i][j] += (data[k][Ninputs + out] - output_i[out]) * (data[k][Ninputs + out] - output_j[out]); } omega[j][i] = (omega[i][j] /= n); } } // Search collinearity. for (int i = 0; i < Nnetworks; i++) { module[i] = 0.0; for (int k = 0; k < Nnetworks; k++) { module[i] += omega[i][k] * omega[i][k]; } module[i] = Math.sqrt(module[i]); collinear[i] = false; } for (int i = 0; i < Nnetworks - 1; i++) { for (int j = i + 1; j < Nnetworks && !collinear[i]; j++) { scalar = 0.0; for (int k = 0; k < Nnetworks; k++) { scalar += omega[i][k] * omega[j][k]; } if (scalar / (module[i] * module[j]) > TH_COS) { collinear[i] = true; } } } // Create non-singular matrixx. for (int i = xx = 0; i < Nnetworks; i++) { if (!collinear[i]) { // Copyy row. for (int j = yy = 0; j < Nnetworks; j++) { if (!collinear[j]) { toinvert[xx][yy] = omega[i][j]; yy++; } } xx++; } } cols = xx; // Invert omega matrix. Matrix.InvertMatrix(toinvert, inverse, cols); // Obtain sum of matrix elements (1 Omega-1 1). double sum = 0.0; for (int i = 0; i < cols; i++) { for (int j = 0; j < cols; j++) { sum += inverse[i][j]; // Obtain weights. } } for (int i = offset = 0; i < Nnetworks; i++) { weights[out][i] = 0.0; if (!collinear[i]) { for (int j = 0; j < cols; j++) { weights[out][i] += inverse[i - offset][j]; } weights[out][i] /= sum; } else { offset++; } } } }