Skip to content

Commit

Permalink
fix Java smoke test failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
wendycwong committed Oct 21, 2024
1 parent e3c6ffe commit 3d07bcf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 45 deletions.
32 changes: 32 additions & 0 deletions h2o-algos/src/test/java/hex/hglm/HGLMUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import static hex.hglm.HGLMTask.ComputationEngineTask.sumAfjAfjAfjTYj;
import static hex.hglm.HGLMUtils.*;
import static hex.hglm.MetricBuilderHGLM.calHGLMllg;
import static org.junit.Assert.assertEquals;

@RunWith(H2ORunner.class)
Expand Down Expand Up @@ -196,4 +197,35 @@ public void checkCumSum(int numLevel2, int numFixedLength) {
TestUtil.checkDoubleArrays(sumAfjTAfj, sumAfjTAfjMat.getArray(), 1e-12);
TestUtil.checkArrays(sumAfjTYj, sumAfjTYjMat.transpose().getArray()[0], 1e-12);
}

@Test
public void testCalLlg() {
checkCalLlg(1, 2, 10, 1);
checkCalLlg(10, 20, 100, 3);
checkCalLlg(18, 8, 20, 1);
checkCalLlg(8, 8, 10, 1);
}

public void checkCalLlg(int numRandomCoeff, int numLevel2, int nobs, int multiplier) {
double varResidual = Math.abs(genRandomMatrix(1, 1, 123)[0][0]);
double yMinsXFixSquare = Math.abs(genRandomMatrix(1,1, 124)[0][0]);
double[][] tmat = genSymPsdMatrix(numRandomCoeff, 124, multiplier);
Matrix tmatInv = new Matrix(tmat).inverse();
double oneOVar = 1.0/varResidual;
double oneOVarSq = oneOVar*oneOVar;
double llgManual = nobs*Math.log(2*Math.PI)+oneOVar*yMinsXFixSquare;
double[][] yMinusXFixTimesZ = genRandomMatrix(numLevel2, numRandomCoeff, 126);
double[][][] zjTimesZj = new double[numLevel2][][];

for (int ind2=0; ind2<numLevel2; ind2++) {
zjTimesZj[ind2] = genSymPsdMatrix(numRandomCoeff, 125+ind2, multiplier);
Matrix tInvPZjZ = tmatInv.plus(new Matrix(zjTimesZj[ind2]).times(oneOVar));
llgManual += Math.log(varResidual * tInvPZjZ.det() * new Matrix(tmat).det());
Matrix yMinusXFTTZj = new Matrix(new double[][]{yMinusXFixTimesZ[ind2]});
llgManual -= yMinusXFTTZj.times(tInvPZjZ.inverse()).times(yMinusXFTTZj.transpose()).getArray()[0][0] * oneOVarSq;
}
llgManual *= -0.5;
double llg = calHGLMllg(nobs, tmat, varResidual, zjTimesZj, yMinsXFixSquare, yMinusXFixTimesZ);
assertEquals(llg, llgManual, 1e-6);
}
}
3 changes: 2 additions & 1 deletion h2o-core/src/main/java/water/util/ArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package water.util;

import Jama.Matrix;
import water.*;
import water.fvec.*;

Expand All @@ -23,7 +24,7 @@ public static int[] cumsum(final int[] from) {
}
return cumsumR;
}

/***
* Given an array with first dimension J and second dimension q, this function will flatten the 2-D array into
* 1-D array of length J*q. It basically concates each row of arr into one big 1-D array.
Expand Down
43 changes: 1 addition & 42 deletions h2o-core/src/test/java/hex/ModelMetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static water.TestUtil.genRandomMatrix;
import static water.TestUtil.genSymPsdMatrix;
import static water.util.ArrayUtils.expandMat;
import static water.util.ArrayUtils.flattenArray;

Expand All @@ -24,37 +25,6 @@ public void testEmptyModelAUC() {
assertTrue(Double.isNaN(ModelMetrics.getMetricFromModelMetric(mm, "auc")));
}

@Test
public void testCalLlg() {
checkCalLlg(1, 2, 10, 1);
checkCalLlg(10, 20, 100, 3);
checkCalLlg(18, 8, 20, 1);
checkCalLlg(8, 8, 10, 1);
}

public void checkCalLlg(int numRandomCoeff, int numLevel2, int nobs, int multiplier) {
double varResidual = Math.abs(genRandomMatrix(1, 1, 123)[0][0]);
double yMinsXFixSquare = Math.abs(genRandomMatrix(1,1, 124)[0][0]);
double[][] tmat = genSymPsdMatrix(numRandomCoeff, 124, multiplier);
Matrix tmatInv = new Matrix(tmat).inverse();
double oneOVar = 1.0/varResidual;
double oneOVarSq = oneOVar*oneOVar;
double llgManual = nobs*Math.log(2*Math.PI)+oneOVar*yMinsXFixSquare;
double[][] yMinusXFixTimesZ = genRandomMatrix(numLevel2, numRandomCoeff, 126);
double[][][] zjTimesZj = new double[numLevel2][][];

for (int ind2=0; ind2<numLevel2; ind2++) {
zjTimesZj[ind2] = genSymPsdMatrix(numRandomCoeff, 125+ind2, multiplier);
Matrix tInvPZjZ = tmatInv.plus(new Matrix(zjTimesZj[ind2]).times(oneOVar));
llgManual += Math.log(varResidual * tInvPZjZ.det() * new Matrix(tmat).det());
Matrix yMinusXFTTZj = new Matrix(new double[][]{yMinusXFixTimesZ[ind2]});
llgManual -= yMinusXFTTZj.times(tInvPZjZ.inverse()).times(yMinusXFTTZj.transpose()).getArray()[0][0] * oneOVarSq;
}
llgManual *= -0.5;
double llg = calHGLMllg(nobs, tmat, varResidual, zjTimesZj, yMinsXFixSquare, yMinusXFixTimesZ);
assertEquals(llg, llgManual, 1e-6);
}

// test loglikelihood calculation from ref [1] of the doc.
@Test
public void testCalLlg2() {
Expand Down Expand Up @@ -110,17 +80,6 @@ public void checkCalICC(int tmatSize) {
TestUtil.checkArrays(iccManual, calICC(symMatArray, varE), 1e-6);
}

public double[][] genSymPsdMatrix(int matSize, long seedValue, int multiplier) {
double[][] mat = genRandomMatrix(matSize, matSize, seedValue);
// generate symmetric matrix
Matrix matT = new Matrix(mat);
Matrix symMat = matT.plus(matT.transpose()).times(0.5);
for (int index=0; index<matSize; index++) {
symMat.set(index, index, Math.abs(genRandomMatrix(1,1,123)[0][0])*multiplier);
}
return symMat.getArray();
}

@Test
public void testCalVMatrix() {
checkCalVMatrix(2, 1, 10);
Expand Down
19 changes: 17 additions & 2 deletions h2o-test-support/src/main/java/water/TestUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package water;

import Jama.Matrix;
import hex.CreateFrame;
import hex.Model;
import hex.SplitFrame;
import hex.genmodel.*;
import hex.genmodel.GenModel;
import hex.genmodel.ModelMojoReader;
import hex.genmodel.MojoReaderBackend;
import hex.genmodel.MojoReaderBackendFactory;
import hex.genmodel.easy.RowData;
import org.junit.AfterClass;
import org.junit.Ignore;
Expand All @@ -20,8 +24,8 @@
import water.parser.DefaultParserProviders;
import water.parser.ParseDataset;
import water.parser.ParseSetup;
import water.util.*;
import water.util.Timer;
import water.util.*;
import water.util.fp.Function;

import java.io.*;
Expand Down Expand Up @@ -198,6 +202,17 @@ public static double[][] genRandomMatrix(int row, int col, long seedValue) {
return randomMat;
}

public static double[][] genSymPsdMatrix(int matSize, long seedValue, int multiplier) {
double[][] mat = genRandomMatrix(matSize, matSize, seedValue);
// generate symmetric matrix
Matrix matT = new Matrix(mat);
Matrix symMat = matT.plus(matT.transpose()).times(0.5);
for (int index=0; index<matSize; index++) {
symMat.set(index, index, Math.abs(genRandomMatrix(1,1,123)[0][0])*multiplier);
}
return symMat.getArray();
}

public static double[] genRandomArray(int length, long seedValue) {
Random random = new Random(seedValue);
return gaussianVector(length, random);
Expand Down

0 comments on commit 3d07bcf

Please sign in to comment.