Skip to content

Commit

Permalink
Updated GLMTest2 to fit with my merge of the glm with new jobs, which…
Browse files Browse the repository at this point in the history
… is not using Job.exec() and Job.invoke() for now.
  • Loading branch information
tomasnykodym committed Oct 7, 2013
1 parent 09c79cd commit 48c2525
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/test/java/hex/GLMTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class GLMTest2 extends TestUtil {
// make data so that the expected coefficients is icept = col[0] = 1.0
FVecTest.makeByteVec(raw, "x,y\n0,0\n1,0.1\n2,0.2\n3,0.3\n4,0.4\n5,0.5\n6,0.6\n7,0.7\n8,0.8\n9,0.9");
Frame fr = ParseDataset2.parse(parsed, new Key[]{raw});
new GLM2("GLM test of gaussian(linear) regression.",modelKey,fr,false,Family.gaussian, Family.gaussian.defaultLink,0,0).fork().get();
new GLM2("GLM test of gaussian(linear) regression.",modelKey,fr,false,Family.gaussian, Family.gaussian.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
HashMap<String, Double> coefs = model.coefficients();
assertEquals(0.0,coefs.get("Intercept"),1e-4);
Expand All @@ -54,7 +54,7 @@ public class GLMTest2 extends TestUtil {
// make data so that the expected coefficients is icept = col[0] = 1.0
FVecTest.makeByteVec(raw, "x,y\n0,2\n1,4\n2,8\n3,16\n4,32\n5,64\n6,128\n7,256");
Frame fr = ParseDataset2.parse(parsed, new Key[]{raw});
new GLM2("GLM test of poisson regression.",modelKey,fr,false,Family.poisson, Family.poisson.defaultLink,0,0).invoke();
new GLM2("GLM test of poisson regression.",modelKey,fr,false,Family.poisson, Family.poisson.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
for(double c:model.beta())assertEquals(Math.log(2),c,1e-4);
// Test 2, example from http://www.biostat.umn.edu/~dipankar/bmtry711.11/lecture_13.pdf
Expand All @@ -64,7 +64,7 @@ public class GLMTest2 extends TestUtil {
UKV.remove(raw);
FVecTest.makeByteVec(raw, "x,y\n1,0\n2,1\n3,2\n4,3\n5,1\n6,4\n7,9\n8,18\n9,23\n10,31\n11,20\n12,25\n13,37\n14,45\n");
fr = ParseDataset2.parse(parsed, new Key[]{raw});
new GLM2("GLM test of poisson regression(2).",modelKey,fr,false,Family.poisson, Family.poisson.defaultLink,0,0).invoke();
new GLM2("GLM test of poisson regression(2).",modelKey,fr,false,Family.poisson, Family.poisson.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
assertEquals(0.3396,model.beta()[1],1e-4);
assertEquals(0.2565,model.beta()[0],1e-4);
Expand Down Expand Up @@ -94,7 +94,7 @@ public class GLMTest2 extends TestUtil {
// /public GLM2(String desc, Key dest, Frame src, Family family, Link link, double alpha, double lambda) {
double [] vals = new double[] {1.0,1.0};
//public GLM2(String desc, Key dest, Frame src, Family family, Link link, double alpha, double lambda) {
new GLM2("GLM test of gamma regression.",modelKey,fr,false,Family.gamma, Family.gamma.defaultLink,0,0).invoke();
new GLM2("GLM test of gamma regression.",modelKey,fr,false,Family.gamma, Family.gamma.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
for(double c:model.beta())assertEquals(1.0, c,1e-4);
}finally{
Expand All @@ -118,7 +118,7 @@ public class GLMTest2 extends TestUtil {
double [] intercepts = new double []{3.643,1.318,9.154};
double [] xs = new double []{-0.260,-0.0284,-0.853};
for(int i = 0; i < powers.length; ++i){
new GLM2("GLM test of gaussian(linear) regression.",modelKey,fr,false,Family.tweedie, Family.tweedie.defaultLink,0,0).setTweedieVarPower(powers[i]).invoke();
new GLM2("GLM test of gaussian(linear) regression.",modelKey,fr,false,Family.tweedie, Family.tweedie.defaultLink,0,0).setTweedieVarPower(powers[i]).run(null).get();
model = DKV.get(modelKey).get();
HashMap<String, Double> coefs = model.coefficients();
assertEquals(intercepts[i],coefs.get("Intercept"),1e-3);
Expand All @@ -138,16 +138,18 @@ public class GLMTest2 extends TestUtil {
* Simple test for poisson, gamma and gaussian families (no regularization, test both lsm solvers).
* Basically tries to predict horse power based on other parameters of the cars in the dataset.
* Compare against the results from standard R glm implementation.
* @throws ExecutionException
* @throws InterruptedException
*/
@Test public void testCars(){
@Test public void testCars() throws InterruptedException, ExecutionException{
Key parsed = Key.make("cars_parsed");
Key modelKey = Key.make("cars_model");
GLMModel model = null;
try{
String [] ignores = new String[]{"name"};
String response = "power (hp)";
Frame fr = getFrameForFile(parsed, "smalldata/cars.csv", ignores, response);
new GLM2("GLM test on cars.",modelKey,fr,true,Family.poisson,Family.poisson.defaultLink,0,0).invoke();
new GLM2("GLM test on cars.",modelKey,fr,true,Family.poisson,Family.poisson.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
HashMap<String,Double> coefs = model.coefficients();
String [] cfs1 = new String[]{"Intercept","economy (mpg)", "cylinders", "displacement (cc)", "weight (lb)", "0-60 mph (s)", "year"};
Expand All @@ -157,15 +159,15 @@ public class GLMTest2 extends TestUtil {
// test gamma
double [] vls2 = new double []{8.992e-03,1.818e-04,-1.125e-04,1.505e-06,-1.284e-06,4.510e-04,-7.254e-05};
model.delete();
new GLM2("GLM test on cars.",modelKey,fr,true,Family.gamma,Family.gamma.defaultLink,0,0).invoke();
new GLM2("GLM test on cars.",modelKey,fr,true,Family.gamma,Family.gamma.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
coefs = model.coefficients();
for(int i = 0; i < cfs1.length; ++i)
assertEquals(vls2[i], coefs.get(cfs1[i]),1e-4);
model.delete();
// test gaussian
double [] vls3 = new double []{166.95862,-0.00531,-2.46690,0.12635,0.02159,-4.66995,-0.85724};
new GLM2("GLM test on cars.",modelKey,fr,true,Family.gaussian,Family.gaussian.defaultLink,0,0).invoke();
new GLM2("GLM test on cars.",modelKey,fr,true,Family.gaussian,Family.gaussian.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
coefs = model.coefficients();
for(int i = 0; i < cfs1.length; ++i)
Expand All @@ -182,8 +184,10 @@ public class GLMTest2 extends TestUtil {
* it gets to test correct processing of categoricals.
*
* Compare against the results from standard R glm implementation.
* @throws ExecutionException
* @throws InterruptedException
*/
@Test public void testProstate(){
@Test public void testProstate() throws InterruptedException, ExecutionException{
Key parsed = Key.make("prostate_parsed");
Key modelKey = Key.make("prostate_model");
GLMModel model = null;
Expand All @@ -196,7 +200,7 @@ public class GLMTest2 extends TestUtil {
// -8.894088 0.001588 -0.009589 0.231777 -0.459937 0.556231 0.556395 0.027854 -0.011355 1.010179
String [] cfs1 = new String [] {"Intercept","AGE", "RACE.R2","RACE.R3", "DPROS", "DCAPS", "PSA", "VOL", "GLEASON"};
double [] vals = new double [] {-8.14867, -0.01368, 0.32337, -0.38028, 0.55964, 0.49548, 0.02794, -0.01104, 0.97704};
new GLM2("GLM test on prostate.",modelKey,fr,false,Family.binomial,Family.binomial.defaultLink,0,0).invoke();
new GLM2("GLM test on prostate.",modelKey,fr,false,Family.binomial,Family.binomial.defaultLink,0,0).run(null).get();
model = DKV.get(modelKey).get();
HashMap<String, Double> coefs = model.coefficients();
for(int i = 0; i < cfs1.length; ++i)
Expand Down

0 comments on commit 48c2525

Please sign in to comment.